/
main.go
40 lines (34 loc) · 949 Bytes
/
main.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
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
var (
base = flag.String("base", "", "Forward to, e.q.: http://myotherdomain.com/")
host = flag.String("host", "localhost:8080", "The host for the proxy")
)
func main() {
flag.Parse()
if base == nil || *base == "" {
fmt.Printf("you must specify the 'base' flag, re-run the program with: -base='http://mytargethost.com'\n")
return
}
if host == nil || *host == "" {
fmt.Printf("you must specify the 'host' flag, re-run the program with: -host=':80'\n")
return
}
baseUrl, err := url.Parse(*base)
if err != nil {
fmt.Printf("invalid target value %v, %v", *baseUrl, err)
return
}
fmt.Printf("hosting proxy at %v\n", *host)
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
http.DefaultTransport = &http.Transport{MaxIdleConnsPerHost: 5000}
if err := http.ListenAndServe(*host, proxy); err != nil {
fmt.Printf("fatal: %v\n", err)
}
}