golang实现windows下切换代理
实现很简单 主要修改注册表文件 利用walk展示页面
界面如下
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
"golang.org/x/sys/windows/registry"
"log"
)
func main() {
var mw *walk.MainWindow
var inTE *walk.TextEdit
if err := (MainWindow{
AssignTo: &mw,
//Icon: "img/logo.ico",
Title: "代理",
Size: Size{300, 200},
Layout: VBox{},
Children: []Widget{
HSplitter{
Children: []Widget{
TextEdit{AssignTo: &inTE, ReadOnly: true},
},
},
PushButton{
Text: "连接",
OnClicked: func() {
status, ip := start()
if status {
inTE.SetText("连接成功\n当前IP" + ip)
} else {
inTE.SetText("连接失败")
}
},
},
PushButton{
Text: "关闭",
OnClicked: func() {
status := editReg("0", "")
if status {
inTE.SetText("连接已关闭")
} else {
inTE.SetText("关闭失败")
}
},
},
},
}.Create()); err != nil {
log.Fatal(err)
}
win.SetWindowLong(mw.Handle(), win.GWL_STYLE, win.GetWindowLong(mw.Handle(), win.GWL_STYLE) & ^win.WS_MAXIMIZEBOX & ^win.WS_THICKFRAME)
mw.Run()
}
func start() (bool, string) {
ip, port := getIp()
status := editReg("1", ip+":"+port)
return status, ip
}
// 获取ip
func getIp() (string, string) {
// 自行获取ip
return "183.88.226.50", "8080"
}
func editReg(enable, proxy string) bool {
key, exists, err := registry.CreateKey(registry.CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", registry.ALL_ACCESS)
if err != nil {
log.Fatal(err)
}
defer key.Close()
if !exists {
return false
}
err = key.SetStringValue("ProxyEnable", enable)
if err != nil {
return false
}
err = key.SetStringValue("ProxyServer", proxy)
if err != nil {
return false
}
return true
}
评论 (0)