代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"log"
"testing"
"time"
"gitee.com/carlmax_my/console-core-go/vpn/linker"
"github.com/samber/lo"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func main() {
runTest()
}
// var okDevice = &wgtypes.Device{Name: "wg0"}
func TestClient(t *testing.T) {
runTest()
}
func runTest() {
// 设置你要修改的WireGuard接口的名称
ifName := "wg0"
iface, err := linker.CreateTun(ifName)
if err != nil {
log.Println("linker.CreateTun err", err.Error())
log.Fatal(err)
}
log.Println("CreateTun success", ifName, iface)
var client *wgctrl.Client
for {
time.Sleep(5 * time.Second)
client, err = checkDevice(iface)
if err != nil {
log.Printf("checkDevice error: %s\n", err.Error())
} else {
break
}
}
TestCfg(client, iface)
}
func checkDevice(iface string) (*wgctrl.Client, error) {
// 打开wgctrl客户端
client, err := wgctrl.New()
if err != nil {
return nil, fmt.Errorf("wgctrl.New err: %s", err.Error())
}
defer client.Close()
devices, err := client.Devices()
if err != nil {
return nil, fmt.Errorf("client.Devices err: %s", err.Error())
}
log.Printf("devices=%+v;%d\n", devices, len(devices))
device, err := client.Device(iface)
if err != nil {
return nil, fmt.Errorf("client.Device err: %s", err.Error())
}
log.Printf("client.Device success: %+v", *device)
return client, nil
}
func TestCfg(client *wgctrl.Client, iface string) error {
cfg := wgtypes.Config{
ListenPort: lo.ToPtr(51821),
}
// 应用新的配置
if err := client.ConfigureDevice(iface, cfg); err != nil {
return fmt.Errorf("client.ConfigureDevice err: %s", err.Error())
}
fmt.Printf("interface %s has been set %+v\n", iface, cfg)
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。