-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
80 lines (73 loc) · 2.15 KB
/
Copy pathcli.go
File metadata and controls
80 lines (73 loc) · 2.15 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
package main
import (
"errors"
"flag"
"strconv"
"strings"
"time"
)
type Configs struct {
ports []int
host string
timeout time.Duration
outputFile string
workers uint
}
var FIFTEEN_SECONDS_IN_MICROSECONDS = 15 * 1000 * 1000 * 1000
var (
flgHost = flag.String("host", "localhost", "Specify host IP address")
flgPorts = flag.String("ports", "1-1000", "Specify ports or port range")
flgWorkers = flag.Uint("workers", 100, "Specify number of workers")
flgTimeout = flag.Duration("timeout", time.Duration(FIFTEEN_SECONDS_IN_MICROSECONDS), "Specify timeout duration for scanning") // time is in microseconds
flgOutput = flag.String("oN", "", "Specify output file")
)
func buildConfigs() (*Configs, error) {
var port_range, err = parse_port_range(*flgPorts)
if err != nil {
return nil, err
}
var con = &Configs{
host: *flgHost,
ports: port_range,
workers: *flgWorkers,
timeout: *flgTimeout,
outputFile: *flgOutput,
}
return con, nil
}
func parse_port_range(s string) ([]int, error) {
/*
Parses any of the following strings into a list of ints, or gives an error if the list has an element that could not be parsed.
For ranges, excludes final value
- "80"
- "80,443,22"
- "1-1024,80,443"
*/
var ret []int
for _, val := range strings.Split(s, ",") {
entries := strings.Split(val, "-")
if len(entries) == 2 {
lower_bound, low_err := strconv.Atoi(entries[0])
upper_bound, upp_err := strconv.Atoi(entries[1])
if upp_err != nil || low_err != nil {
err := errors.New("Could not parse port arguments. Upper or lower bound of specified range cannot be parsed to integer.")
return nil, err
}
for lower_bound <= upper_bound {
ret = append(ret, lower_bound)
lower_bound++
}
} else if len(entries) == 1 {
it, it_err := strconv.Atoi(entries[0])
if it_err != nil {
return nil, errors.New("Could not parse port arguments. Listed distinct value cannot be cast to integer.")
} else {
ret = append(ret, it)
}
} else {
err := errors.New("Could not parse port arguments. Multiple-hyphenated ranges are not supported.")
return nil, err
}
}
return ret, nil
}