-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (71 loc) · 1.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
79
80
81
82
package main
import (
"encoding/json"
"fmt"
"math/rand"
"net"
"os"
"strconv"
"time"
)
type ScanResult struct {
Port string
Status string
Host string
}
func scanPort(protocol, hostname string, port int) ScanResult {
result := ScanResult{Port: strconv.Itoa(port) + string("/") + protocol}
address := hostname + ":" + strconv.Itoa(port)
ips, _ := net.LookupIP(hostname)
for _, ip := range ips {
if ipv4 := ip.To4(); ipv4 != nil {
result.Host = ip.String()
} else {
result.Host = hostname
}
}
conn, err := net.DialTimeout(protocol, address, 60*time.Second)
if err != nil {
result.Status = "Close"
return result
}
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
panic("Error!")
}
}(conn)
result.Status = "Open"
return result
}
func PrettyPrint(v interface{}) (err error) {
b, err := json.MarshalIndent(v, "", " ")
if err == nil {
fmt.Println(string(b))
}
name := "Object-" + strconv.Itoa(rand.Intn(100)) + ".json"
_ = os.WriteFile(name, b, 0644)
return
}
func main() {
var results []ScanResult
var port int
var proto string
var host string
var err error
if len(os.Args) > 1 {
proto = os.Args[1]
host = os.Args[2]
port, err = strconv.Atoi(os.Args[3])
if err != nil {
panic(err)
}
for i := 0; i < port; i++ {
results = append(results, scanPort(proto, host, i))
}
PrettyPrint(results)
//fmt.Printf("%v",results)
} else {
fmt.Println("Args not found ...")
}
}