proxy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Enable = true
  40. auth.Username = proxyUrl.User.Username()
  41. auth.Passwd, _ = proxyUrl.User.Password()
  42. }
  43. switch proxyUrl.Scheme {
  44. case "http":
  45. return DialTcpByHttpProxy(proxyUrl.Host, addr, auth)
  46. case "socks5":
  47. return DialTcpBySocks5Proxy(proxyUrl.Host, addr, auth)
  48. default:
  49. err = fmt.Errorf("Proxy URL scheme must be http or socks5, not [%s]", proxyUrl.Scheme)
  50. return
  51. }
  52. }
  53. func DialTcpByHttpProxy(proxyHost string, dstAddr string, auth *ProxyAuth) (c net.Conn, err error) {
  54. if c, err = net.Dial("tcp", proxyHost); err != nil {
  55. return
  56. }
  57. req, err := http.NewRequest("CONNECT", "http://"+dstAddr, nil)
  58. if err != nil {
  59. return
  60. }
  61. if auth.Enable {
  62. req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth.Username+":"+auth.Passwd)))
  63. }
  64. req.Header.Set("User-Agent", "Mozilla/5.0")
  65. req.Write(c)
  66. resp, err := http.ReadResponse(bufio.NewReader(c), req)
  67. if err != nil {
  68. return nil, err
  69. }
  70. resp.Body.Close()
  71. if resp.StatusCode != 200 {
  72. err = fmt.Errorf("DialTcpByHttpProxy error, StatusCode [%d]", resp.StatusCode)
  73. return
  74. }
  75. return
  76. }
  77. func DialTcpBySocks5Proxy(proxyHost string, dstAddr string, auth *ProxyAuth) (c net.Conn, err error) {
  78. var s5Auth *proxy.Auth
  79. if auth.Enable {
  80. s5Auth = &proxy.Auth{
  81. User: auth.Username,
  82. Password: auth.Passwd,
  83. }
  84. }
  85. dialer, err := proxy.SOCKS5("tcp", proxyHost, s5Auth, nil)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if c, err = dialer.Dial("tcp", dstAddr); err != nil {
  90. return
  91. }
  92. return
  93. }