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
|
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.Conn, error) {
if Debug {
log.Println("Call:", r.Address())
}
rc, err := DialTCP("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
}
a, addr, port, err := ParseAddress(rc.LocalAddr().String())
if err != nil {
rc.Close()
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
}
if a == ATYPDomain {
addr = addr[1:]
}
p := NewReply(RepSuccess, a, addr, port)
if _, err := p.WriteTo(w); err != nil {
rc.Close()
return nil, err
}
return rc, nil
}
|