Skip to content

Commit

Permalink
fix(lanzou): download not find file sgin (close #5046 in #5048)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxorcat authored Aug 20, 2023
1 parent b6cd40e commit 358c505
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
77 changes: 77 additions & 0 deletions drivers/lanzou/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,83 @@ func findJSVarFunc(key, data string) string {
return values[1]
}

var findFunction = regexp.MustCompile(`(?ims)^function[^{]+`)
var findFunctionAll = regexp.MustCompile(`(?is)function[^{]+`)

// 查找所有方法位置
func findJSFunctionIndex(data string, all bool) [][2]int {
findFunction := findFunction
if all {
findFunction = findFunctionAll
}

indexs := findFunction.FindAllStringIndex(data, -1)
fIndexs := make([][2]int, 0, len(indexs))

for _, index := range indexs {
if len(index) != 2 {
continue
}
count, data := 0, data[index[1]:]
for ii, v := range data {
if v == ' ' && count == 0 {
continue
}
if v == '{' {
count++
}

if v == '}' {
count--
}
if count == 0 {
fIndexs = append(fIndexs, [2]int{index[0], index[1] + ii + 1})
break
}
}
}
return fIndexs
}

// 删除JS全局方法
func removeJSGlobalFunction(html string) string {
indexs := findJSFunctionIndex(html, false)
block := make([]string, len(indexs))
for i, next := len(indexs)-1, len(html); i >= 0; i-- {
index := indexs[i]
block[i] = html[index[1]:next]
next = index[0]
}
return strings.Join(block, "")
}

// 根据名称获取方法
func getJSFunctionByName(html string, name string) (string, error) {
indexs := findJSFunctionIndex(html, true)
for _, index := range indexs {
data := html[index[0]:index[1]]
if regexp.MustCompile(`function\s+` + name + `[()\s]+{`).MatchString(data) {
return data, nil
}
}
return "", fmt.Errorf("not find %s function", name)
}

// 解析html中的JSON,选择最长的数据
func htmlJsonToMap2(html string) (map[string]string, error) {
datas := findDataReg.FindAllStringSubmatch(html, -1)
var sData string
for _, data := range datas {
if len(datas) > 0 && len(data[1]) > len(sData) {
sData = data[1]
}
}
if sData == "" {
return nil, fmt.Errorf("not find data")
}
return jsonToMap(sData, html), nil
}

// 解析html中的JSON
func htmlJsonToMap(html string) (map[string]string, error) {
datas := findDataReg.FindStringSubmatch(html)
Expand Down
9 changes: 6 additions & 3 deletions drivers/lanzou/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ func (d *LanZou) getFilesByShareUrl(shareID, pwd string, sharePageData string) (

// 需要密码
if strings.Contains(sharePageData, "pwdload") || strings.Contains(sharePageData, "passwddiv") {
param, err := htmlFormToMap(sharePageData)
sharePageData, err := getJSFunctionByName(sharePageData, "down_p")
if err != nil {
return nil, err
}
param, err := htmlJsonToMap(sharePageData)
if err != nil {
return nil, err
}
Expand All @@ -370,8 +374,7 @@ func (d *LanZou) getFilesByShareUrl(shareID, pwd string, sharePageData string) (
if err != nil {
return nil, err
}
nextPageData := RemoveNotes(string(data))

nextPageData := removeJSGlobalFunction(RemoveNotes(string(data)))
param, err = htmlJsonToMap(nextPageData)
if err != nil {
return nil, err
Expand Down

0 comments on commit 358c505

Please sign in to comment.