feat: add proxy layer option for requests

Fixes #56 #21
This commit is contained in:
rramiachraf
2024-05-03 12:45:58 +01:00
parent c940b4a2cd
commit 2c0f43b8f7
15 changed files with 78 additions and 450 deletions

View File

@ -26,7 +26,7 @@ func album(l *utils.Logger) http.HandlerFunc {
url := fmt.Sprintf("https://genius.com/albums/%s/%s", artist, albumName)
resp, err := sendRequest(url)
resp, err := utils.SendRequest(url)
if err != nil {
l.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
@ -53,7 +53,7 @@ func album(l *utils.Logger) http.HandlerFunc {
cf := doc.Find(".cloudflare_content").Length()
if cf > 0 {
l.Error("cloudflare got in the way")
views.ErrorPage(500, "i'll fix this later #21").Render(context.Background(), w)
views.ErrorPage(500, "cloudflare is detected").Render(context.Background(), w)
return
}

View File

@ -31,7 +31,7 @@ func annotations(l *utils.Logger) http.HandlerFunc {
}
url := fmt.Sprintf("https://genius.com/api/referents/%s?text_format=html", id)
resp, err := sendRequest(url)
resp, err := utils.SendRequest(url)
if err != nil {
l.Error(err.Error())

View File

@ -2,7 +2,12 @@ package handlers
import (
"context"
"io"
"mime"
"net/http"
"os"
"path"
"strings"
"github.com/a-h/templ"
gorillaHandlers "github.com/gorilla/handlers"
@ -14,7 +19,7 @@ import (
func New(logger *utils.Logger) *mux.Router {
r := mux.NewRouter()
r.Use(mustHeaders)
r.Use(utils.MustHeaders)
r.Use(gorillaHandlers.CompressHandler)
r.Handle("/", templ.Handler(views.HomePage()))
@ -26,7 +31,25 @@ func New(logger *utils.Logger) *mux.Router {
r.HandleFunc("/search", search(logger)).Methods("GET")
r.HandleFunc("/{annotation-id}/{artist-song}/{verse}/annotations", annotations(logger)).Methods("GET")
r.HandleFunc("/instances.json", instances(logger)).Methods("GET")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
r.PathPrefix("/static/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
url := strings.Replace(r.URL.Path, "/static", "static", 1)
f, err := os.Open(url)
if err != nil {
w.WriteHeader(http.StatusNotFound)
views.ErrorPage(http.StatusNotFound, "page not found")
return
}
defer f.Close()
mimeType := mime.TypeByExtension(path.Ext(r.URL.Path))
w.Header().Set("content-type", mimeType)
if _, err := io.Copy(w, f); err != nil {
logger.Error(err.Error())
}
})
r.PathPrefix("/{annotation-id}/{artist-song}-lyrics").HandlerFunc(lyrics(logger)).Methods("GET")
r.PathPrefix("/{annotation-id}/{artist-song}").HandlerFunc(lyrics(logger)).Methods("GET")
r.PathPrefix("/{annotation-id}").HandlerFunc(lyrics(logger)).Methods("GET")

View File

@ -30,7 +30,7 @@ func instances(l *utils.Logger) http.HandlerFunc {
}
}
res, err := sendRequest("https://raw.githubusercontent.com/rramiachraf/dumb/main/instances.json")
res, err := utils.SendRequest("https://raw.githubusercontent.com/rramiachraf/dumb/main/instances.json")
if err != nil {
sendError(err, http.StatusInternalServerError, "something went wrong", l, w)
return

View File

@ -28,7 +28,7 @@ func lyrics(l *utils.Logger) http.HandlerFunc {
}
url := fmt.Sprintf("https://genius.com/%s", id)
resp, err := sendRequest(url)
resp, err := utils.SendRequest(url)
if err != nil {
l.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
@ -55,7 +55,7 @@ func lyrics(l *utils.Logger) http.HandlerFunc {
cf := doc.Find(".cloudflare_content").Length()
if cf > 0 {
l.Error("cloudflare got in the way")
views.ErrorPage(500, "TODO: fix Cloudflare #21").Render(context.Background(), w)
views.ErrorPage(500, "cloudflare is detected").Render(context.Background(), w)
return
}

View File

@ -39,7 +39,7 @@ func imageProxy(l *utils.Logger) http.HandlerFunc {
// first segment of URL resize the image to reduce bandwith usage.
url := fmt.Sprintf("https://t2.genius.com/unsafe/300x300/https://images.genius.com/%s.%s", f, ext)
res, err := sendRequest(url)
res, err := utils.SendRequest(url)
if err != nil {
l.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)

View File

@ -17,7 +17,7 @@ func search(l *utils.Logger) http.HandlerFunc {
query := r.URL.Query().Get("q")
url := fmt.Sprintf(`https://genius.com/api/search/multi?q=%s`, url.QueryEscape(query))
res, err := sendRequest(url)
res, err := utils.SendRequest(url)
if err != nil {
l.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)

View File

@ -1,42 +0,0 @@
package handlers
import (
"net/http"
"net/url"
"time"
"github.com/Danny-Dasilva/CycleTLS/cycletls"
fhttp "github.com/Danny-Dasilva/fhttp"
)
func mustHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
csp := "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self'; object-src 'none'"
w.Header().Add("content-security-policy", csp)
w.Header().Add("referrer-policy", "no-referrer")
w.Header().Add("x-content-type-options", "nosniff")
next.ServeHTTP(w, r)
})
}
const UA = "Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0"
const JA3 = "771,4865-4867-4866-49195-49199-52393-52392-49196-49200-49162-49161-49171-49172-156-157-47-53,0-23-65281-10-11-16-5-34-51-43-13-45-28-65037-41,29-23-24-25-256-257,0"
func sendRequest(u string) (*fhttp.Response, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
}
client := &fhttp.Client{
Transport: cycletls.NewTransport(JA3, UA),
Timeout: 20 * time.Second,
}
req := &fhttp.Request{
Method: http.MethodGet,
URL: url,
}
return client.Do(req)
}

View File

@ -1,32 +0,0 @@
package handlers
/*
import (
"encoding/json"
"testing"
)
func TestSendRequest(t *testing.T) {
res, err := sendRequest("https://tls.peet.ws/api/clean")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
type fingerprint struct {
JA3 string `json:"ja3"`
}
decoder := json.NewDecoder(res.Body)
var fg fingerprint
if err := decoder.Decode(&fg); err != nil {
t.Fatal(err)
}
if fg.JA3 != JA3 {
t.Fatalf("expected %q, got %q\n", JA3, fg.JA3)
}
}
*/