-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
233 lines (209 loc) 路 5.46 KB
/
login.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package client
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"syscall"
"github.com/fatih/color"
"github.com/lcian/cf-tool/cookiejar"
"github.com/lcian/cf-tool/util"
"golang.org/x/crypto/ssh/terminal"
)
// genFtaa generate a random one
func genFtaa() string {
return util.RandString(18)
}
// genBfaa generate a bfaa
func genBfaa() string {
return "f1b3f18c715565b589b7823cda7448ce"
}
// ErrorNotLogged not logged in
var ErrorNotLogged = "Not logged in"
// findHandle if logged return (handle, nil), else return ("", ErrorNotLogged)
func findHandle(body []byte) (string, error) {
reg := regexp.MustCompile(`handle = "([\s\S]+?)"`)
tmp := reg.FindSubmatch(body)
if len(tmp) < 2 {
return "", errors.New(ErrorNotLogged)
}
return string(tmp[1]), nil
}
func findCsrf(body []byte) (string, error) {
reg := regexp.MustCompile(`csrf='(.+?)'`)
tmp := reg.FindSubmatch(body)
if len(tmp) < 2 {
return "", errors.New("Cannot find csrf")
}
return string(tmp[1]), nil
}
// Prompt the user for the cf_clearance Cloudflare cookie and store it in the cookie jar
func (c *Client) GetCloudflareCookieFromUser() (err error) {
color.Cyan("Cloudflare cookie required")
color.Cyan("Inspect the cookies in your browser (e.g. in Chrome, on the Codeforces home page: Right click > Inspect > Application > Cookies > https://codeforces.com) and copy the required cookie")
fmt.Printf("cf_clearance: ")
cf_clearance := util.ScanlineTrim()
cf_clearance_cookie := http.Cookie{
Name: "cf_clearance",
Value: cf_clearance,
Path: "/",
Domain: "codeforces.com",
}
url, _ := url.Parse("codeforces.com")
c.client.Jar.SetCookies(url, append(c.client.Jar.Cookies(url), &cf_clearance_cookie))
return nil
}
// Login codeforces with handler and password
func (c *Client) Login() (err error) {
color.Cyan("Login %v...\n", c.HandleOrEmail)
password, err := c.DecryptPassword()
if err != nil {
return
}
jar, _ := cookiejar.New(nil)
c.client.Jar = jar
body, err := util.GetBody(c.client, c.host+"/enter")
if err != nil {
return
}
csrf, err := findCsrf(body)
if err != nil {
color.Red(err.Error())
/* One of the failure scenarios for findCsrf is when our request for "/enter" is redirected to a Cloudflare challenge page
* Let's get the cf_clearance cookie that Cloudflare sets when a challenge is passed and retry
*/
err = c.GetCloudflareCookieFromUser()
if err != nil {
color.Red(err.Error())
return
}
body, err = util.GetBody(c.client, c.host+"/enter")
if err != nil {
return
}
csrf, err = findCsrf(body)
if err != nil {
return
}
}
ftaa := genFtaa()
bfaa := genBfaa()
body, err = util.PostBody(c.client, c.host+"/enter", url.Values{
"csrf_token": {csrf},
"action": {"enter"},
"ftaa": {ftaa},
"bfaa": {bfaa},
"handleOrEmail": {c.HandleOrEmail},
"password": {password},
"_tta": {"176"},
"remember": {"on"},
})
if err != nil {
return
}
handle, err := findHandle(body)
if err != nil {
return
}
c.Ftaa = ftaa
c.Bfaa = bfaa
c.Handle = handle
c.Jar = jar
color.Green("Succeed!!")
color.Green("Welcome %v~", handle)
return c.save()
}
func createHash(key string) []byte {
hasher := md5.New()
hasher.Write([]byte(key))
return hasher.Sum(nil)
}
func encrypt(handle, password string) (ret string, err error) {
block, err := aes.NewCipher(createHash("glhf" + handle + "233"))
if err != nil {
return
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return
}
text := gcm.Seal(nonce, nonce, []byte(password), nil)
ret = hex.EncodeToString(text)
return
}
func decrypt(handle, password string) (ret string, err error) {
data, err := hex.DecodeString(password)
if err != nil {
err = errors.New("Cannot decode the password")
return
}
block, err := aes.NewCipher(createHash("glhf" + handle + "233"))
if err != nil {
return
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return
}
nonceSize := gcm.NonceSize()
nonce, text := data[:nonceSize], data[nonceSize:]
plain, err := gcm.Open(nil, nonce, text, nil)
if err != nil {
return
}
ret = string(plain)
return
}
// DecryptPassword get real password
func (c *Client) DecryptPassword() (string, error) {
if len(c.Password) == 0 || len(c.HandleOrEmail) == 0 {
return "", errors.New("You have to configure your handle and password by `cf config`")
}
return decrypt(c.HandleOrEmail, c.Password)
}
// ConfigLogin configure handle and password
func (c *Client) ConfigLogin() (err error) {
if c.Handle != "" {
color.Green("Current user: %v", c.Handle)
}
color.Cyan("Configure handle/email and password")
color.Cyan("Note: The password is invisible, just type it correctly.")
fmt.Printf("handle/email: ")
handleOrEmail := util.ScanlineTrim()
password := ""
if terminal.IsTerminal(int(syscall.Stdin)) {
fmt.Printf("password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
fmt.Println()
if err.Error() == "EOF" {
fmt.Println("Interrupted.")
return nil
}
return err
}
password = string(bytePassword)
fmt.Println()
} else {
color.Red("Your terminal does not support the hidden password.")
fmt.Printf("password: ")
password = util.Scanline()
}
c.HandleOrEmail = handleOrEmail
c.Password, err = encrypt(handleOrEmail, password)
if err != nil {
return
}
return c.Login()
}