-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
380 lines (332 loc) · 7.68 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"bytes"
"encoding/json"
"fmt"
"hash/fnv"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
var networkHash uint32
var appdataPath = os.Getenv("appdata")
var f *os.File
var name string
var network = make(map[string]string)
var onlineTimes = make(map[string]int64)
var port = ":9999"
var mux = sync.Mutex{}
var chat *tview.TextView
var online *tview.TextView
var uiMain *tview.Flex
var app = tview.NewApplication()
var debug = false
var myIP = ownIP()
var ipregex = regexp.MustCompile(`^([0-9]{1,3}[.]){3}[0-9]{1,3}$`)
var colors = []string{"maroon", "green", "olive", "navy", "purple", "teal", "silver", "gray", "red", "lime", "yellow", "blue", "fuchsia", "aqua", "white"}
func main() {
defer f.Close()
if len(os.Args) == 2 && os.Args[1] == "-d" {
debug = true
df, _ := os.Create(appdataPath + "/quicky.log")
f = df
}
// myIP
appdataPath = appdataPath + "/quicky.json"
var netJSON, err = ioutil.ReadFile(appdataPath)
debugLog(appdataPath)
if err == nil {
json.Unmarshal([]byte(netJSON), &network)
}
if val, ok := network[myIP]; ok {
debugLog(val)
name = val
} else {
network[myIP] = "Anon"
name = "Anon"
}
netHash()
onlineTimes[myIP] = time.Now().Unix()
uiMain, chat, online = ui()
// listener
go func() {
laddr, err := net.ResolveUDPAddr("udp", port)
udp, err := net.ListenUDP("udp", laddr)
defer udp.Close()
if err != nil {
panic("Wtf")
}
buf := make([]byte, 1024)
for {
len, addr, _ := udp.ReadFromUDP(buf)
receive(string(buf[:len]), addr.IP.String())
}
}()
// pinger
go func() {
for {
sendAll(fmt.Sprintf("/ping %s %d", name, networkHash))
time.Sleep(5 * time.Second)
}
}()
// online updater
go func() {
for {
var onlineNames bytes.Buffer
onlineCount := 0
mux.Lock()
now := time.Now().Unix()
ips := make([]string, 0, len(network))
for ip := range network {
ips = append(ips, ip)
}
sort.Strings(ips)
for _, ip := range ips {
if val, ok := onlineTimes[ip]; ok {
if now-val < 15 {
onlineNames.WriteString(fmt.Sprintf("[%s]%s %s\n", colors[colorIndex(ip)], network[ip], ip))
onlineCount++
}
}
}
mux.Unlock()
online.SetText(onlineNames.String())
app.Draw()
fmt.Fprintf(f, "online: %s", onlineNames.String())
online.SetTitle(fmt.Sprintf("Online (%d)", onlineCount))
time.Sleep(5 * time.Second)
}
}()
if err := app.SetRoot(uiMain, true).Run(); err != nil {
panic(err)
}
}
func send(msg string) {
msg = strings.TrimSpace(msg)
if msg[0] == '/' {
words := strings.Fields(msg)
if len(words) == 2 {
switch words[0] {
case "/add":
add(words[1])
case "/name":
setName(words[1], myIP)
name = words[1]
default:
fmt.Fprint(chat, "[red]quicky [white]unkown command\n")
}
} else {
fmt.Fprint(chat, "[red]quicky [white]unkown command\n")
}
} else {
sendAll(fmt.Sprintf("quicky %s", msg))
}
}
func add(ip string) {
if ipregex.Match([]byte(ip)) {
go sendSingle(fmt.Sprintf("/add %s", name), ip)
setName("Anon", ip)
} else {
fmt.Fprint(chat, "[red]quicky [white]invalid ip\n")
}
}
func receive(rec string, addr string) {
if strings.HasPrefix(rec, "quicky") {
recvMessage(rec[len("quicky")+1:], addr)
}
if strings.HasPrefix(rec, "/network") {
recNetwork(rec[len("/network")+1:])
} else if rec[0] == '/' {
words := strings.Fields(rec)
if len(words) == 2 {
words := strings.Fields(rec)
switch words[0] {
case "/add":
recvAdd(words[1], addr)
}
} else if len(words) == 3 {
switch words[0] {
case "/add-single":
setName(words[1], words[2])
case "/ping":
setName(words[1], addr)
syncNetworks(words[2], addr)
updateOnline(addr)
}
}
}
}
func syncNetworks(hstr, ip string) {
n, _ := strconv.ParseUint(hstr, 0, 32)
if uint32(n) != networkHash {
sendSingle(fmt.Sprintf("/network %s", string(networkMarshall())), ip)
}
}
func updateOnline(ip string) {
mux.Lock()
onlineTimes[ip] = time.Now().Unix()
mux.Unlock()
}
func recvMessage(msg, addr string) {
mux.Lock()
fmt.Fprintf(f, "recv: %s: %s\n", network[addr], msg)
fmt.Fprintf(chat, "[%s]%s: %s\n", colors[colorIndex(addr)], network[addr], msg)
mux.Unlock()
}
// C adds B
func recvAdd(name string, ip string) {
// B tells C about everyone
sendSingle(fmt.Sprintf("/network %s", string(networkMarshall())), ip)
// B tells everyone about C
sendAll(fmt.Sprintf("/add-single %s %s", name, ip))
setName(name, ip)
}
func recNetwork(jsonStr string) {
debugLog(jsonStr)
newNetwork := make(map[string]string)
json.Unmarshal([]byte(jsonStr), &newNetwork)
mux.Lock()
for k, v := range newNetwork {
network[k] = v
}
mux.Unlock()
saveState()
}
func sendAll(msg string) {
mux.Lock()
for ip := range network {
go sendSingle(msg, ip)
}
mux.Unlock()
}
func sendSingle(msg string, ip string) {
debugLog(fmt.Sprintf("sent: %s, ip: %s", msg, ip+port))
conn, err := net.Dial("udp", ip+port)
if err == nil {
fmt.Fprint(conn, msg)
conn.Close()
}
}
func setName(name string, ip string) {
mux.Lock()
debugLog(fmt.Sprintf("setName: %s %s", name, ip))
network[ip] = name
mux.Unlock()
saveState()
}
func saveState() {
netHash()
ioutil.WriteFile(appdataPath, networkMarshall(), 0644)
}
func networkMarshall() []byte {
mux.Lock()
networkBytes, _ := json.Marshal(network)
mux.Unlock()
return networkBytes
}
func debugLog(log string) {
if debug {
fmt.Fprintln(f, log)
}
}
func ui() (*tview.Flex, *tview.TextView, *tview.TextView) {
online := tview.NewTextView().
SetDynamicColors(true).
SetScrollable(true)
input := tview.NewInputField().
SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return true
}).SetPlaceholder("my ip " + myIP + " /name [name], /add [ip], /exit").SetPlaceholderTextColor(tcell.ColorAqua)
input.SetDoneFunc(func(key tcell.Key) {
txt := input.GetText()
if len(txt) > 0 {
if txt == "/exit" {
app.Stop()
} else {
send(txt)
}
}
input.SetText("")
})
chat := tview.NewTextView().
SetScrollable(true).
SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyTAB {
input.GetFocusable()
}
}).SetDynamicColors(true)
chat.SetChangedFunc(func() {
app.Draw()
})
chat.SetBorder(true).SetTitle("Chat")
online.SetBorder(true).SetTitle("Online")
flex := tview.NewFlex().
AddItem(tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(online, 0, 1, false).
AddItem(chat, 0, 3, false), 0, 1, false).
SetDirection(tview.FlexRow).
AddItem(input, 1, 1, true)
boxes := []tview.Primitive{input, online, chat}
focus := 0
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTAB {
if focus == 3 {
focus = 0
}
app.SetFocus(boxes[focus])
focus++
debugLog(fmt.Sprintf("focus %d\n", focus))
}
return event
})
return flex, chat, online
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func colorIndex(s string) uint32 {
debugLog(fmt.Sprintf("colorindex: %d", hash(s)%uint32(len(colors))))
return hash(s) % uint32(len(colors))
}
func netHash() {
mux.Lock()
keys := make([]string, 0, len(network))
for k := range network {
keys = append(keys, k)
}
mux.Unlock()
sort.Strings(keys)
networkHash = hash(strings.Join(keys, ""))
}
func ownIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
// func isIpValid(ip string) bool {
// return ipregex.Match(ip)
// hostname, err := net.LookupHost(ip)
// if err != nil {
// return false
// }
// if len(hostname) > 0 {
// debugLog("ip checked: " + hostname[0])
// }
// return true
// }