()
| 32 | } |
| 33 | |
| 34 | func (proxy *Socks5Proxy) Dial() (net.Conn, error) { |
| 35 | var NOT_SUPPORT = errors.New("unknown protocol") |
| 36 | var WRONG_AUTH = errors.New("wrong auth method") |
| 37 | var SERVER_ERROR = errors.New("proxy server error") |
| 38 | var TOO_LONG = errors.New("user/pass too long(max 255)") |
| 39 | var AUTH_FAIL = errors.New("wrong username/password") |
| 40 | |
| 41 | proxyConn, err := net.Dial("tcp", proxy.ProxyAddr) |
| 42 | if err != nil { |
| 43 | return proxyConn, err |
| 44 | } |
| 45 | |
| 46 | host, portS, err := net.SplitHostPort(proxy.PeerAddr) |
| 47 | if err != nil { |
| 48 | return proxyConn, err |
| 49 | } |
| 50 | portUint64, err := strconv.ParseUint(portS, 10, 16) |
| 51 | if err != nil { |
| 52 | return proxyConn, err |
| 53 | } |
| 54 | |
| 55 | port := uint16(portUint64) |
| 56 | portB := make([]byte, 2) |
| 57 | binary.BigEndian.PutUint16(portB, port) |
| 58 | // No Auth |
| 59 | if proxy.UserName == "" && proxy.Password == "" { |
| 60 | proxyConn.Write([]byte{0x05, 0x01, 0x00}) |
| 61 | } else { |
| 62 | // u and p |
| 63 | proxyConn.Write([]byte{0x05, 0x01, 0x02}) |
| 64 | } |
| 65 | |
| 66 | authWayBuf := make([]byte, 2) |
| 67 | |
| 68 | _, err = io.ReadFull(proxyConn, authWayBuf) |
| 69 | if err != nil { |
| 70 | return proxyConn, err |
| 71 | } |
| 72 | |
| 73 | if authWayBuf[0] == 0x05 { |
| 74 | switch authWayBuf[1] { |
| 75 | case 0x00: |
| 76 | case 0x02: |
| 77 | userLen := len(proxy.UserName) |
| 78 | passLen := len(proxy.Password) |
| 79 | if userLen > 255 || passLen > 255 { |
| 80 | return proxyConn, TOO_LONG |
| 81 | } |
| 82 | |
| 83 | buff := make([]byte, 0, 3+userLen+passLen) |
| 84 | buff = append(buff, 0x01, byte(userLen)) |
| 85 | buff = append(buff, []byte(proxy.UserName)...) |
| 86 | buff = append(buff, byte(passLen)) |
| 87 | buff = append(buff, []byte(proxy.Password)...) |
| 88 | proxyConn.Write(buff) |
| 89 | |
| 90 | responseBuf := make([]byte, 2) |
| 91 | _, err = io.ReadFull(proxyConn, responseBuf) |
nothing calls this directly
no test coverage detected