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