api_info.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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正则表达式,支持域名和IP地址格式
  31. // 域名格式:https://example.com 或 https://sub.example.com:8080
  32. // IP地址格式:https://192.168.1.1 或 https://192.168.1.1:8080
  33. 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])?|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?::[0-9]{1,5})?(?:/.*)?$`)
  34. for i, apiInfo := range apiInfoList {
  35. // 检查必填字段
  36. urlStr, ok := apiInfo["url"].(string)
  37. if !ok || urlStr == "" {
  38. return fmt.Errorf("第%d个API信息缺少URL字段", i+1)
  39. }
  40. route, ok := apiInfo["route"].(string)
  41. if !ok || route == "" {
  42. return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1)
  43. }
  44. description, ok := apiInfo["description"].(string)
  45. if !ok || description == "" {
  46. return fmt.Errorf("第%d个API信息缺少说明字段", i+1)
  47. }
  48. color, ok := apiInfo["color"].(string)
  49. if !ok || color == "" {
  50. return fmt.Errorf("第%d个API信息缺少颜色字段", i+1)
  51. }
  52. // 验证URL格式
  53. if !urlRegex.MatchString(urlStr) {
  54. return fmt.Errorf("第%d个API信息的URL格式不正确", i+1)
  55. }
  56. // 验证URL可解析性
  57. if _, err := url.Parse(urlStr); err != nil {
  58. return fmt.Errorf("第%d个API信息的URL无法解析:%s", i+1, err.Error())
  59. }
  60. // 验证字段长度
  61. if len(urlStr) > 500 {
  62. return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1)
  63. }
  64. if len(route) > 100 {
  65. return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1)
  66. }
  67. if len(description) > 200 {
  68. return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1)
  69. }
  70. // 验证颜色值
  71. if !validColors[color] {
  72. return fmt.Errorf("第%d个API信息的颜色值不合法", i+1)
  73. }
  74. // 检查并过滤危险字符(防止XSS)
  75. dangerousChars := []string{"<script", "<iframe", "javascript:", "onload=", "onerror=", "onclick="}
  76. for _, dangerous := range dangerousChars {
  77. if strings.Contains(strings.ToLower(description), dangerous) {
  78. return fmt.Errorf("第%d个API信息的说明包含不允许的内容", i+1)
  79. }
  80. if strings.Contains(strings.ToLower(route), dangerous) {
  81. return fmt.Errorf("第%d个API信息的线路描述包含不允许的内容", i+1)
  82. }
  83. }
  84. }
  85. return nil
  86. }
  87. // GetApiInfo 获取API信息列表
  88. func GetApiInfo() []map[string]interface{} {
  89. // 从OptionMap中获取API信息,如果不存在则返回空数组
  90. common.OptionMapRWMutex.RLock()
  91. apiInfoStr, exists := common.OptionMap["ApiInfo"]
  92. common.OptionMapRWMutex.RUnlock()
  93. if !exists || apiInfoStr == "" {
  94. // 如果没有配置,返回空数组
  95. return []map[string]interface{}{}
  96. }
  97. // 解析存储的API信息
  98. var apiInfo []map[string]interface{}
  99. if err := json.Unmarshal([]byte(apiInfoStr), &apiInfo); err != nil {
  100. // 如果解析失败,返回空数组
  101. return []map[string]interface{}{}
  102. }
  103. return apiInfo
  104. }