diff options
Diffstat (limited to 'server/main.go')
-rw-r--r-- | server/main.go | 63 |
1 files changed, 63 insertions, 0 deletions
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)) +} |