console.go 4.2 KB

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