model.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Package model defines the database models and data structures used by the 3x-ui panel.
  2. package model
  3. import (
  4. "fmt"
  5. "github.com/mhsanaei/3x-ui/v2/util/json_util"
  6. "github.com/mhsanaei/3x-ui/v2/xray"
  7. )
  8. // Protocol represents the protocol type for Xray inbounds.
  9. type Protocol string
  10. // Protocol constants for different Xray inbound protocols
  11. const (
  12. VMESS Protocol = "vmess"
  13. VLESS Protocol = "vless"
  14. Tunnel Protocol = "tunnel"
  15. HTTP Protocol = "http"
  16. Trojan Protocol = "trojan"
  17. Shadowsocks Protocol = "shadowsocks"
  18. Mixed Protocol = "mixed"
  19. WireGuard Protocol = "wireguard"
  20. Hysteria Protocol = "hysteria"
  21. )
  22. // User represents a user account in the 3x-ui panel.
  23. type User struct {
  24. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  25. Username string `json:"username"`
  26. Password string `json:"password"`
  27. }
  28. // Inbound represents an Xray inbound configuration with traffic statistics and settings.
  29. type Inbound struct {
  30. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"` // Unique identifier
  31. UserId int `json:"-"` // Associated user ID
  32. Up int64 `json:"up" form:"up"` // Upload traffic in bytes
  33. Down int64 `json:"down" form:"down"` // Download traffic in bytes
  34. Total int64 `json:"total" form:"total"` // Total traffic limit in bytes
  35. AllTime int64 `json:"allTime" form:"allTime" gorm:"default:0"` // All-time traffic usage
  36. Remark string `json:"remark" form:"remark"` // Human-readable remark
  37. Enable bool `json:"enable" form:"enable" gorm:"index:idx_enable_traffic_reset,priority:1"` // Whether the inbound is enabled
  38. ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` // Expiration timestamp
  39. TrafficReset string `json:"trafficReset" form:"trafficReset" gorm:"default:never;index:idx_enable_traffic_reset,priority:2"` // Traffic reset schedule
  40. LastTrafficResetTime int64 `json:"lastTrafficResetTime" form:"lastTrafficResetTime" gorm:"default:0"` // Last traffic reset timestamp
  41. ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"` // Client traffic statistics
  42. // Xray configuration fields
  43. Listen string `json:"listen" form:"listen"`
  44. Port int `json:"port" form:"port"`
  45. Protocol Protocol `json:"protocol" form:"protocol"`
  46. Settings string `json:"settings" form:"settings"`
  47. StreamSettings string `json:"streamSettings" form:"streamSettings"`
  48. Tag string `json:"tag" form:"tag" gorm:"unique"`
  49. Sniffing string `json:"sniffing" form:"sniffing"`
  50. }
  51. // OutboundTraffics tracks traffic statistics for Xray outbound connections.
  52. type OutboundTraffics struct {
  53. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
  54. Tag string `json:"tag" form:"tag" gorm:"unique"`
  55. Up int64 `json:"up" form:"up" gorm:"default:0"`
  56. Down int64 `json:"down" form:"down" gorm:"default:0"`
  57. Total int64 `json:"total" form:"total" gorm:"default:0"`
  58. }
  59. // InboundClientIps stores IP addresses associated with inbound clients for access control.
  60. type InboundClientIps struct {
  61. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  62. ClientEmail string `json:"clientEmail" form:"clientEmail" gorm:"unique"`
  63. Ips string `json:"ips" form:"ips"`
  64. }
  65. // HistoryOfSeeders tracks which database seeders have been executed to prevent re-running.
  66. type HistoryOfSeeders struct {
  67. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  68. SeederName string `json:"seederName"`
  69. }
  70. // GenXrayInboundConfig generates an Xray inbound configuration from the Inbound model.
  71. func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
  72. listen := i.Listen
  73. // Default to 0.0.0.0 (all interfaces) when listen is empty
  74. // This ensures proper dual-stack IPv4/IPv6 binding in systems where bindv6only=0
  75. if listen == "" {
  76. listen = "0.0.0.0"
  77. }
  78. listen = fmt.Sprintf("\"%v\"", listen)
  79. return &xray.InboundConfig{
  80. Listen: json_util.RawMessage(listen),
  81. Port: i.Port,
  82. Protocol: string(i.Protocol),
  83. Settings: json_util.RawMessage(i.Settings),
  84. StreamSettings: json_util.RawMessage(i.StreamSettings),
  85. Tag: i.Tag,
  86. Sniffing: json_util.RawMessage(i.Sniffing),
  87. }
  88. }
  89. // Setting stores key-value configuration settings for the 3x-ui panel.
  90. type Setting struct {
  91. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
  92. Key string `json:"key" form:"key"`
  93. Value string `json:"value" form:"value"`
  94. }
  95. type CustomGeoResource struct {
  96. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  97. Type string `json:"type" gorm:"not null;uniqueIndex:idx_custom_geo_type_alias;column:geo_type"`
  98. Alias string `json:"alias" gorm:"not null;uniqueIndex:idx_custom_geo_type_alias"`
  99. Url string `json:"url" gorm:"not null"`
  100. LocalPath string `json:"localPath" gorm:"column:local_path"`
  101. LastUpdatedAt int64 `json:"lastUpdatedAt" gorm:"default:0;column:last_updated_at"`
  102. LastModified string `json:"lastModified" gorm:"column:last_modified"`
  103. CreatedAt int64 `json:"createdAt" gorm:"autoCreateTime;column:created_at"`
  104. UpdatedAt int64 `json:"updatedAt" gorm:"autoUpdateTime;column:updated_at"`
  105. }
  106. // Client represents a client configuration for Xray inbounds with traffic limits and settings.
  107. type Client struct {
  108. ID string `json:"id,omitempty"` // Unique client identifier
  109. Security string `json:"security"` // Security method (e.g., "auto", "aes-128-gcm")
  110. Password string `json:"password,omitempty"` // Client password
  111. Flow string `json:"flow,omitempty"` // Flow control (XTLS)
  112. Auth string `json:"auth,omitempty"` // Auth password (Hysteria)
  113. Email string `json:"email"` // Client email identifier
  114. LimitIP int `json:"limitIp"` // IP limit for this client
  115. TotalGB int64 `json:"totalGB" form:"totalGB"` // Total traffic limit in GB
  116. ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` // Expiration timestamp
  117. Enable bool `json:"enable" form:"enable"` // Whether the client is enabled
  118. TgID int64 `json:"tgId" form:"tgId"` // Telegram user ID for notifications
  119. SubID string `json:"subId" form:"subId"` // Subscription identifier
  120. Comment string `json:"comment" form:"comment"` // Client comment
  121. Reset int `json:"reset" form:"reset"` // Reset period in days
  122. CreatedAt int64 `json:"created_at,omitempty"` // Creation timestamp
  123. UpdatedAt int64 `json:"updated_at,omitempty"` // Last update timestamp
  124. }