request.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package request
  2. import (
  3. "bufio"
  4. "bytes"
  5. "crypto/tls"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "time"
  14. "github.com/fatedier/frp/test/e2e/pkg/rpc"
  15. libnet "github.com/fatedier/golib/net"
  16. )
  17. type Request struct {
  18. protocol string
  19. // for all protocol
  20. addr string
  21. port int
  22. body []byte
  23. timeout time.Duration
  24. // for http or https
  25. method string
  26. host string
  27. path string
  28. headers map[string]string
  29. tlsConfig *tls.Config
  30. proxyURL string
  31. }
  32. func New() *Request {
  33. return &Request{
  34. protocol: "tcp",
  35. addr: "127.0.0.1",
  36. method: "GET",
  37. path: "/",
  38. }
  39. }
  40. func (r *Request) Protocol(protocol string) *Request {
  41. r.protocol = protocol
  42. return r
  43. }
  44. func (r *Request) TCP() *Request {
  45. r.protocol = "tcp"
  46. return r
  47. }
  48. func (r *Request) UDP() *Request {
  49. r.protocol = "udp"
  50. return r
  51. }
  52. func (r *Request) HTTP() *Request {
  53. r.protocol = "http"
  54. return r
  55. }
  56. func (r *Request) HTTPS() *Request {
  57. r.protocol = "https"
  58. return r
  59. }
  60. func (r *Request) Proxy(url string) *Request {
  61. r.proxyURL = url
  62. return r
  63. }
  64. func (r *Request) Addr(addr string) *Request {
  65. r.addr = addr
  66. return r
  67. }
  68. func (r *Request) Port(port int) *Request {
  69. r.port = port
  70. return r
  71. }
  72. func (r *Request) HTTPParams(method, host, path string, headers map[string]string) *Request {
  73. r.method = method
  74. r.host = host
  75. r.path = path
  76. r.headers = headers
  77. return r
  78. }
  79. func (r *Request) HTTPHost(host string) *Request {
  80. r.host = host
  81. return r
  82. }
  83. func (r *Request) HTTPPath(path string) *Request {
  84. r.path = path
  85. return r
  86. }
  87. func (r *Request) HTTPHeaders(headers map[string]string) *Request {
  88. r.headers = headers
  89. return r
  90. }
  91. func (r *Request) TLSConfig(tlsConfig *tls.Config) *Request {
  92. r.tlsConfig = tlsConfig
  93. return r
  94. }
  95. func (r *Request) Timeout(timeout time.Duration) *Request {
  96. r.timeout = timeout
  97. return r
  98. }
  99. func (r *Request) Body(content []byte) *Request {
  100. r.body = content
  101. return r
  102. }
  103. func (r *Request) Do() (*Response, error) {
  104. var (
  105. conn net.Conn
  106. err error
  107. )
  108. addr := net.JoinHostPort(r.addr, strconv.Itoa(r.port))
  109. // for protocol http and https
  110. if r.protocol == "http" || r.protocol == "https" {
  111. return r.sendHTTPRequest(r.method, fmt.Sprintf("%s://%s%s", r.protocol, addr, r.path),
  112. r.host, r.headers, r.proxyURL, r.body, r.tlsConfig)
  113. }
  114. // for protocol tcp and udp
  115. if len(r.proxyURL) > 0 {
  116. if r.protocol != "tcp" {
  117. return nil, fmt.Errorf("only tcp protocol is allowed for proxy")
  118. }
  119. conn, err = libnet.DialTcpByProxy(r.proxyURL, addr)
  120. if err != nil {
  121. return nil, err
  122. }
  123. } else {
  124. switch r.protocol {
  125. case "tcp":
  126. conn, err = net.Dial("tcp", addr)
  127. case "udp":
  128. conn, err = net.Dial("udp", addr)
  129. default:
  130. return nil, fmt.Errorf("invalid protocol")
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. }
  136. defer conn.Close()
  137. if r.timeout > 0 {
  138. conn.SetDeadline(time.Now().Add(r.timeout))
  139. }
  140. buf, err := r.sendRequestByConn(conn, r.body)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return &Response{Content: buf}, nil
  145. }
  146. type Response struct {
  147. Code int
  148. Header http.Header
  149. Content []byte
  150. }
  151. func (r *Request) sendHTTPRequest(method, urlstr string, host string, headers map[string]string,
  152. proxy string, body []byte, tlsConfig *tls.Config,
  153. ) (*Response, error) {
  154. var inBody io.Reader
  155. if len(body) != 0 {
  156. inBody = bytes.NewReader(body)
  157. }
  158. req, err := http.NewRequest(method, urlstr, inBody)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if host != "" {
  163. req.Host = host
  164. }
  165. for k, v := range headers {
  166. req.Header.Set(k, v)
  167. }
  168. tr := &http.Transport{
  169. DialContext: (&net.Dialer{
  170. Timeout: time.Second,
  171. KeepAlive: 30 * time.Second,
  172. DualStack: true,
  173. }).DialContext,
  174. MaxIdleConns: 100,
  175. IdleConnTimeout: 90 * time.Second,
  176. TLSHandshakeTimeout: 10 * time.Second,
  177. ExpectContinueTimeout: 1 * time.Second,
  178. TLSClientConfig: tlsConfig,
  179. }
  180. if len(proxy) != 0 {
  181. tr.Proxy = func(req *http.Request) (*url.URL, error) {
  182. return url.Parse(proxy)
  183. }
  184. }
  185. client := http.Client{Transport: tr}
  186. resp, err := client.Do(req)
  187. if err != nil {
  188. return nil, err
  189. }
  190. ret := &Response{Code: resp.StatusCode, Header: resp.Header}
  191. buf, err := ioutil.ReadAll(resp.Body)
  192. if err != nil {
  193. return nil, err
  194. }
  195. ret.Content = buf
  196. return ret, nil
  197. }
  198. func (r *Request) sendRequestByConn(c net.Conn, content []byte) ([]byte, error) {
  199. _, err := rpc.WriteBytes(c, content)
  200. if err != nil {
  201. return nil, fmt.Errorf("write error: %v", err)
  202. }
  203. var reader io.Reader = c
  204. if r.protocol == "udp" {
  205. reader = bufio.NewReader(c)
  206. }
  207. buf, err := rpc.ReadBytes(reader)
  208. if err != nil {
  209. return nil, fmt.Errorf("read error: %v", err)
  210. }
  211. return buf, nil
  212. }