/
interactive.go
274 lines (265 loc) · 6.96 KB
/
interactive.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
package cli
import (
"bufio"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/Dliv3/Venom/admin/dispather"
"github.com/Dliv3/Venom/node"
"github.com/Dliv3/Venom/utils"
)
// admin节点想要操作的对端节点的ID,主要用于goto命令
var currentPeerNodeHashID string
func checkCurrentPeerNode() bool {
if currentPeerNodeHashID == "" {
fmt.Println("you should select node first")
return false
} else if node.Nodes[currentPeerNodeHashID] == nil {
fmt.Println("the node is disconnected.")
return false
}
return true
}
func printNetworkMap() {
printed := make(map[string]bool)
fmt.Println("A")
printed[node.CurrentNode.HashID] = true
printEachMap(node.CurrentNode.HashID, 0, printed)
}
func printEachMap(nodeID string, depth int, printed map[string]bool) {
for _, value := range node.GNetworkTopology.NetworkMap[nodeID] {
if _, ok := printed[value]; ok {
continue
}
for i := 0; i < depth; i++ {
fmt.Print(" ")
}
fmt.Print("+ -- ")
fmt.Println(node.GNodeInfo.NodeUUID2Number[value])
printed[value] = true
printEachMap(value, depth+1, printed)
}
}
// Interactive 交互式控制
func Interactive() {
// 处理ctrl c的SIGINT信号
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT) //, syscall.SIGTERM)
shellExit := true
go func() {
for {
<-sigs
if !shellExit {
// ctrl c 处理函数
fmt.Println("Ctrl-C")
} else {
os.Exit(0)
}
}
}()
var nodeID int
var peerNode *node.Node
for {
if currentPeerNodeHashID == "" {
fmt.Print("(admin node) >>> ")
} else {
fmt.Printf("(node %d) >>> ", nodeID)
}
var cmdStr string
fmt.Scanf("%s", &cmdStr)
switch cmdStr {
case "help":
ShowUsage()
case "show":
dispather.SendSyncCmd()
printNetworkMap()
case "setdes":
if !checkCurrentPeerNode() {
break
}
var description string
reader := bufio.NewReader(os.Stdin)
descriptionBytes, _, _ := reader.ReadLine()
description = string(descriptionBytes)
node.GNodeInfo.NodeDescription[currentPeerNodeHashID] = description
case "getdes":
if !checkCurrentPeerNode() {
break
}
fmt.Println(node.GNodeInfo.NodeDescription[currentPeerNodeHashID])
case "goto":
var tmpNodeID int
fmt.Scanf("%d", &tmpNodeID)
fmt.Println("node", tmpNodeID)
if _, ok := node.GNodeInfo.NodeNumber2UUID[tmpNodeID]; ok {
nodeID = tmpNodeID
} else {
fmt.Println("unknown node id.")
break
}
currentPeerNodeHashID = node.GNodeInfo.NodeNumber2UUID[nodeID]
// nextNodeID := node.GNetworkTopology.RouteTable[currentPeerNodeHashID]
// nextNode = node.Nodes[nextNodeID]
peerNode = node.Nodes[currentPeerNodeHashID]
case "listen":
if !checkCurrentPeerNode() {
break
}
var port uint16
fmt.Scanf("%d", &port)
fmt.Println("listen", port)
if port > 65535 || port < 1 {
fmt.Println("port number error.")
break
}
dispather.SendListenCmd(peerNode, port)
case "connect":
if !checkCurrentPeerNode() {
break
}
var ipString string
var port uint16
fmt.Scanf("%s %d", &ipString, &port)
fmt.Println("connect to", ipString, port)
ip := net.ParseIP(ipString)
if ip == nil {
fmt.Println("invalid ip address.")
break
}
dispather.SendConnectCmd(peerNode, ipString, port)
case "socks":
if !checkCurrentPeerNode() {
break
}
var port uint16
fmt.Scanf("%d", &port)
if port > 65535 || port < 1 {
fmt.Println("port number error.")
break
}
dispather.SendSocks5Cmd(peerNode, port)
case "shell":
if !checkCurrentPeerNode() {
break
}
utils.HandleWindowsCR()
fmt.Println("You can execute commands in this shell :D, 'exit' to exit.")
shellExit = false
dispather.SendShellCmd(peerNode)
shellExit = true
continue
case "upload":
if !checkCurrentPeerNode() {
break
}
var localPath string
var remotePath string
fmt.Scanf("%s %s", &localPath, &remotePath)
fmt.Println("upload", localPath, fmt.Sprintf("to node %d:", nodeID), remotePath)
dispather.SendUploadCmd(peerNode, localPath, remotePath)
case "download":
if !checkCurrentPeerNode() {
break
}
var remotePath string
var localPath string
fmt.Scanf("%s %s", &remotePath, &localPath)
fmt.Println("download", localPath, fmt.Sprintf("from node %d:", nodeID), remotePath)
dispather.SendDownloadCmd(peerNode, remotePath, localPath)
case "lforward":
if !checkCurrentPeerNode() {
break
}
var sport uint16
var dport uint16
var lhostString string
fmt.Scanf("%s %d %d", &lhostString, &sport, &dport)
lhost := net.ParseIP(lhostString)
if lhost == nil {
fmt.Println("invalid ip address.")
break
}
fmt.Printf("forward local network %s port %d to remote port %d\n", lhostString, sport, dport)
dispather.SendLForwardCmd(peerNode, sport, lhostString, dport)
case "rforward":
if !checkCurrentPeerNode() {
break
}
var sport uint16
var dport uint16
var rhostString string
fmt.Scanf("%s %d %d", &rhostString, &sport, &dport)
rhost := net.ParseIP(rhostString)
if rhost == nil {
fmt.Println("invalid ip address.")
break
}
fmt.Printf("forward remote network %s port %d to local port %d\n", rhostString, sport, dport)
dispather.SendRForwardCmd(peerNode, rhostString, sport, dport)
case "sshconnect":
// sshconnect user:password@10.1.1.1:22 9999
if !checkCurrentPeerNode() {
break
}
var sshString string
var dport uint16
fmt.Scanf("%s %d", &sshString, &dport)
var sshUser string
var sshHost string
var sshPort uint16
if parts := strings.Split(sshString, "@"); len(parts) > 1 {
sshUser = parts[0]
sshHost = parts[1]
}
parts := strings.Split(sshHost, ":")
sshHost = parts[0]
if len(parts) > 1 {
tmp, _ := strconv.Atoi(parts[1])
sshPort = uint16(tmp)
} else if len(parts) == 1 {
sshPort = 22
}
if net.ParseIP(sshHost) == nil {
fmt.Println("invalid ssh server ip address.")
break
}
fmt.Print("use password (1) / ssh key (2)? ")
var choice uint16
fmt.Scanf("%d", &choice)
switch choice {
case 1:
fmt.Print("password: ")
var password string
fmt.Scanf("%s", &password)
fmt.Printf("connect to target host's %d through ssh tunnel (%s@%s:%d).\n", dport, sshUser, sshHost, sshPort)
dispather.SendSshConnectCmd(peerNode, sshUser, sshHost, sshPort, dport, choice, password)
case 2:
fmt.Print("file path of ssh key: ")
var path string
fmt.Scanf("%s", &path)
sshKey, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("ssh key error:", err)
break
}
fmt.Printf("connect to target host's %d through ssh tunnel (%s@%s:%d).\n", dport, sshUser, sshHost, sshPort)
dispather.SendSshConnectCmd(peerNode, sshUser, sshHost, sshPort, dport, choice, string(sshKey))
default:
fmt.Println("unknown choice.")
break
}
case "exit":
os.Exit(0)
case "":
continue
default:
fmt.Println("unknown command, use \"help\" to see all commands.")
}
utils.HandleWindowsCR()
}
}