Skip to content

Commit

Permalink
Merge pull request #18 from Patrick-Star-CN/main
Browse files Browse the repository at this point in the history
修复了正方的bug
  • Loading branch information
Patrick-Star-CN authored Jan 22, 2024
2 parents 0af2b0d + 7bbdc4e commit b6fc622
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 20 deletions.
5 changes: 5 additions & 0 deletions app/controller/controllerHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func ErrorHandle(context *gin.Context, err error) {
exp = errors.SessionExpired
break
}
case errors.ERR_OAUTH_ERROR:
{
exp = errors.OauthError
break
}
}

println(err.Error())
Expand Down
4 changes: 4 additions & 0 deletions app/controller/zfController/controllerHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func ZFTermInfoHandle(context *gin.Context, cb func(*model.User, string, string)

if err == errors.ERR_Session_Expired {
user, err = controller.LoginHandle(context, zfService.GetUser, false)
if err != nil {
controller.ErrorHandle(context, err)
return nil, err
}
result, err = cb(user, context.PostForm("year"), context.PostForm("term"))
}

Expand Down
2 changes: 2 additions & 0 deletions app/controller/zfController/zfController.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ func GetProgInfo(context *gin.Context) {

user, err := controller.LoginHandle(context, zfService.GetUser, false)
if err != nil {
controller.ErrorHandle(context, err)
return
}
result, err := zfService.GetTrainingPrograms(user)
if err == errors.ERR_SESSION_EXPIRES {
user, err = controller.LoginHandle(context, zfService.GetUser, false)
if err != nil {
controller.ErrorHandle(context, err)
return
}
result, err = zfService.GetTrainingPrograms(user)
Expand Down
1 change: 1 addition & 0 deletions app/errors/funnelErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ var ERR_WRONG_Captcha = errors.New("ERR_WRONG_Captcha")
var ERR_INVALID_ARGS = errors.New("invalid args")
var ERR_SESSION_NOT_EXIST = errors.New("ERR_SESSION_NOT_EXIST")
var ERR_SESSION_EXPIRES = errors.New("ERR_SESSION_EXPIRES")
var ERR_OAUTH_ERROR = errors.New("ERR_OAUTH_ERROR")
1 change: 1 addition & 0 deletions app/errors/httpResponseErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ var InvalidArgs = HttpResponseError{410, "参数错误"}
var WrongPassword = HttpResponseError{412, "密码错误"}
var CaptchaFailed = HttpResponseError{413, "验证码错误"}
var SessionExpired = HttpResponseError{414, "缓存过期"}
var OauthError = HttpResponseError{415, "缓存过期"}
1 change: 1 addition & 0 deletions app/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type User struct {
Username string
Password string
Session http.Cookie
Route http.Cookie
}
2 changes: 1 addition & 1 deletion app/service/libraryService/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ func login(username string, password string) (*model.User, error) {
if len(f.Cookie) == 0 || err != nil {
return nil, errors.ERR_WRONG_PASSWORD
}
return service.SetUser(service.LibraryPrefix, username, password, f.Cookie[0])
return service.SetUser(service.LibraryPrefix, username, password, f.Cookie[0], nil)
}
6 changes: 3 additions & 3 deletions app/service/serviceUniFunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func GetUser(prefix string, username string, password string) (*model.User, erro
return user, nil
}

func SetUser(prefix string, username string, password string, cookie *http.Cookie) (*model.User, error) {
user := model.User{Username: username, Password: password, Session: *cookie}
func SetUser(prefix string, username string, password string, sessionCookie *http.Cookie, routeCookie *http.Cookie) (*model.User, error) {
user := model.User{Username: username, Password: password, Session: *sessionCookie, Route: *routeCookie}
userJson, _ := json.Marshal(user)
config.Redis.Set(getRediskey(prefix, username, password), string(userJson), cookie.Expires.Sub(time.Now().Add(time.Minute*5)))
config.Redis.Set(getRediskey(prefix, username, password), string(userJson), sessionCookie.Expires.Sub(time.Now().Add(time.Minute*5)))
return &user, nil
}

Expand Down
28 changes: 17 additions & 11 deletions app/service/zfService/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ func login(username string, password string) (*model.User, error) {
if len(f.Cookie) < 1 {
return nil, errors.ERR_UNKNOWN_LOGIN_ERROR
}

var URL string
if strings.Compare(config.Redis.Get("zf_url").String(), "new") == 0 {
URL = apis.CAPTCHA_NEW_BREAKER_URL
} else {
URL = apis.CAPTCHA_BREAKER_URL
}

captcha, err := f.Get(URL + f.Cookie[0].Value)
captcha, err := f.Get(URL + "?session=" + f.Cookie[0].Value + "&route=" + f.Cookie[1].Value)
if err != nil {
return nil, err
}
Expand All @@ -67,16 +65,20 @@ func login(username string, password string) (*model.User, error) {
if strings.Contains(string(s), "用户名或密码不正确") {
return nil, errors.ERR_WRONG_PASSWORD
}
var cookie *http.Cookie
var sessionCookie *http.Cookie
var routeCookie *http.Cookie
for _, v := range f.Cookie {
if v.Name == "JSESSIONID" {
cookie = v
sessionCookie = v
}
if v.Name == "route" {
routeCookie = v
}
}
if cookie == nil {
if sessionCookie == nil {
return nil, errors.ERR_UNKNOWN_LOGIN_ERROR
}
return service.SetUser(service.ZFPrefix, username, password, cookie)
return service.SetUser(service.ZFPrefix, username, password, sessionCookie, routeCookie)
}

func loginByOauth(username string, password string) (*model.User, error) {
Expand Down Expand Up @@ -120,14 +122,18 @@ func loginByOauth(username string, password string) (*model.User, error) {
if err != nil {
return nil, err
}
var cookie *http.Cookie
var sessionCookie *http.Cookie
var routeCookie *http.Cookie
for _, v := range f.Cookie {
if v.Name == "JSESSIONID" {
cookie = v
sessionCookie = v
}
if v.Name == "route" {
routeCookie = v
}
}
if cookie == nil {
if sessionCookie == nil {
return nil, errors.ERR_UNKNOWN_LOGIN_ERROR
}
return service.SetUser(service.ZFPrefix, username, password, cookie)
return service.SetUser(service.ZFPrefix, username, password, sessionCookie, routeCookie)
}
3 changes: 3 additions & 0 deletions app/service/zfService/zfService.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func fetchTermRelatedInfo(stu *model.User, requestUrl, year, term string, examIn
f := fetch.Fetch{}
f.Init()
f.Cookie = append(f.Cookie, &stu.Session)
f.Cookie = append(f.Cookie, &stu.Route)
if term == "上" {
term = "3"
} else if term == "下" {
Expand Down Expand Up @@ -112,6 +113,7 @@ func GetTrainingPrograms(stu *model.User) ([]byte, error) {
f := fetch.Fetch{}
f.Init()
f.Cookie = append(f.Cookie, &stu.Session)
f.Cookie = append(f.Cookie, &stu.Route)
response, err := f.GetRaw(zf.ZfUserInfo())

if err != nil {
Expand All @@ -135,6 +137,7 @@ func GetEmptyRoomInfo(stu *model.User, year string, term string, campus string,
f := fetch.Fetch{}
f.Init()
f.Cookie = append(f.Cookie, &stu.Session)
f.Cookie = append(f.Cookie, &stu.Route)
if term == "上" {
term = "3"
} else if term == "下" {
Expand Down
13 changes: 8 additions & 5 deletions app/utils/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package fetch

import (
"crypto/tls"
"errors"
"fmt"
errors2 "funnel/app/errors"
"io"
"net/http"
"net/url"
Expand All @@ -18,7 +19,7 @@ type Fetch struct {
func (f *Fetch) InitUnSafe() {
f.client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
Timeout: time.Second * 2,
Timeout: time.Second * 20,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Expand All @@ -28,7 +29,7 @@ func (f *Fetch) InitUnSafe() {
func (f *Fetch) Init() {
f.client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
Timeout: time.Second * 10,
Timeout: time.Second * 20,
}
}
func (f *Fetch) SkipTlsCheck() {
Expand All @@ -55,7 +56,7 @@ func (f *Fetch) GetRedirect(url string) (*url.URL, error) {
return nil, err
}
if response.StatusCode != 302 {
return nil, errors.New("network_error")
return nil, errors2.ERR_OAUTH_ERROR
}
location, err := response.Location()
if err != nil {
Expand Down Expand Up @@ -106,7 +107,9 @@ func (f *Fetch) PostFormRedirect(url string, requestData url.Values) (*url.URL,
return nil, err
}
if response.StatusCode != 302 {
return nil, errors.New("network_error")
fmt.Println(url)
fmt.Println(response.StatusCode)
return nil, errors2.ERR_WRONG_PASSWORD
}
f.Cookie = cookieMerge(f.Cookie, response.Cookies())
return response.Location()
Expand Down

0 comments on commit b6fc622

Please sign in to comment.