-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate.go
More file actions
73 lines (56 loc) · 1.41 KB
/
private.go
File metadata and controls
73 lines (56 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/gocolly/colly/v2"
)
// Returns true if home page redirects to login.
func IsPrivate() (bool, error) {
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
r, err := client.Get(CAKO_IO_URL)
if err != nil {
return false, err
}
loc, err := r.Location()
if err != nil {
return false, err
}
if r.StatusCode == http.StatusFound && strings.Contains(loc.Path, "private") {
return true, nil
}
return false, nil
}
// Lol
func Login(collector *colly.Collector, password string) error {
loginUrl := CAKO_IO_URL + "private/"
ghostCookieName := "ghost-private"
client := http.Client{
/* Keep 302 response so we can extract express session cookie */
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
params := url.Values{}
params.Add("password", password)
req, err := http.NewRequest("POST", loginUrl, strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r, err := client.Do(req)
if err != nil {
return err
}
setCookie := r.Header.Get("Set-Cookie")
if !strings.Contains(setCookie, ghostCookieName) {
return fmt.Errorf("Login failed.")
}
collector.SetCookies(CAKO_IO_URL, r.Cookies())
return nil
}