Files
goodlink/netstack/setup_windows.go
2025-08-06 11:47:38 +08:00

77 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//go:build windows
package netstack
import (
"crypto/tls"
"fmt"
"goodlink/winipcfg"
"gotools"
"io"
"net/http"
"net/netip"
"os"
"time"
)
func SetTunIP(wintunEP *Device, ip string, mask int) error {
var ipf netip.Prefix
var err error
link := winipcfg.LUID((*wintunEP).(*TUN).GetNt().LUID())
// 将IP地址和掩码组合为CIDR格式如192.168.1.1/24
if ipf, err = netip.ParsePrefix(fmt.Sprintf("%s/%d", ip, mask)); err != nil {
return err
}
if err = link.SetIPAddresses([]netip.Prefix{ipf}); err != nil {
return err
}
routeData := &winipcfg.RouteData{
Destination: ipf, // 目标网络CIDR格式
NextHop: netip.MustParseAddr(ip), // 下一跳地址本机IP
Metric: 0, // 路由优先级(数值越小优先级越高)
}
if err = link.SetRoutes([]*winipcfg.RouteData{
routeData,
}); err != nil {
return err
}
return nil
}
func InitWintunDll() error {
if _, err := os.Stat("wintun.dll"); os.IsExist(err) {
return nil
}
var res []byte
var err error
var resp *http.Response
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // 跳过证书验证
},
},
Timeout: 3 * time.Second,
}
if resp, err = client.Get("https://gitee.com/konyshe/goodlink_conf/raw/master/wintun.dll"); err != nil {
return err
}
defer resp.Body.Close()
if res, err = io.ReadAll(resp.Body); err != nil {
return err
}
gotools.Utils().FileAppend("wintun.dll", res)
return nil
}