first commit

This commit is contained in:
sprov
2021-05-18 12:59:22 +08:00
parent bc6a518c9a
commit 56ed8f355c
62 changed files with 63166 additions and 0 deletions

65
database/db.go Normal file
View File

@@ -0,0 +1,65 @@
package database
import (
"gorm.io/gorm"
"io/fs"
"os"
"path"
"x-ui/database/model"
)
import "gorm.io/driver/sqlite"
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 InitDB(dbPath string) error {
dir := path.Dir(dbPath)
err := os.MkdirAll(dir, fs.ModeDir)
if err != nil {
return err
}
c := &gorm.Config{}
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
}
return nil
}
func GetDB() *gorm.DB {
return db
}

39
database/model/model.go Normal file
View File

@@ -0,0 +1,39 @@
package model
import "time"
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" gorm:"primaryKey;autoIncrement"`
UserId int `json:"user_id"`
Up int64 `json:"up"`
Down int64 `json:"down"`
Remark string `json:"remark"`
Enable bool `json:"enable"`
ExpiryTime time.Time `json:"expiry_time"`
// config part
Listen string `json:"listen"`
Port int `json:"port"`
Protocol Protocol `json:"protocol"`
Settings string `json:"settings"`
StreamSettings string `json:"stream_settings"`
Tag string `json:"tag"`
Sniffing string `json:"sniffing"`
}