MCPcopy
hub / github.com/gorilla/websocket / connect

Method connect

x_net_proxy.go:344–473  ·  x_net_proxy.go::proxy_socks5.connect

connect takes an existing connection to a socks5 proxy server, and commands the server to extend that connection to target, which must be a canonical address with a host and port.

(conn net.Conn, target string)

Source from the content-addressed store, hash-verified

342// and commands the server to extend that connection to target,
343// which must be a canonical address with a host and port.
344func (s *proxy_socks5) connect(conn net.Conn, target string) error {
345 host, portStr, err := net.SplitHostPort(target)
346 if err != nil {
347 return err
348 }
349
350 port, err := strconv.Atoi(portStr)
351 if err != nil {
352 return errors.New("proxy: failed to parse port number: " + portStr)
353 }
354 if port < 1 || port > 0xffff {
355 return errors.New("proxy: port number out of range: " + portStr)
356 }
357
358 // the size here is just an estimate
359 buf := make([]byte, 0, 6+len(host))
360
361 buf = append(buf, proxy_socks5Version)
362 if len(s.user) > 0 && len(s.user) < 256 && len(s.password) < 256 {
363 buf = append(buf, 2 /* num auth methods */, proxy_socks5AuthNone, proxy_socks5AuthPassword)
364 } else {
365 buf = append(buf, 1 /* num auth methods */, proxy_socks5AuthNone)
366 }
367
368 if _, err := conn.Write(buf); err != nil {
369 return errors.New("proxy: failed to write greeting to SOCKS5 proxy at " + s.addr + ": " + err.Error())
370 }
371
372 if _, err := io.ReadFull(conn, buf[:2]); err != nil {
373 return errors.New("proxy: failed to read greeting from SOCKS5 proxy at " + s.addr + ": " + err.Error())
374 }
375 if buf[0] != 5 {
376 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " has unexpected version " + strconv.Itoa(int(buf[0])))
377 }
378 if buf[1] == 0xff {
379 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " requires authentication")
380 }
381
382 // See RFC 1929
383 if buf[1] == proxy_socks5AuthPassword {
384 buf = buf[:0]
385 buf = append(buf, 1 /* password protocol version */)
386 buf = append(buf, uint8(len(s.user)))
387 buf = append(buf, s.user...)
388 buf = append(buf, uint8(len(s.password)))
389 buf = append(buf, s.password...)
390
391 if _, err := conn.Write(buf); err != nil {
392 return errors.New("proxy: failed to write authentication request to SOCKS5 proxy at " + s.addr + ": " + err.Error())
393 }
394
395 if _, err := io.ReadFull(conn, buf[:2]); err != nil {
396 return errors.New("proxy: failed to read authentication reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())
397 }
398
399 if buf[1] != 0 {
400 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " rejected username/password")
401 }

Callers 1

DialMethod · 0.95

Calls 2

WriteMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected