Skip to content

Commit

Permalink
Add fasthttpproxy.FasthttpHTTPDialer
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed Apr 20, 2020
1 parent 96a8ad6 commit 54df169
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions fasthttpproxy/http.go
@@ -0,0 +1,61 @@
package fasthttpproxy

import (
"bufio"
"encoding/base64"
"fmt"
"net"
"strings"

"github.com/valyala/fasthttp"
)

// FasthttpHTTPDialer returns a fasthttp.DialFunc that dials using
// the provided HTTP proxy.
//
// Example usage:
// c := &fasthttp.Client{
// Dial: fasthttpproxy.FasthttpHTTPDialer("username:password@localhost:9050"),
// }
func FasthttpHTTPDialer(proxy string) fasthttp.DialFunc {
return func(addr string) (net.Conn, error) {
var auth string

if strings.Contains(proxy, "@") {
split := strings.Split(proxy, "@")
auth = base64.StdEncoding.EncodeToString([]byte(split[0]))
proxy = split[1]

}

conn, err := fasthttp.Dial(proxy)
if err != nil {
return nil, err
}

req := "CONNECT " + addr + " HTTP/1.1\r\n"
if auth != "" {
req += "Proxy-Authorization: Basic " + auth + "\r\n"
}
req += "\r\n"

if _, err := conn.Write([]byte(req)); err != nil {
return nil, err
}

res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)

res.SkipBody = true

if err := res.Read(bufio.NewReader(conn)); err != nil {
conn.Close()
return nil, err
}
if res.Header.StatusCode() != 200 {
conn.Close()
return nil, fmt.Errorf("could not connect to proxy")
}
return conn, nil
}
}

0 comments on commit 54df169

Please sign in to comment.