forked from txthinking/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
49 lines (45 loc) · 1.22 KB
/
connect.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
package socks5
import (
"io"
"log"
"net"
)
// Connect remote conn which u want to connect with your dialer
// Error or OK both replied.
func (r *Request) Connect(w io.Writer) (*net.TCPConn, error) {
if Debug {
log.Println("Call:", r.Address())
}
tmp, err := Dial.Dial("tcp", 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(w); err != nil {
return nil, err
}
return nil, err
}
rc := tmp.(*net.TCPConn)
a, addr, port, err := ParseAddress(rc.LocalAddr().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(w); err != nil {
return nil, err
}
return nil, err
}
p := NewReply(RepSuccess, a, addr, port)
if _, err := p.WriteTo(w); err != nil {
return nil, err
}
return rc, nil
}