api_info.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package setting
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "one-api/common"
  7. "regexp"
  8. "strings"
  9. )
  10. // ValidateApiInfo 验证API信息格式
  11. func ValidateApiInfo(apiInfoStr string) error {
  12. if apiInfoStr == "" {
  13. return nil // 空字符串是合法的
  14. }
  15. var apiInfoList []map[string]interface{}
  16. if err := json.Unmarshal([]byte(apiInfoStr), &apiInfoList); err != nil {
  17. return fmt.Errorf("API信息格式错误:%s", err.Error())
  18. }
  19. // 验证数组长度
  20. if len(apiInfoList) > 50 {
  21. return fmt.Errorf("API信息数量不能超过50个")
  22. }
  23. // 允许的颜色值
  24. validColors := map[string]bool{
  25. "blue": true, "green": true, "cyan": true, "purple": true, "pink": true,
  26. "red": true, "orange": true, "amber": true, "yellow": true, "lime": true,
  27. "light-green": true, "teal": true, "light-blue": true, "indigo": true,
  28. "violet": true, "grey": true,
  29. }
  30. // URL正则表达式
  31. urlRegex := regexp.MustCompile(`^https?://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(/.*)?$`)
  32. for i, apiInfo := range apiInfoList {
  33. // 检查必填字段
  34. urlStr, ok := apiInfo["url"].(string)
  35. if !ok || urlStr == "" {
  36. return fmt.Errorf("第%d个API信息缺少URL字段", i+1)
  37. }
  38. route, ok := apiInfo["route"].(string)
  39. if !ok || route == "" {
  40. return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1)
  41. }
  42. description, ok := apiInfo["description"].(string)
  43. if !ok || description == "" {
  44. return fmt.Errorf("第%d个API信息缺少说明字段", i+1)
  45. }
  46. color, ok := apiInfo["color"].(string)
  47. if !ok || color == "" {
  48. return fmt.Errorf("第%d个API信息缺少颜色字段", i+1)
  49. }
  50. // 验证URL格式
  51. if !urlRegex.MatchString(urlStr) {
  52. return fmt.Errorf("第%d个API信息的URL格式不正确", i+1)
  53. }
  54. // 验证URL可解析性
  55. if _, err := url.Parse(urlStr); err != nil {
  56. return fmt.Errorf("第%d个API信息的URL无法解析:%s", i+1, err.Error())
  57. }
  58. // 验证字段长度
  59. if len(urlStr) > 500 {
  60. return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1)
  61. }
  62. if len(route) > 100 {
  63. return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1)
  64. }
  65. if len(description) > 200 {
  66. return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1)
  67. }
  68. // 验证颜色值
  69. if !validColors[color] {
  70. return fmt.Errorf("第%d个API信息的颜色值不合法", i+1)
  71. }
  72. // 检查并过滤危险字符(防止XSS)
  73. dangerousChars := []string{"<script", "<iframe", "javascript:", "onload=", "onerror=", "onclick="}
  74. for _, dangerous := range dangerousChars {
  75. if strings.Contains(strings.ToLower(description), dangerous) {
  76. return fmt.Errorf("第%d个API信息的说明包含不允许的内容", i+1)
  77. }
  78. if strings.Contains(strings.ToLower(route), dangerous) {
  79. return fmt.Errorf("第%d个API信息的线路描述包含不允许的内容", i+1)
  80. }
  81. }
  82. }
  83. return nil
  84. }
  85. // GetApiInfo 获取API信息列表
  86. func GetApiInfo() []map[string]interface{} {
  87. // 从OptionMap中获取API信息,如果不存在则返回空数组
  88. common.OptionMapRWMutex.RLock()
  89. apiInfoStr, exists := common.OptionMap["ApiInfo"]
  90. common.OptionMapRWMutex.RUnlock()
  91. if !exists || apiInfoStr == "" {
  92. // 如果没有配置,返回空数组
  93. return []map[string]interface{}{}
  94. }
  95. // 解析存储的API信息
  96. var apiInfo []map[string]interface{}
  97. if err := json.Unmarshal([]byte(apiInfoStr), &apiInfo); err != nil {
  98. // 如果解析失败,返回空数组
  99. return []map[string]interface{}{}
  100. }
  101. return apiInfo
  102. }