mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-17 16:20:26 +08:00
fix: use nic mtu that minus packet header
This commit is contained in:
21
lower/nic.go
21
lower/nic.go
@@ -1,7 +1,6 @@
|
||||
package lower
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -11,14 +10,8 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type NICIO interface {
|
||||
io.ReadWriteCloser
|
||||
Up()
|
||||
Down()
|
||||
}
|
||||
|
||||
// NIC 虚拟网卡
|
||||
type NIC struct {
|
||||
// NICIO 虚拟网卡
|
||||
type NICIO struct {
|
||||
ifce *water.Interface
|
||||
ip net.IP
|
||||
subnet *net.IPNet
|
||||
@@ -31,7 +24,7 @@ type NIC struct {
|
||||
// 网卡地址为 ip, 所属子网为 subnet
|
||||
// 以本网卡为下一跳的所有子网为 cidrs
|
||||
// cidrs 不包括本网卡 subnet
|
||||
func NewNIC(ip net.IP, subnet *net.IPNet, 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)
|
||||
@@ -41,7 +34,7 @@ func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO {
|
||||
if bitsn != 32 {
|
||||
panic("mask len " + strconv.Itoa(bitsn) + " is not supported")
|
||||
}
|
||||
n := &NIC{
|
||||
n := &NICIO{
|
||||
ifce: ifce,
|
||||
ip: ip,
|
||||
subnet: subnet,
|
||||
@@ -53,16 +46,16 @@ func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO {
|
||||
}
|
||||
|
||||
// Read 匹配 PacketsIO Interface
|
||||
func (nc *NIC) Read(buf []byte) (int, error) {
|
||||
func (nc *NICIO) Read(buf []byte) (int, error) {
|
||||
return nc.ifce.Read(buf)
|
||||
}
|
||||
|
||||
func (nc *NIC) Write(packet []byte) (int, error) {
|
||||
func (nc *NICIO) Write(packet []byte) (int, error) {
|
||||
return nc.ifce.Write(packet)
|
||||
}
|
||||
|
||||
// Close 关闭网卡
|
||||
func (n *NIC) Close() error {
|
||||
func (n *NICIO) Close() error {
|
||||
return n.ifce.Close()
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ package lower
|
||||
|
||||
import "net"
|
||||
|
||||
func (n *NIC) Up() {
|
||||
func (n *NICIO) Up() {
|
||||
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159
|
||||
execute(
|
||||
"ifconfig", n.ifce.Name(),
|
||||
@@ -19,7 +19,7 @@ func (n *NIC) Up() {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
func (n *NICIO) Down() {
|
||||
execute("route", "delete", n.subnet.String(), "-interface", n.ifce.Name())
|
||||
for _, c := range n.cidrs {
|
||||
execute("route", "delete", c, "-interface", n.ifce.Name())
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
package lower
|
||||
|
||||
func (n *NIC) Up() {
|
||||
func (n *NICIO) Up() {
|
||||
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu)
|
||||
execute("/sbin/ip", "addr", "add", n.rawipnet, "dev", n.ifce.Name())
|
||||
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up")
|
||||
@@ -12,7 +12,7 @@ func (n *NIC) Up() {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
func (n *NICIO) Down() {
|
||||
for _, c := range n.cidrs {
|
||||
execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name())
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
package lower
|
||||
|
||||
func (n *NIC) Up() {
|
||||
func (n *NICIO) Up() {
|
||||
panic("not support lower on this os now")
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
func (n *NICIO) Down() {
|
||||
panic("not support lower on this os now")
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ package lower
|
||||
|
||||
import "net"
|
||||
|
||||
func (n *NIC) Up() {
|
||||
func (n *NICIO) Up() {
|
||||
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 {
|
||||
@@ -17,7 +17,7 @@ func (n *NIC) Up() {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
func (n *NICIO) Down() {
|
||||
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled")
|
||||
for _, c := range n.cidrs {
|
||||
ip, _, err := net.ParseCIDR(c)
|
||||
|
||||
Reference in New Issue
Block a user