socks5.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proxy
  5. import (
  6. "context"
  7. "net"
  8. "golang.org/x/net/internal/socks"
  9. )
  10. // SOCKS5 returns a Dialer that makes SOCKSv5 connections to the given
  11. // address with an optional username and password.
  12. // See RFC 1928 and RFC 1929.
  13. func SOCKS5(network, address string, auth *Auth, forward Dialer) (Dialer, error) {
  14. d := socks.NewDialer(network, address)
  15. if forward != nil {
  16. if f, ok := forward.(ContextDialer); ok {
  17. d.ProxyDial = func(ctx context.Context, network string, address string) (net.Conn, error) {
  18. return f.DialContext(ctx, network, address)
  19. }
  20. } else {
  21. d.ProxyDial = func(ctx context.Context, network string, address string) (net.Conn, error) {
  22. return dialContext(ctx, forward, network, address)
  23. }
  24. }
  25. }
  26. if auth != nil {
  27. up := socks.UsernamePassword{
  28. Username: auth.User,
  29. Password: auth.Password,
  30. }
  31. d.AuthMethods = []socks.AuthMethod{
  32. socks.AuthMethodNotRequired,
  33. socks.AuthMethodUsernamePassword,
  34. }
  35. d.Authenticate = up.Authenticate
  36. }
  37. return d, nil
  38. }