Skip to content

Commit

Permalink
# 这是一个 2 个提交的组合。
Browse files Browse the repository at this point in the history
# 这是第一个提交说明:

BREAKING CHANGE: Require context for FdControl

# 提交说明 SagerNet#2 将被跳过:

# Fix
  • Loading branch information
xchacha20-poly1305 committed Jul 19, 2024
1 parent a8e0f58 commit d9d088b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ import (
const DefaultTimeout = 5000 * time.Millisecond

// FdControl do some control before connect.
var FdControl func(fd int) = nil

func TcpPing(ctx context.Context, addr M.Socksaddr) (latency time.Duration, err error) {
dialer := &net.Dialer{}
var FdControl func(ctx context.Context, fd int) = nil

// TcpPing use TCP to probe `addr`.
// In unix, this function will use dialer.ControlContext.
func TcpPing(ctx context.Context, dialer *net.Dialer, addr M.Socksaddr) (latency time.Duration, err error) {
if isUnix {
dialer.ControlContext = func(_ context.Context, network, address string, c syscall.RawConn) error {
oldControl := dialer.ControlContext
dialer.ControlContext = func(ctx context.Context, network, address string, c syscall.RawConn) error {
if oldControl != nil {
err := oldControl(ctx, network, address, c)
if err != nil {
return err
}
}
return c.Control(func(fd uintptr) {
if FdControl != nil {
FdControl(int(fd))
FdControl(ctx, int(fd))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion ping_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func IcmpPing(ctx context.Context, addr M.Socksaddr, payload []byte) (time.Durat
f := os.NewFile(uintptr(fd), "dgram")

if FdControl != nil {
FdControl(fd)
FdControl(ctx, fd)
}

conn, err := net.FilePacketConn(f)
Expand Down

0 comments on commit d9d088b

Please sign in to comment.