Files
ip_scan_go/internal/ui/scanform.go
T
2026-07-17 16:57:54 +08:00

176 lines
4.2 KiB
Go

package ui
import (
"fmt"
"net/url"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
const appVersion = "V1.5.1"
const (
ipPlaceholder = "例子:\n127.0.0.1 - 单IP\n192.168.0.0/24 - 掩码IP段\n0.0.0.0-255.255.255.255 - IP段\ndomain.com - 单域名"
portPlaceholder = "例子:\n22 - 单端口\n80;443 - 多个不连续端口\n3000-65535 - 连续的端口范围"
)
type scanForm struct {
app fyne.App
ipList *widget.Entry
portList *widget.Entry
timeout *widget.Entry
threads *widget.Entry
output *widget.Entry
progress *widget.ProgressBar
scanBtn *widget.Button
pingBtn *widget.Button
scanning bool
}
func newScanForm(app fyne.App) *scanForm {
f := &scanForm{app: app}
f.ipList = widget.NewMultiLineEntry()
f.ipList.SetPlaceHolder(ipPlaceholder)
f.ipList.SetMinRowsVisible(4)
f.portList = widget.NewMultiLineEntry()
f.portList.SetPlaceHolder(portPlaceholder)
f.portList.SetMinRowsVisible(4)
f.timeout = newIntEntry(100, 1, 10000)
f.threads = newIntEntry(64, 1, 1024)
f.output = widget.NewMultiLineEntry()
f.output.SetMinRowsVisible(6)
f.progress = widget.NewProgressBar()
f.scanBtn = widget.NewButton("开始扫描", f.toggleScan)
f.pingBtn = widget.NewButton("仅Ping", f.togglePing)
return f
}
func (f *scanForm) build() fyne.CanvasObject {
ipSection := container.NewVBox(
widget.NewLabel("IP"),
f.ipList,
newIPKeypad(f.ipList),
)
portSection := container.NewVBox(
widget.NewLabel("端口"),
f.portList,
newPortKeypad(f.portList),
)
topRow := container.New(layout.NewGridLayoutWithColumns(2), ipSection, portSection)
controlRow := f.buildControlRow()
topArea := container.NewVBox(topRow, controlRow)
footer := container.NewHBox(
widget.NewLabel(appVersion),
layout.NewSpacer(),
widget.NewButton("Blog", func() { f.openURL("https://wnfed.com/") }),
widget.NewButton("GitLab", func() { f.openURL("https://git.lmve.net/kevin/tcp_ip_scan") }),
widget.NewButton("GitHub", func() { f.openURL("https://github.com/wuwenfengmi1998/tcp_ip_scan") }),
)
return container.NewBorder(topArea, footer, nil, nil, f.output)
}
func (f *scanForm) buildControlRow() fyne.CanvasObject {
left := container.NewHBox(
widget.NewButton("清空输出", func() { f.output.SetText("") }),
widget.NewButton("保存输出", f.saveOutput),
container.NewHBox(widget.NewLabel("超时ms"), f.timeout),
container.NewHBox(widget.NewLabel("线程"), f.threads),
widget.NewButton("清空IP", func() { f.ipList.SetText("") }),
f.pingBtn,
)
right := container.NewHBox(
widget.NewButton("清空端口", func() { f.portList.SetText("") }),
f.scanBtn,
)
return container.NewBorder(nil, nil, left, right, f.progress)
}
func (f *scanForm) toggleScan() {
if !f.scanning {
f.scanning = true
f.scanBtn.SetText("停止扫描")
f.pingBtn.Disable()
f.setInputsEnabled(false)
f.progress.SetValue(0)
f.output.Append("开始扫描... (TODO: 接入扫描逻辑)\n")
} else {
f.scanning = false
f.scanBtn.SetText("开始扫描")
f.pingBtn.Enable()
f.setInputsEnabled(true)
f.output.Append("已停止扫描\n")
}
}
func (f *scanForm) togglePing() {
if !f.scanning {
f.scanning = true
f.pingBtn.SetText("停止Ping")
f.scanBtn.Disable()
f.setInputsEnabled(false)
f.progress.SetValue(0)
f.output.Append("开始Ping... (TODO: 接入扫描逻辑)\n")
} else {
f.scanning = false
f.pingBtn.SetText("仅Ping")
f.scanBtn.Enable()
f.setInputsEnabled(true)
f.output.Append("已停止Ping\n")
}
}
func (f *scanForm) setInputsEnabled(enabled bool) {
if enabled {
f.ipList.Enable()
f.portList.Enable()
f.timeout.Enable()
f.threads.Enable()
} else {
f.ipList.Disable()
f.portList.Disable()
f.timeout.Disable()
f.threads.Disable()
}
}
func (f *scanForm) saveOutput() {
f.output.Append("(TODO: 保存输出到文件)\n")
}
func (f *scanForm) openURL(urlStr string) {
u, err := url.Parse(urlStr)
if err != nil {
return
}
_ = f.app.OpenURL(u)
}
func newIntEntry(val, min, max int) *widget.Entry {
e := widget.NewEntry()
e.SetText(strconv.Itoa(val))
e.Validator = func(s string) error {
n, err := strconv.Atoi(s)
if err != nil || n < min || n > max {
return fmt.Errorf("需为 %d-%d", min, max)
}
return nil
}
return e
}