diff options
author | Anthony Wang | 2023-05-12 21:28:48 -0400 |
---|---|---|
committer | Anthony Wang | 2023-05-12 21:28:48 -0400 |
commit | 16952576144cda87c9e01e73b21f6a49d01af2a5 (patch) | |
tree | 6473476a49f4a9c83adc05164a826c3291045061 /client | |
parent | ae29c8b047e16eb8357fd30f1cd970d124a71ec1 (diff) |
Finish storage server implementation
Diffstat (limited to 'client')
-rw-r--r-- | client/main.go | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/client/main.go b/client/main.go index 5397384..150e160 100644 --- a/client/main.go +++ b/client/main.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/ed25519" "encoding/base64" + "encoding/binary" "flag" "fmt" "net/http" @@ -19,10 +20,25 @@ var servers []string // Post new server list to DHT func dhtPost(s string) { - message := []byte(fmt.Sprint(time.Now().Unix()) + "\n" + strings.Join(servers, "\n")) - fmt.Print(message) - message = append(message, ed25519.Sign(privKey, message)...) - http.Post(s+"/dht/"+id, "application/octet-stream", bytes.NewBuffer(message)) + buf := new(bytes.Buffer) + err := binary.Write(buf, binary.LittleEndian, time.Now().Unix()) + if err != nil { + panic(err) + } + _, err = buf.WriteString(strings.Join(servers, "\n")) + if err != nil { + panic(err) + } + var message []byte + _, err = buf.Read(message) + if err != nil { + panic(err) + } + _, err = buf.Write(ed25519.Sign(privKey, message)) + if err != nil { + panic(err) + } + fmt.Println(http.Post(s+"/dht/"+id, "application/octet-stream", buf)) } func main() { @@ -55,19 +71,22 @@ func main() { if err != nil { panic(err) } - pubKey := ed25519.PublicKey(pubKeyBytes) + pubKey = ed25519.PublicKey(pubKeyBytes) privKeyBytes, err := os.ReadFile("privkey") if err != nil { panic(err) } - privKey := ed25519.PublicKey(privKeyBytes) + privKey = ed25519.PrivateKey(privKeyBytes) serversBytes, err := os.ReadFile("servers") if err != nil { panic(err) } id = base64.RawURLEncoding.EncodeToString(pubKey) - servers := strings.Split(string(serversBytes), "\n") - fmt.Println(pubKey, privKey, servers) + servers = strings.Split(string(serversBytes), "\n") + if servers[0] == "" { + servers = servers[1:] + } + fmt.Println(id, servers) if flag.Arg(0) == "add" { // Add server |