aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorAnthony Wang2023-05-12 11:09:07 -0400
committerAnthony Wang2023-05-12 11:09:07 -0400
commitae29c8b047e16eb8357fd30f1cd970d124a71ec1 (patch)
tree12479e1e526f4675f031f55e14aeddfe99ed2bd1 /client
parentcdd295e371d78f02a2051044e8e4417f79b378dd (diff)
Finish DHT server and client implementation
Diffstat (limited to 'client')
-rw-r--r--client/main.go75
1 files changed, 68 insertions, 7 deletions
diff --git a/client/main.go b/client/main.go
index 22e5230..5397384 100644
--- a/client/main.go
+++ b/client/main.go
@@ -1,14 +1,30 @@
package main
import (
+ "bytes"
"crypto/ed25519"
"encoding/base64"
"flag"
"fmt"
+ "net/http"
"os"
"strings"
+ "time"
)
+var pubKey ed25519.PublicKey
+var privKey ed25519.PrivateKey
+var id string
+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))
+}
+
func main() {
flag.Parse()
@@ -18,11 +34,11 @@ func main() {
if err != nil {
panic(err)
}
- err = os.WriteFile("pubkey", pubKey, 644)
+ err = os.WriteFile("pubkey", pubKey, 0644)
if err != nil {
panic(err)
}
- err = os.WriteFile("privkey", privKey, 600)
+ err = os.WriteFile("privkey", privKey, 0600)
if err != nil {
panic(err)
}
@@ -31,7 +47,7 @@ func main() {
if err != nil {
panic(err)
}
- fmt.Printf("Success! Your user ID: %s", base64.RawURLEncoding.EncodeToString(pubKey))
+ fmt.Printf("Success! Your user ID: %s\n", base64.RawURLEncoding.EncodeToString(pubKey))
return
}
@@ -49,11 +65,56 @@ func main() {
if err != nil {
panic(err)
}
+ id = base64.RawURLEncoding.EncodeToString(pubKey)
servers := strings.Split(string(serversBytes), "\n")
+ fmt.Println(pubKey, privKey, servers)
-
- if flag.Arg(0) == "associate" {
-
-
+ if flag.Arg(0) == "add" {
+ // Add server
+ servers = append(servers, flag.Arg(1))
+ dhtPost(flag.Arg(1))
+ http.Get(flag.Arg(1) + "/user/" + id)
+ if servers[0] != flag.Arg(1) {
+ http.Get(servers[0] + "/user/" + id)
+ }
+ err := os.WriteFile("servers", []byte(strings.Join(servers, "\n")), 0644)
+ if err != nil {
+ panic(err)
+ }
+ } else if flag.Arg(0) == "remove" {
+ // Remove server
+ for i := range servers {
+ if servers[i] == flag.Arg(1) {
+ servers = append(servers[:i], servers[i+1:]...)
+ break
+ }
+ }
+ if len(servers) > 0 {
+ dhtPost(servers[0])
+ http.Get(servers[0] + "/user/" + id)
+ } else {
+ dhtPost(flag.Arg(1))
+ }
+ http.Get(flag.Arg(1) + "/user/" + id)
+ err := os.WriteFile("servers", []byte(strings.Join(servers, "\n")), 0644)
+ if err != nil {
+ panic(err)
+ }
+ } else if flag.Arg(0) == "primary" {
+ // Make server a primary
+ for i := range servers {
+ if i > 0 && servers[i] == flag.Arg(1) {
+ servers[i] = servers[0]
+ servers[0] = flag.Arg(i)
+ dhtPost(flag.Arg(i))
+ http.Get(servers[0] + "/user/" + id)
+ http.Get(servers[i] + "/user/" + id)
+ break
+ }
+ }
+ err := os.WriteFile("servers", []byte(strings.Join(servers, "\n")), 0644)
+ if err != nil {
+ panic(err)
+ }
}
}