-
Notifications
You must be signed in to change notification settings - Fork 8
/
basic.go
51 lines (45 loc) · 1.31 KB
/
basic.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
44
45
46
47
48
49
50
51
package proxyplease
import (
"bufio"
"encoding/base64"
"errors"
"fmt"
"net"
"net/http"
"net/url"
)
func dialBasic(p Proxy, addr string, baseDial func() (net.Conn, error)) (net.Conn, error) {
debugf("basic> Attempting to authenticate")
conn, err := baseDial()
if err != nil {
debugf("basic> Could not call dial context with proxy: %s", err)
return conn, err
}
u := fmt.Sprintf("%s:%s", p.Username, p.Password)
h := p.Headers.Clone()
h.Set("Proxy-Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(u))))
h.Set("Proxy-Connection", "Keep-Alive")
connect := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: addr},
Host: addr,
Header: h,
}
if err := connect.Write(conn); err != nil {
debugf("basic> Could not write authorization message to proxy: %s", err)
return conn, err
}
br := bufio.NewReader(conn)
resp, err := http.ReadResponse(br, connect)
if err != nil {
debugf("basic> Could not read response from proxy: %s", err)
return conn, err
}
if resp.StatusCode == http.StatusOK {
// Succussfully authorized with Basic
debugf("basic> Successfully injected Basic to connection")
return conn, nil
}
debugf("basic> Expected %d as return status, got: %d", http.StatusOK, resp.StatusCode)
return conn, errors.New(http.StatusText(resp.StatusCode))
}