1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-24 21:00:49 +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
}