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

58
utils/request.go Normal file
View File

@ -0,0 +1,58 @@
package utils
import (
"fmt"
"net/http"
"net/url"
"os"
"time"
)
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")
w.Header().Add("content-type", "text/html")
next.ServeHTTP(w, r)
})
}
const UA = "Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0"
func SendRequest(u string) (*http.Response, error) {
proxy := os.Getenv("PROXY")
url, err := url.Parse(u)
if err != nil {
return nil, err
}
client := &http.Client{
Timeout: 20 * time.Second,
}
if proxy != "" {
proxyURL, err := url.Parse(proxy)
if err != nil {
return nil, fmt.Errorf("unable to parse proxy url: %s", err.Error())
}
client.Timeout = time.Minute * 1
client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
}
headers := http.Header{}
headers.Set("user-agent", UA)
req := &http.Request{
Method: http.MethodGet,
URL: url,
Header: headers,
}
return client.Do(req)
}

32
utils/request_test.go Normal file
View File

@ -0,0 +1,32 @@
package utils
/*
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)
}
}
*/