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