mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-08 10:10:12 +08:00
44 lines
935 B
Go
44 lines
935 B
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
)
|
|
|
|
type icmpHandleFunc func()
|
|
|
|
func WithICMPHandler(_ icmpHandleFunc) Option {
|
|
return func(s *stack.Stack) error {
|
|
// Add default route table for IPv4 and IPv6
|
|
// This will handle all incoming ICMP packets.
|
|
s.SetRouteTable([]tcpip.Route{
|
|
{
|
|
Destination: mustSubnet("0.0.0.0/0"),
|
|
NIC: defaultNICID,
|
|
},
|
|
{
|
|
Destination: mustSubnet("::/0"),
|
|
NIC: defaultNICID,
|
|
},
|
|
})
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// mustSubnet returns tcpip.Subnet from CIDR string.
|
|
func mustSubnet(s string) tcpip.Subnet {
|
|
_, ipNet, err := net.ParseCIDR(s)
|
|
if err != nil {
|
|
panic(fmt.Errorf("unable to ParseCIDR(%s): %w", s, err))
|
|
}
|
|
|
|
subnet, err := tcpip.NewSubnet(tcpip.Address(ipNet.IP), tcpip.AddressMask(ipNet.Mask))
|
|
if err != nil {
|
|
panic(fmt.Errorf("unable to NewSubnet(%s): %w", ipNet, err))
|
|
}
|
|
return subnet
|
|
}
|