diff options
author | Anthony Wang | 2023-05-12 22:20:09 -0400 |
---|---|---|
committer | Anthony Wang | 2023-05-12 22:20:09 -0400 |
commit | 70d158c6d107cbb29251985c34c503e53c8dd4e7 (patch) | |
tree | 691d7afa61ab602e8eb77ff25d0f4837198db4f9 /client | |
parent | ccd58080b6d6949ed10136efe8a2d953f42d90ac (diff) |
More logging and fix some bugs
Diffstat (limited to 'client')
-rw-r--r-- | client/main.go | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/client/main.go b/client/main.go index d490d56..d37fd2d 100644 --- a/client/main.go +++ b/client/main.go @@ -19,27 +19,26 @@ var privKey ed25519.PrivateKey var id string var servers []string -// Post new server list to DHT -func dhtPost(s string) { +// Sign and post to a server +func signPost(s string, b []byte) { 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")) + _, err = buf.Write(b) if err != nil { panic(err) } - var message []byte - _, err = buf.Read(message) + _, err = buf.Write(ed25519.Sign(privKey, buf.Bytes())) if err != nil { panic(err) } - _, err = buf.Write(ed25519.Sign(privKey, message)) + resp, err := http.Post(s, "application/octet-stream", buf) if err != nil { panic(err) } - fmt.Println(http.Post(s+"/dht/"+id, "application/octet-stream", buf)) + fmt.Println(resp) } func main() { @@ -92,7 +91,7 @@ func main() { if flag.Arg(0) == "add" { // Add server servers = append(servers, flag.Arg(1)) - dhtPost(flag.Arg(1)) + signPost(flag.Arg(1)+"/dht/"+id, []byte(strings.Join(servers, "\n"))) http.Get(flag.Arg(1) + "/user/" + id) if servers[0] != flag.Arg(1) { http.Get(servers[0] + "/user/" + id) @@ -110,10 +109,10 @@ func main() { } } if len(servers) > 0 { - dhtPost(servers[0]) + signPost(servers[0]+"/dht/"+id, []byte(strings.Join(servers, "\n"))) http.Get(servers[0] + "/user/" + id) } else { - dhtPost(flag.Arg(1)) + signPost(flag.Arg(1)+"/dht/"+id, []byte(strings.Join(servers, "\n"))) } http.Get(flag.Arg(1) + "/user/" + id) err := os.WriteFile("servers", []byte(strings.Join(servers, "\n")), 0644) @@ -126,7 +125,7 @@ func main() { if i > 0 && servers[i] == flag.Arg(1) { servers[i] = servers[0] servers[0] = flag.Arg(i) - dhtPost(flag.Arg(i)) + signPost(flag.Arg(1)+"/dht/"+id, []byte(strings.Join(servers, "\n"))) http.Get(servers[0] + "/user/" + id) http.Get(servers[i] + "/user/" + id) break @@ -139,16 +138,24 @@ func main() { } else if flag.Arg(0) == "get" { user := flag.Arg(1) filename := flag.Arg(2) - response, _ := http.Get(servers[0] + "/storage/" + user + "/" + filename) - responseBodyBytes, _ := io.ReadAll(response.Body) - os.WriteFile(user + "/" + filename, responseBodyBytes, 0644) + response, err := http.Get(servers[0] + "/storage/" + user + "/" + filename) + if err != nil { + panic(err) + } + b, err := io.ReadAll(response.Body) + if err != nil { + panic(err) + } + err = os.WriteFile(filename, b, 0644) + if err != nil { + panic(err) + } } else if flag.Arg(0) == "post" { filename := flag.Arg(1) - file, _ := os.Open(filename) - http.Post( - servers[0] + "/storage/" + id + "/" + filename, - "application/octet-stream", - file, - ) + file, err := os.ReadFile(filename) + if err != nil { + panic(err) + } + signPost(servers[0] + "/storage/" + id + "/" + filename, file) } } |