forked from txthinking/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
56 lines (53 loc) · 1.74 KB
/
udp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package socks5
import (
"bytes"
"log"
"net"
)
// UDP remote conn which u want to connect with your dialer.
// Error or OK both replied.
// Addr can be used to associate TCP connection with the coming UDP connection.
func (r *Request) UDP(c *net.TCPConn, serverAddr *net.UDPAddr) (*net.UDPAddr, error) {
var clientAddr *net.UDPAddr
var err error
if bytes.Compare(r.DstPort, []byte{0x00, 0x00}) == 0 {
// If the requested Host/Port is all zeros, the relay should simply use the Host/Port that sent the request.
// https://stackoverflow.com/questions/62283351/how-to-use-socks-5-proxy-with-tidudpclient-properly
clientAddr, err = net.ResolveUDPAddr("udp", c.RemoteAddr().String())
} else {
clientAddr, err = net.ResolveUDPAddr("udp", r.Address())
}
if err != nil {
var p *Reply
if r.Atyp == ATYPIPv4 || r.Atyp == ATYPDomain {
p = NewReply(RepHostUnreachable, ATYPIPv4, []byte{0x00, 0x00, 0x00, 0x00}, []byte{0x00, 0x00})
} else {
p = NewReply(RepHostUnreachable, ATYPIPv6, []byte(net.IPv6zero), []byte{0x00, 0x00})
}
if _, err := p.WriteTo(c); err != nil {
return nil, err
}
return nil, err
}
if Debug {
log.Println("Client wants to start UDP talk use", clientAddr.String())
}
a, addr, port, err := ParseAddress(serverAddr.String())
if err != nil {
var p *Reply
if r.Atyp == ATYPIPv4 || r.Atyp == ATYPDomain {
p = NewReply(RepHostUnreachable, ATYPIPv4, []byte{0x00, 0x00, 0x00, 0x00}, []byte{0x00, 0x00})
} else {
p = NewReply(RepHostUnreachable, ATYPIPv6, []byte(net.IPv6zero), []byte{0x00, 0x00})
}
if _, err := p.WriteTo(c); err != nil {
return nil, err
}
return nil, err
}
p := NewReply(RepSuccess, a, addr, port)
if _, err := p.WriteTo(c); err != nil {
return nil, err
}
return clientAddr, nil
}