-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
43 lines (34 loc) · 868 Bytes
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package fcm
import (
"bufio"
"fmt"
"net"
"strings"
"github.com/valyala/fasthttp"
)
func FasthttpHTTPDialer(proxyAddr string) fasthttp.DialFunc {
return func(addr string) (net.Conn, error) {
conn, err := fasthttp.Dial(strings.Replace(strings.Replace(proxyAddr, "https://", "", 1), "http://", "", 1))
if err != nil {
return nil, err
}
req := "CONNECT " + addr + " HTTP/1.1\r\n"
// req += "Proxy-Authorization: xxx\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
}
}