optional proxy

master
bel 2022-08-14 22:47:40 -06:00
parent 6698e8100a
commit 36c5577c83
2 changed files with 21 additions and 1 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
exec-random-redirect
random-redirect
**/*.sw*

19
main.go
View File

@ -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)
}