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

2 years ago
package ui
import (
"fmt"
"os"
2 years ago
"proxy_socks5/service"
"regexp"
"strconv"
2 years ago
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
2 years ago
const reg = "^[0-9]*[1-9][0-9]*$"
2 years ago
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))
2 years ago
var port string
label := widget.NewLabel("")
label.Hidden = true
input := widget.NewEntry()
input.SetPlaceHolder("Please Input The Port...")
2 years ago
w.SetContent(container.NewVBox(
2 years ago
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)
}),
2 years ago
))
2 years ago
w.Resize(fyne.NewSize(240, 100))
2 years ago
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()
}