aboutsummaryrefslogtreecommitdiff
path: root/client/main.go
blob: d37fd2da3f2a28c2a814aac0f7c62c92233963d8 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main

import (
	"bytes"
	"crypto/ed25519"
	"encoding/base64"
	"encoding/binary"
	"flag"
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
	"time"
)

var pubKey ed25519.PublicKey
var privKey ed25519.PrivateKey
var id string
var servers []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.Write(b)
	if err != nil {
		panic(err)
	}
	_, err = buf.Write(ed25519.Sign(privKey, buf.Bytes()))
	if err != nil {
		panic(err)
	}
	resp, err := http.Post(s, "application/octet-stream", buf)
	if err != nil {
		panic(err)
	}
	fmt.Println(resp)
}

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.PrivateKey(privKeyBytes)
	serversBytes, err := os.ReadFile("servers")
	if err != nil {
		panic(err)
	}
	id = base64.RawURLEncoding.EncodeToString(pubKey)
	servers = strings.Split(string(serversBytes), "\n")
	if servers[0] == "" {
		servers = servers[1:]
	}
	fmt.Println(id, servers)

	if flag.Arg(0) == "add" {
		// Add server
		servers = append(servers, 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)
		}
		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 {
			signPost(servers[0]+"/dht/"+id, []byte(strings.Join(servers, "\n")))
			http.Get(servers[0] + "/user/" + id)
		} else {
			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)
		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)
				signPost(flag.Arg(1)+"/dht/"+id, []byte(strings.Join(servers, "\n")))
				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)
		}
	} else if flag.Arg(0) == "get" {
		user := flag.Arg(1)
		filename := flag.Arg(2)
		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, err := os.ReadFile(filename)
		if err != nil {
			panic(err)
		}
		signPost(servers[0] + "/storage/" + id + "/" + filename, file)
	}
}