fix: 修复 detect 逻辑错误若干

This commit is contained in:
Mmx233
2024-01-01 19:00:22 +08:00
parent ca2215b44e
commit c20a8f7532
2 changed files with 53 additions and 18 deletions

View File

@@ -41,8 +41,6 @@ func Login(eth *tools.Eth, debugOutput bool) error {
acidOnReality = true acidOnReality = true
log.Debugf("使用嗅探 acid: %s", acid) log.Debugf("使用嗅探 acid: %s", acid)
srunClient.LoginInfo.Meta.Acid = acid srunClient.LoginInfo.Meta.Acid = acid
} else {
log.Debugln("Reality 流程未找到 acid")
} }
} }
} }

View File

@@ -12,15 +12,15 @@ import (
"unsafe" "unsafe"
) )
func (a *Api) NewDetector() Detector { func (a *Api) NewDetector() *Detector {
redirectReg, err := regexp.Compile( redirectReg, err := regexp.Compile(
`<script>top\.self\.location\.href='(.*)'</script>|<meta http-equiv="refresh" content=".*url=(.*?)">`, `<script>top\.self\.location\.href='(.*)'</script>|<meta http-equiv="refresh" content=".*?url=(.*?)"`,
) )
if err != nil { if err != nil {
panic(err) panic(err)
} }
return Detector{ return &Detector{
api: a, api: a,
redirectReg: redirectReg, redirectReg: redirectReg,
} }
@@ -32,7 +32,8 @@ type Detector struct {
redirectReg *regexp.Regexp redirectReg *regexp.Regexp
// 登录页 html data // 登录页 html data
page []byte pageUrl string
page []byte
} }
func (a *Detector) _JoinRedirectLocation(addr *url.URL, loc string) (*url.URL, error) { func (a *Detector) _JoinRedirectLocation(addr *url.URL, loc string) (*url.URL, error) {
@@ -40,7 +41,13 @@ func (a *Detector) _JoinRedirectLocation(addr *url.URL, loc string) (*url.URL, e
return nil, errors.New("目标跳转地址缺失") return nil, errors.New("目标跳转地址缺失")
} }
if strings.HasPrefix(loc, "/") { if strings.HasPrefix(loc, "/") {
addr.Path = strings.TrimPrefix(loc, "/") locSplit := strings.Split(loc, "?")
addr.Path = locSplit[0]
if len(locSplit) > 1 {
addr.RawQuery = locSplit[1]
} else {
addr.RawQuery = ""
}
return addr, nil return addr, nil
} else { } else {
return url.Parse(loc) return url.Parse(loc)
@@ -79,11 +86,16 @@ func (a *Detector) _FollowRedirect(addr *url.URL, conf _FollowRedirectConfig) (*
return nil, nil, err return nil, nil, err
} }
locMatch := a.redirectReg.FindSubmatch(body) locMatch := a.redirectReg.FindSubmatch(body)
if len(locMatch) > 2 { if len(locMatch) >= 2 {
locBytes := locMatch[1] for i := 1; i < len(locMatch); i++ {
addr, err = a._JoinRedirectLocation(addr, unsafe.String(unsafe.SliceData(locBytes), len(locBytes))) locBytes := locMatch[i]
if err != nil { if len(locBytes) != 0 {
return nil, nil, err addr, err = a._JoinRedirectLocation(addr, unsafe.String(unsafe.SliceData(locBytes), len(locBytes)))
if err != nil {
return nil, nil, err
}
break
}
} }
} else { } else {
break break
@@ -116,19 +128,38 @@ func (a *Detector) _SearchAcid(query url.Values) (string, bool) {
return addr, addr != "" return addr, addr != ""
} }
func (a *Detector) DetectEnc() (string, error) { // 用于直接获取登录页数据
if a.page == nil { func (a *Detector) _RequestPageBytes() ([]byte, error) {
log.Debugln("HTTP GET", a.api.BaseUrl) if a.pageUrl != "" {
res, err := a.api.Client.Get(a.api.BaseUrl) log.Debugln("HTTP GET", a.pageUrl)
res, err := a.api.Client.Get(a.pageUrl)
if err != nil { if err != nil {
return "", err return nil, err
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
_, _ = io.Copy(io.Discard, res.Body) _, _ = io.Copy(io.Discard, res.Body)
return "", fmt.Errorf("server return http status: %d", res.StatusCode) return nil, fmt.Errorf("server return http status: %d", res.StatusCode)
} }
a.page, err = io.ReadAll(res.Body) a.page, err = io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return a.page, nil
}
baseUrl, err := url.Parse(a.api.BaseUrl)
if err != nil {
return nil, err
}
_, a.page, err = a._FollowRedirect(baseUrl, _FollowRedirectConfig{})
return a.page, err
}
func (a *Detector) DetectEnc() (string, error) {
if a.page == nil {
_, err := a._RequestPageBytes()
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -142,6 +173,7 @@ func (a *Detector) DetectEnc() (string, error) {
if len(jsPathMatch) == 3 { if len(jsPathMatch) == 3 {
jsPathBytes := jsPathMatch[1] jsPathBytes := jsPathMatch[1]
jsPath := unsafe.String(unsafe.SliceData(jsPathBytes), len(jsPathBytes)) jsPath := unsafe.String(unsafe.SliceData(jsPathBytes), len(jsPathBytes))
fmt.Println("111", jsPath)
jsUrl, err := url.Parse(a.api.BaseUrl) jsUrl, err := url.Parse(a.api.BaseUrl)
if err != nil { if err != nil {
return "", err return "", err
@@ -198,6 +230,7 @@ func (a *Detector) DetectAcid() (string, error) {
var ok bool var ok bool
acid, ok = a._SearchAcid(addr.Query()) acid, ok = a._SearchAcid(addr.Query())
if ok { if ok {
a.pageUrl = addr.String()
return AcidFound return AcidFound
} }
return nil return nil
@@ -234,6 +267,9 @@ func (a *Detector) Reality(addr string, getAcid bool) (acid string, online bool,
var AlreadyOnline = errors.New("already online") var AlreadyOnline = errors.New("already online")
finalRes, pageBytes, err := a._FollowRedirect(startUrl, _FollowRedirectConfig{ finalRes, pageBytes, err := a._FollowRedirect(startUrl, _FollowRedirectConfig{
onNextAddr: func(addr *url.URL) error { onNextAddr: func(addr *url.URL) error {
if addr.Host == startUrl.Host {
return AlreadyOnline
}
if getAcid { if getAcid {
acid, _ = a._SearchAcid(addr.Query()) acid, _ = a._SearchAcid(addr.Query())
} }
@@ -254,5 +290,6 @@ func (a *Detector) Reality(addr string, getAcid bool) (acid string, online bool,
} }
func (a *Detector) Reset() { func (a *Detector) Reset() {
a.pageUrl = ""
a.page = nil a.page = nil
} }