1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-11 12:10:26 +08:00

optimize(lower): nic route setting

This commit is contained in:
源文雨
2024-07-17 14:08:59 +09:00
parent 04a3c9a10b
commit 58cb7e09a8
5 changed files with 38 additions and 26 deletions

View File

@@ -2,8 +2,10 @@ package lower
import (
"io"
"net"
"os"
"os/exec"
"strconv"
"github.com/fumiama/water"
"github.com/sirupsen/logrus"
@@ -17,29 +19,35 @@ type NICIO interface {
// NIC 虚拟网卡
type NIC struct {
ifce *water.Interface
ip string
subnet string
mtu string
cidrs []string
ifce *water.Interface
ip net.IP
subnet *net.IPNet
rawipnet string
mtu string
cidrs []string
}
// NewNIC 新建 TUN 网络接口卡
// 网卡地址为 ip, 所属子网为 subnet
// 以本网卡为下一跳的所有子网为 cidrs
// cidrs 不包括本网卡 subnet
func NewNIC(ip, subnet, mtu string, cidrs ...string) NICIO {
func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO {
ifce, err := water.New(water.Config{DeviceType: water.TUN})
if err != nil {
logrus.Error(err)
os.Exit(1)
}
subn, bitsn := subnet.Mask.Size()
if bitsn != 32 {
panic("mask len " + strconv.Itoa(bitsn) + " is not supported")
}
n := &NIC{
ifce: ifce,
ip: ip,
subnet: subnet,
mtu: mtu,
cidrs: cidrs,
ifce: ifce,
ip: ip,
subnet: subnet,
rawipnet: ip.String() + "/" + strconv.Itoa(subn),
mtu: mtu,
cidrs: cidrs,
}
return n
}

View File

@@ -3,17 +3,24 @@
package lower
import "net"
func (n *NIC) Up() {
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159
execute("ifconfig", n.ifce.Name(), "inet", n.ip, n.ip, "up")
execute("route", "add", n.subnet, "-interface", n.ifce.Name())
execute(
"ifconfig", n.ifce.Name(),
"inet", n.ip.String(), n.ip.String(),
"netmask", (net.IP)(n.subnet.Mask).String(),
"up",
)
execute("route", "add", n.subnet.String(), "-interface", n.ifce.Name())
for _, c := range n.cidrs {
execute("route", "add", c, "-interface", n.ifce.Name())
}
}
func (n *NIC) Down() {
execute("route", "delete", n.subnet, "-interface", n.ifce.Name())
execute("route", "delete", n.subnet.String(), "-interface", n.ifce.Name())
for _, c := range n.cidrs {
execute("route", "delete", c, "-interface", n.ifce.Name())
}

View File

@@ -5,16 +5,14 @@ package lower
func (n *NIC) Up() {
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", "addr", "add", n.rawipnet, "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())
for _, c := range n.cidrs {
execute("/sbin/ip", "route", "add", c, "dev", n.ifce.Name())
}
}
func (n *NIC) Down() {
execute("/sbin/ip", "route", "del", n.subnet, "dev", n.ifce.Name())
for _, c := range n.cidrs {
execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name())
}

View File

@@ -6,19 +6,14 @@ package lower
import "net"
func (n *NIC) Up() {
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "enabled")
_, ipn, err := net.ParseCIDR(n.subnet)
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.String()+"\" mask=\""+(net.IP)(n.subnet.Mask).String()+"\" gateway=none")
execute("cmd", "/c", "netsh interface ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu)
for _, c := range n.cidrs {
ip, cidr, err := net.ParseCIDR(c)
if err != nil {
panic(err)
}
execute("cmd", "/c", "route ADD "+ip.String()+" MASK "+(net.IP)(cidr.Mask).String()+" "+n.ip)
execute("cmd", "/c", "route ADD "+ip.String()+" MASK "+(net.IP)(cidr.Mask).String()+" "+n.ip.String())
}
}