- service.go: 移除无意义的 peerIP = serverIP(对端指向自己),改为 nil - service.go: Configure 之后新增 AddSubnetRoute 调用,确保内核有到 VPN 子网的路由 - tun_linux.go: Configure 增加 peerIP != nil 守卫,nil 时直接走无 peer 分支 - tun_darwin.go: 同上 peerIP != nil 守卫
37 lines
948 B
Go
37 lines
948 B
Go
//go:build darwin
|
|
|
|
package vpn
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
func inetFamily(ip net.IP) string {
|
|
if ip.To4() == nil {
|
|
return "inet6"
|
|
}
|
|
return "inet"
|
|
}
|
|
|
|
func (t *TUNInterface) Configure(localIP net.IP, prefix int, peerIP net.IP) error {
|
|
if localIP == nil {
|
|
return execCmd("ifconfig", t.Name(), "up")
|
|
}
|
|
localCidr := fmt.Sprintf("%s/%d", localIP.String(), prefix)
|
|
inetType := inetFamily(localIP)
|
|
if t.Iface.IsTUN() && inetType == "inet" && peerIP != nil {
|
|
return execCmd("ifconfig", t.Name(), inetType, localCidr, peerIP.String(), "up")
|
|
}
|
|
return execCmd("ifconfig", t.Name(), inetType, localCidr, "up")
|
|
}
|
|
|
|
func (t *TUNInterface) AddSubnetRoute(subnet *net.IPNet) error {
|
|
inetType := inetFamily(subnet.IP)
|
|
return execCmd("route", "add", fmt.Sprintf("-%s", inetType), "-net", subnet.String(), "-interface", t.Name())
|
|
}
|
|
|
|
func (t *TUNInterface) SetMTU(mtu int) error {
|
|
return execCmd("ifconfig", t.Name(), "mtu", fmt.Sprintf("%d", mtu))
|
|
}
|