mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-12 04:43:22 +08:00
完善主程序
This commit is contained in:
109
lower/nic.go
Normal file
109
lower/nic.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package lower
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/songgao/water"
|
||||
"github.com/songgao/water/waterutil"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/gold/link"
|
||||
)
|
||||
|
||||
// NIC 虚拟网卡
|
||||
type NIC struct {
|
||||
ifce *water.Interface
|
||||
ip string
|
||||
subnet string
|
||||
hasstart bool
|
||||
}
|
||||
|
||||
// NewNIC 新建 TUN 网络接口卡
|
||||
// 网卡地址为 ip, 所属子网为 subnet
|
||||
func NewNIC(ip, subnet string) (n *NIC) {
|
||||
ifce, err := water.New(water.Config{DeviceType: water.TUN})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
n = &NIC{
|
||||
ifce: ifce,
|
||||
ip: ip,
|
||||
subnet: subnet,
|
||||
}
|
||||
n.prepare()
|
||||
return
|
||||
}
|
||||
|
||||
// Start 开始处理网卡消息,阻塞
|
||||
func (nc *NIC) Start(m *link.Me) {
|
||||
if nc.hasstart {
|
||||
return
|
||||
}
|
||||
nc.hasstart = true
|
||||
go func() { // 接收到 NIC
|
||||
for nc.hasstart {
|
||||
packet := m.Read()
|
||||
logrus.Infoln("[lower] recv", len(packet.Data), "bytes packet")
|
||||
if !waterutil.IsIPv4(packet.Data) {
|
||||
logrus.Warnln("[lower] recv recv non-ipv4 packet")
|
||||
continue
|
||||
}
|
||||
_, err := nc.ifce.Write(packet.Data)
|
||||
if err != nil {
|
||||
logrus.Errorln("[lower] recv write to nic err:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
buf := make([]byte, 4096)
|
||||
for nc.hasstart { // 从 NIC 发送
|
||||
packet := buf
|
||||
n, err := nc.ifce.Read(packet)
|
||||
if err != nil {
|
||||
logrus.Errorln("[lower] send read from nic err:", err)
|
||||
break
|
||||
}
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
packet = packet[:n]
|
||||
if !waterutil.IsIPv4(packet) {
|
||||
logrus.Warnln("[lower] send recv non-ipv4 packet")
|
||||
continue
|
||||
}
|
||||
logrus.Infoln("[lower] send", n, "bytes packet")
|
||||
dst := waterutil.IPv4Destination(packet)
|
||||
srcport := waterutil.IPv4SourcePort(packet)
|
||||
dstport := waterutil.IPv4DestinationPort(packet)
|
||||
lnk, err := m.Connect(dst.String())
|
||||
if err != nil {
|
||||
logrus.Errorln("[lower] connect to peer err:", err)
|
||||
continue
|
||||
}
|
||||
lnk.Write(head.NewPacket(head.ProtoData, srcport, dstport, packet))
|
||||
}
|
||||
}
|
||||
|
||||
// Stop 停止处理
|
||||
func (n *NIC) Stop() {
|
||||
n.hasstart = false
|
||||
}
|
||||
|
||||
// Destroy 关闭网卡
|
||||
func (n *NIC) Destroy() error {
|
||||
return n.ifce.Close()
|
||||
}
|
||||
|
||||
func execute(c string, args ...string) {
|
||||
logrus.Printf("[lower] exec cmd: %v %v:", c, args)
|
||||
cmd := exec.Command(c, args...)
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stdin = os.Stdin
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
logrus.Panicln("[lower] failed to exec cmd:", err)
|
||||
}
|
||||
}
|
||||
17
lower/tun_darwin.go
Normal file
17
lower/tun_darwin.go
Normal file
@@ -0,0 +1,17 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package lower
|
||||
|
||||
func (n *NIC) prepare() {
|
||||
execute("ifconfig", n.ifce.Name(), "inet", n.ip, n.ip, "up")
|
||||
execute("route", "add", n.subnet, "-interface", n.ifce.Name())
|
||||
}
|
||||
|
||||
func (n *NIC) Up() {
|
||||
execute("ifconfig", n.ifce.Name(), "inet", n.ip, n.ip, "up")
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
execute("ifconfig", n.ifce.Name(), "down")
|
||||
}
|
||||
18
lower/tun_linux.go
Normal file
18
lower/tun_linux.go
Normal file
@@ -0,0 +1,18 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package lower
|
||||
|
||||
func (n *NIC) prepare() {
|
||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "mtu", "1500")
|
||||
execute("/sbin/ip", "addr", "add", ip, "dev", ifcename)
|
||||
execute("/sbin/ip", "route", "add", subnet, "dev", ifcename)
|
||||
}
|
||||
|
||||
func (n *NIC) Up() {
|
||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "up")
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "down")
|
||||
}
|
||||
16
lower/tun_stub.go
Normal file
16
lower/tun_stub.go
Normal file
@@ -0,0 +1,16 @@
|
||||
//go:build !darwin && !linux && !windows
|
||||
// +build !darwin,!linux,!windows
|
||||
|
||||
package lower
|
||||
|
||||
func (n *NIC) prepare() {
|
||||
panic("not support this os now")
|
||||
}
|
||||
|
||||
func (n *NIC) Up() {
|
||||
panic("not support this os now")
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
panic("not support this os now")
|
||||
}
|
||||
18
lower/tun_windows.go
Normal file
18
lower/tun_windows.go
Normal file
@@ -0,0 +1,18 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package lower
|
||||
|
||||
func (n *NIC) prepare() {
|
||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "mtu", "1500")
|
||||
execute("/sbin/ip", "addr", "add", ip, "dev", ifcename)
|
||||
execute("/sbin/ip", "route", "add", subnet, "dev", ifcename)
|
||||
}
|
||||
|
||||
func (n *NIC) Up() {
|
||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "up")
|
||||
}
|
||||
|
||||
func (n *NIC) Down() {
|
||||
execute("/sbin/ip", "link", "set", "dev", ifcename, "down")
|
||||
}
|
||||
Reference in New Issue
Block a user