-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 1.54 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
flag "github.com/spf13/pflag"
"tailscale.com/tsnet"
"tailscale.com/types/logger"
)
func main() {
stateDir := flag.StringP("statedir", "d", "", "Directory for storing state")
flag.Parse()
if flag.NArg() != 2 {
log.Fatal("Usage: ./funnelproxy [-d|--statedir <dir>] <hostname> <target-url>")
}
targetURL, err := url.Parse(flag.Arg(1))
if err != nil {
log.Fatalf("Error parsing target URL: %v", err)
}
s := &tsnet.Server{
Dir: *stateDir,
Logf: logger.Discard,
Hostname: flag.Arg(0),
}
defer s.Close()
ln, err := s.ListenFunnel("tcp", ":443")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
fmt.Printf("Listening on https://%v\n", s.CertDomains()[0])
err = http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxyURL := targetURL.ResolveReference(r.URL)
proxyRequest, err := http.NewRequest(r.Method, proxyURL.String(), r.Body)
if err != nil {
http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
return
}
proxyRequest.Header = r.Header
client := &http.Client{}
resp, err := client.Do(proxyRequest)
if err != nil {
http.Error(w, "Error executing proxy request", http.StatusBadGateway)
return
}
defer resp.Body.Close()
for k, v := range resp.Header {
for _, vv := range v {
w.Header().Add(k, vv)
}
}
w.WriteHeader(resp.StatusCode)
if _, err := io.Copy(w, resp.Body); err != nil {
log.Printf("Error copying response body: %v", err)
}
}))
log.Fatal(err)
}