You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.4 KiB

package ui
import (
"fmt"
"os"
"proxy_socks5/service"
"regexp"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
const reg = "^[0-9]*[1-9][0-9]*$"
type UI struct {
Options *Options
mw *fyne.Window
}
func New(op ...Option) GUIApp {
opts := &Options{}
for _, o := range op {
o(opts)
}
if err := opts.Validate(); err != nil {
panic(fmt.Errorf("init ui failed: %v", err))
}
//fyne
a := app.New()
w := a.NewWindow(fmt.Sprintf("%v %v", opts.Name, opts.Version))
var port string
label := widget.NewLabel("")
label.Hidden = true
input := widget.NewEntry()
input.SetPlaceHolder("Please Input The Port...")
w.SetContent(container.NewVBox(
label,
input,
widget.NewButton("Listen", func() {
port = input.Text
r, _ := regexp.Compile(reg)
if r.MatchString(port) { //是数字
i, _ := strconv.ParseInt(port, 10, 64)
if 1 < 0 || i > 65535 {
port = "1080"
}
} else { //否则
port = "1080"
}
input.Hidden = true
label.Hidden = false
label.TextStyle.Bold = true
label.SetText(fmt.Sprintf("Listening at %v Succeed!!!", port))
service.RunServe(port)
}),
))
w.Resize(fyne.NewSize(240, 100))
return &UI{
Options: opts,
mw: &w,
}
}
func (u *UI) Run(exitCh chan os.Signal) int {
window := *u.mw
window.ShowAndRun()
return 0
}
func (u *UI) Close() {
window := *u.mw
window.Close()
}