-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (123 loc) · 3.11 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
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
package main
import (
"fmt"
"net"
"strconv"
"sync"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Port Scanner")
// UI elements
startIPEntry := widget.NewEntry()
startIPEntry.SetPlaceHolder("Start IP")
endIPEntry := widget.NewEntry()
endIPEntry.SetPlaceHolder("End IP")
portEntry := widget.NewEntry()
portEntry.SetPlaceHolder("Port")
timeoutEntry := widget.NewEntry()
timeoutEntry.SetPlaceHolder("Timeout (ms)")
workersEntry := widget.NewEntry()
workersEntry.SetPlaceHolder("Workers")
progress := widget.NewProgressBar()
results := widget.NewMultiLineEntry()
results.SetPlaceHolder("Results will be displayed here...")
scanButton := widget.NewButton("Scan", func() {
startIP := startIPEntry.Text
endIP := endIPEntry.Text
port := portEntry.Text
timeout := timeoutEntry.Text
workers := workersEntry.Text
progress.SetValue(0)
results.SetText("")
if startIP == "" || endIP == "" || port == "" || timeout == "" || workers == "" {
results.SetText("Please fill in all fields")
return
}
go func() {
networkScan(startIP, endIP, port, timeout, workers, progress, results)
}()
})
// Layout
myWindow.SetContent(container.NewVBox(
widget.NewLabel("Port Scanner"),
startIPEntry,
endIPEntry,
portEntry,
timeoutEntry,
workersEntry,
progress,
results,
scanButton,
))
myWindow.Resize(fyne.NewSize(600, 400))
myWindow.ShowAndRun()
}
func networkScan(startIP, endIP, port, timeout, workers string, progress *widget.ProgressBar, results *widget.Entry) {
start := ipToUint32(net.ParseIP(startIP))
end := ipToUint32(net.ParseIP(endIP))
workerCount, _ := strconv.Atoi(workers)
timeoutMs, _ := strconv.Atoi(timeout)
ipChan := make(chan uint32, workerCount)
resultChan := make(chan string, workerCount)
var wg sync.WaitGroup
var mu sync.Mutex
// Start worker goroutines
for i := 0; i < workerCount; i++ {
go func() {
for ip := range ipChan {
address := fmt.Sprintf("%s:%s", uint32ToIP(ip).String(), port)
if isPortOpen(address, timeoutMs) {
mu.Lock()
resultChan <- address
mu.Unlock()
}
wg.Done()
}
}()
}
// Close result channel when done
go func() {
wg.Wait()
close(resultChan)
}()
totalIPs := float64(end - start + 1)
current := 0.0
// Enqueue IPs
for ip := start; ip <= end; ip++ {
wg.Add(1)
ipChan <- ip
mu.Lock()
current++
progress.SetValue(current / totalIPs)
mu.Unlock()
}
close(ipChan)
// Collect and display results
for openAddr := range resultChan {
mu.Lock()
results.SetText(results.Text + openAddr + "\n")
mu.Unlock()
}
progress.SetValue(1)
}
func isPortOpen(address string, timeoutMs int) bool {
conn, err := net.DialTimeout("tcp", address, time.Duration(timeoutMs)*time.Millisecond)
if err != nil {
return false
}
conn.Close()
return true
}
func ipToUint32(ip net.IP) uint32 {
ipv4 := ip.To4()
return uint32(ipv4[0])<<24 | uint32(ipv4[1])<<16 | uint32(ipv4[2])<<8 | uint32(ipv4[3])
}
func uint32ToIP(n uint32) net.IP {
return net.IPv4(byte(n>>24), byte(n>>16), byte(n>>8), byte(n))
}