diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..941f365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +exec-random-redirect +random-redirect +**/*.sw* diff --git a/main.go b/main.go index f2fffdf..ba413c2 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bytes" "flag" "fmt" + "io" "io/ioutil" "log" "math/rand" @@ -15,8 +16,10 @@ import ( func main() { var port int var conf string + var proxy bool flag.IntVar(&port, "p", 8080, "port to listen on") flag.StringVar(&conf, "c", "/dev/null", "line delimited file to read urls from") + flag.BoolVar(&proxy, "proxy", false, "proxy content rather than redirect, so refresh works") flag.Parse() b, err := ioutil.ReadFile(conf) if err != nil { @@ -31,7 +34,21 @@ func main() { for len(link) == 0 { link = string(links[rand.Intn(len(links))]) } - http.Redirect(w, r, link, http.StatusTemporaryRedirect) + if proxy { + resp, err := http.Get(link) + if err != nil { + http.Error(w, err.Error(), http.StatusBadGateway) + } + for k, v := range resp.Header { + for _, v2 := range v { + w.Header().Add(k, v2) + } + } + w.WriteHeader(resp.StatusCode) + io.Copy(w, resp.Body) + } else { + http.Redirect(w, r, link, http.StatusTemporaryRedirect) + } })); err != nil { panic(err) }