| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/elazarl/goproxy/ext/auth" |
| 14 |
|
| 15 |
"github.com/elazarl/goproxy" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
ProxyAuthHeader = "Proxy-Authorization" |
| 20 |
) |
| 21 |
|
| 22 |
func SetBasicAuth(username, password string, req *http.Request) { |
| 23 |
req.Header.Set(ProxyAuthHeader, fmt.Sprintf("Basic %s", basicAuth(username, password))) |
| 24 |
} |
| 25 |
|
| 26 |
func basicAuth(username, password string) string { |
| 27 |
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) |
| 28 |
} |
| 29 |
|
| 30 |
func GetBasicAuth(req *http.Request) (username, password string, ok bool) { |
| 31 |
auth := req.Header.Get(ProxyAuthHeader) |
| 32 |
if auth == "" { |
| 33 |
return |
| 34 |
} |
| 35 |
|
| 36 |
const prefix = "Basic " |
| 37 |
if !strings.HasPrefix(auth, prefix) { |
| 38 |
return |
| 39 |
} |
| 40 |
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) |
| 41 |
if err != nil { |
| 42 |
return |
| 43 |
} |
| 44 |
cs := string(c) |
| 45 |
s := strings.IndexByte(cs, ':') |
| 46 |
if s < 0 { |
| 47 |
return |
| 48 |
} |
| 49 |
return cs[:s], cs[s+1:], true |
| 50 |
} |
| 51 |
|
| 52 |
func main() { |
| 53 |
username, password := "foo", "bar" |
| 54 |
|
| 55 |
// start end proxy server |
| 56 |
endProxy := goproxy.NewProxyHttpServer() |
| 57 |
endProxy.Verbose = true |
| 58 |
auth.ProxyBasic(endProxy, "my_realm", func(user, pwd string) bool { |
| 59 |
return user == username && password == pwd |
| 60 |
}) |
| 61 |
log.Println("serving end proxy server at localhost:8082") |
| 62 |
go http.ListenAndServe("localhost:8082", endProxy) |
| 63 |
|
| 64 |
// start middle proxy server |
| 65 |
middleProxy := goproxy.NewProxyHttpServer() |
| 66 |
middleProxy.Verbose = true |
| 67 |
middleProxy.Tr.Proxy = func(req *http.Request) (*url.URL, error) { |
| 68 |
return url.Parse("http://localhost:8082") |
| 69 |
} |
| 70 |
connectReqHandler := func(req *http.Request) { |
| 71 |
SetBasicAuth(username, password, req) |
| 72 |
} |
| 73 |
middleProxy.ConnectDial = middleProxy.NewConnectDialToProxyWithHandler("http://localhost:8082", connectReqHandler) |
| 74 |
middleProxy.OnRequest().Do(goproxy.FuncReqHandler(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { |
| 75 |
SetBasicAuth(username, password, req) |
| 76 |
return req, nil |
| 77 |
})) |
| 78 |
log.Println("serving middle proxy server at localhost:8081") |
| 79 |
go http.ListenAndServe("localhost:8081", middleProxy) |
| 80 |
|
| 81 |
time.Sleep(1 * time.Second) |
| 82 |
|
| 83 |
// fire a http request: client --> middle proxy --> end proxy --> internet |
| 84 |
proxyUrl := "http://localhost:8081" |
| 85 |
request, err := http.NewRequest("GET", "https://ip.cn", nil) |
| 86 |
if err != nil { |
| 87 |
log.Fatalf("new request failed:%v", err) |
| 88 |
} |
| 89 |
tr := &http.Transport{Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse(proxyUrl) }} |
| 90 |
client := &http.Client{Transport: tr} |
| 91 |
rsp, err := client.Do(request) |
| 92 |
if err != nil { |
| 93 |
log.Fatalf("get rsp failed:%v", err) |
| 94 |
|
| 95 |
} |
| 96 |
defer rsp.Body.Close() |
| 97 |
data, _ := ioutil.ReadAll(rsp.Body) |
| 98 |
|
| 99 |
if rsp.StatusCode != http.StatusOK { |
| 100 |
log.Fatalf("status %d, data %s", rsp.StatusCode, data) |
| 101 |
} |
| 102 |
|
| 103 |
log.Printf("rsp:%s", data) |
| 104 |
} |