Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

允许绑定出口到指定网卡上 #817

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Chain struct {
isRoute bool
Retries int
Mark int
Interface string
nodeGroups []*NodeGroup
route []Node // nodes in the selected route
}
Expand All @@ -36,9 +37,11 @@ func NewChain(nodes ...Node) *Chain {

// newRoute creates a chain route.
// a chain route is the final route after node selection.
func newRoute(nodes ...Node) *Chain {
func (c *Chain) newRoute(nodes ...Node) *Chain {
chain := NewChain(nodes...)
chain.isRoute = true
chain.Interface = c.Interface
chain.Mark = c.Mark
return chain
}

Expand Down Expand Up @@ -166,6 +169,18 @@ func (c *Chain) dialWithOptions(ctx context.Context, network, address string, op
}
}

if c.Interface != "" {
controlFunction = func(_, _ string, cc syscall.RawConn) error {
return cc.Control(func(fd uintptr) {
err := setSocketInterface(int(fd), c.Interface)

if err != nil {
log.Logf("net dialer set interface %s error: %s", c.Interface, err)
}
})
}
}

if route.IsEmpty() {
switch network {
case "udp", "udp4", "udp6":
Expand Down Expand Up @@ -303,13 +318,13 @@ func (c *Chain) selectRoute() (route *Chain, err error) {
// selectRouteFor selects route with bypass testing.
func (c *Chain) selectRouteFor(addr string) (route *Chain, err error) {
if c.IsEmpty() {
return newRoute(), nil
return c.newRoute(), nil
}
if c.isRoute {
return c, nil
}

route = newRoute()
route = c.newRoute()
var nl []Node

for _, group := range c.nodeGroups {
Expand All @@ -327,7 +342,7 @@ func (c *Chain) selectRouteFor(addr string) (route *Chain, err error) {
node.DialOptions = append(node.DialOptions,
ChainDialOption(route),
)
route = newRoute() // cutoff the chain for multiplex node.
route = c.newRoute() // cutoff the chain for multiplex node.
}

route.AddNode(node)
Expand Down
1 change: 1 addition & 0 deletions cmd/gost/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
flag.Var(&baseCfg.route.ServeNodes, "L", "listen address, can listen on multiple ports (required)")
flag.IntVar(&baseCfg.route.Mark, "M", 0, "Specify out connection mark")
flag.StringVar(&configureFile, "C", "", "configure file")
flag.StringVar(&baseCfg.route.Interface, "I", "", "Interface to bind")
flag.BoolVar(&baseCfg.Debug, "D", false, "enable debug log")
flag.BoolVar(&printVersion, "V", false, "print version")
if pprofEnabled {
Expand Down
2 changes: 2 additions & 0 deletions cmd/gost/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ type route struct {
ChainNodes stringList
Retries int
Mark int
Interface string
}

func (r *route) parseChain() (*gost.Chain, error) {
chain := gost.NewChain()
chain.Retries = r.Retries
chain.Mark = r.Mark
chain.Interface = r.Interface
gid := 1 // group ID

for _, ns := range r.ChainNodes {
Expand Down
4 changes: 4 additions & 0 deletions sockopts_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import "syscall"
func setSocketMark(fd int, value int) (e error) {
return syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_MARK, value)
}

func setSocketInterface(fd int, value string) (e error) {
return syscall.SetsockoptString(fd, syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, value)
}
4 changes: 4 additions & 0 deletions sockopts_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ package gost
func setSocketMark(fd int, value int) (e error) {
return nil
}

func setSocketInterface(fd int, value string) (e error) {
return nil
}