Skip to content
This repository was archived by the owner on Jul 28, 2021. It is now read-only.

Commit 3e6681c

Browse files
committed
Some code changes
1 parent 4f4270b commit 3e6681c

File tree

1 file changed

+107
-94
lines changed

1 file changed

+107
-94
lines changed

cmd/cli/main.go

Lines changed: 107 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (uaf *uint16ArrFlags) Set(value string) error {
8080

8181
var (
8282
fwr *p2pforwarder.Forwarder
83+
fwrCancel func()
8384
connections = make(map[string]func())
8485
openTCPPorts = make(map[uint16]func())
8586
openUDPPorts = make(map[uint16]func())
@@ -99,11 +100,9 @@ func main() {
99100

100101
zap.L().Info("Initialization...")
101102

102-
var (
103-
cancel func()
104-
err error
105-
)
106-
fwr, cancel, err = p2pforwarder.NewForwarder()
103+
var err error
104+
105+
fwr, fwrCancel, err = p2pforwarder.NewForwarder()
107106
if err != nil {
108107
zap.S().Error(err)
109108
}
@@ -178,24 +177,26 @@ loop:
178177
case str := <-cmdch:
179178
executeCommand(str)
180179
case <-termSignal:
181-
zap.L().Info("Shutdown...")
182-
183-
cancel()
184-
185-
for _, cancel := range connections {
186-
cancel()
187-
}
188-
for _, cancel := range openTCPPorts {
189-
cancel()
190-
}
191-
for _, cancel := range openUDPPorts {
192-
cancel()
193-
}
194-
180+
shutdown()
195181
break loop
196182
}
197183
}
198184
}
185+
func shutdown() {
186+
zap.L().Info("Shutdown...")
187+
188+
fwrCancel()
189+
190+
for _, cancel := range connections {
191+
cancel()
192+
}
193+
for _, cancel := range openTCPPorts {
194+
cancel()
195+
}
196+
for _, cancel := range openUDPPorts {
197+
cancel()
198+
}
199+
}
199200

200201
func parseArgs(argsStr string, n int) []string {
201202
if n <= 0 {
@@ -233,8 +234,6 @@ func parseArgs(argsStr string, n int) []string {
233234

234235
if argStart != -1 {
235236
args[a] = string(argsRunes[argStart:])
236-
argStart = -1
237-
a++
238237
}
239238

240239
return args
@@ -247,96 +246,110 @@ func executeCommand(str string) {
247246

248247
switch cmd {
249248
case "connect":
250-
id := params[0]
251-
252-
zap.L().Info("Connecting to " + id)
249+
cmdConnect(params)
250+
case "disconnect":
251+
cmdDisconnect(params)
252+
case "open":
253+
cmdOpen(params)
254+
case "close":
255+
cmdClose(params)
256+
default:
257+
zap.L().Info("")
258+
zap.L().Info("Cli commands list:")
259+
zap.L().Info("connect [ID_HERE]")
260+
zap.L().Info("disconnect [ID_HERE]")
261+
zap.L().Info("open [TCP_OR_UDP_HERE] [PORT_NUMBER_HERE]")
262+
zap.L().Info("close [UDP_OR_UDP_HERE] [PORT_NUMBER_HERE]")
263+
zap.L().Info("")
264+
}
265+
}
253266

254-
listenip, cancel, err := fwr.Connect(id)
255-
if err != nil {
256-
zap.S().Error(err)
257-
return
258-
}
267+
func cmdConnect(params []string) {
268+
id := params[0]
259269

260-
connections[id] = cancel
270+
zap.L().Info("Connecting to " + id)
261271

262-
zap.L().Info("Connections to " + id + "'s ports are listened on " + listenip)
272+
listenip, cancel, err := fwr.Connect(id)
273+
if err != nil {
274+
zap.S().Error(err)
275+
return
276+
}
263277

264-
case "disconnect":
265-
id := params[0]
278+
connections[id] = cancel
266279

267-
close := connections[id]
280+
zap.L().Info("Connections to " + id + "'s ports are listened on " + listenip)
281+
}
268282

269-
if close == nil {
270-
zap.L().Error("You are not connected to specified id")
271-
return
272-
}
283+
func cmdDisconnect(params []string) {
284+
id := params[0]
273285

274-
zap.L().Info("Disconnecting from " + id)
286+
close := connections[id]
275287

276-
close()
288+
if close == nil {
289+
zap.L().Error("You are not connected to specified id")
290+
return
291+
}
277292

278-
delete(connections, id)
293+
zap.L().Info("Disconnecting from " + id)
279294

280-
case "open":
281-
networkType := strings.ToLower(params[0])
295+
close()
282296

283-
portStr := params[1]
284-
portUint64, err := strconv.ParseUint(portStr, 10, 16)
285-
if err != nil {
286-
zap.S().Error(err)
287-
return
288-
}
289-
port := uint16(portUint64)
297+
delete(connections, id)
298+
}
290299

291-
zap.L().Info("Opening " + networkType + ":" + portStr)
300+
func cmdOpen(params []string) {
301+
networkType := strings.ToLower(params[0])
292302

293-
cancel, err := fwr.OpenPort(networkType, port)
294-
if err != nil {
295-
zap.S().Error(err)
296-
return
297-
}
303+
portStr := params[1]
304+
portUint64, err := strconv.ParseUint(portStr, 10, 16)
305+
if err != nil {
306+
zap.S().Error(err)
307+
return
308+
}
309+
port := uint16(portUint64)
298310

299-
switch networkType {
300-
case "tcp":
301-
openTCPPorts[port] = cancel
302-
case "udp":
303-
openUDPPorts[port] = cancel
304-
}
305-
case "close":
306-
networkType := strings.ToLower(params[0])
307-
portStr := params[1]
308-
portUint64, err := strconv.ParseUint(portStr, 10, 16)
309-
if err != nil {
310-
zap.S().Error(err)
311-
return
312-
}
313-
port := uint16(portUint64)
314-
315-
var cancel func()
316-
switch networkType {
317-
case "tcp":
318-
cancel = openTCPPorts[port]
319-
case "udp":
320-
cancel = openUDPPorts[port]
321-
}
311+
zap.L().Info("Opening " + networkType + ":" + portStr)
322312

323-
if cancel == nil {
324-
zap.L().Error("Specified port is not opened")
325-
return
326-
}
313+
cancel, err := fwr.OpenPort(networkType, port)
314+
if err != nil {
315+
zap.S().Error(err)
316+
return
317+
}
327318

328-
zap.L().Info("Closing " + networkType + ":" + portStr)
319+
switch networkType {
320+
case "tcp":
321+
openTCPPorts[port] = cancel
322+
case "udp":
323+
openUDPPorts[port] = cancel
324+
}
325+
}
329326

330-
cancel()
327+
func cmdClose(params []string) {
328+
networkType := strings.ToLower(params[0])
329+
portStr := params[1]
330+
portUint64, err := strconv.ParseUint(portStr, 10, 16)
331+
if err != nil {
332+
zap.S().Error(err)
333+
return
334+
}
335+
port := uint16(portUint64)
336+
337+
var cancel func()
338+
switch networkType {
339+
case "tcp":
340+
cancel = openTCPPorts[port]
341+
case "udp":
342+
cancel = openUDPPorts[port]
343+
}
331344

332-
delete(openTCPPorts, port)
333-
default:
334-
zap.L().Info("")
335-
zap.L().Info("Cli commands list:")
336-
zap.L().Info("connect [ID_HERE]")
337-
zap.L().Info("disconnect [ID_HERE]")
338-
zap.L().Info("open [TCP_OR_UDP_HERE] [PORT_NUMBER_HERE]")
339-
zap.L().Info("close [UDP_OR_UDP_HERE] [PORT_NUMBER_HERE]")
340-
zap.L().Info("")
345+
if cancel == nil {
346+
zap.L().Error("Specified port is not opened")
347+
return
341348
}
349+
350+
zap.L().Info("Closing " + networkType + ":" + portStr)
351+
352+
cancel()
353+
354+
delete(openTCPPorts, port)
342355
}

0 commit comments

Comments
 (0)