forked from mehrdadrad/mylg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylg.go
562 lines (506 loc) · 11.5 KB
/
mylg.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// myLG is an open source software utility which combines the functions
// of the different network probes in one network diagnostic tool.
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/briandowns/spinner"
"github.com/mehrdadrad/mylg/cli"
"github.com/mehrdadrad/mylg/disc"
"github.com/mehrdadrad/mylg/http/ping"
"github.com/mehrdadrad/mylg/icmp"
"github.com/mehrdadrad/mylg/lg"
"github.com/mehrdadrad/mylg/nms"
"github.com/mehrdadrad/mylg/ns"
"github.com/mehrdadrad/mylg/packet"
"github.com/mehrdadrad/mylg/peeringdb"
"github.com/mehrdadrad/mylg/scan"
"github.com/mehrdadrad/mylg/services/httpd"
"github.com/mehrdadrad/mylg/whois"
)
const (
version = "0.2.6"
)
// Provider represents looking glass
type Provider interface {
Set(host, version string)
GetDefaultNode() string
GetNodes() []string
ChangeNode(node string) bool
Ping() (string, error)
Trace() chan string
BGP() chan string
}
var (
pNames = providerNames()
req = make(chan string, 1)
nxt = make(chan struct{}, 1)
spin = spinner.New(spinner.CharSets[26], 220*time.Millisecond)
eArgs = os.Args
args string
prompt string
cPName string
noIf bool = true
cfg cli.Config
nmsClient nms.Client
nsr *ns.Request
c *cli.Readline
// register looking glass hosts
providers = map[string]Provider{
"telia": new(lg.Telia), // telia
"level3": new(lg.Level3), // level3
"cogent": new(lg.Cogent), //cogent
}
// map cmd to function
cmdFunc = map[string]func(){
"web": web, // web dashboard
"dump": dump, // dump traffic
"disc": discovery, // network discovery
"scan": scanPorts, // network scan
"mode": mode, // editor mode
"ping": pingQuery, // ping
"trace": trace, // trace route
"bgp": BGP, // BGP
"whois": whoisLookup, // whois / dns lookup
"peering": peeringDB, // peering DB
"hping": hping, // hping
"dig": dig, // dig
"nms": setNMS, // network management system
"node": node, // change node
"connect": connect, // connect to a country or LG
"local": local, // local
"help": help, // help
"exit": cleanUp, // clean up
"quit": cleanUp, // clean up
"show": show, // show config
"set": setConfig, // set config
"lg": setLG, // prepare looking glass
"ns": setNS, // prepare name server
"version": printVersion, // prints version
}
)
// init
func init() {
// load configuration
cfg = cli.LoadConfig()
// initialize name server
nsr = ns.NewRequest()
go nsr.Init()
// set current provider, prompt
cPName = "local"
prompt = "local"
// with interface
if len(eArgs) == 1 {
// initialize cli
c = cli.Init(version)
go c.Run(req, nxt)
// start web server
go httpd.Run(cfg)
// set interface enabled
noIf = false
// set local as default
local()
}
}
func main() {
// command line w/o interface
if noIf {
cmd := eArgs[1]
args = strings.Join(eArgs[2:], " ")
if f, ok := cmdFunc[cmd]; ok {
f()
} else {
println("Invalid command please try mylg help")
}
return
}
// command like w/ interface
LOOP:
for {
select {
case request, ok := <-req:
if !ok {
break LOOP
}
if len(request) < 1 {
c.Next()
continue
}
subReq := cli.CMDRgx().FindStringSubmatch(request)
if len(subReq) == 0 {
println("syntax error")
c.Next()
continue
}
prompt = c.GetPrompt()
args = strings.TrimSpace(subReq[2])
cmd := strings.TrimSpace(subReq[1])
if f, ok := cmdFunc[cmd]; ok {
f()
} else {
println("Invalid command please try help")
}
c.Next()
}
}
}
// providerName
func providerNames() []string {
pNames := []string{}
for p := range providers {
pNames = append(pNames, p)
}
return pNames
}
// validateProvider
func validateProvider(p string) (string, error) {
pNames := []string{}
match, _ := regexp.MatchString("("+strings.Join(pNames, "|")+")", p)
p = strings.ToLower(p)
if match {
return p, nil
}
return "", errors.New("provider not support")
}
// node handles node cmd
func node() {
switch {
case strings.HasPrefix(prompt, "lg"):
if _, ok := providers[cPName]; ok {
if providers[cPName].ChangeNode(args) {
c.UpdatePromptN(args, 3)
return
}
}
println("the specified node doesn't support")
case strings.HasPrefix(prompt, "ns"):
if !nsr.ChkNode(args) {
println("error: argument is not valid")
} else {
c.UpdatePromptN(args, 3)
}
default:
if cPName == "local" {
println("local doesn't support node")
}
}
}
// dig gets dig info
func dig() {
if ok := nsr.SetOptions(args, prompt); ok {
nsr.Dig()
}
}
// web tries to open web interface at default web browser
func web() {
var openCmd = "open"
println("opening default web broswer ...")
if runtime.GOOS != "darwin" {
openCmd = "xdg-open"
}
cmd := exec.Command(openCmd, fmt.Sprintf("http://%s:%d", cfg.Web.Address, cfg.Web.Port))
err := cmd.Start()
if err != nil {
println("error opening default browser")
}
}
// dump provides decoding packets
func dump() {
p, err := packet.NewPacket(args)
if p == nil || err != nil {
return
}
println(p.Banner())
for l := range p.Open() {
l.PrintPretty()
}
}
// connect handles connect cmd
func connect() {
var (
pName string
err error
)
switch {
case strings.HasPrefix(prompt, "lg"):
if pName, err = validateProvider(args); err != nil {
println("provider not available")
c.Next()
return
}
cPName = pName
if _, ok := providers[cPName]; ok {
c.UpdatePromptN(cPName+"/"+providers[cPName].GetDefaultNode(), 2)
go func() {
c.UpdateCompleter("node", providers[cPName].GetNodes())
}()
} else {
println("it doesn't support")
}
case strings.HasPrefix(prompt, "ns"):
if !nsr.ChkCountry(args) {
println("error: argument is not valid")
} else {
c.SetPrompt("ns/" + args)
c.UpdateCompleter("node", nsr.NodeList())
}
case strings.HasPrefix(prompt, "nms"):
nmsClient, err = nms.NewClient(args, cfg)
if err != nil {
println("error:", err.Error())
} else if nmsClient.Host == "" {
return
} else {
c.SetPrompt("nms/" + nmsClient.Host)
}
}
}
// mode set editor mode
func mode() {
if args == "vim" {
c.SetVim()
} else if args == "emacs" {
c.SetEmacs()
} else {
println("the request mode doesn't support")
}
}
// trace tries to trace from local and lg
func trace() {
switch {
case strings.HasPrefix(prompt, "local"):
trace, err := icmp.NewTrace(args, cfg)
if err != nil {
println(err.Error())
}
if trace == nil {
break
}
trace.Print()
case strings.HasPrefix(prompt, "lg"):
spin.Prefix = "please wait "
spin.Start()
providers[cPName].Set(args, "ipv4")
for l := range providers[cPName].Trace() {
if spin.Prefix != "" {
spin.Stop()
spin.Prefix = ""
fmt.Printf("\n%s\n", l)
} else {
fmt.Println(l)
}
}
spin.Stop()
}
}
// hping tries to ping a web server by http
func hping() {
// it should work at local mode
if cPName != "local" {
return
}
p, err := ping.NewPing(args, cfg)
if err != nil {
println(err.Error())
} else {
p.Run()
}
}
// pingQuery runs ping command (local/LG)
func pingQuery() {
if cPName == "local" {
pingLocal()
} else {
pingLG()
}
}
// pingLG tries to ping through a looking glass
func pingLG() {
spin.Prefix = "please wait "
spin.Start()
providers[cPName].Set(args, "ipv4")
m, err := providers[cPName].Ping()
spin.Stop()
if err != nil {
println(err.Error())
} else {
println(m)
}
}
// pingLocal tries to ping from local source ip
func pingLocal() {
p, err := icmp.NewPing(args, cfg)
if err != nil {
println(err.Error())
}
if p == nil {
return
}
if !p.IsCIDR() {
resp := p.Run()
p.PrintPretty(resp)
}
}
// scanPorts tries to scan tcp/ip ports
func scanPorts() {
scan, err := scan.NewScan(args, cfg)
if err != nil {
println(err.Error())
} else {
scan.Run()
}
}
// BGP tries to get BGP lookup from a LG
func BGP() {
if cPName == "local" {
println("no provider selected")
return
}
providers[cPName].Set(args, "ipv4")
for l := range providers[cPName].BGP() {
println(l)
}
}
// discovery handles disc command
func discovery() {
var (
wg sync.WaitGroup
//ts = time.Now()
)
d := disc.New(args)
// help requested
if d == nil {
return
}
spin.Prefix = "please wait "
spin.Start()
// load OUI async
go func() {
wg.Add(1)
d.LoadOUI()
wg.Done()
}()
d.PingLan()
time.Sleep(5 * time.Second)
if err := d.GetARPTable(); err != nil {
println(err.Error())
return
}
wg.Wait()
spin.Stop()
println("\nNetwork LAN Discovery")
d.PrintPretty()
}
// nms
func setNMS() {
items := []string{"interface", "config"}
c.UpdateCompleter("show", items)
c.SetPrompt("nms")
}
// setConfig
func setConfig() {
if err := cli.SetConfig(args, &cfg); err != nil {
println(err.Error())
}
}
// show command
func show() {
var err error
re := regexp.MustCompile(`^([a-z]+)\s*(.*)$`)
m := re.FindStringSubmatch(args)
if len(m) < 3 {
return
}
subItem := m[1]
subArgs := m[2]
switch subItem {
case "config":
cli.ShowConfig(&cfg)
case "interface":
if strings.HasPrefix(prompt, "nms") {
err = nmsClient.ShowInterface(subArgs)
} else {
println("it's available under nms")
}
if err != nil {
println(err.Error())
}
}
}
// setLG set lg prompt and completer
func setLG() {
cPName = "telia"
c.UpdateCompleter("connect", pNames)
c.SetPrompt("lg/" + cPName + "/" + providers[cPName].GetDefaultNode())
go func() {
c.UpdateCompleter("node", providers[cPName].GetNodes())
}()
}
// setNS set ns prompt and update completers
func setNS() {
c.UpdateCompleter("connect", nsr.CountryList())
c.UpdateCompleter("node", []string{})
c.SetPrompt("ns")
}
// peeringDB gets peer info
func peeringDB() {
peeringdb.Search(args)
}
// whoisLookup gets ANS/Prefix info
func whoisLookup() {
whois.Lookup(args)
}
// local set prompts to local
func local() {
nsr.Local()
cPName = "local"
c.UpdateCompleter("show", []string{"config"})
c.SetPrompt(cPName)
}
// printVersion prints version and exits
func printVersion() {
fmt.Printf("myLG v%s\n", version)
}
// cleanUp
func cleanUp() {
c.Close(nxt)
close(req)
}
// help
func help() {
if noIf {
// without command line
h := `
***** TRY IT WITHOUT ANYTHING TO HAVE INTERFACE *****
Usage:
mylg [command] [args...]
Available commands:
ping ping ip address or domain name
trace trace ip address or domain name (real-time w/ -r option)
dig name server looking up
whois resolve AS number/IP/CIDR to holder (provides by ripe ncc)
hping Ping through HTTP/HTTPS w/ GET/HEAD methods
scan scan tcp ports (you can provide range >scan host minport maxport)
dump prints out a description of the contents of packets on a network interface
disc discover all the devices on a LAN
peering peering information (provides by peeringdb.com)
version shows mylg version
Example:
mylg trace freebsd.org -r
mylg whois 8.8.8.8
mylg scan 127.0.0.1
mylg dig google.com +trace
`
fmt.Println(h)
} else {
// with command line interface
c.Help()
}
}