parser.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/PuerkitoBio/goquery"
  8. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  9. "strings"
  10. )
  11. type ParserService interface {
  12. GetMessage(ctx context.Context, req []byte) (string, error)
  13. ParseAlert(html string) (message string, err error)
  14. GetRuleId(ctx context.Context, htmlBytes []byte) (string, error)
  15. ParseSDKOnlineHTMLTable(htmlContent string) ([]v1.SDKInfo, error)
  16. CheckSDKKeyStatus(htmlData string, sdkKeyToFind string) error
  17. GetRuleIdByColumnName(ctx context.Context, htmlBytes []byte, columnName string) (string, error)
  18. }
  19. func NewParserService(
  20. service *Service,
  21. ) ParserService {
  22. return &parserService{
  23. Service: service,
  24. }
  25. }
  26. type parserService struct {
  27. *Service
  28. }
  29. // 解析 alert 消息
  30. func (s *parserService) ParseAlert(html string) (message string, err error) {
  31. doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
  32. if err != nil {
  33. return "", err
  34. }
  35. sel := doc.Find(".alert")
  36. if sel.Length() == 0 {
  37. // 没有 .alert 元素
  38. return "", nil
  39. }
  40. // 找到 .alert,继续提取
  41. t := strings.TrimSpace(sel.Find("h4").Text())
  42. full := strings.TrimSpace(sel.Text())
  43. full = strings.TrimPrefix(full, "×")
  44. full = strings.TrimSpace(full)
  45. m := strings.TrimSpace(strings.TrimPrefix(full, t))
  46. return m, nil
  47. }
  48. func (s *parserService) GetMessage(ctx context.Context, req []byte) (string, error) {
  49. type msg struct {
  50. Message string `json:"msg"` // 如果字段叫 msg,用 `json:"msg"`
  51. }
  52. var m msg
  53. if err := json.Unmarshal(req, &m); err != nil {
  54. return "", fmt.Errorf("解析 message 失败: %v", err)
  55. }
  56. if m.Message == "no affect row" {
  57. return "", fmt.Errorf("没有该条数据")
  58. }
  59. return m.Message, nil
  60. }
  61. func (s *parserService) GetRuleId(ctx context.Context, htmlBytes []byte) (string, error) {
  62. // 1. 把 []byte 包成 io.Reader
  63. reader := bytes.NewReader(htmlBytes)
  64. // 2. 用 goquery 解析
  65. doc, err := goquery.NewDocumentFromReader(reader)
  66. if err != nil {
  67. return "", err
  68. }
  69. // 方法一:按位置拿(第 2 个 tr、第 2 个 td)
  70. id := doc.
  71. Find("table.table tbody tr").Eq(1). // 跳过表头行,拿第一条数据
  72. Find("td").Eq(1).Text() // 第 2 个 td
  73. return strings.TrimSpace(id), nil
  74. }
  75. func (s *parserService) GetRuleIdByColumnName(ctx context.Context, htmlBytes []byte, name string) (string, error) {
  76. // 1. 定义我们用来搜索的列所有可能的名称。
  77. possibleKeyNames := []string{"标签", "网关组名称"}
  78. // 2. 解析HTML。
  79. reader := bytes.NewReader(htmlBytes)
  80. doc, err := goquery.NewDocumentFromReader(reader)
  81. if err != nil {
  82. return "", fmt.Errorf("failed to parse html: %w", err)
  83. }
  84. // 3. 动态查找第一个匹配的“关键字列”的索引。
  85. headerRow := doc.Find("table.table tbody tr:first-child")
  86. if headerRow.Length() == 0 {
  87. return "", nil
  88. }
  89. keyColumnIndex := -1
  90. // 我们使用 EachWithBreak,一旦找到有效的列就立即停止搜索,提高效率。
  91. headerRow.Find("th").EachWithBreak(func(index int, th *goquery.Selection) bool {
  92. headerText := strings.TrimSpace(th.Text())
  93. // 检查当前的表头文本是否匹配我们预设的任何一个可能名称。
  94. for _, possibleName := range possibleKeyNames {
  95. if headerText == possibleName {
  96. keyColumnIndex = index
  97. return false // 找到了,立即停止遍历表头(th)。
  98. }
  99. }
  100. return true // 没找到,继续遍历下一个表头(th)。
  101. })
  102. // 4. 检查我们是否找到了任何一个可能的关键字列。
  103. if keyColumnIndex == -1 {
  104. return "", fmt.Errorf("找不到任何一个指定的关键字列: %v", possibleKeyNames)
  105. }
  106. // 5. 遍历数据行以查找匹配的行。
  107. var foundID string
  108. var found bool
  109. doc.Find("table.table tbody tr").Slice(1, goquery.ToEnd).EachWithBreak(func(i int, row *goquery.Selection) bool {
  110. // 使用动态找到的索引来获取正确的单元格。
  111. keyCell := row.Find("td").Eq(keyColumnIndex)
  112. keyText := strings.TrimSpace(keyCell.Text())
  113. if keyText == name {
  114. // 如果关键字匹配,就从固定位置(第二个<td>,索引为1)获取值。
  115. idCell := row.Find("td").Eq(1)
  116. foundID = strings.TrimSpace(idCell.Text())
  117. found = true
  118. return false // 找到了目标行,停止循环。
  119. }
  120. return true // 不是目标行,继续。
  121. })
  122. // 6. 返回结果,如果未找到则返回错误。
  123. if !found {
  124. return "", fmt.Errorf("找不到关键字列值为 '%s' 的行", name)
  125. }
  126. return foundID, nil
  127. }
  128. // 解析 Sdk在线情况 表格
  129. func (s *parserService) ParseSDKOnlineHTMLTable(htmlContent string) ([]v1.SDKInfo, error) {
  130. // 创建goquery文档
  131. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlContent))
  132. if err != nil {
  133. return nil, fmt.Errorf("解析HTML失败: %v", err)
  134. }
  135. var sdkInfos []v1.SDKInfo
  136. // 查找表格并解析数据行
  137. doc.Find("table.table.table-hover tbody tr").Each(func(i int, s *goquery.Selection) {
  138. // 跳过表头行(如果有的话)
  139. if s.Find("th").Length() > 0 {
  140. return
  141. }
  142. var info v1.SDKInfo
  143. // 解析每一列的数据
  144. s.Find("td").Each(func(j int, td *goquery.Selection) {
  145. text := strings.TrimSpace(td.Text())
  146. // 根据列的位置分配到对应字段(跳过第一列的复选框)
  147. switch j {
  148. case 1: // 规则ID
  149. info.RuleID = text
  150. case 2: // 客户端IP
  151. info.ClientIP = text
  152. //case 3: // 网关IP
  153. // info.GatewayIP = text
  154. case 4: // SDK-UUID
  155. info.SDKUUID = text
  156. case 5: // 会话ID
  157. info.SessionID = text
  158. case 6: // SDK类型
  159. info.SDKType = text
  160. //case 7: // SDK版本
  161. // info.SDKVersion = text
  162. case 8: // 系统
  163. info.System = text
  164. case 9: // 附加信息
  165. // 对于附加信息列,提取JSON内容
  166. info.ExtraInfo = extractJSONFromExtraInfo(text)
  167. }
  168. })
  169. // 只有当规则ID不为空时才添加记录
  170. if info.RuleID != "" {
  171. sdkInfos = append(sdkInfos, info)
  172. }
  173. })
  174. return sdkInfos, nil
  175. }
  176. // extractJSONFromExtraInfo 从附加信息字符串中提取JSON内容
  177. func extractJSONFromExtraInfo(text string) string {
  178. text = strings.TrimSpace(text)
  179. // 尝试直接解析
  180. if result := tryParseJSON(text); result != "" {
  181. return result
  182. }
  183. // 尝试解析JSON字符串(去掉外层引号)
  184. if strings.HasPrefix(text, `"`) && strings.HasSuffix(text, `"`) {
  185. var jsonContent string
  186. if json.Unmarshal([]byte(text), &jsonContent) == nil {
  187. if result := tryParseJSON(jsonContent); result != "" {
  188. return result
  189. }
  190. }
  191. }
  192. // 从复杂文本中提取JSON
  193. return extractFromComplexText(text)
  194. }
  195. // 统一的JSON解析和格式化函数
  196. func tryParseJSON(text string) string {
  197. var temp interface{}
  198. if json.Unmarshal([]byte(text), &temp) == nil {
  199. if formatted, err := json.Marshal(temp); err == nil {
  200. return string(formatted)
  201. }
  202. return text
  203. }
  204. return ""
  205. }
  206. // 简化的复杂文本JSON提取
  207. func extractFromComplexText(text string) string {
  208. // 找到最后一个完整的JSON对象
  209. for end := strings.LastIndex(text, "}"); end != -1; end = strings.LastIndex(text[:end], "}") {
  210. // 向前查找匹配的开始大括号
  211. braceCount := 1
  212. for start := end - 1; start >= 0; start-- {
  213. switch text[start] {
  214. case '}':
  215. braceCount++
  216. case '{':
  217. braceCount--
  218. if braceCount == 0 {
  219. candidate := text[start : end+1]
  220. if result := tryParseJSON(candidate); result != "" {
  221. return result
  222. }
  223. break // 跳出内层循环,继续寻找下一个'}'
  224. }
  225. }
  226. }
  227. }
  228. return "查看"
  229. }
  230. // CheckSDKKeyStatus 检查SDKKey是否存在且过期
  231. func (s *parserService) CheckSDKKeyStatus(htmlData string, sdkKeyToFind string) error {
  232. // 使用 strings.NewReader 将字符串转换为一个 io.Reader,这是 go-query 所需的输入格式。
  233. // go-query 会加载并解析这个HTML,返回一个可供查询的文档对象(doc)。
  234. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlData))
  235. if err != nil {
  236. // 如果go-query无法加载或解析HTML,则返回错误。
  237. return fmt.Errorf("无法解析HTML: %w", err)
  238. }
  239. // 定义两个变量用于在循环结束后判断状态
  240. var keyFound bool = false // 标记是否找到了Key
  241. var resultErr error = nil // 用于存储找到Key后的最终错误状态(如果是过期的话)
  242. // 使用go-query的选择器找到class为 "table" 的表格(table.table)的主体(tbody)中的所有行(tr)。
  243. // .EachWithBreak 方法会遍历每一行,并允许我们在满足特定条件时提前中断循环。
  244. doc.Find("table.table tbody tr").EachWithBreak(func(i int, row *goquery.Selection) bool {
  245. // 在当前行(row)中,查找第7个单元格(td:nth-of-type(7)),这是“SDK启动KEY”所在的列。
  246. // Key本身被隐藏在一个 <pre> 标签内,我们直接定位它。
  247. keyCell := row.Find("td:nth-of-type(7) pre")
  248. fullKeyText := keyCell.Text() // 获取 <pre> 标签内的所有文本内容。
  249. // 原始文本的格式是固定的,我们需要从中提取出真正的KEY。
  250. // 格式: "原始内容:... >>> SDK启动KEY如下,复制后启动SDK使用 <<< [THE_ACTUAL_KEY]"
  251. parts := strings.Split(fullKeyText, ">>> SDK启动KEY如下,复制后启动SDK使用 <<<")
  252. if len(parts) < 2 {
  253. // 如果当前行不符合这个格式,跳到下一行处理。
  254. return true // `true` 在 EachWithBreak 中表示继续循环
  255. }
  256. // 提取并清理KEY字符串,去掉它前后的所有空格和换行符。
  257. extractedKey := strings.TrimSpace(parts[1])
  258. // 检查从页面提取出的KEY是否与我们要找的KEY相匹配。
  259. if extractedKey == sdkKeyToFind {
  260. keyFound = true // 首先,标记我们已经找到了Key
  261. // 接着,在同一行中查找过期状态。
  262. // 第11个单元格(td:nth-of-type(11))包含“过期时间”信息。
  263. expirationCell := row.Find("td:nth-of-type(11)")
  264. expirationText := expirationCell.Text() // 获取该单元格的文本内容。
  265. // 检查过期时间文本中是否包含`(已过期)`
  266. if strings.Contains(expirationText, "(已过期)") {
  267. // 如果包含,我们将错误信息赋值给外部的 resultErr 变量
  268. resultErr = fmt.Errorf("该KEY已过期")
  269. }
  270. // 注意:即使未过期,resultErr 仍然是 nil,这正是我们想要的结果。
  271. // 我们已经找到了目标并处理完毕,没有必要再检查剩下的行了。
  272. return false // `false` 在 EachWithBreak 中表示中断循环
  273. }
  274. // 如果当前行的KEY不匹配,继续下一行的查找。
  275. return true
  276. })
  277. // --- 循环结束后的最终判断 ---
  278. // 如果 keyFound 标志位仍然是 false,说明遍历了所有行都没有找到匹配的Key。
  279. if !keyFound {
  280. return fmt.Errorf("未找到指定的Key")
  281. }
  282. // 如果 keyFound 是 true,说明找到了Key。
  283. // 此时,我们返回在循环中确定的 resultErr。
  284. // - 如果Key未过期,resultErr 就是它初始的 nil 值。
  285. // - 如果Key已过期,resultErr 就是我们在循环里设置的那个 error。
  286. return resultErr
  287. }