Sockopt: Cancel dialing after failed to bind interface, external controllers included such as TUN inbound's (#7173)

https://github.com/XTLS/Xray-core/pull/7161#issuecomment-6185101741

Fixes https://github.com/XTLS/Xray-core/issues/7160#issuecomment-6200788453

---------

Co-authored-by: Gleb <a1212gleb@gmail.com>
This commit is contained in:
风扇滑翔翼
2026-10-16 06:37:52 +00:00
committed by GitHub
co-authored by Gleb
parent d15966c76c
commit fe2ae23f42
6 changed files with 66 additions and 28 deletions
+31 -17
View File
@@ -68,19 +68,27 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
}
}
var lc net.ListenConfig
lc.Control = func(network, address string, c syscall.RawConn) error {
for _, ctl := range Controllers {
if err := ctl(network, address, c); err != nil {
errors.LogInfoInner(ctx, err, "failed to apply external controller")
}
}
return c.Control(func(fd uintptr) {
if sockopt != nil {
if err := applyOutboundSocketOptions(network, destAddr.String(), fd, sockopt); err != nil {
errors.LogInfo(ctx, err, "failed to apply socket options")
if sockopt != nil || len(Controllers) > 0 {
lc.Control = func(network, address string, c syscall.RawConn) error {
for _, ctl := range Controllers {
if err := ctl(network, address, c); err != nil {
return errors.New("failed to apply external controller").Base(err)
}
}
})
if sockopt != nil {
var sockErr, controlErr error
controlErr = c.Control(func(fd uintptr) {
if sockErr = applyOutboundSocketOptions(network, destAddr.String(), fd, sockopt); sockErr != nil {
errors.LogInfoInner(ctx, sockErr, "failed to apply socket options")
}
})
if _, ok := sockErr.(*BindError); ok {
return sockErr
}
return controlErr
}
return nil
}
}
packetConn, err := lc.ListenPacket(ctx, srcAddr.Network(), srcAddr.String())
if err != nil {
@@ -128,16 +136,22 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
dialer.Control = func(network, address string, c syscall.RawConn) error {
for _, ctl := range Controllers {
if err := ctl(network, address, c); err != nil {
errors.LogInfoInner(ctx, err, "failed to apply external controller")
return errors.New("failed to apply external controller").Base(err)
}
}
return c.Control(func(fd uintptr) {
if sockopt != nil {
if err := applyOutboundSocketOptions(network, address, fd, sockopt); err != nil {
errors.LogInfoInner(ctx, err, "failed to apply socket options")
if sockopt != nil {
var sockErr, controlErr error
controlErr = c.Control(func(fd uintptr) {
if sockErr = applyOutboundSocketOptions(network, address, fd, sockopt); sockErr != nil {
errors.LogInfoInner(ctx, sockErr, "failed to apply socket options")
}
})
if _, ok := sockErr.(*BindError); ok {
return sockErr
}
})
return controlErr
}
return nil
}
}