heron
2017-08-12 74b76f59383e9871cac132abc479d732b8309e69
add shell
1 files modified
2 files added
443 ■■■■■ changed files
src/accessmotion.my/cmd/debug.go 5 ●●●●● patch | view | raw | blame | history
src/accessmotion.my/cmd/shell.go 30 ●●●●● patch | view | raw | blame | history
src/accessmotion.my/shell/shell.go 408 ●●●●● patch | view | raw | blame | history
src/accessmotion.my/cmd/debug.go
....@@ -12,9 +12,8 @@
1212 Use: "on",
1313 Short: "debug on MASK",
1414 Long: `MASK: 0:off 1:system 2:dns 4:err 8:domain 16:sesion 32:uri 64:aaa 128:tx 256:gtp 512:service 1024:ssl
15
- (example) 1209 = 1: system + 8: domain + 16: sesion + 32:uri + 128:tx + 1024:ssl
16
- # debug on 1209
17
- `,
15
+(example) 1209 = 1: system + 8: domain + 16: sesion + 32:uri + 128:tx + 1024:ssl
16
+# debug on 1209`,
1817 Run: func(cmd *cobra.Command, args []string) {
1918 cmdName := "echo"
2019 input := strings.Join(args, " ")
src/accessmotion.my/cmd/shell.go
....@@ -0,0 +1,30 @@
1
+package cmd
2
+
3
+import (
4
+ "github.com/spf13/cobra"
5
+ ss "accessmotion.my/shell"
6
+)
7
+
8
+func init() {
9
+ RootCmd.AddCommand(shell)
10
+}
11
+
12
+var shell = &cobra.Command{
13
+ Use: "shell",
14
+ Short: "shell",
15
+ Run: func(cmd *cobra.Command, args []string) {
16
+ ss.Shell()
17
+ },
18
+}
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
src/accessmotion.my/shell/shell.go
....@@ -0,0 +1,408 @@
1
+package shell
2
+
3
+import (
4
+ "github.com/chzyer/readline"
5
+ "fmt"
6
+ "log"
7
+ "io"
8
+ "strings"
9
+ "io/ioutil"
10
+ "strconv"
11
+ "os/exec"
12
+)
13
+
14
+func usage(w io.Writer) {
15
+ io.WriteString(w, "commands:\n")
16
+ io.WriteString(w, completer.Tree(" "))
17
+}
18
+
19
+// Function constructor - constructs new function for listing given directory
20
+func listFiles(path string) func(string) []string {
21
+ return func(line string) []string {
22
+ names := make([]string, 0)
23
+ files, _ := ioutil.ReadDir(path)
24
+ for _, f := range files {
25
+ names = append(names, f.Name())
26
+ }
27
+ return names
28
+ }
29
+}
30
+
31
+var completer = readline.NewPrefixCompleter(
32
+
33
+
34
+ readline.PcItem("help"),
35
+
36
+ readline.PcItem("logTransfer",
37
+ readline.PcItem("on"),
38
+ readline.PcItem("off"),
39
+ ),
40
+ readline.PcItem("whois"),
41
+
42
+
43
+ readline.PcItem("show",
44
+ readline.PcItem("configs",
45
+ ),
46
+ readline.PcItem("injection",
47
+ readline.PcItem("status"),
48
+ ),
49
+
50
+ readline.PcItem("user-adb"),
51
+
52
+ readline.PcItem("user-pct"),
53
+ ),
54
+
55
+ readline.PcItem("set",
56
+ readline.PcItem("injection",
57
+ readline.PcItem("on"),
58
+ readline.PcItem("off"),
59
+ ),
60
+ readline.PcItem("debug",
61
+ readline.PcItem("session"),
62
+ readline.PcItem("injection"),
63
+ readline.PcItem("on"),
64
+ readline.PcItem("off"),
65
+ ),
66
+ readline.PcItem("url"),
67
+ ),
68
+
69
+ readline.PcItem("exit"),
70
+)
71
+
72
+func filterInput(r rune) (rune, bool) {
73
+ switch r {
74
+ // block CtrlZ feature
75
+ case readline.CharCtrlZ:
76
+ return r, false
77
+ }
78
+ return r, true
79
+}
80
+
81
+
82
+
83
+func Shell() {
84
+ l, err := readline.NewEx(&readline.Config{
85
+ Prompt: "IPES-SHELL>> ",
86
+ HistoryFile: "/tmp/IPES-SHELL.tmp",
87
+ AutoComplete: completer,
88
+ InterruptPrompt: "^C",
89
+ EOFPrompt: "exit",
90
+
91
+ HistorySearchFold: true,
92
+ FuncFilterInputRune: filterInput,
93
+ })
94
+ if err != nil {
95
+ panic(err)
96
+ }
97
+ defer l.Close()
98
+
99
+ setPasswordCfg := l.GenPasswordConfig()
100
+ setPasswordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
101
+ l.SetPrompt(fmt.Sprintf("Enter password(%v): ", len(line)))
102
+ l.Refresh()
103
+ return nil, 0, false
104
+ })
105
+
106
+ log.SetOutput(l.Stderr())
107
+ for {
108
+ line, err := l.Readline()
109
+ if err == readline.ErrInterrupt {
110
+ if len(line) == 0 {
111
+ break
112
+ } else {
113
+ continue
114
+ }
115
+ } else if err == io.EOF {
116
+ break
117
+ }
118
+
119
+ line = strings.TrimSpace(line)
120
+ switch {
121
+ case strings.HasPrefix(line, "show"):
122
+ line := strings.TrimSpace(line[4:])
123
+ if len(line) == 0 {
124
+ usage(l.Stderr())
125
+ break
126
+ }
127
+ switch {
128
+ case strings.HasPrefix(line, "user"):
129
+ showUser(line)
130
+ break
131
+ case strings.HasPrefix(line, "config"):
132
+ showConfig()
133
+ break
134
+ case strings.HasPrefix(line, "injection"):
135
+ line = strings.TrimSpace(line[9:])
136
+ switch {
137
+ case strings.HasPrefix(line, "status"):
138
+ showInjectionStatus()
139
+ break
140
+ }
141
+ }
142
+ case strings.HasPrefix(line, "set"):
143
+ line := strings.TrimSpace(line[3:])
144
+ if len(line) == 0 {
145
+ usage(l.Stderr())
146
+ break
147
+ }
148
+ switch {
149
+ case strings.HasPrefix(line, "debug"):
150
+ line = strings.TrimSpace(line[5:])
151
+ switch {
152
+ case strings.HasPrefix(line, "on"):
153
+ break
154
+ case strings.HasPrefix(line, "off"):
155
+ break
156
+ case strings.HasPrefix(line, "session"):
157
+ break
158
+ case strings.HasPrefix(line, "injection"):
159
+ break
160
+ }
161
+ case strings.HasPrefix(line, "injection"):
162
+ line = strings.TrimSpace(line[9:])
163
+ switch {
164
+ case strings.HasPrefix(line, "on"):
165
+ break
166
+ case strings.HasPrefix(line, "off"):
167
+ break
168
+ }
169
+ }
170
+ case strings.HasPrefix(line, "whois"):
171
+ line := strings.TrimSpace(line[5:])
172
+ if len(line) == 0 {
173
+ usage(l.Stderr())
174
+ break
175
+ }
176
+ whois(strings.TrimSpace(line))
177
+ break
178
+ case line == "help":
179
+ usage(l.Stderr())
180
+ case line == "exit":
181
+ goto exit
182
+ case line == "":
183
+ default:
184
+ log.Println("you command:", strconv.Quote(line))
185
+ usage(l.Stderr())
186
+ }
187
+ }
188
+exit:
189
+}
190
+
191
+
192
+func showInjectionStatus(){
193
+ out, err := exec.Command("cat","/proc/ipswitch/configs").Output()
194
+ if err != nil {
195
+ fmt.Printf("err %s", err)
196
+ }
197
+ var outputs []string = strings.Split(string(out),"\n")
198
+ i:=0
199
+ for i < 61 {
200
+ var output []string = strings.Split(string(outputs[i]),",")
201
+ if "41" == string(output[1]){
202
+ if strings.TrimSpace(string(output[3])) == "1"{
203
+ fmt.Printf("%s\n", "injection status off")
204
+ }else if strings.TrimSpace(string(output[3])) == "0"{
205
+ fmt.Printf("%s\n", "injection status on")
206
+ }
207
+ }else{
208
+ }
209
+ i = i + 1
210
+ }
211
+}
212
+
213
+func showUser(service string) {
214
+
215
+ serviceCode := ""
216
+ if service == "user-pct"{
217
+ serviceCode = "23"
218
+ }else if service == "user-adb"{
219
+ serviceCode = "24"
220
+ }
221
+
222
+
223
+ out, err := exec.Command("cat","/proc/ipswitch/configs").Output()
224
+ if err != nil {
225
+ fmt.Printf("err %s", err)
226
+ }
227
+ var outputs []string = strings.Split(string(out),"\n")
228
+ i:=0
229
+ for i < 61 {
230
+ var output []string = strings.Split(string(outputs[i]),",")
231
+ if serviceCode == string(output[1]){
232
+
233
+ fmt.Printf("%s %s\n",
234
+ strings.TrimSpace(string(output[3])),
235
+ service)
236
+ }else{
237
+ }
238
+ i = i + 1
239
+ }
240
+}
241
+
242
+func showConfig(){
243
+ out, err := exec.Command("cat","/proc/ipswitch/configs").Output()
244
+ if err != nil {
245
+ fmt.Printf("err %s", err)
246
+ }
247
+ var outputs []string = strings.Split(string(out),"\n")
248
+ i:=0
249
+ for i < 61 {
250
+ //fmt.Printf("%s\n",strings.TrimSpace(string(outputs[i])))
251
+ desc, prs := configDesc[i]
252
+ if prs == false{
253
+
254
+ }else {
255
+ var output []string = strings.Split(string(outputs[i]),",")
256
+ fmt.Printf("value: %s -- %s \n",
257
+ strings.TrimSpace(string(output[3])),desc)
258
+ }
259
+ i = i + 1
260
+ }
261
+}
262
+
263
+func setDebug(){
264
+
265
+}
266
+
267
+func whois(number string){
268
+ cmdName := "echo"
269
+ whoisCmd := strings.Join([]string{cmdName,number,">","/proc/ipswitch/whois"}," ")
270
+ out, err := exec.Command("bash","-c",whoisCmd).Output()
271
+ if err != nil {
272
+ fmt.Printf("err %s", err)
273
+ }
274
+
275
+ out, err = exec.Command("cat","/proc/ipswitch/whois").Output()
276
+ if err != nil {
277
+ fmt.Printf("err %s", err)
278
+ }
279
+
280
+
281
+ var outputs []string = strings.Split(string(out),"\n")
282
+
283
+ i:=0
284
+ len := len(outputs)
285
+ for i < len -1 {
286
+ var output []string = strings.Split(string(outputs[i]),",")
287
+
288
+ fmt.Printf("%s:%s number:%s ip:%s\n",
289
+ strings.TrimSpace(string(output[7])),strings.TrimSpace(string(output[8])),
290
+ output[1],strings.TrimSpace(string(output[2])))
291
+ i = i + 1
292
+ }
293
+}
294
+
295
+func setConfig(configName string,valueType string, value string){
296
+ cmdName := "echo"
297
+ input :=strings.TrimSpace(value)
298
+
299
+ desc, prs := configNames[configName]
300
+ if prs == false{
301
+ }else {
302
+ mask := strings.Join([]string{"u",string(desc),valueType,input}, ",")
303
+ whoisCmd := strings.Join([]string{cmdName,mask,">","/proc/ipswitch/configs"}," ")
304
+ out, err := exec.Command("bash","-c",whoisCmd).Output()
305
+ if err != nil {
306
+ fmt.Printf("err %s", err)
307
+ }
308
+ fmt.Printf("%s", out)
309
+ }
310
+}
311
+
312
+func getConfig(configName string){
313
+ out, err := exec.Command("cat","/proc/ipswitch/configs").Output()
314
+ if err != nil {
315
+ fmt.Printf("err %s", err)
316
+ }
317
+ var outputs []string = strings.Split(string(out),"\n")
318
+ i:=0
319
+ for i < 61 {
320
+ desc, prs := configNames[configName]
321
+ if prs == false{
322
+ }else {
323
+ var output []string = strings.Split(string(outputs[i]),",")
324
+ if string(desc) == string(output[1]){
325
+ fmt.Printf("value: %s -- %s \n",
326
+ strings.TrimSpace(string(output[3])),desc)
327
+ }else{
328
+ }
329
+ }
330
+ i = i + 1
331
+ }
332
+}
333
+
334
+var configNames = map[string]int{
335
+ "enable":0,
336
+ "cache_age":3,
337
+ //"sysboot_time":4,
338
+ "dns_cname":6,
339
+ "location":10,
340
+ "dns_mode":11,
341
+ "dns_answer4":15,
342
+ "dns_answer6":16,
343
+ /*"domains_num":19,
344
+ "uripath_num":20,
345
+ "telcose_num":21,
346
+ "ads_session_num":22,
347
+ "prc_session_num":23,
348
+ "adb_session_num":24,*///read-only
349
+ "telco_backup_age":25,
350
+ //"telco_backup_cnt":26,
351
+ "tcpsend_skip":41,
352
+ "logusrs_skip":42,
353
+ "virtual_gateway":43,
354
+ "virtual_msisdn":44,
355
+ "agent_filter":45,
356
+ "parental_control_url":46,
357
+ "ad0_maxcount":48,
358
+ "ad1_maxcount":49,
359
+ "ad0_interval":50,
360
+ "ad1_interval":51,
361
+ "staff_ad0_interval":53,
362
+ "staff_ad1_interval":54,
363
+ //"ad0_tx_count":55,
364
+ //"ad1_tx_count":56,
365
+ "ad_timings_pol":57,
366
+ "domains_pol":58,
367
+ "uripath_pol":59,
368
+ "debug_state":60,
369
+}
370
+
371
+
372
+var configDesc = map[int]string{
373
+ 0:"Probing Server On/Off",
374
+ 3:"script cache max-age",
375
+ 4:"system boot time",
376
+ 6:"dns CNAME (not implemented)",
377
+ 10:"Http 302 Location URL protocol://hostname",
378
+ 11:"dns operating mode 0:disabled, 1: enabled with A record",
379
+ 15:"dns answer A(ipv4) record ip",
380
+ 16:"dns answer AAAA(ipv6) record ip default is ::1/128",
381
+ 19:"current total nums of domains",
382
+ 20:"current total nums of user uri",
383
+ 21:"current total nums of telco(master) session",
384
+ 22:"current total nums of ADs service session",
385
+ 23:"current total nums of Parental Control service session",
386
+ 24:"current total nums of AD Blocker service session",
387
+ 25:"telco time machine backup, 0(all from boot), 3600(1hr),7200(2hr), 86400(1day)",
388
+ 26:"telco restored session count",
389
+ 41:"skip Injection & Tcp Reset when value=0 Tx is enabled",
390
+ 42:"skip user request log operations, value =0, log is enabled",
391
+ 43:"Router's external ip-address , It's activated only with compiling POC_ENABLED defined",
392
+ 44:"Simulated AAA's MSISDN , default is 010-200-3000, Activated only with compiling POC_ENABLED",
393
+ 45:"Exclude InApp browser(disabel=0),if this >0, enable and it's minimum agent's string length",
394
+ 46:"warning url of parental control (ex: http://warning.or.kr)",
395
+ 48:"http.302 AD[0] max count a day (<256)",
396
+ 49:"http.200 AD[1] max count a day (<256)",
397
+ 50:"http.302 AD[0] injection interval(sec)",
398
+ 51:"http.200 AD[1] injection interval(sec)",
399
+ 53:"staff's AD0 interval (same as MARKETING/9999 domain user's interval)",
400
+ 54:"staff's AD1 interval (same as MARKETING/9999 domain user's interval)",
401
+ 55:"AD0 injection count from boot",
402
+ 56:"AD1 injection count from boot",
403
+ 57:"policy, if '1', nevertheless adtime is 0, ads are displayed",
404
+ 58:"0: whitelist mode, 1: blacklist mode",
405
+ 59:"0: whitelist mode, 1: blacklist mode",
406
+ 60:"log level (0: nothing ) , see below table for more information",
407
+}
408
+