aboutsummaryrefslogtreecommitdiff
path: root/client/main.go
blob: d490d5638595400f24e1809dcaddcb977a7202b1 (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
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

// Post new server list to DHT
func dhtPost(s string) {
	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"))
	if err != nil {
		panic(err)
	}
	var message []byte
	_, err = buf.Read(message)
	if err != nil {
		panic(err)
	}
	_, err = buf.Write(ed25519.Sign(privKey, message))
	if err != nil {
		panic(err)
	}
	fmt.Println(http.Post(s+"/dht/"+id, "application/octet-stream", buf))
}

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