-
Notifications
You must be signed in to change notification settings - Fork 3
/
jwch.go
130 lines (105 loc) · 3.8 KB
/
jwch.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
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package jwch
import (
"bytes"
"crypto/tls"
"encoding/json"
"net/http"
"strings"
"github.com/west2-online/jwch/constants"
"github.com/west2-online/jwch/errno"
"github.com/antchfx/htmlquery"
"github.com/go-resty/resty/v2"
"golang.org/x/net/html"
)
func NewStudent() *Student {
// Disable HTTP/2.0
// Disable Redirect
client := resty.New().SetTransport(&http.Transport{
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}).SetRedirectPolicy(resty.NoRedirectPolicy())
return &Student{
client: client,
}
}
func (s *Student) WithLoginData(identifier string, cookies []*http.Cookie) *Student {
s.Identifier = identifier
s.cookies = cookies
s.client.SetCookies(cookies)
return s
}
// WithUser 携带账号密码,这部分考虑整合到Login中,因为实际上我们不需要这个东西
func (s *Student) WithUser(id, password string) *Student {
s.ID = id
s.Password = password
return s
}
func (s *Student) SetIdentifier(identifier string) {
s.Identifier = identifier
}
func (s *Student) SetCookies(cookies []*http.Cookie) {
s.cookies = cookies
s.client.SetCookies(cookies)
}
func (s *Student) ClearLoginData() {
s.cookies = []*http.Cookie{}
s.client.Cookies = []*http.Cookie{}
}
func (s *Student) NewRequest() *resty.Request {
return s.client.R()
}
func (s *Student) GetWithIdentifier(url string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", constants.JwchReferer).SetQueryParam("id", s.Identifier).Get(url)
// 会话过期:会直接重定向,但我们禁用了重定向,所以会有error
if err != nil {
return nil, errno.CookieError
}
// 还有一种情况是 id 或 cookie 缺失或者解析错误 TODO: 判断条件有点简陋
if strings.Contains(string(resp.Body()), "重新登录") {
return nil, errno.CookieError
}
return htmlquery.Parse(bytes.NewReader(resp.Body()))
}
// PostWithIdentifier returns parse tree for the resp of the request.
func (s *Student) PostWithIdentifier(url string, formData map[string]string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", constants.JwchReferer).SetQueryParam("id", s.Identifier).SetFormData(formData).Post(url)
s.NewRequest().EnableTrace()
// 会话过期:会直接重定向,但我们禁用了重定向,所以会有error
if err != nil {
return nil, errno.CookieError.WithErr(err)
}
// id 或 cookie 缺失或者解析错误 TODO: 判断条件有点简陋
if strings.Contains(string(resp.Body()), "处理URL失败") {
return nil, errno.CookieError
}
return htmlquery.Parse(strings.NewReader(strings.TrimSpace(string(resp.Body()))))
}
// GetValidateCode 获取验证码
func GetValidateCode(image string) (string, error) {
// 请求西二服务器,自动识别验证码
code := verifyCodeResponse{}
s := NewStudent()
resp, err := s.NewRequest().SetFormData(map[string]string{
"validateCode": image,
}).Post("https://statistics.fzuhelper.w2fzu.com/api/login/validateCode?validateCode")
if err != nil {
return "", errno.HTTPQueryError.WithMessage("automatic code identification failed")
}
err = json.Unmarshal(resp.Body(), &code)
if err != nil {
return "", errno.HTTPQueryError.WithErr(err)
}
return code.Message, nil
}