proxy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2018 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package net
  15. import (
  16. "bufio"
  17. "encoding/base64"
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "net/url"
  22. "golang.org/x/net/proxy"
  23. )
  24. type ProxyAuth struct {
  25. Enable bool
  26. Username string
  27. Passwd string
  28. }
  29. func DialTcpByProxy(proxyStr string, addr string) (c net.Conn, err error) {
  30. if proxyStr == "" {
  31. return net.Dial("tcp", addr)
  32. }
  33. var proxyUrl *url.URL
  34. if proxyUrl, err = url.Parse(proxyStr); err != nil {
  35. return
  36. }
  37. auth := &ProxyAuth{}
  38. if proxyUrl.User != nil {
  39. auth.Username = proxyUrl.User.Username()
  40. auth.Passwd, _ = proxyUrl.User.Password()
  41. }
  42. switch proxyUrl.Scheme {
  43. case "http":
  44. return DialTcpByHttpProxy(proxyUrl.Host, addr, auth)
  45. case "socks5":
  46. return DialTcpBySocks5Proxy(proxyUrl.Host, addr, auth)
  47. default:
  48. err = fmt.Errorf("Proxy URL scheme must be http or socks5, not [%s]", proxyUrl.Scheme)
  49. return
  50. }
  51. }
  52. func DialTcpByHttpProxy(proxyHost string, dstAddr string, auth *ProxyAuth) (c net.Conn, err error) {
  53. if c, err = net.Dial("tcp", proxyHost); err != nil {
  54. return
  55. }
  56. req, err := http.NewRequest("CONNECT", "http://"+dstAddr, nil)
  57. if err != nil {
  58. return
  59. }
  60. if auth.Enable {
  61. req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth.Username+":"+auth.Passwd)))
  62. }
  63. req.Header.Set("User-Agent", "Mozilla/5.0")
  64. req.Write(c)
  65. resp, err := http.ReadResponse(bufio.NewReader(c), req)
  66. if err != nil {
  67. return nil, err
  68. }
  69. resp.Body.Close()
  70. if resp.StatusCode != 200 {
  71. err = fmt.Errorf("DialTcpByHttpProxy error, StatusCode [%d]", resp.StatusCode)
  72. return
  73. }
  74. return
  75. }
  76. func DialTcpBySocks5Proxy(proxyHost string, dstAddr string, auth *ProxyAuth) (c net.Conn, err error) {
  77. var s5Auth *proxy.Auth
  78. if auth.Enable {
  79. s5Auth = &proxy.Auth{
  80. User: auth.Username,
  81. Password: auth.Passwd,
  82. }
  83. }
  84. dialer, err := proxy.SOCKS5("tcp", proxyHost, s5Auth, nil)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if c, err = dialer.Dial("tcp", dstAddr); err != nil {
  89. return
  90. }
  91. return
  92. }