-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (47 loc) · 1.13 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
)
var (
proxy *httputil.ReverseProxy
version string
)
func main() {
forwardTo := os.Getenv("FORWARD_TO")
if forwardTo == "" {
fmt.Fprintln(os.Stderr, "You must set FORWARD_TO environment variable")
os.Exit(1)
}
fmt.Printf("proxy-skill %s\n", version)
fmt.Printf("Forwarding to: %s\n", forwardTo)
http.HandleFunc("/", handle(forwardTo))
log.Fatal(http.ListenAndServe(":3000", nil))
}
func handle(forwardTo string) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
url, _ := url.Parse(forwardTo)
proxy = httputil.NewSingleHostReverseProxy(url)
proxy.Director = func(req *http.Request) {
req.Header.Set("X-Forwarded-Host", req.Header.Get("Host"))
req.URL.Host = url.Host
req.URL.Scheme = url.Scheme
req.Host = url.Host
req.URL.Path = url.Path
}
proxy.Transport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 3 * time.Second,
}).Dial,
TLSHandshakeTimeout: 3 * time.Second,
}
proxy.ServeHTTP(res, req)
}
}