aboutsummaryrefslogtreecommitdiff
path: root/client/main.go
blob: 53973849e3a3fdede7eae029a3a30c6f640966f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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()

	if flag.Arg(0) == "setup" {
		// Create keys
		pubKey, privKey, err := ed25519.GenerateKey(nil)
		if err != nil {
			panic(err)
		}
		err = os.WriteFile("pubkey", pubKey, 0644)
		if err != nil {
			panic(err)
		}
		err = os.WriteFile("privkey", privKey, 0600)
		if err != nil {
			panic(err)
		}
		// Create servers file
		_, err = os.Create("servers")
		if err != nil {
			panic(err)
		}
		fmt.Printf("Success! Your user ID: %s\n", base64.RawURLEncoding.EncodeToString(pubKey))
		return
	}

	pubKeyBytes, err := os.ReadFile("pubkey")
	if err != nil {
		panic(err)
	}
	pubKey := ed25519.PublicKey(pubKeyBytes)
	privKeyBytes, err := os.ReadFile("privkey")
	if err != nil {
		panic(err)
	}
	privKey := ed25519.PublicKey(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)

	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)
		}
	}
}