-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.go
More file actions
261 lines (226 loc) · 6.14 KB
/
Copy pathclient.go
File metadata and controls
261 lines (226 loc) · 6.14 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package zlib
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"time"
)
const (
defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
defaultTimeout = 180 * time.Second
)
type Client struct {
httpClient *http.Client
domain string
loginDomain string
cookies map[string]string
loggedIn bool
}
func NewClient(opts ...ClientOption) *Client {
jar, _ := cookiejar.New(nil)
c := &Client{
httpClient: &http.Client{
Jar: jar,
Timeout: defaultTimeout,
},
domain: CurrentDefaultDomain(),
loginDomain: buildLoginURL(CurrentDefaultDomain()),
cookies: make(map[string]string),
}
for _, opt := range opts {
opt(c)
}
if proxyURL := strings.TrimSpace(os.Getenv(EnvProxy)); proxyURL != "" {
WithProxy(proxyURL)(c)
}
return c
}
type ClientOption func(*Client)
func WithDomain(domain string) ClientOption {
return func(c *Client) {
c.SetDomain(domain)
}
}
func WithProxy(proxyURL string) ClientOption {
return func(c *Client) {
u, err := url.Parse(proxyURL)
if err != nil {
return
}
c.httpClient.Transport = &http.Transport{Proxy: http.ProxyURL(u)}
}
}
func WithOnion(proxyURL string) ClientOption {
return func(c *Client) {
c.SetDomain(TorDomain)
WithProxy(proxyURL)(c)
}
}
func (c *Client) Login(email, password string) error {
data := url.Values{
"isModal": {"true"},
"email": {email},
"password": {password},
"site_mode": {"books"},
"action": {"login"},
"isSingleLogin": {"1"},
"redirectUrl": {""},
"gg_json_mode": {"1"},
}
req, err := http.NewRequest("POST", c.loginDomain, strings.NewReader(data.Encode()))
if err != nil {
return fmt.Errorf("%w: %v", ErrLoginFailed, err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", defaultUserAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("%w: %v", ErrLoginFailed, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("%w: %v", ErrLoginFailed, err)
}
var result struct {
Response struct {
ValidationError interface{} `json:"validationError"`
} `json:"response"`
}
if err := json.Unmarshal(body, &result); err != nil {
return fmt.Errorf("%w: invalid response: %v", ErrLoginFailed, err)
}
if result.Response.ValidationError != nil {
return fmt.Errorf("%w: %v", ErrLoginFailed, result.Response.ValidationError)
}
// Extract cookies
u, _ := url.Parse(c.loginDomain)
for _, cookie := range c.httpClient.Jar.Cookies(u) {
c.cookies[cookie.Name] = cookie.Value
}
c.loggedIn = true
return nil
}
func (c *Client) Logout() {
c.cookies = make(map[string]string)
c.loggedIn = false
jar, _ := cookiejar.New(nil)
c.httpClient.Jar = jar
}
func (c *Client) Domain() string {
return c.domain
}
func (c *Client) SetDomain(domain string) {
domain = normalizeDomain(domain)
if domain == "" {
domain = CurrentDefaultDomain()
}
c.domain = domain
c.loginDomain = buildLoginURL(domain)
if c.httpClient == nil || c.httpClient.Jar == nil {
return
}
u, err := url.Parse(domain)
if err != nil {
return
}
var httpCookies []*http.Cookie
for k, v := range c.cookies {
httpCookies = append(httpCookies, &http.Cookie{Name: k, Value: v})
}
if len(httpCookies) > 0 {
c.httpClient.Jar.SetCookies(u, httpCookies)
}
}
func (c *Client) Cookies() map[string]string {
return c.cookies
}
func (c *Client) SetCookies(cookies map[string]string) {
c.cookies = cookies
c.loggedIn = true
u, _ := url.Parse(c.domain)
var httpCookies []*http.Cookie
for k, v := range cookies {
httpCookies = append(httpCookies, &http.Cookie{Name: k, Value: v})
}
c.httpClient.Jar.SetCookies(u, httpCookies)
}
func isChallengePage(html string) bool {
return len(html) < 20000 && challengeRe.MatchString(html)
}
// isLoginPage reports whether html is Z-Library's login page, which the server
// returns (with status 200) for authenticated requests once the session has
// expired. The login form carries a stable id we can match on.
func isLoginPage(html string) bool {
return strings.Contains(html, `id="loginForm"`)
}
func (c *Client) get(rawURL string) (string, error) {
req, err := http.NewRequest("GET", rawURL, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", defaultUserAgent)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
req.Header.Set("Accept-Language", "en-US,en;q=0.9")
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("Sec-Fetch-Dest", "document")
req.Header.Set("Sec-Fetch-Mode", "navigate")
req.Header.Set("Sec-Fetch-Site", "none")
req.Header.Set("Sec-Fetch-User", "?1")
req.Header.Set("Upgrade-Insecure-Requests", "1")
for k, v := range c.cookies {
req.AddCookie(&http.Cookie{Name: k, Value: v})
}
resp, err := c.httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if isUnexpectedBookRedirect(req.URL, resp.Request.URL) {
return "", fmt.Errorf("%w: requested %s but received %s", ErrParseFailed, req.URL.Path, resp.Request.URL.Path)
}
var reader io.Reader = resp.Body
if resp.Header.Get("Content-Encoding") == "gzip" {
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", err
}
defer gz.Close()
reader = gz
}
body, err := io.ReadAll(reader)
if err != nil {
return "", err
}
html := string(body)
// Auto-solve JS challenge and retry once
if isChallengePage(html) {
token, err := solveChallenge(html)
if err != nil {
return "", fmt.Errorf("challenge solve failed: %w", err)
}
c.cookies["c_token"] = token
return c.get(rawURL)
}
// Once the session expires, the server serves the login page (HTTP 200)
// in place of the requested authenticated page.
if isLoginPage(html) {
return "", ErrSessionExpired
}
return html, nil
}
func isUnexpectedBookRedirect(requested, received *url.URL) bool {
if requested == nil || received == nil {
return false
}
if !strings.HasPrefix(requested.Path, bookPathPrefix) {
return false
}
return !strings.HasPrefix(received.Path, bookPathPrefix)
}