github.com/metacubex/mihomo@v1.18.5/component/dialer/patch_android.go (about)

     1  //go:build android && cmfa
     2  
     3  package dialer
     4  
     5  import (
     6  	"context"
     7  	"net"
     8  	"net/netip"
     9  	"syscall"
    10  )
    11  
    12  type SocketControl func(network, address string, conn syscall.RawConn) error
    13  
    14  var DefaultSocketHook SocketControl
    15  
    16  func dialContextHooked(ctx context.Context, network string, destination netip.Addr, port string) (net.Conn, error) {
    17  	dialer := &net.Dialer{
    18  		Control: DefaultSocketHook,
    19  	}
    20  
    21  	conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(destination.String(), port))
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	if t, ok := conn.(*net.TCPConn); ok {
    27  		t.SetKeepAlive(false)
    28  	}
    29  
    30  	return conn, nil
    31  }
    32  
    33  func listenPacketHooked(ctx context.Context, network, address string) (net.PacketConn, error) {
    34  	lc := &net.ListenConfig{
    35  		Control: DefaultSocketHook,
    36  	}
    37  
    38  	return lc.ListenPacket(ctx, network, address)
    39  }