diff options
author | Anthony Wang | 2023-05-10 19:20:06 -0400 |
---|---|---|
committer | Anthony Wang | 2023-05-10 19:20:06 -0400 |
commit | 8ca9aeba69001d8bf6a6115d9eba210229e9daca (patch) | |
tree | 85ed98cfcf9826acc510214dd5db5ad2588757fc /server | |
parent | a12ce3ce396399553251381e349b1d4816dc4bf4 (diff) |
Implement part of the DHT handler
Diffstat (limited to 'server')
-rw-r--r-- | server/server.go | 95 |
1 files changed, 84 insertions, 11 deletions
diff --git a/server/server.go b/server/server.go index e84b4e0..11aae25 100644 --- a/server/server.go +++ b/server/server.go @@ -1,6 +1,7 @@ package main import ( + "crypto/ed25519" "crypto/sha256" "encoding/hex" "flag" @@ -13,20 +14,26 @@ import ( "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 { - // Get the sha256sum of string as a hex string b := sha256.Sum256([]byte(s)) return hex.EncodeToString(b[:]) } +// Try to peer with another server func addPeer(peer string) error { - // Try to peer with another server peerHash := sha256sum(peer) // Check if already peered mu.Lock() @@ -52,8 +59,11 @@ func addPeer(peer string) error { log.Printf("%s successfully peered with %s", me, peer) mu.Lock() - peerHashes = append(peerHashes, peerHash) - sort.Sort(sort.StringSlice(peerHashes)) + i := sort.SearchStrings(peerHashes, peerHash) + peerHashes = append(peerHashes, "") + copy(peerHashes[i+1:], peerHashes[i:]) + peerHashes[i] = peerHash + myPos = sort.SearchStrings(peerHashes, me) mu.Unlock() // Read response body defer resp.Body.Close() @@ -69,26 +79,87 @@ func addPeer(peer string) error { return nil } +// Handle incoming peer requests func peerHandler(w http.ResponseWriter, r *http.Request) { - // Handle incoming peer requests r.ParseForm() peer := r.Form.Get("peer") if peer == "" { w.WriteHeader(http.StatusBadRequest) return } - go addPeer(peer) for _, p := range hashToDomain { fmt.Fprintf(w, "%s\n", p) } + go addPeer(peer) } -func getHandler(w http.ResponseWriter, r *http.Request) { - +// Handle DHT requests +func dhtHandler(w http.ResponseWriter, r *http.Request) { + key := r.URL.String()[5:] + keyHash := sha256sum(key) + fmt.Println(key, keyHash) + mu.Lock() + keyPos := sort.SearchStrings(peerHashes, keyHash) + if keyPos < myPos { + keyPos += len(peerHashes) + } + mu.Unlock() + if r.Method == "GET" { + if keyPos - myPos < 5 { + mu.Lock() + if val, ok := kvstore[key]; ok { + w.Write([]byte(val)) + } else { + w.WriteHeader(http.StatusNotFound) + } + mu.Unlock() + } else { + for i := 0; i < 5 && i < len(peerHashes); i++ { + j := hashToDomain[peerHashes[(keyPos+i)%len(peerHashes)]] + go func() string { + resp, err := http.Get(j + r.URL.String()) + if err != nil { + return "" + } + b, err := io.ReadAll(resp.Body) + if err != nil { + return "" + } + return string(b) + }() + + } + + + } + } else if r.Method == "PUT" { + b, err := io.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + + + if keyPos - myPos < 5 { + mu.Lock() + kvstore[key] = string(b) + } + } else { + w.WriteHeader(http.StatusMethodNotAllowed) + } } -func setHandler(w http.ResponseWriter, r *http.Request) { +// 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) + } } func main() { @@ -101,6 +172,8 @@ func main() { // Record myself me = *domain + myHash = sha256sum(me) + myPos = 0 peerHashes = append(peerHashes, sha256sum(me)) hashToDomain = map[string]string{peerHashes[0]: me} @@ -109,7 +182,7 @@ func main() { } http.HandleFunc("/peer", peerHandler) - http.HandleFunc("/get", getHandler) - http.HandleFunc("/set", setHandler) + http.HandleFunc("/dht", dhtHandler) + http.HandleFunc("/storage", storageHandler) log.Fatal(http.ListenAndServe(*bindAddr, nil)) } |