mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-05 08:47:00 +08:00
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
|
|
"github.com/xjasonlyu/tun2socks/v2/core/option"
|
|
)
|
|
|
|
const (
|
|
// nicPromiscuousModeEnabled is the value used by stack to enable
|
|
// or disable NIC's promiscuous mode.
|
|
nicPromiscuousModeEnabled = true
|
|
|
|
// nicSpoofingEnabled is the value used by stack to enable or disable
|
|
// NIC's spoofing.
|
|
nicSpoofingEnabled = true
|
|
)
|
|
|
|
// withCreatingNIC creates NIC for stack.
|
|
func withCreatingNIC(nicID tcpip.NICID, ep stack.LinkEndpoint) option.Option {
|
|
return func(s *stack.Stack) error {
|
|
if err := s.CreateNICWithOptions(nicID, ep,
|
|
stack.NICOptions{
|
|
Disabled: false,
|
|
// If no queueing discipline was specified
|
|
// provide a stub implementation that just
|
|
// delegates to the lower link endpoint.
|
|
QDisc: nil,
|
|
}); err != nil {
|
|
return fmt.Errorf("create NIC: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// withPromiscuousMode sets promiscuous mode in the given NICs.
|
|
func withPromiscuousMode(nicID tcpip.NICID, v bool) option.Option {
|
|
return func(s *stack.Stack) error {
|
|
if err := s.SetPromiscuousMode(nicID, v); err != nil {
|
|
return fmt.Errorf("set promiscuous mode: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// withSpoofing sets address spoofing in the given NICs, allowing
|
|
// endpoints to bind to any address in the NIC.
|
|
func withSpoofing(nicID tcpip.NICID, v bool) option.Option {
|
|
return func(s *stack.Stack) error {
|
|
if err := s.SetSpoofing(nicID, v); err != nil {
|
|
return fmt.Errorf("set spoofing: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
}
|