add support for linux and udp

This commit is contained in:
kony
2025-04-01 13:45:40 +08:00
parent ab67769c18
commit 32135e6423
13 changed files with 317 additions and 132 deletions

80
netstack/setup.go Normal file
View File

@@ -0,0 +1,80 @@
package netstack
import (
"fmt"
"github.com/quic-go/quic-go"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
)
func setNetStack(s *stack.Stack, nicID tcpip.NICID) error {
s.SetRouteTable([]tcpip.Route{
{
Destination: header.IPv4EmptySubnet,
NIC: nicID,
},
})
if err := netstack_stack.SetPromiscuousMode(nicID, true); err != nil {
return fmt.Errorf("promisc: %s", err)
}
if err := netstack_stack.SetSpoofing(nicID, true); err != nil {
return fmt.Errorf("spoofing: %s", err)
}
return nil
}
var (
init_stack_suss = false
netstack_stack *stack.Stack
)
func Start() error {
if init_stack_suss {
return nil
}
netstack_stack = stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
})
wintunEP, err := Open(GetName(), 1490) //因为要加自定义头防止超出1500
if err != nil {
return fmt.Errorf("请管理员权限运行")
}
SetTunIP(&wintunEP, GetRemoteIP(), 32)
// 将TUN设备注册到协议栈中使用NIC ID 1
nicID := tcpip.NICID(1)
if err := netstack_stack.CreateNIC(nicID, wintunEP); err != nil {
return fmt.Errorf("设备注册: %v", err)
}
setNetStack(netstack_stack, nicID)
init_stack_suss = true
return nil
}
func SetForWarder(stun_quic_conn quic.Connection) {
netstack_stack.SetTransportProtocolHandler(tcp.ProtocolNumber, NewTcpForwarder(netstack_stack, stun_quic_conn).HandlePacket)
netstack_stack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUdpForwarder(netstack_stack, stun_quic_conn).HandlePacket)
}
func GetRemoteIP() string {
return "192.17.19.1"
}
func GetName() string {
return "GoodLink"
}