-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_proxy.go
37 lines (31 loc) · 861 Bytes
/
http_proxy.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/elazarl/goproxy"
"github.com/elazarl/goproxy/ext/auth"
)
func setupHTTPProxy() (*Tunnel, error) {
token := GenerateToken()
port, err := GetFreePort()
if err != nil {
return nil, fmt.Errorf("get free port to listen: %w", err)
}
addr := fmt.Sprintf("localhost:%v", port)
// Start HTTP proxy
proxy := goproxy.NewProxyHttpServer()
auth.ProxyBasic(proxy, "mitten", func(user, password string) bool {
return user == "mitten" && password == token
})
go func() {
if err := http.ListenAndServe(addr, proxy); err != nil {
log.Fatalf("listen and serve: %v", err)
}
}()
tunnel := &Tunnel{
Command: fmt.Sprintf(`export http_proxy="http://mitten:%s@%s";export https_proxy=$http_proxy;`, token, addr),
ForwardSpec: fmt.Sprintf("-R %v:%s", port, addr),
}
return tunnel, nil
}