85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
func GetLinkFromMgnet(URL string) (string, error) {
|
|
resp, err := Request().Get(URL)
|
|
if err != nil {
|
|
return "", fmt.Errorf("Error while requesting URL: %s: %s", err, URL)
|
|
}
|
|
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
|
if err != nil {
|
|
return "", fmt.Errorf("Error while parsing HTML: %s: %s", err, URL)
|
|
}
|
|
ad_form_data := doc.Find("[name='ad_form_data']").AttrOr("value", "")
|
|
if ad_form_data == "" {
|
|
return "", fmt.Errorf("Failed to get ad_form_data: %s", URL)
|
|
}
|
|
token_fields := doc.Find("[name='_Token[fields]']").AttrOr("value", "")
|
|
if token_fields == "" {
|
|
return "", fmt.Errorf("Failed to get _Token[fields]: %s", URL)
|
|
}
|
|
token_unlocked := doc.Find("[name='_Token[unlocked]']").AttrOr("value", "")
|
|
if token_unlocked == "" {
|
|
return "", fmt.Errorf("Failed to get _Token[unlocked]: %s", URL)
|
|
}
|
|
cookies := resp.Cookies()
|
|
csrfToken := ""
|
|
for _, cookie := range cookies {
|
|
if cookie.Name == "csrfToken" {
|
|
csrfToken = cookie.Value
|
|
break
|
|
}
|
|
}
|
|
if csrfToken == "" {
|
|
return "", fmt.Errorf("Failed to get csrfToken: %s", URL)
|
|
}
|
|
|
|
params := url.Values{}
|
|
params.Set("_method", "POST")
|
|
params.Set("_csrfToken", csrfToken)
|
|
params.Set("ad_form_data", ad_form_data)
|
|
params.Set("_Token[fields]", token_fields)
|
|
params.Set("_Token[unlocked]", token_unlocked)
|
|
cookies = append(cookies, &http.Cookie{
|
|
Name: "ab",
|
|
Value: "2",
|
|
})
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
resp, err = Request().SetHeaders(map[string]string{
|
|
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
"Referer": URL,
|
|
}).SetCookies(cookies).SetBody(params.Encode()).Post("https://mgnet.site/links/go")
|
|
if err != nil {
|
|
return "", fmt.Errorf("Error while requesting URL: %s: %s", err, "https://mgnet.site/links/go")
|
|
}
|
|
|
|
type requestResult struct {
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
res := requestResult{}
|
|
err = json.Unmarshal(resp.Body(), &res)
|
|
if err != nil {
|
|
return "", fmt.Errorf("Error while parsing JSON: %s", err)
|
|
}
|
|
if res.Status != "success" {
|
|
return "", fmt.Errorf("Failed to get link: %s: %s: %+v", res.Message, URL, res)
|
|
}
|
|
return res.URL, nil
|
|
}
|