support lieral

master^2
bel 2022-08-31 20:37:28 -06:00
parent e200f2a6eb
commit 9d3da9265c
2 changed files with 25 additions and 14 deletions

37
main.go
View File

@ -9,6 +9,7 @@ import (
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"strings"
"time" "time"
"golang.org/x/time/rate" "golang.org/x/time/rate"
@ -36,22 +37,30 @@ func main() {
for len(link) == 0 { for len(link) == 0 {
link = string(links[rand.Intn(len(links))]) link = string(links[rand.Intn(len(links))])
} }
if proxy { if strings.HasPrefix(link, "http") {
resp, err := http.Get(link) serveHTTP(w, r, proxy, link)
if err != nil { } else if strings.HasPrefix(link, "literal://") {
http.Error(w, err.Error(), http.StatusBadGateway) w.Write([]byte(strings.TrimPrefix(link, "literal://") + "\n"))
}
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 { })); err != nil {
panic(err) panic(err)
} }
} }
func serveHTTP(w http.ResponseWriter, r *http.Request, proxy bool, link string) {
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)
}
}

2
testdata/literal vendored Normal file
View File

@ -0,0 +1,2 @@
literal://hello world
literal://foo bar