Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'database')
-rw-r--r--database/db.go104
-rw-r--r--database/model/model.go81
2 files changed, 185 insertions, 0 deletions
diff --git a/database/db.go b/database/db.go
new file mode 100644
index 00000000..92fca27a
--- /dev/null
+++ b/database/db.go
@@ -0,0 +1,104 @@
+package database
+
+import (
+ "gorm.io/driver/sqlite"
+ "gorm.io/gorm"
+ "gorm.io/gorm/logger"
+ "io/fs"
+ "os"
+ "path"
+ "x-ui/config"
+ "x-ui/xray"
+ "x-ui/database/model"
+)
+
+var db *gorm.DB
+
+func initUser() error {
+ err := db.AutoMigrate(&model.User{})
+ if err != nil {
+ return err
+ }
+ var count int64
+ err = db.Model(&model.User{}).Count(&count).Error
+ if err != nil {
+ return err
+ }
+ if count == 0 {
+ user := &model.User{
+ Username: "admin",
+ Password: "admin",
+ }
+ return db.Create(user).Error
+ }
+ return nil
+}
+
+func initInbound() error {
+ return db.AutoMigrate(&model.Inbound{})
+}
+
+func initSetting() error {
+ return db.AutoMigrate(&model.Setting{})
+}
+func initInboundClientIps() error {
+ return db.AutoMigrate(&model.InboundClientIps{})
+}
+func initClientTraffic() error {
+ return db.AutoMigrate(&xray.ClientTraffic{})
+}
+
+func InitDB(dbPath string) error {
+ dir := path.Dir(dbPath)
+ err := os.MkdirAll(dir, fs.ModeDir)
+ if err != nil {
+ return err
+ }
+
+ var gormLogger logger.Interface
+
+ if config.IsDebug() {
+ gormLogger = logger.Default
+ } else {
+ gormLogger = logger.Discard
+ }
+
+ c := &gorm.Config{
+ Logger: gormLogger,
+ }
+ db, err = gorm.Open(sqlite.Open(dbPath), c)
+ if err != nil {
+ return err
+ }
+
+ err = initUser()
+ if err != nil {
+ return err
+ }
+ err = initInbound()
+ if err != nil {
+ return err
+ }
+ err = initSetting()
+ if err != nil {
+ return err
+ }
+ err = initInboundClientIps()
+ if err != nil {
+ return err
+ }
+ err = initClientTraffic()
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func GetDB() *gorm.DB {
+ return db
+}
+
+func IsNotFound(err error) bool {
+ return err == gorm.ErrRecordNotFound
+}
diff --git a/database/model/model.go b/database/model/model.go
new file mode 100644
index 00000000..30e348be
--- /dev/null
+++ b/database/model/model.go
@@ -0,0 +1,81 @@
+package model
+
+import (
+ "fmt"
+ "x-ui/util/json_util"
+ "x-ui/xray"
+)
+
+type Protocol string
+
+const (
+ VMess Protocol = "vmess"
+ VLESS Protocol = "vless"
+ Dokodemo Protocol = "Dokodemo-door"
+ Http Protocol = "http"
+ Trojan Protocol = "trojan"
+ Shadowsocks Protocol = "shadowsocks"
+)
+
+type User struct {
+ Id int `json:"id" gorm:"primaryKey;autoIncrement"`
+ Username string `json:"username"`
+ Password string `json:"password"`
+}
+
+type Inbound struct {
+ Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
+ UserId int `json:"-"`
+ Up int64 `json:"up" form:"up"`
+ Down int64 `json:"down" form:"down"`
+ Total int64 `json:"total" form:"total"`
+ Remark string `json:"remark" form:"remark"`
+ Enable bool `json:"enable" form:"enable"`
+ ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
+ ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"`
+
+ // config part
+ Listen string `json:"listen" form:"listen"`
+ Port int `json:"port" form:"port" gorm:"unique"`
+ Protocol Protocol `json:"protocol" form:"protocol"`
+ Settings string `json:"settings" form:"settings"`
+ StreamSettings string `json:"streamSettings" form:"streamSettings"`
+ Tag string `json:"tag" form:"tag" gorm:"unique"`
+ Sniffing string `json:"sniffing" form:"sniffing"`
+}
+type InboundClientIps struct {
+ Id int `json:"id" gorm:"primaryKey;autoIncrement"`
+ ClientEmail string `json:"clientEmail" form:"clientEmail" gorm:"unique"`
+ Ips string `json:"ips" form:"ips"`
+}
+
+func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
+ listen := i.Listen
+ if listen != "" {
+ listen = fmt.Sprintf("\"%v\"", listen)
+ }
+ return &xray.InboundConfig{
+ Listen: json_util.RawMessage(listen),
+ Port: i.Port,
+ Protocol: string(i.Protocol),
+ Settings: json_util.RawMessage(i.Settings),
+ StreamSettings: json_util.RawMessage(i.StreamSettings),
+ Tag: i.Tag,
+ Sniffing: json_util.RawMessage(i.Sniffing),
+ }
+}
+
+type Setting struct {
+ Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
+ Key string `json:"key" form:"key"`
+ Value string `json:"value" form:"value"`
+}
+type Client struct {
+ ID string `json:"id"`
+ AlterIds uint16 `json:"alterId"`
+ Email string `json:"email"`
+ LimitIP int `json:"limitIp"`
+ Security string `json:"security"`
+ TotalGB int64 `json:"totalGB" form:"totalGB"`
+ ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
+} \ No newline at end of file