Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AnGgIt86 committed Sep 7, 2024
2 parents 7489dee + 97ef211 commit 2df580b
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions AndroidLibXrayLite/libv2ray_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (d *ProtectedDialer) Dial(ctx context.Context,
}

curIP := d.vServer.currentIP()
conn, err := d.fdConn(ctx, curIP, d.vServer.Port, fd)
conn, err := d.fdConn(ctx, curIP, d.vServer.Port, dest.Network, fd)
if err != nil {
d.vServer.NextIP()
return nil, err
Expand All @@ -273,14 +273,14 @@ func (d *ProtectedDialer) Dial(ctx context.Context,

// use the first resolved address.
// the result IP may vary, eg: IPv6 addrs comes first if client has ipv6 address
return d.fdConn(ctx, resolved.IPs[0], resolved.Port, fd)
return d.fdConn(ctx, resolved.IPs[0], resolved.Port, dest.Network, fd)
}

func (d *ProtectedDialer) DestIpAddress() net.IP {
return d.vServer.currentIP()
}

func (d *ProtectedDialer) fdConn(ctx context.Context, ip net.IP, port int, fd int) (net.Conn, error) {
func (d *ProtectedDialer) fdConn(ctx context.Context, ip net.IP, port int, network v2net.Network, fd int) (net.Conn, error) {

defer unix.Close(fd)

Expand All @@ -295,9 +295,16 @@ func (d *ProtectedDialer) fdConn(ctx context.Context, ip net.IP, port int, fd in
}
copy(sa.Addr[:], ip.To16())

if err := unix.Connect(fd, sa); err != nil {
log.Printf("fdConn unix.Connect err, Close Fd: %d Err: %v", fd, err)
return nil, err
if network == v2net.Network_UDP {
if err := unix.Bind(fd, &unix.SockaddrInet6{}); err != nil {
log.Printf("fdConn unix.Bind err, Close Fd: %d Err: %v", fd, err)
return nil, err
}
} else {
if err := unix.Connect(fd, sa); err != nil {
log.Printf("fdConn unix.Connect err, Close Fd: %d Err: %v", fd, err)
return nil, err
}
}

file := os.NewFile(uintptr(fd), "Socket")
Expand All @@ -308,11 +315,25 @@ func (d *ProtectedDialer) fdConn(ctx context.Context, ip net.IP, port int, fd in

defer file.Close()
//Closing conn does not affect file, and closing file does not affect conn.
conn, err := net.FileConn(file)
if err != nil {
log.Printf("fdConn FileConn Close Fd: %d Err: %v", fd, err)
return nil, err
if network == v2net.Network_UDP {
packetConn, err := net.FilePacketConn(file)
if err != nil {
log.Printf("fdConn FilePacketConn Close Fd: %d Err: %v", fd, err)
return nil, err
}
return &v2internet.PacketConnWrapper{
Conn: packetConn,
Dest: &net.UDPAddr{
IP: ip,
Port: port,
},
}, nil
} else {
conn, err := net.FileConn(file)
if err != nil {
log.Printf("fdConn FileConn Close Fd: %d Err: %v", fd, err)
return nil, err
}
return conn, nil
}

return conn, nil
}

0 comments on commit 2df580b

Please sign in to comment.