mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-05 07:50:24 +08:00
optimize(lower): nic route setting
This commit is contained in:
30
lower/nic.go
30
lower/nic.go
@@ -2,8 +2,10 @@ package lower
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/fumiama/water"
|
"github.com/fumiama/water"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -17,29 +19,35 @@ type NICIO interface {
|
|||||||
|
|
||||||
// NIC 虚拟网卡
|
// NIC 虚拟网卡
|
||||||
type NIC struct {
|
type NIC struct {
|
||||||
ifce *water.Interface
|
ifce *water.Interface
|
||||||
ip string
|
ip net.IP
|
||||||
subnet string
|
subnet *net.IPNet
|
||||||
mtu string
|
rawipnet string
|
||||||
cidrs []string
|
mtu string
|
||||||
|
cidrs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNIC 新建 TUN 网络接口卡
|
// NewNIC 新建 TUN 网络接口卡
|
||||||
// 网卡地址为 ip, 所属子网为 subnet
|
// 网卡地址为 ip, 所属子网为 subnet
|
||||||
// 以本网卡为下一跳的所有子网为 cidrs
|
// 以本网卡为下一跳的所有子网为 cidrs
|
||||||
// cidrs 不包括本网卡 subnet
|
// 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})
|
ifce, err := water.New(water.Config{DeviceType: water.TUN})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
subn, bitsn := subnet.Mask.Size()
|
||||||
|
if bitsn != 32 {
|
||||||
|
panic("mask len " + strconv.Itoa(bitsn) + " is not supported")
|
||||||
|
}
|
||||||
n := &NIC{
|
n := &NIC{
|
||||||
ifce: ifce,
|
ifce: ifce,
|
||||||
ip: ip,
|
ip: ip,
|
||||||
subnet: subnet,
|
subnet: subnet,
|
||||||
mtu: mtu,
|
rawipnet: ip.String() + "/" + strconv.Itoa(subn),
|
||||||
cidrs: cidrs,
|
mtu: mtu,
|
||||||
|
cidrs: cidrs,
|
||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,24 @@
|
|||||||
|
|
||||||
package lower
|
package lower
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
func (n *NIC) Up() {
|
func (n *NIC) Up() {
|
||||||
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159
|
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159
|
||||||
execute("ifconfig", n.ifce.Name(), "inet", n.ip, n.ip, "up")
|
execute(
|
||||||
execute("route", "add", n.subnet, "-interface", n.ifce.Name())
|
"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 {
|
for _, c := range n.cidrs {
|
||||||
execute("route", "add", c, "-interface", n.ifce.Name())
|
execute("route", "add", c, "-interface", n.ifce.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NIC) Down() {
|
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 {
|
for _, c := range n.cidrs {
|
||||||
execute("route", "delete", c, "-interface", n.ifce.Name())
|
execute("route", "delete", c, "-interface", n.ifce.Name())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,14 @@ package lower
|
|||||||
|
|
||||||
func (n *NIC) Up() {
|
func (n *NIC) Up() {
|
||||||
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu)
|
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", "link", "set", "dev", n.ifce.Name(), "up")
|
||||||
execute("/sbin/ip", "route", "add", n.subnet, "dev", n.ifce.Name())
|
|
||||||
for _, c := range n.cidrs {
|
for _, c := range n.cidrs {
|
||||||
execute("/sbin/ip", "route", "add", c, "dev", n.ifce.Name())
|
execute("/sbin/ip", "route", "add", c, "dev", n.ifce.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NIC) Down() {
|
func (n *NIC) Down() {
|
||||||
execute("/sbin/ip", "route", "del", n.subnet, "dev", n.ifce.Name())
|
|
||||||
for _, c := range n.cidrs {
|
for _, c := range n.cidrs {
|
||||||
execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name())
|
execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,19 +6,14 @@ package lower
|
|||||||
import "net"
|
import "net"
|
||||||
|
|
||||||
func (n *NIC) Up() {
|
func (n *NIC) Up() {
|
||||||
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "enabled")
|
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")
|
||||||
_, 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 ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu)
|
execute("cmd", "/c", "netsh interface ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu)
|
||||||
for _, c := range n.cidrs {
|
for _, c := range n.cidrs {
|
||||||
ip, cidr, err := net.ParseCIDR(c)
|
ip, cidr, err := net.ParseCIDR(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ func (wg *WG) init(srcport, dstport uint16) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
myip := net.ParseIP(wg.c.IP)
|
||||||
|
if myip == nil {
|
||||||
|
panic("invalid ip " + wg.c.IP)
|
||||||
|
}
|
||||||
for _, p := range wg.c.Peers {
|
for _, p := range wg.c.Peers {
|
||||||
for _, ip := range p.AllowedIPs {
|
for _, ip := range p.AllowedIPs {
|
||||||
if len(ip) == 0 || ip[0] == 'x' {
|
if len(ip) == 0 || ip[0] == 'x' {
|
||||||
@@ -94,11 +98,11 @@ func (wg *WG) init(srcport, dstport uint16) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wg.me = link.NewMe(&link.MyConfig{
|
wg.me = link.NewMe(&link.MyConfig{
|
||||||
MyIPwithMask: wg.c.IP + "/32",
|
MyIPwithMask: myip.String() + "/32",
|
||||||
MyEndpoint: wg.c.EndPoint,
|
MyEndpoint: wg.c.EndPoint,
|
||||||
Network: wg.c.Network,
|
Network: wg.c.Network,
|
||||||
PrivateKey: &wg.key,
|
PrivateKey: &wg.key,
|
||||||
NIC: lower.NewNIC(wg.c.IP, wg.c.SubNet, strconv.FormatInt(wg.c.MTU, 10), cidrs...),
|
NIC: lower.NewNIC(myip, mysubnet, strconv.FormatInt(wg.c.MTU, 10), cidrs...),
|
||||||
SrcPort: srcport,
|
SrcPort: srcport,
|
||||||
DstPort: dstport,
|
DstPort: dstport,
|
||||||
MTU: uint16(wg.c.MTU),
|
MTU: uint16(wg.c.MTU),
|
||||||
|
|||||||
Reference in New Issue
Block a user