make staticcheck happy

main
Bill Mill 2 years ago
parent c0e16386d9
commit 5becbf2c23
  1. 10
      client/main.go
  2. 29
      server/main.go

@ -28,7 +28,7 @@ func validKey() (ed25519.PublicKey, ed25519.PrivateKey) {
// Because we have an odd-length hex string, we don't encode the '8' here
// and instead check it specifically in the hot loop... I'm open to ideas
// about how to do this better. I'd like to keep everything in the hot loop
// using the `bytes.compare` function which is assembly on most platforms,
// using the `bytes.Equal` function which is assembly on most platforms,
// but we don't have a full byte for the `8`
keyEnd := fmt.Sprintf("3e%s", time.Now().AddDate(2, 0, 0).Format("0106"))
target, err := hex.DecodeString(keyEnd)
@ -48,7 +48,7 @@ func validKey() (ed25519.PublicKey, ed25519.PrivateKey) {
waitGroup.Add(nRoutines)
for i := 0; i < nRoutines; i++ {
go func(num int) {
go func() {
for publicKey == nil {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
@ -56,8 +56,8 @@ func validKey() (ed25519.PublicKey, ed25519.PrivateKey) {
}
// Here's where we check for the `8`; we do it after the
// bytes.Compare to keep the hot loop fast
if bytes.Compare(pub[29:32], target) == 0 && pub[28]&0x0F == 0x08 {
// bytes.Equal to keep the hot loop fast
if bytes.Equal(pub[29:32], target) && pub[28]&0x0F == 0x08 {
once.Do(func() {
fmt.Printf("found %x\n", pub)
publicKey = pub
@ -66,7 +66,7 @@ func validKey() (ed25519.PublicKey, ed25519.PrivateKey) {
}
}
waitGroup.Done()
}(i)
}()
}
waitGroup.Wait()

@ -249,7 +249,7 @@ func (s *Spring83Server) publishBoard(w http.ResponseWriter, r *http.Request) {
// curBoard is nil if there is no existing board for this key, and a Board object otherwise
curBoard, err := s.getBoard(keyStr)
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
@ -260,7 +260,7 @@ func (s *Spring83Server) publishBoard(w http.ResponseWriter, r *http.Request) {
if curBoard == nil {
difficultyFactor, keyThreshold, err := s.getDifficulty()
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
@ -320,7 +320,7 @@ func (s *Spring83Server) publishBoard(w http.ResponseWriter, r *http.Request) {
// The server may also use a denylist to block certain keys, rejecting all PUTs for those keys.
denylist := []string{"fad415fbaa0339c4fd372d8287e50f67905321ccfd9c43fa4c20ac40afed1983"}
for _, key := range denylist {
if bytes.Compare(signature, []byte(key)) == 0 {
if bytes.Equal(signature, []byte(key)) {
http.Error(w, "Denied", http.StatusUnauthorized)
}
}
@ -349,7 +349,7 @@ func (s *Spring83Server) publishBoard(w http.ResponseWriter, r *http.Request) {
// expires on the last day of the month of its issuance; here we're just
// giving it an extra month. TODO be more accurate
twoYearsInHours := (365 * 2 * 24.0) + 31*24.0
timeDiff := last4Time.Sub(time.Now()).Hours()
timeDiff := time.Until(last4Time).Hours()
if keyStr[57:60] != "83e" {
log.Printf("Expected 83e %s", string(keyStr[57:60]))
http.Error(w, "Key must end with 83eMMYY", http.StatusBadRequest)
@ -488,14 +488,14 @@ func randstr() string {
func (s *Spring83Server) showAllBoards(w http.ResponseWriter, r *http.Request) {
boards, err := s.loadBoards()
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "Unable to load boards", http.StatusInternalServerError)
return
}
difficultyFactor, _, err := s.getDifficulty()
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
@ -517,7 +517,7 @@ func (s *Spring83Server) showAllBoards(w http.ResponseWriter, r *http.Request) {
boardBytes, err := json.Marshal(boards)
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "Unable to marshal boards", http.StatusInternalServerError)
return
}
@ -536,7 +536,7 @@ func (s *Spring83Server) showAllBoards(w http.ResponseWriter, r *http.Request) {
func (s *Spring83Server) showBoard(w http.ResponseWriter, r *http.Request) {
board, err := s.getBoard(r.URL.Path[1:])
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "Unable to load boards", http.StatusInternalServerError)
return
}
@ -550,7 +550,7 @@ func (s *Spring83Server) showBoard(w http.ResponseWriter, r *http.Request) {
difficultyFactor, _, err := s.getDifficulty()
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
@ -566,7 +566,7 @@ func (s *Spring83Server) showBoard(w http.ResponseWriter, r *http.Request) {
boardBytes, err := json.Marshal([]*Board{board})
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
http.Error(w, "Unable to marshal boards", http.StatusInternalServerError)
return
}
@ -593,17 +593,18 @@ func (s *Spring83Server) Options(w http.ResponseWriter, r *http.Request) {
}
func (s *Spring83Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
switch r.Method {
case "OPTIONS":
s.Options(w, r)
} else if r.Method == "PUT" {
case "PUT":
s.publishBoard(w, r)
} else if r.Method == "GET" {
case "GET":
if len(r.URL.Path) == 1 {
s.showAllBoards(w, r)
} else {
s.showBoard(w, r)
}
} else {
default:
http.Error(w, "Invalid method", http.StatusBadRequest)
}
}

Loading…
Cancel
Save