diff options
-rw-r--r-- | client/client.go | 7 | ||||
-rw-r--r-- | client/main.go | 13 | ||||
-rw-r--r-- | server/dht.go (renamed from server/server.go) | 83 | ||||
-rw-r--r-- | server/main.go | 63 | ||||
-rw-r--r-- | server/message.go | 10 | ||||
-rw-r--r-- | server/storage.go | 19 |
6 files changed, 122 insertions, 73 deletions
diff --git a/client/client.go b/client/client.go deleted file mode 100644 index b7d2a32..0000000 --- a/client/client.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("Hello, World!") -} diff --git a/client/main.go b/client/main.go new file mode 100644 index 0000000..d996531 --- /dev/null +++ b/client/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "crypto/ed25519" + "flag" +) + +func main() { + flag.Parse() + if flag.Arg(0) == "register" { + + } +} diff --git a/server/server.go b/server/dht.go index 11aae25..e5d96f1 100644 --- a/server/server.go +++ b/server/dht.go @@ -2,36 +2,15 @@ package main import ( "crypto/ed25519" - "crypto/sha256" - "encoding/hex" - "flag" + "encoding/base64" "fmt" "io" "log" "net/http" "sort" "strings" - "sync" ) -type user struct { - pubkey []byte -} - -var mu sync.Mutex -var me string -var myHash string -var myPos int -var hashToDomain map[string]string -var peerHashes []string -var kvstore map[string]string - -// Get the sha256sum of string as a hex string -func sha256sum(s string) string { - b := sha256.Sum256([]byte(s)) - return hex.EncodeToString(b[:]) -} - // Try to peer with another server func addPeer(peer string) error { peerHash := sha256sum(peer) @@ -66,7 +45,6 @@ func addPeer(peer string) error { myPos = sort.SearchStrings(peerHashes, me) mu.Unlock() // Read response body - defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return err @@ -97,7 +75,8 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { func dhtHandler(w http.ResponseWriter, r *http.Request) { key := r.URL.String()[5:] keyHash := sha256sum(key) - fmt.Println(key, keyHash) + pubKey := asPubKey(key) + fmt.Println(key, keyHash, pubKey) mu.Lock() keyPos := sort.SearchStrings(peerHashes, keyHash) if keyPos < myPos { @@ -133,56 +112,28 @@ func dhtHandler(w http.ResponseWriter, r *http.Request) { } } else if r.Method == "PUT" { + // Read request body b, err := io.ReadAll(r.Body) if err != nil { w.WriteHeader(http.StatusInternalServerError) + return + } + // Extract signature + valSplit := strings.Split(string(b), "\n") + sig := valSplit[len(valSplit)-1] + // Verify signature + if !ed25519.Verify(pubKey, b[:len(b)-len(sig)-1], []byte(sig)) { + w.WriteHeader(http.StatusUnauthorized) + return } - - if keyPos - myPos < 5 { mu.Lock() - kvstore[key] = string(b) - } - } else { - w.WriteHeader(http.StatusMethodNotAllowed) - } -} - -// Handle storage requests -func storageHandler(w http.ResponseWriter, r *http.Request) { - filename := r.URL.String()[5:] - if r.Method == "GET" { - - } else if r.Method == "PUT" { - - } else if r.Method == "DELETE" { + kvstore[key] = string(b[:len(b)-len(sig)-1]) + mu.Unlock() + } else { + } } else { w.WriteHeader(http.StatusMethodNotAllowed) } } - -func main() { - bindAddr := flag.String("b", ":4200", "bind address") - domain := flag.String("d", "http://localhost:4200", "full domain name") - peer := flag.String("i", "", "initial peer") - flag.Parse() - - log.Printf("Starting %s %s %s", *bindAddr, *domain, *peer) - - // Record myself - me = *domain - myHash = sha256sum(me) - myPos = 0 - peerHashes = append(peerHashes, sha256sum(me)) - hashToDomain = map[string]string{peerHashes[0]: me} - - if *peer != "" { - go addPeer(*peer) - } - - http.HandleFunc("/peer", peerHandler) - http.HandleFunc("/dht", dhtHandler) - http.HandleFunc("/storage", storageHandler) - log.Fatal(http.ListenAndServe(*bindAddr, nil)) -} diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..c0f7450 --- /dev/null +++ b/server/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "crypto/ed25519" + "crypto/sha256" + "encoding/base64" + "flag" + "log" + "net/http" + "sync" +) + +type user struct { + pubkey []byte +} + +var mu sync.Mutex +var me string +var myHash string +var myPos int +var hashToDomain map[string]string +var peerHashes []string +var kvstore map[string]string + +// Get the sha256sum of string as a URL-safe unpadded base64 string +func sha256sum(s string) string { + b := sha256.Sum256([]byte(s)) + return base64.RawURLEncoding.EncodeToString(b[:]) +} + +// Decode an ID to a public key +func asPubKey(s string) ed25519.PublicKey { + b, err := base64.RawURLEncoding.DecodeString(s) + if err != nil { + return nil + } + return ed25519.PublicKey(b) +} + +func main() { + bindAddr := flag.String("b", ":4200", "bind address") + publicURL := flag.String("u", "http://localhost:4200", "public URL") + peer := flag.String("i", "", "initial peer") + flag.Parse() + log.Printf("Starting %s %s %s", *bindAddr, *publicURL, *peer) + + // Record myself + me = *publicURL + myHash = sha256sum(me) + myPos = 0 + peerHashes = append(peerHashes, sha256sum(me)) + hashToDomain = map[string]string{peerHashes[0]: me} + + if *peer != "" { + go addPeer(*peer) + } + + http.HandleFunc("/peer", peerHandler) + http.HandleFunc("/dht", dhtHandler) + http.HandleFunc("/storage", storageHandler) + http.HandleFunc("/message", messageHandler) + log.Fatal(http.ListenAndServe(*bindAddr, nil)) +} diff --git a/server/message.go b/server/message.go new file mode 100644 index 0000000..b127727 --- /dev/null +++ b/server/message.go @@ -0,0 +1,10 @@ +package main + +import ( + "net/http" +) + +// Handle message requests +func messageHandler(w http.ResponseWriter, r *http.Request) { + +} diff --git a/server/storage.go b/server/storage.go new file mode 100644 index 0000000..dd11f22 --- /dev/null +++ b/server/storage.go @@ -0,0 +1,19 @@ +package main + +import ( + "net/http" +) + +// Handle storage requests +func storageHandler(w http.ResponseWriter, r *http.Request) { + // filename := r.URL.String()[5:] + if r.Method == "GET" { + + } else if r.Method == "PUT" { + + } else if r.Method == "DELETE" { + + } else { + w.WriteHeader(http.StatusMethodNotAllowed) + } +} |