Refactor duplicate code
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
@@ -17,33 +15,13 @@ func Api(c *fiber.Ctx) error {
|
|||||||
// Get the url from the URL
|
// Get the url from the URL
|
||||||
urlQuery := c.Params("*")
|
urlQuery := c.Params("*")
|
||||||
|
|
||||||
u, err := url.Parse(urlQuery)
|
body, req, resp, err := fetchSite(urlQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("ERROR:", err)
|
||||||
|
c.SendStatus(500)
|
||||||
return c.SendString(err.Error())
|
return c.SendString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(u.String())
|
|
||||||
|
|
||||||
// Fetch the site
|
|
||||||
client := &http.Client{}
|
|
||||||
req, _ := http.NewRequest("GET", u.String(), nil)
|
|
||||||
req.Header.Set("User-Agent", UserAgent)
|
|
||||||
req.Header.Set("X-Forwarded-For", ForwardedFor)
|
|
||||||
req.Header.Set("Referer", u.String())
|
|
||||||
req.Header.Set("Host", u.Host)
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return c.SendString(err.Error())
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
bodyB, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("ERROR", err)
|
|
||||||
return c.SendString(err.Error())
|
|
||||||
}
|
|
||||||
body := rewriteHtml(bodyB, u)
|
|
||||||
response := Response{
|
response := Response{
|
||||||
Version: version,
|
Version: version,
|
||||||
Body: body,
|
Body: body,
|
||||||
|
|||||||
@@ -20,13 +20,23 @@ func ProxySite(c *fiber.Ctx) error {
|
|||||||
// Get the url from the URL
|
// Get the url from the URL
|
||||||
urlQuery := c.Params("*")
|
urlQuery := c.Params("*")
|
||||||
|
|
||||||
u, err := url.Parse(urlQuery)
|
body, _, resp, err := fetchSite(urlQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR", err)
|
log.Println("ERROR:", err)
|
||||||
c.SendStatus(500)
|
c.SendStatus(500)
|
||||||
return c.SendString(err.Error())
|
return c.SendString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.Set("Content-Type", resp.Header.Get("Content-Type"))
|
||||||
|
return c.SendString(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchSite(urlQuery string) (string, *http.Request, *http.Response, error) {
|
||||||
|
u, err := url.Parse(urlQuery)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
log.Println(u.String())
|
log.Println(u.String())
|
||||||
|
|
||||||
// Fetch the site
|
// Fetch the site
|
||||||
@@ -39,20 +49,17 @@ func ProxySite(c *fiber.Ctx) error {
|
|||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.SendString(err.Error())
|
return "", nil, nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
bodyB, err := io.ReadAll(resp.Body)
|
bodyB, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR", err)
|
return "", nil, nil, err
|
||||||
c.SendStatus(500)
|
|
||||||
return c.SendString(err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
body := rewriteHtml(bodyB, u)
|
body := rewriteHtml(bodyB, u)
|
||||||
c.Set("Content-Type", resp.Header.Get("Content-Type"))
|
return body, req, resp, nil
|
||||||
return c.SendString(body)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func rewriteHtml(bodyB []byte, u *url.URL) string {
|
func rewriteHtml(bodyB []byte, u *url.URL) string {
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
@@ -13,32 +10,11 @@ func Raw(c *fiber.Ctx) error {
|
|||||||
// Get the url from the URL
|
// Get the url from the URL
|
||||||
urlQuery := c.Params("*")
|
urlQuery := c.Params("*")
|
||||||
|
|
||||||
u, err := url.Parse(urlQuery)
|
body, _, _, err := fetchSite(urlQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("ERROR:", err)
|
||||||
|
c.SendStatus(500)
|
||||||
return c.SendString(err.Error())
|
return c.SendString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(u.String())
|
|
||||||
|
|
||||||
// Fetch the site
|
|
||||||
client := &http.Client{}
|
|
||||||
req, _ := http.NewRequest("GET", u.String(), nil)
|
|
||||||
req.Header.Set("User-Agent", UserAgent)
|
|
||||||
req.Header.Set("X-Forwarded-For", ForwardedFor)
|
|
||||||
req.Header.Set("Referer", u.String())
|
|
||||||
req.Header.Set("Host", u.Host)
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return c.SendString(err.Error())
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
bodyB, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("ERROR", err)
|
|
||||||
return c.SendString(err.Error())
|
|
||||||
}
|
|
||||||
body := rewriteHtml(bodyB, u)
|
|
||||||
return c.SendString(body)
|
return c.SendString(body)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user