-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (103 loc) · 2.66 KB
/
Copy pathmain.go
File metadata and controls
120 lines (103 loc) · 2.66 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var debug = false
func main() {
debugStr, debugExists := os.LookupEnv("DEBUG")
debug = debugExists && debugStr != ""
systemPortString := os.Getenv("PORT")
systemPort, err := strconv.Atoi(systemPortString)
log.Println("Now listening on port", systemPortString)
if err != nil {
log.Fatal("invalid required env var PORT")
}
mux := http.NewServeMux()
mux.HandleFunc("/proxy/", proxyHandler)
mux.HandleFunc("/", infoHandler(systemPort))
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", systemPort), mux)
}
func infoHandler(port int) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
addressStrings := []string{}
for _, addr := range addrs {
listenAddr := strings.Split(addr.String(), "/")[0]
addressStrings = append(addressStrings, listenAddr)
}
respBytes, err := json.Marshal(struct {
ListenAddresses []string
Port int
}{
ListenAddresses: addressStrings,
Port: port,
})
if err != nil {
panic(err)
}
resp.Write(respBytes)
}
}
func proxyHandler(resp http.ResponseWriter, req *http.Request) {
if debug {
fmt.Println("====================")
fmt.Println("ACCESS LOG to proxy")
_ = req.Write(os.Stdout)
fmt.Println("--------------------")
}
destination := strings.TrimPrefix(req.URL.Path, "/proxy/")
destination = "http://" + destination
httpClient := buildHTTPClient()
getResp, err := httpClient.Get(destination)
if err != nil {
log.Printf("request failed: %s\n", err)
http.Error(resp, fmt.Sprintf("request failed: %s", err), http.StatusInternalServerError)
return
}
defer getResp.Body.Close()
readBytes, err := ioutil.ReadAll(getResp.Body)
if err != nil {
http.Error(resp, fmt.Sprintf("read body failed: %s", err), http.StatusInternalServerError)
return
}
if debug {
fmt.Println("RESPONSE from proxied host")
fmt.Println("response status code:", getResp.StatusCode)
fmt.Println("response body:")
fmt.Println(string(readBytes))
fmt.Println("====================")
}
_, err = resp.Write(readBytes)
if err != nil {
log.Printf("failed to reply: %s\n", err)
}
}
func buildHTTPClient() *http.Client {
skipTLSVerify, err := strconv.ParseBool(os.Getenv("SKIP_CERT_VERIFY"))
if err != nil {
skipTLSVerify = false
}
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLSVerify},
DisableKeepAlives: true,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 0,
}).Dial,
},
}
}