-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
267 lines (235 loc) · 6.51 KB
/
main.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"errors"
"fmt"
"log"
"sync"
"net"
"net/http"
"strconv"
"time"
"strings"
)
type PeerTime int64
// I'd like to use generics to optimize IPv4 addresses
// to be [4]byte when specified but Go gives me a hard
// time with that right now...
type PeerDatabase struct {
Lookup map[[16]byte]PeerTime
Mutex sync.RWMutex
};
func makePeerTime(timestamp int32, port uint16, expires uint8) PeerTime {
return PeerTime(int64(timestamp) << 32 | int64(port) << 16 | int64(expires))
}
func (peertime PeerTime) Fields() (int32, uint16, uint8) {
return int32(peertime >> 32), uint16((peertime >> 16) & 0xffff), uint8(peertime & 0xff)
}
func (database *PeerDatabase) Add(ip net.IP, timestamp int32, port uint16, expires uint8) int32 {
database.Mutex.Lock()
defer database.Mutex.Unlock()
var ipBytes [16]byte
var timestampNew = timestamp
copy(ipBytes[:], ip)
peertime, ok := database.Lookup[ipBytes]
if ok {
prevStamp, prevPort, _ := peertime.Fields()
if (prevPort == port) {
timestampNew = prevStamp
}
}
database.Lookup[ipBytes] = makePeerTime(timestampNew, port, expires)
return timestampNew
}
func (database *PeerDatabase) Tick(delta uint8) {
database.Mutex.Lock()
defer database.Mutex.Unlock()
for ip, peertime := range database.Lookup {
timestamp, port, expires := peertime.Fields()
if expires <= delta {
log.Printf("%v expired!", ip);
delete(database.Lookup, ip)
} else {
database.Lookup[ip] = makePeerTime(timestamp, port, expires - delta)
}
}
}
func dumpIpPortPair(ip net.IP, port uint16) string {
if (len(ip) == 16) {
return fmt.Sprintf("[%s]:%d", ip.String(), port)
} else {
return fmt.Sprintf("%s:%d", ip.String(), port)
}
}
func (database *PeerDatabase) Dump(lower int32, portFilter uint16, subnet net.IPNet) string {
database.Mutex.RLock()
defer database.Mutex.RUnlock()
var response strings.Builder
for ipBytes, peertime := range database.Lookup {
current, port, _ := peertime.Fields()
ip := net.IP(ipBytes[:len(subnet.IP)])
if current < lower || !subnet.Contains(ip) || (portFilter != 0 && portFilter != port) {
continue
}
response.WriteString(fmt.Sprintf("\"%s\"", dumpIpPortPair(ip, port)))
response.WriteString(",")
}
return response.String()
}
var v4Database = PeerDatabase{ Lookup: make(map[[16]byte]PeerTime) }
var v6Database = PeerDatabase{ Lookup: make(map[[16]byte]PeerTime) }
var srvEpoch = time.Now()
var expireInit = uint8(60)
var expirePeriod = uint8(10)
const portDefault = uint16(443)
func getRemoteIp(addr string) (net.IP, error) {
var ip []byte
host, _, err := net.SplitHostPort(addr)
if err != nil {
return ip, err
}
ipParsed := net.ParseIP(host)
if ipParsed != nil {
ip = ipParsed
return ip, nil
}
return ip, errors.New(fmt.Sprintf("IP of remote addr '%s' not found!", addr))
}
func handleRegister(w http.ResponseWriter, r *http.Request) (int, error) {
err := r.ParseForm()
if err != nil {
return http.StatusBadRequest, err
}
timestamp := int32(time.Now().Sub(srvEpoch) / 1_000_000_000)
var port = portDefault
ports := r.PostForm["port"]
if len(ports) != 0 {
parsed, err := strconv.ParseUint(ports[0], 10, 16)
if err != nil {
return http.StatusBadRequest, err
}
port = uint16(parsed)
}
ip, err := getRemoteIp(r.RemoteAddr)
if err != nil {
return http.StatusInternalServerError, err
}
ip4, expires := ip.To4(), expireInit
var timestampNew int32
if ip4 != nil {
timestampNew = v4Database.Add(ip4, timestamp, port, expires)
} else {
timestampNew = v6Database.Add(ip, timestamp, port, expires)
}
fmt.Fprintf(w, "{\"peer\":\"%s\",\"timestamp\":%d,\"expires\":%d}",
dumpIpPortPair(ip, port), timestampNew, expires)
return http.StatusOK, nil
}
func handleQuery(w http.ResponseWriter, r *http.Request) (int, error) {
err := r.ParseForm()
if err != nil {
return http.StatusBadRequest, err
}
timestamps, preferences := r.Form["timestamp"], r.Form["prefer"]
subnets4, subnets6 := r.Form["subnet4"], r.Form["subnet6"]
ports := r.Form["port"]
now := (time.Now().Sub(srvEpoch) / 1_000_000_000)
var _, subnet4, _ = net.ParseCIDR("0.0.0.0/0")
var _, subnet6, _ = net.ParseCIDR("::/0")
var port = uint16(0)
var timestamp = int32(0)
var prefer = "4";
if len(ports) != 0 {
parsed, err := strconv.ParseUint(ports[0], 10, 16)
if err != nil {
return http.StatusBadRequest, err
}
port = uint16(parsed)
}
if len(timestamps) != 0 {
parsed, err := strconv.ParseInt(timestamps[0], 10, 32)
if err != nil {
return http.StatusBadRequest, err
}
timestamp = int32(parsed)
}
if len(preferences) != 0 {
prefer = preferences[0]
}
if prefer != "4" && prefer != "6" {
return http.StatusBadRequest,
errors.New(fmt.Sprintf(
"Expected preference of '4' or '6', got '%s'",
prefer))
}
if len(subnets4) != 0 {
_, tmp, err := net.ParseCIDR(subnets4[0])
if err != nil {
return http.StatusBadRequest, err
}
if len(tmp.IP) != 4 {
return http.StatusBadRequest,
errors.New(fmt.Sprintf(
"Specified IPv6 CIDR '%s' as IPv4 subnet",
tmp.String()))
}
subnet4 = tmp
}
if len(subnets6) != 0 {
_, tmp, err := net.ParseCIDR(subnets6[0])
if err != nil {
return http.StatusBadRequest, err
}
if len(tmp.IP) != 16 {
return http.StatusBadRequest,
errors.New(fmt.Sprintf(
"Specified IPv4 CIDR '%s' as IPv6 subnet",
tmp.String()))
}
subnet6 = tmp
}
var response = ""
var peers = ""
if prefer == "4" {
peers = v4Database.Dump(timestamp, port, *subnet4) + v6Database.Dump(timestamp, port, *subnet6)
} else {
peers = v6Database.Dump(timestamp, port, *subnet6) + v4Database.Dump(timestamp, port, *subnet4)
}
if len(peers) != 0 {
peers = peers[:len(peers) - 1]
}
response = fmt.Sprintf("{\"now\":%d,\"peers\":[%s]}", now, peers)
log.Print(response)
fmt.Fprint(w, response)
return http.StatusOK, nil
}
func main() {
go func() {
period := time.Duration(expirePeriod) * time.Second
timer := time.NewTimer(period)
for {
<-timer.C
log.Print("Expire period elapsed!\n")
v4Database.Tick(expirePeriod)
v6Database.Tick(expirePeriod)
timer.Reset(period)
}
}();
http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
code, err := handleRegister(w, r)
if err != nil {
w.WriteHeader(code)
log.Printf("[ERROR /register]: %s", err)
fmt.Fprint(w, err)
}
})
http.HandleFunc("/query", func(w http.ResponseWriter, r *http.Request) {
code, err := handleQuery(w, r)
if err != nil {
w.WriteHeader(code)
log.Printf("[ERROR /query]: %s", err)
fmt.Fprint(w, err)
}
})
log.Print("Listening on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}