2024-05-03 12:45:58 +01:00
|
|
|
package utils
|
2024-03-04 14:59:47 +01:00
|
|
|
|
|
|
|
import (
|
2024-05-03 12:45:58 +01:00
|
|
|
"fmt"
|
2024-03-04 14:59:47 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-05-03 12:45:58 +01:00
|
|
|
"os"
|
2024-03-04 14:59:47 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-05-03 12:45:58 +01:00
|
|
|
func MustHeaders(next http.Handler) http.Handler {
|
2024-03-04 14:59:47 +01:00
|
|
|
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")
|
2024-05-03 12:45:58 +01:00
|
|
|
w.Header().Add("content-type", "text/html")
|
2024-03-04 14:59:47 +01:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:12:22 +01:00
|
|
|
const UA = "Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0"
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-05-03 12:45:58 +01:00
|
|
|
func SendRequest(u string) (*http.Response, error) {
|
|
|
|
proxy := os.Getenv("PROXY")
|
2024-03-04 14:59:47 +01:00
|
|
|
url, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-03 12:45:58 +01:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:12:22 +01:00
|
|
|
}
|
|
|
|
|
2024-05-03 12:45:58 +01:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Set("user-agent", UA)
|
|
|
|
|
|
|
|
req := &http.Request{
|
2024-03-04 14:59:47 +01:00
|
|
|
Method: http.MethodGet,
|
|
|
|
URL: url,
|
2024-05-03 12:45:58 +01:00
|
|
|
Header: headers,
|
2024-03-04 14:59:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.Do(req)
|
|
|
|
}
|