1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-05 07:50:24 +08:00
This commit is contained in:
fumiama
2022-01-01 20:15:43 +08:00
parent 6f096bd9e4
commit 2b0d5059ec
5 changed files with 9 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ type NIC struct {
ifce *water.Interface
ip string
subnet string
mtu string
cidrs []string
}
@@ -27,7 +28,7 @@ type NIC struct {
// 网卡地址为 ip, 所属子网为 subnet
// 以本网卡为下一跳的所有子网为 cidrs
// cidrs 不包括本网卡 subnet
func NewNIC(ip, subnet string, cidrs ...string) NICIO {
func NewNIC(ip, subnet, mtu string, cidrs ...string) NICIO {
ifce, err := water.New(water.Config{DeviceType: water.TUN})
if err != nil {
panic(err)
@@ -35,8 +36,9 @@ func NewNIC(ip, subnet string, cidrs ...string) NICIO {
n := &NIC{
ifce: ifce,
ip: ip,
cidrs: cidrs,
subnet: subnet,
mtu: mtu,
cidrs: cidrs,
}
return n
}

View File

@@ -4,6 +4,7 @@
package lower
func (n *NIC) Up() {
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu)
execute("ifconfig", n.ifce.Name(), "inet", n.ip, n.ip, "up")
execute("route", "add", n.subnet, "-interface", n.ifce.Name())
for _, c := range n.cidrs {

View File

@@ -4,7 +4,7 @@
package lower
func (n *NIC) Up() {
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", "1500")
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu)
execute("/sbin/ip", "addr", "add", n.ip, "dev", n.ifce.Name())
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up")
execute("/sbin/ip", "route", "add", n.subnet, "dev", n.ifce.Name())

View File

@@ -11,7 +11,7 @@ func (n *NIC) Up() {
if err != nil {
panic(err)
}
execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip+"\" mask=\""+(net.IP)(ipn.Mask).String()+"\" gateway=none")
execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip+"\" mask=\""+(net.IP)(ipn.Mask).String()+"\" gateway=none mtu="+n.mtu)
for _, c := range n.cidrs {
ip, cidr, err := net.ParseCIDR(c)
if err != nil {

View File

@@ -2,6 +2,7 @@ package wg
import (
"errors"
"fmt"
"net"
base14 "github.com/fumiama/go-base16384"
@@ -92,7 +93,7 @@ func (wg *WG) init(srcport, destport, mtu uint16) {
&wg.key,
wg.c.IP+"/32",
wg.c.EndPoint,
lower.NewNIC(wg.c.IP, wg.c.SubNet, cidrs...),
lower.NewNIC(wg.c.IP, wg.c.SubNet, fmt.Sprintf("%d", mtu), cidrs...),
srcport, destport, mtu,
)