Skip to content

Commit

Permalink
refactor: id and cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
SchwarzSail committed Sep 23, 2024
1 parent b840bfa commit a2b2494
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 94 deletions.
2 changes: 1 addition & 1 deletion calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (s *Student) GetSchoolCalendar() (*SchoolCalendar, error) {
resp, err := s.GetWithSession(constants.SchoolCalendarURL)
resp, err := s.GetWithIdentifier(constants.SchoolCalendarURL)

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion course.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// 获取我的学期
func (s *Student) GetTerms() (*Term, error) {
resp, err := s.GetWithSession(constants.CourseURL)
resp, err := s.GetWithIdentifier(constants.CourseURL)

if err != nil {
return nil, err
Expand Down
54 changes: 11 additions & 43 deletions jwch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/west2-online/jwch/errno"
"github.com/west2-online/jwch/utils"

"github.com/antchfx/htmlquery"
"github.com/go-resty/resty/v2"
Expand All @@ -32,41 +31,26 @@ func (s *Student) WithUser(id, password string) *Student {
return s
}

func (s *Student) WithSession(session string) *Student {
s.LoginData.Session = session
return s
}

// SaveLoginData save session and cookie to localfile
func (s *Student) SaveLoginData(filePath string) error {
return utils.SaveData(filePath, []byte(utils.PrintStruct(s.LoginData)))
func (s *Student) SetIdentifier(identifier string) {
s.Identifier = identifier
}

func (s *Student) GetLoginDataJSON() string {
return utils.PrintStruct(s.LoginData)
func (s *Student) SetCookies(cookies []*http.Cookie) {
s.cookies = cookies
s.client.SetCookies(cookies)
}

func (s *Student) ClearLoginData() {
s.LoginData = LoginData{}
s.client.Cookies = s.LoginData.Cookies
}

func (s *Student) SetLoginData(data LoginData) {
s.LoginData = data
s.client = s.client.SetCookies(s.LoginData.Cookies)
}

func (s *Student) AppendCookies(cookies []*http.Cookie) {
s.LoginData.Cookies = append(s.LoginData.Cookies, cookies...)
s.cookies = []*http.Cookie{}
s.client.Cookies = []*http.Cookie{}
}

func (s *Student) NewRequest() *resty.Request {
return s.client.R()
}

// GetWithSession returns parse tree for the resp of the request.
func (s *Student) GetWithSession(url string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.LoginData.Session).Get(url)
func (s *Student) GetWithIdentifier(url string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.Identifier).Get(url)

if err != nil {
return nil, errno.HTTPQueryError.WithErr(err)
Expand All @@ -80,25 +64,9 @@ func (s *Student) GetWithSession(url string) (*html.Node, error) {
return htmlquery.Parse(bytes.NewReader(resp.Body()))
}

// GetWithSessionRaw returns the raw data of response
func (s *Student) GetWithSessionRaw(url string) (*resty.Response, error) {
resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.LoginData.Session).Get(url)

if err != nil {
return nil, errno.HTTPQueryError.WithErr(err)
}

// 会话过期 TODO: 判断条件有点简陋
if strings.Contains(string(resp.Body()), "重新登录") {
return nil, errno.SessionExpiredError
}

return resp, nil
}

// PostWithSession returns parse tree for the resp of the request.
func (s *Student) PostWithSession(url string, formdata map[string]string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.LoginData.Session).SetFormData(formdata).Post(url)
func (s *Student) PostWithSession(url string, formData map[string]string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.Identifier).SetFormData(formData).Post(url)

s.NewRequest().EnableTrace()

Expand Down
26 changes: 0 additions & 26 deletions jwch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,6 @@ func Test_Login(t *testing.T) {
}
}

func Test_LoginFromLocal(t *testing.T) {
var res LoginData
err := utils.JSONUnmarshalFromFile(localfile, &res)
if err != nil {
t.Error(err)
}
stu.SetLoginData(res)

err = stu.CheckSession()

if err != nil {
t.Log("session expire, relogin")
err = stu.Login()
if err != nil {
t.Error(err)
}
err = stu.CheckSession()

if err != nil {
t.Error(err)
}

stu.SaveLoginData(localfile)
}
}

func Test_GetCourse(t *testing.T) {
if !islogin {
err := login()
Expand Down
4 changes: 2 additions & 2 deletions mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// 获取成绩,由于教务处缺陷,这里会返回全部的成绩
func (s *Student) GetMarks() (resp []*Mark, err error) {
res, err := s.GetWithSession(constants.MarksQueryURL)
res, err := s.GetWithIdentifier(constants.MarksQueryURL)

if err != nil {
return nil, err
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *Student) GetMarks() (resp []*Mark, err error) {

// 获取CET成绩
func (s *Student) GetCET() error {
resp, err := s.GetWithSession(constants.CETQueryURL)
resp, err := s.GetWithIdentifier(constants.CETQueryURL)

if err != nil {
return err
Expand Down
20 changes: 8 additions & 12 deletions model.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package jwch

import (
"net/http"

"github.com/go-resty/resty/v2"
"net/http"
)

// 本地数据
type LoginData struct {
Cookies []*http.Cookie `json:"cookies"`
Session string `json:"session"`
}

// 学生对象
type Student struct {
ID string `json:"id"` // 学号
Password string `json:"password"` // 密码
LoginData LoginData `json:"login_data"` // 登录凭证
client *resty.Client // Request对象
ID string `json:"id"` // 学号
Password string `json:"password"` // 密码
cookies []*http.Cookie //cookies中将包含session_id和其他数据
//如果我们使用client进行登陆的话,此时该字段失效,因为client会在登录时自动保存登陆凭证(session)
//所以该字段用于其他服务调用时传递登陆凭证
Identifier string //位于url上id=....的一个标识符,主要用于组成url
client *resty.Client // Request对象
}

// 学生信息详情
Expand Down
2 changes: 1 addition & 1 deletion room.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *Student) GetQiShanEmptyRoom(req EmptyRoomReq) ([]string, error) {

// 获取VIEWSTATE和EVENTVALIDATION
func (s *Student) getEmptyRoomState() (map[string]string, error) {
resp, err := s.GetWithSession(constants.ClassroomQueryURL)
resp, err := s.GetWithIdentifier(constants.ClassroomQueryURL)
if err != nil {
return nil, err
}
Expand Down
15 changes: 7 additions & 8 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (s *Student) Login() error {
return errno.LoginCheckFailedError
}

// 获取session的id和num
id := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error())[1]
num := regexp.MustCompile(`num=(.*?)&`).FindStringSubmatch(err.Error())[1]

Expand All @@ -98,7 +97,7 @@ func (s *Student) Login() error {
return errno.SSOLoginFailedError
}

// 获取session
// 获取cookies
resp, err = s.NewRequest().SetHeaders(map[string]string{
"Referer": "https://jwcjwxt1.fzu.edu.cn/",
"Origin": "https://jwcjwxt2.fzu.edu.cn/",
Expand All @@ -111,20 +110,20 @@ func (s *Student) Login() error {
}).Get("https://jwcjwxt2.fzu.edu.cn:81/loginchk_xs.aspx")

// 保存这部分Cookie,这部分Cookie是用来后续鉴权的[ASP.NET_SessionId]
s.AppendCookies(resp.RawResponse.Cookies())
s.SetCookies(resp.RawResponse.Cookies())

// 这里是err == nil 因为禁止了重定向,正常登录是会出现异常的
if err == nil {
return errno.GetSessionFailedError
}

session := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error())
data := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error())

if len(session) < 1 {
if len(data) < 1 {
return errno.GetSessionFailedError
}

s.WithSession(session[1])
s.SetIdentifier(data[1])

return nil
}
Expand All @@ -135,7 +134,7 @@ func (s *Student) CheckSession() error {
// 旧版处理过程: 查询Body中是否含有[当前用户]这四个字

// 检查过期
resp, err := s.GetWithSession(constants.UserInfoURL)
resp, err := s.GetWithIdentifier(constants.UserInfoURL)
if err != nil {
return err
}
Expand All @@ -156,7 +155,7 @@ func (s *Student) CheckSession() error {

// 获取学生个人信息
func (s *Student) GetInfo() (resp *StudentDetail, err error) {
res, err := s.GetWithSession(constants.UserInfoURL)
res, err := s.GetWithIdentifier(constants.UserInfoURL)

if err != nil {
return nil, err
Expand Down

0 comments on commit a2b2494

Please sign in to comment.