-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (54 loc) · 1.89 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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"github.com/akamensky/argparse"
)
func handlePreflight(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func handlePassThrough(w http.ResponseWriter, r *http.Request) {
target := "http://" + r.URL.Path[1:]
url, err := url.Parse(target)
if err != nil {
log.Println("Could not handle request", *r)
fmt.Fprintf(w, "Could not parse URL: %s", err)
return
}
proxy := httputil.NewSingleHostReverseProxy(url)
r.URL = url
r.URL.Host = url.Host
r.URL.Scheme = url.Scheme
r.Host = url.Host
proxy.ServeHTTP(w, r)
}
func handler(w http.ResponseWriter, r *http.Request) {
log.Println("Handling request: ", r.Method, r.URL)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
if r.Method == "OPTIONS" {
handlePreflight(w, r)
} else {
handlePassThrough(w, r)
}
}
func main() {
parser := argparse.NewParser("corsproxy", "Add CORS support via a proxy to servers that don't support it.")
port := parser.String("p", "port", &argparse.Options{Required: false, Help: "The port to listen on", Default: "8080"})
inface := parser.String("i", "interface", &argparse.Options{Required: false, Help: "The interface to listen on. localhost or 127.0.0.1 or 0.0.0.0 is common. Non routable LAN interface (192.168.X.Y or 10.0.X.Y) is useful. Wan/Public is not recommended.", Default: "127.0.0.1"})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
http.HandleFunc("/", handler)
localServerURL := *inface + ":" + *port
log.Println("Listening on: ", localServerURL)
log.Fatal(http.ListenAndServe(localServerURL, nil))
}