model.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // UI stores Hysteria v1 and v2 both as "hysteria" and uses
  21. // settings.version to discriminate. Imports from outside the panel
  22. // can carry the literal "hysteria2" string, so IsHysteria below
  23. // accepts both.
  24. Hysteria Protocol = "hysteria"
  25. Hysteria2 Protocol = "hysteria2"
  26. )
  27. // IsHysteria returns true for both "hysteria" and "hysteria2".
  28. // Use instead of a bare ==model.Hysteria check: a v2 inbound stored
  29. // with the literal v2 string would otherwise fall through (#4081).
  30. func IsHysteria(p Protocol) bool {
  31. return p == Hysteria || p == Hysteria2
  32. }
  33. // User represents a user account in the 3x-ui panel.
  34. type User struct {
  35. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  36. Username string `json:"username"`
  37. Password string `json:"password"`
  38. }
  39. // Inbound represents an Xray inbound configuration with traffic statistics and settings.
  40. type Inbound struct {
  41. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"` // Unique identifier
  42. UserId int `json:"-"` // Associated user ID
  43. Up int64 `json:"up" form:"up"` // Upload traffic in bytes
  44. Down int64 `json:"down" form:"down"` // Download traffic in bytes
  45. Total int64 `json:"total" form:"total"` // Total traffic limit in bytes
  46. AllTime int64 `json:"allTime" form:"allTime" gorm:"default:0"` // All-time traffic usage
  47. Remark string `json:"remark" form:"remark"` // Human-readable remark
  48. Enable bool `json:"enable" form:"enable" gorm:"index:idx_enable_traffic_reset,priority:1"` // Whether the inbound is enabled
  49. ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` // Expiration timestamp
  50. TrafficReset string `json:"trafficReset" form:"trafficReset" gorm:"default:never;index:idx_enable_traffic_reset,priority:2"` // Traffic reset schedule
  51. LastTrafficResetTime int64 `json:"lastTrafficResetTime" form:"lastTrafficResetTime" gorm:"default:0"` // Last traffic reset timestamp
  52. ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"` // Client traffic statistics
  53. // Xray configuration fields
  54. Listen string `json:"listen" form:"listen"`
  55. Port int `json:"port" form:"port"`
  56. Protocol Protocol `json:"protocol" form:"protocol"`
  57. Settings string `json:"settings" form:"settings"`
  58. StreamSettings string `json:"streamSettings" form:"streamSettings"`
  59. Tag string `json:"tag" form:"tag" gorm:"unique"`
  60. Sniffing string `json:"sniffing" form:"sniffing"`
  61. }
  62. // OutboundTraffics tracks traffic statistics for Xray outbound connections.
  63. type OutboundTraffics struct {
  64. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
  65. Tag string `json:"tag" form:"tag" gorm:"unique"`
  66. Up int64 `json:"up" form:"up" gorm:"default:0"`
  67. Down int64 `json:"down" form:"down" gorm:"default:0"`
  68. Total int64 `json:"total" form:"total" gorm:"default:0"`
  69. }
  70. // InboundClientIps stores IP addresses associated with inbound clients for access control.
  71. type InboundClientIps struct {
  72. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  73. ClientEmail string `json:"clientEmail" form:"clientEmail" gorm:"unique"`
  74. Ips string `json:"ips" form:"ips"`
  75. }
  76. // HistoryOfSeeders tracks which database seeders have been executed to prevent re-running.
  77. type HistoryOfSeeders struct {
  78. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  79. SeederName string `json:"seederName"`
  80. }
  81. // GenXrayInboundConfig generates an Xray inbound configuration from the Inbound model.
  82. func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
  83. listen := i.Listen
  84. // Default to 0.0.0.0 (all interfaces) when listen is empty
  85. // This ensures proper dual-stack IPv4/IPv6 binding in systems where bindv6only=0
  86. if listen == "" {
  87. listen = "0.0.0.0"
  88. }
  89. listen = fmt.Sprintf("\"%v\"", listen)
  90. return &xray.InboundConfig{
  91. Listen: json_util.RawMessage(listen),
  92. Port: i.Port,
  93. Protocol: string(i.Protocol),
  94. Settings: json_util.RawMessage(i.Settings),
  95. StreamSettings: json_util.RawMessage(i.StreamSettings),
  96. Tag: i.Tag,
  97. Sniffing: json_util.RawMessage(i.Sniffing),
  98. }
  99. }
  100. // Setting stores key-value configuration settings for the 3x-ui panel.
  101. type Setting struct {
  102. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
  103. Key string `json:"key" form:"key"`
  104. Value string `json:"value" form:"value"`
  105. }
  106. type CustomGeoResource struct {
  107. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  108. Type string `json:"type" gorm:"not null;uniqueIndex:idx_custom_geo_type_alias;column:geo_type"`
  109. Alias string `json:"alias" gorm:"not null;uniqueIndex:idx_custom_geo_type_alias"`
  110. Url string `json:"url" gorm:"not null"`
  111. LocalPath string `json:"localPath" gorm:"column:local_path"`
  112. LastUpdatedAt int64 `json:"lastUpdatedAt" gorm:"default:0;column:last_updated_at"`
  113. LastModified string `json:"lastModified" gorm:"column:last_modified"`
  114. CreatedAt int64 `json:"createdAt" gorm:"autoCreateTime;column:created_at"`
  115. UpdatedAt int64 `json:"updatedAt" gorm:"autoUpdateTime;column:updated_at"`
  116. }
  117. // Client represents a client configuration for Xray inbounds with traffic limits and settings.
  118. type Client struct {
  119. ID string `json:"id,omitempty"` // Unique client identifier
  120. Security string `json:"security"` // Security method (e.g., "auto", "aes-128-gcm")
  121. Password string `json:"password,omitempty"` // Client password
  122. Flow string `json:"flow,omitempty"` // Flow control (XTLS)
  123. Auth string `json:"auth,omitempty"` // Auth password (Hysteria)
  124. Email string `json:"email"` // Client email identifier
  125. LimitIP int `json:"limitIp"` // IP limit for this client
  126. TotalGB int64 `json:"totalGB" form:"totalGB"` // Total traffic limit in GB
  127. ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` // Expiration timestamp
  128. Enable bool `json:"enable" form:"enable"` // Whether the client is enabled
  129. TgID int64 `json:"tgId" form:"tgId"` // Telegram user ID for notifications
  130. SubID string `json:"subId" form:"subId"` // Subscription identifier
  131. Comment string `json:"comment" form:"comment"` // Client comment
  132. Reset int `json:"reset" form:"reset"` // Reset period in days
  133. CreatedAt int64 `json:"created_at,omitempty"` // Creation timestamp
  134. UpdatedAt int64 `json:"updated_at,omitempty"` // Last update timestamp
  135. }