aboutsummaryrefslogtreecommitdiff
path: root/server/storage.go
blob: e845dd5b246add0fe47ad4fc9ac8cdf83157f255 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main

import (
	"crypto/ed25519"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"
)

// Replicate a user's log to another server
func replicate(id, s string) {
	log.Printf("Starting replication for %s %s", id, s)
	for true {
		mu.Lock()
		// Make sure that this server is still the primary for this user
		user, ok := users[id]
		if !ok {
			mu.Unlock()
			return
		}
		if me != user.servers[0] {
			user.nextIndex = nil
			mu.Unlock()
			return
		}
		// Make sure that the target server is still associated with this user
		idx, ok := user.nextIndex[s]
		if !ok {
			mu.Unlock()
			return
		}
		if idx == len(user.log) {
			// Up to date
			mu.Unlock()
			time.Sleep(50 * time.Millisecond)
			continue
		}

		op := user.log[idx]
		mu.Unlock()
		file, _ := os.Open(op)
		resp, err := http.Post(s+"/storage/"+id+"/"+op+"?idx="+fmt.Sprint(idx), "application/octet-stream", file)
		if err != nil {
			time.Sleep(50 * time.Millisecond)
			continue
		}
		b, err := io.ReadAll(resp.Body)
		if err != nil {
			time.Sleep(50 * time.Millisecond)
			continue
		}

		mu.Lock()
		user.nextIndex[s], _ = strconv.Atoi(string(b))
		mu.Unlock()
	}
}

// Handle storage requests
func storageHandler(w http.ResponseWriter, r *http.Request) {
	pathSplit := strings.Split(r.URL.Path, "/")
	id := pathSplit[2]
	filename := pathSplit[3]
	r.ParseForm()
	if r.Method == "GET" {
		if r.Form.Has("direct") {
			// Directly read and respond with file
			file, err := os.ReadFile(dataDir + "/" + id + "/" + filename)
			if err != nil {
				log.Print(err)
				w.WriteHeader(http.StatusNotFound)
				return
			}
			w.Write(file)
			return
		}
		val := dhtGet(id, false)
		err := verify(id, val)
		if err != nil {
			verify(id, val)
			w.WriteHeader(http.StatusNotFound)
			return
		}
		if _, ok := users[id]; ok {
			reconfigure(id, val)
		}
		servers := strings.Split(string(val[8:len(val)-ed25519.SignatureSize]), "\n")
		if servers[0] == me {
			file, err := os.ReadFile(dataDir + "/" + id + "/" + filename)
			if err != nil {
				log.Print(err)
				w.WriteHeader(http.StatusNotFound)
				return
			}
			w.Write(file)
			return
		}
		for _, server := range servers {
			resp, err := http.Get(server + "/storage/" + id + "/" + filename)
			if err != nil {
				continue
			}
			b, err := io.ReadAll(resp.Body)
			if err != nil {
				continue
			}
			w.Write(b)
			return
		}
		w.WriteHeader(http.StatusNotFound)
	} else if r.Method == "POST" {
		mu.Lock()
		defer mu.Unlock()
		user, ok := users[id]
		if !ok {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		b, err := io.ReadAll(r.Body)
		if err != nil {
			log.Print(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		err = verify(id, b)
		if err != nil {
			log.Print(err)
			w.WriteHeader(http.StatusUnauthorized)
			return
		}

		if r.Form.Has("idx") {
			idx, err := strconv.Atoi(r.Form.Get("idx"))
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				return
			}

			if idx > len(user.log) {
				// Missing log entries
				w.Write([]byte(fmt.Sprint(len(user.log))))
				return
			}
			if idx < len(user.log) {
				// Too many log entries
				ops := make(map[string]interface{})
				for i := idx; i < len(user.log); i++ {
					ops[user.log[i]] = nil
				}
				for op := range ops {
					// Fetch older version of file
					resp, err := http.Get(user.servers[0] + "/storage/" + id + "/" + op)
					if err != nil {
						log.Print(err)
						w.WriteHeader(http.StatusInternalServerError)
						return
					}
					b, err := io.ReadAll(resp.Body)
					if err != nil {
						log.Print(err)
						w.WriteHeader(http.StatusInternalServerError)
						return
					}
					err = os.WriteFile(dataDir+"/"+id+"/"+op, b, 0644)
					if err != nil {
						log.Print(err)
						w.WriteHeader(http.StatusInternalServerError)
						return
					}
				}
			}
		}

		err = os.WriteFile(dataDir+"/"+id+"/"+filename, b, 0644)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		user.log = append(user.log, filename)
	}
}