Forked from belek/gist:9f56ca11f444ca515e57889acbe8ad8c
Created
July 14, 2019 20:21
-
-
Save alyatwa/efb188b550508e6559675931dcdcf531 to your computer and use it in GitHub Desktop.
ahrefs agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"net/url" | |
"fmt" | |
"golang.org/x/net/html" | |
"io/ioutil" | |
) | |
func getElementByName(name string, n *html.Node) (element *html.Node, ok bool) { | |
for _, a := range n.Attr { | |
if a.Key == "name" && a.Val == name { | |
return n, true | |
} | |
} | |
for c := n.FirstChild; c != nil; c = c.NextSibling { | |
if element, ok = getElementByName(name, c); ok { | |
return | |
} | |
} | |
return | |
} | |
type myjar struct { | |
jar map[string] []*http.Cookie | |
} | |
func (p* myjar) SetCookies(u *url.URL, cookies []*http.Cookie) { | |
p.jar [u.Host] = cookies | |
} | |
func (p *myjar) Cookies(u *url.URL) []*http.Cookie { | |
return p.jar[u.Host] | |
} | |
func main() { | |
client := &http.Client{} | |
jar := &myjar{} | |
jar.jar = make(map[string] []*http.Cookie) | |
client.Jar = jar | |
csrf_token := "" | |
response, err := client.Get("https://ahrefs.com/user/login/") | |
for _, cookie := range response.Cookies() { | |
fmt.Println(cookie.Name) | |
if cookie.Name == "XSRF-TOKEN"{ | |
csrf_token = cookie.Value | |
} | |
} | |
log.Println(csrf_token) | |
root, err := html.Parse(response.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
element, ok := getElementByName("_token", root) | |
if !ok { | |
log.Println("element not found") | |
} | |
token := "" | |
for _, a := range element.Attr { | |
if a.Key == "content" { | |
token = a.Val | |
} | |
} | |
values := make(url.Values) | |
values.Set("email", "[email protected]") | |
values.Set("password", "Aau4bqRxfc4ZEvu") | |
//values.Set("_csrf_token", csrf_token) | |
values.Set("_token", token) | |
response, err = client.PostForm("https://ahrefs.com/user/login/", values) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println(response) | |
body, err := ioutil.ReadAll(response.Body) | |
log.Println(string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment