model.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. )
  21. // User represents a user account in the 3x-ui panel.
  22. type User struct {
  23. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  24. Username string `json:"username"`
  25. Password string `json:"password"`
  26. }
  27. // Inbound represents an Xray inbound configuration with traffic statistics and settings.
  28. type Inbound struct {
  29. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"` // Unique identifier
  30. UserId int `json:"-"` // Associated user ID
  31. Up int64 `json:"up" form:"up"` // Upload traffic in bytes
  32. Down int64 `json:"down" form:"down"` // Download traffic in bytes
  33. Total int64 `json:"total" form:"total"` // Total traffic limit in bytes
  34. AllTime int64 `json:"allTime" form:"allTime" gorm:"default:0"` // All-time traffic usage
  35. Remark string `json:"remark" form:"remark"` // Human-readable remark
  36. Enable bool `json:"enable" form:"enable" gorm:"index:idx_enable_traffic_reset,priority:1"` // Whether the inbound is enabled
  37. ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` // Expiration timestamp
  38. TrafficReset string `json:"trafficReset" form:"trafficReset" gorm:"default:never;index:idx_enable_traffic_reset,priority:2"` // Traffic reset schedule
  39. LastTrafficResetTime int64 `json:"lastTrafficResetTime" form:"lastTrafficResetTime" gorm:"default:0"` // Last traffic reset timestamp
  40. ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"` // Client traffic statistics
  41. // Xray configuration fields
  42. Listen string `json:"listen" form:"listen"`
  43. Port int `json:"port" form:"port"`
  44. Protocol Protocol `json:"protocol" form:"protocol"`
  45. Settings string `json:"settings" form:"settings"`
  46. StreamSettings string `json:"streamSettings" form:"streamSettings"`
  47. Tag string `json:"tag" form:"tag" gorm:"unique"`
  48. Sniffing string `json:"sniffing" form:"sniffing"`
  49. }
  50. // OutboundTraffics tracks traffic statistics for Xray outbound connections.
  51. type OutboundTraffics struct {
  52. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
  53. Tag string `json:"tag" form:"tag" gorm:"unique"`
  54. Up int64 `json:"up" form:"up" gorm:"default:0"`
  55. Down int64 `json:"down" form:"down" gorm:"default:0"`
  56. Total int64 `json:"total" form:"total" gorm:"default:0"`
  57. }
  58. // InboundClientIps stores IP addresses associated with inbound clients for access control.
  59. type InboundClientIps struct {
  60. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  61. ClientEmail string `json:"clientEmail" form:"clientEmail" gorm:"unique"`
  62. Ips string `json:"ips" form:"ips"`
  63. }
  64. // HistoryOfSeeders tracks which database seeders have been executed to prevent re-running.
  65. type HistoryOfSeeders struct {
  66. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  67. SeederName string `json:"seederName"`
  68. }
  69. // GenXrayInboundConfig generates an Xray inbound configuration from the Inbound model.
  70. func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
  71. listen := i.Listen
  72. if listen != "" {
  73. listen = fmt.Sprintf("\"%v\"", listen)
  74. }
  75. return &xray.InboundConfig{
  76. Listen: json_util.RawMessage(listen),
  77. Port: i.Port,
  78. Protocol: string(i.Protocol),
  79. Settings: json_util.RawMessage(i.Settings),
  80. StreamSettings: json_util.RawMessage(i.StreamSettings),
  81. Tag: i.Tag,
  82. Sniffing: json_util.RawMessage(i.Sniffing),
  83. }
  84. }
  85. // Setting stores key-value configuration settings for the 3x-ui panel.
  86. type Setting struct {
  87. Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
  88. Key string `json:"key" form:"key"`
  89. Value string `json:"value" form:"value"`
  90. }
  91. // Client represents a client configuration for Xray inbounds with traffic limits and settings.
  92. type Client struct {
  93. ID string `json:"id"` // Unique client identifier
  94. Security string `json:"security"` // Security method (e.g., "auto", "aes-128-gcm")
  95. Password string `json:"password"` // Client password
  96. Flow string `json:"flow"` // Flow control (XTLS)
  97. Email string `json:"email"` // Client email identifier
  98. LimitIP int `json:"limitIp"` // IP limit for this client
  99. TotalGB int64 `json:"totalGB" form:"totalGB"` // Total traffic limit in GB
  100. ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` // Expiration timestamp
  101. Enable bool `json:"enable" form:"enable"` // Whether the client is enabled
  102. TgID int64 `json:"tgId" form:"tgId"` // Telegram user ID for notifications
  103. SubID string `json:"subId" form:"subId"` // Subscription identifier
  104. Comment string `json:"comment" form:"comment"` // Client comment
  105. Reset int `json:"reset" form:"reset"` // Reset period in days
  106. CreatedAt int64 `json:"created_at,omitempty"` // Creation timestamp
  107. UpdatedAt int64 `json:"updated_at,omitempty"` // Last update timestamp
  108. }
  109. // VLESSSettings contains VLESS protocol-specific configuration settings.
  110. type VLESSSettings struct {
  111. Clients []Client `json:"clients"` // List of VLESS clients
  112. Decryption string `json:"decryption"` // Decryption method
  113. Encryption string `json:"encryption"` // Encryption method (usually "none" for VLESS)
  114. Fallbacks []any `json:"fallbacks"` // Fallback configurations
  115. }