diff --git a/main.go b/main.go index 9d5bea7..5084339 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "flag" "fmt" + "github.com/jordanorelli/moon/lib" "io" "net/http" "net/http/httputil" @@ -13,6 +15,7 @@ import ( var client = new(http.Client) func httpHandler(w http.ResponseWriter, r *http.Request) { + fmt.Println("from:", r.RemoteAddr) b, err := httputil.DumpRequest(r, true) if err != nil { fmt.Printf("error dumping request: %s\n", err) @@ -33,6 +36,9 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { for k, v := range res.Header { w.Header()[k] = v } + if _, ok := w.Header()["Proxy-Connection"]; ok { + delete(w.Header(), "Proxy-Connection") + } w.WriteHeader(res.StatusCode) if _, err := io.Copy(w, res.Body); err != nil { @@ -40,7 +46,30 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { } } +func bail(status int, t string, args ...interface{}) { + if status == 0 { + fmt.Fprintf(os.Stdout, t+"\n", args...) + } else { + fmt.Fprintf(os.Stderr, t+"\n", args...) + } + os.Exit(status) +} + func main() { + var configPath string + flag.StringVar(&configPath, "config", "./prox_config.moon", "path to configuration file") + flag.Parse() + + conf, err := moon.ReadFile(configPath) + if err != nil { + bail(1, "unable to read config: %s", err) + } + + var addr string + if err := conf.Get("proxy_addr", &addr); err != nil { + bail(1, "error reading proxy_addr from config: %s", err) + } + http.HandleFunc("/", httpHandler) - http.ListenAndServe(":8080", nil) + http.ListenAndServe(addr, nil) } diff --git a/prox_config.moon b/prox_config.moon new file mode 100644 index 0000000..5d8bbad --- /dev/null +++ b/prox_config.moon @@ -0,0 +1,3 @@ +# address for web browsers to use in their proxy settings. Browsers send their +# normal traffic to this port to be proxied. +proxy_addr: ":8080"