Skip to content

Instantly share code, notes, and snippets.

@leveryd
Created July 10, 2021 05:33
Show Gist options
  • Save leveryd/465d19610d5fcf99199beb9284cd6f8c to your computer and use it in GitHub Desktop.
Save leveryd/465d19610d5fcf99199beb9284cd6f8c to your computer and use it in GitHub Desktop.
func ExtractFromText(text string) []string {
s := utils.NewStringSet()
reg := regexp.MustCompile(`'(/[\w\d\-./]+)'?`)
apis := reg.FindAllString(text, -1)
for _, api := range apis {
api = reg.ReplaceAllString(api, "$1")
if strings.HasPrefix(api, "//") == true {
continue
}
s.Add(api)
}
reg = regexp.MustCompile(`"(/[\w\d\-./]+)"?`)
apis = reg.FindAllString(text, -1)
for _, api := range apis {
api = reg.ReplaceAllString(api, "$1")
if strings.HasPrefix(api, "//") == true {
continue
}
s.Add(api)
}
// api = `/api/xxx`
reg = regexp.MustCompile("`" + `(/[\w\d\-./]+)` + "`?")
apis = reg.FindAllString(text, -1)
for _, api := range apis {
api = reg.ReplaceAllString(api, "$1")
if strings.HasPrefix(api, "//") == true {
continue
}
s.Add(api)
}
// resource: "report"
reg = regexp.MustCompile(`resource:"([\w\d\-./]+)"?`)
apis = reg.FindAllString(text, -1)
// 去除误报,以//开头的字符串不当做api
for _, api := range apis {
api = reg.ReplaceAllString(api, "$1")
if strings.HasPrefix(api, "//") == true {
continue
}
s.Add(api)
}
// 这条规则误报有点高
// "admin/xxx"
reg = regexp.MustCompile(`['"]{1}([\w\d\-]+/[\w\d\-./]+)['"]{1}?`)
apis = reg.FindAllString(text, -1)
for _, api := range apis {
api = reg.ReplaceAllString(api, "$1")
if strings.HasPrefix(api, "/") == true {
continue
}
s.Add(api)
}
return s.List()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment