-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcheckip.go
More file actions
96 lines (81 loc) · 1.9 KB
/
Copy pathcheckip.go
File metadata and controls
96 lines (81 loc) · 1.9 KB
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
// Checkip is a command-line tool that provides information on IP addresses.
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"sync"
"github.com/jreisinger/checkip/check"
"github.com/jreisinger/checkip/cli"
)
func init() {
log.SetFlags(0)
log.SetPrefix(filepath.Base(os.Args[0]) + ": ")
}
var j = flag.Bool("j", false, "detailed output in JSON")
var noCache = flag.Bool("no-cache", false, "disable in-memory and persistent result cache")
var noActive = flag.Bool("no-active", false, "disable active checks that contact the target IP directly")
var p = flag.Int("p", 5, "check `n` IP addresses in parallel")
type Result struct {
IP net.IP
Checks cli.Checks
}
func validateParallelism(parallelism int) error {
if parallelism < 1 {
return fmt.Errorf("invalid -p value %d: must be > 0", parallelism)
}
return nil
}
func selectedDefinitions(disableActive bool) []check.Definition {
if !disableActive {
return check.Definitions
}
return check.WithoutActive(check.Definitions)
}
func main() {
flag.Parse()
if err := validateParallelism(*p); err != nil {
log.Fatal(err)
}
runner := cli.NewRunnerWithOptions(selectedDefinitions(*noActive), cli.RunnerOptions{
DisableCache: *noCache,
})
ipaddrs := make(chan net.IP)
results := make(chan Result)
var wg sync.WaitGroup
wg.Add(1)
go func() {
cli.GetIpAddrs(flag.Args(), ipaddrs)
wg.Done()
}()
for i := 0; i < *p; i++ {
wg.Add(1)
go func() {
for ipaddr := range ipaddrs {
checks, errors := runner.Run(ipaddr)
for _, e := range errors {
log.Print(e)
}
results <- Result{ipaddr, checks}
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(results)
}()
for result := range results {
if *j {
result.Checks.PrintJSON(result.IP)
} else {
fmt.Printf("--- %s ---\n", result.IP.String())
result.Checks.SortByName()
result.Checks.PrintSummary()
result.Checks.PrintMalicious()
}
}
}