-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
78 lines (68 loc) · 2.44 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
helpers "./helpers"
ps "./portscanner"
"github.com/akamensky/argparse"
)
var protocol string = "tcp"
var common bool = false
var rangeScan bool = false
var duration int = 10
func main() {
parser := argparse.NewParser("Port scanner", "")
ip := parser.String("i", "ip", &argparse.Options{Required: true, Help: "\n *Required* \n specifies an ip that you like to scan. \n must be a valid ip"})
port := parser.String("p", "port", &argparse.Options{Required: false, Help: "\n specifies a port that you like to scan.\n must be a number between '0' and '65535'"})
sProtocol := parser.String("l", "protocol", &argparse.Options{Required: false, Help: "\n specifies the protocol. \n must be either 'tcp' or 'udp' \n default value is 'tcp'"})
fCommon := parser.String("c", "common", &argparse.Options{Required: false, Help: "\n if 'true' scans common ports: '0' to '1024' \n default value is 'false' "})
fRangeScane := parser.String("r", "range", &argparse.Options{Required: false, Help: "\n if 'true' asks for ranges \n default value is 'false' "})
sDuration := parser.String("d", "duration", &argparse.Options{Required: false, Help: "\n duration of connection. \n must be a value between '7' and '300'\n default value is '10' "})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
if *fCommon != "" {
common = helpers.StringToBool(*fCommon)
}
if *fRangeScane != "" {
rangeScan = helpers.StringToBool(*fRangeScane)
}
if *sProtocol != "" {
protocol = *sProtocol
}
if *port == "" {
*port = "0"
}
if *sDuration != "" {
duration = helpers.StringToInt(*sDuration)
}
toDo(*ip, *port, protocol, common, rangeScan, duration)
}
func toDo(ip, port, dProtocol string, dCommon, dRangeScan bool, dDuration int) {
if !dCommon && !rangeScan && port != "" {
ps.ScanAPort(ip, port, protocol, duration)
return
}
if dCommon && !rangeScan {
ps.ScanRange(0, 1024, ip, port, protocol, duration)
return
}
if !dCommon && rangeScan {
from := readNum("Enter the firts port number")
to := readNum("Enter the seconde port number")
ps.ScanRange(from, to, ip, port, protocol, duration)
return
}
fmt.Println("Wronge arguments! use --help for more information.")
}
func readNum(message string) int {
reader := bufio.NewReader(os.Stdin)
fmt.Print(message + ": ")
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
return helpers.StringToInt(text)
}