-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
52 lines (41 loc) · 882 Bytes
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func runProxy(server string, localPort int) {
addr := fmt.Sprintf(":%v", localPort)
log.Println("Proxying on", addr, "to", server)
client := &http.Client{}
log.Fatal(http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
url := "http://" + server + "/" + r.RequestURI
req, err := http.NewRequest(r.Method, url, r.Body)
if err != nil {
log.Println(err)
w.WriteHeader(502)
return
}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
w.WriteHeader(502)
return
}
for key, val := range resp.Header {
w.Header().Del(key)
for _, v := range val {
w.Header().Add(key, v)
}
}
_, err = io.Copy(w, resp.Body)
if err != nil {
log.Println(err)
}
err = resp.Body.Close()
if err != nil {
log.Println(err)
}
})))
}