Skip to content

Commit

Permalink
完成xray启动
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxilu committed May 27, 2021
1 parent 56ed8f3 commit 3cd25ce
Show file tree
Hide file tree
Showing 42 changed files with 3,625 additions and 227 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
tmp
bin/xray-darwin-arm64
bin/xray-darwin-arm64
bin/config.json
Empty file modified bin/geoip.dat
100644 → 100755
Empty file.
Empty file modified bin/geosite.dat
100644 → 100755
Empty file.
Binary file added bin/xray-linux-arm64
Binary file not shown.
40 changes: 13 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "fmt"
import (
"fmt"
"os"
)

type LogLevel string

Expand All @@ -11,10 +14,6 @@ const (
Error LogLevel = "error"
)

func init() {

}

func GetVersion() string {
return "0.0.1"
}
Expand All @@ -23,34 +22,21 @@ func GetName() string {
return "x-ui"
}

func GetListen() string {
return ":27827"
}

func GetCertFile() string {
return ""
}

func GetKeyFile() string {
return ""
}

func GetLogLevel() LogLevel {
return Debug
if IsDebug() {
return Debug
}
logLevel := os.Getenv("XUI_LOG_LEVEL")
if logLevel == "" {
return Info
}
return LogLevel(logLevel)
}

func IsDebug() bool {
return true
}

func GetSecret() []byte {
return []byte("")
return os.Getenv("XUI_DEBUG") == "true"
}

func GetDBPath() string {
return fmt.Sprintf("/etc/%s/%s.db", GetName(), GetName())
}

func GetBasePath() string {
return "/"
}
28 changes: 26 additions & 2 deletions database/db.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package database

import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"io/fs"
"os"
"path"
"x-ui/config"
"x-ui/database/model"
)
import "gorm.io/driver/sqlite"

var db *gorm.DB

Expand Down Expand Up @@ -35,14 +37,28 @@ func initInbound() error {
return db.AutoMigrate(&model.Inbound{})
}

func initSetting() error {
return db.AutoMigrate(&model.Setting{})
}

func InitDB(dbPath string) error {
dir := path.Dir(dbPath)
err := os.MkdirAll(dir, fs.ModeDir)
if err != nil {
return err
}

c := &gorm.Config{}
var gormLogger logger.Interface

if config.IsDebug() {
gormLogger = logger.Discard
} else {
gormLogger = logger.Default
}

c := &gorm.Config{
Logger: gormLogger,
}
db, err = gorm.Open(sqlite.Open(dbPath), c)
if err != nil {
return err
Expand All @@ -56,10 +72,18 @@ func InitDB(dbPath string) error {
if err != nil {
return err
}
err = initSetting()
if err != nil {
return err
}

return nil
}

func GetDB() *gorm.DB {
return db
}

func IsNotFound(err error) bool {
return err == gorm.ErrRecordNotFound
}
51 changes: 36 additions & 15 deletions database/model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package model

import "time"
import (
"encoding/json"
"x-ui/xray"
)

type Protocol string

Expand All @@ -20,20 +23,38 @@ type User struct {
}

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"`
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
UserId int `json:"user_id" form:"user_id"`
Up int64 `json:"up" form:"up"`
Down int64 `json:"down" form:"down"`
Remark string `json:"remark" form:"remark"`
Enable bool `json:"enable" form:"enable"`
ExpiryTime int64 `json:"expiry_time" form:"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"`
Listen string `json:"listen" form:"listen"`
Port int `json:"port" form:"port"`
Protocol Protocol `json:"protocol" form:"protocol"`
Settings string `json:"settings" form:"settings"`
StreamSettings string `json:"stream_settings" form:"stream_settings"`
Tag string `json:"tag" form:"tag"`
Sniffing string `json:"sniffing" form:"sniffing"`
}

func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
return &xray.InboundConfig{
Listen: i.Listen,
Port: i.Port,
Protocol: string(i.Protocol),
Settings: json.RawMessage(i.Settings),
StreamSettings: json.RawMessage(i.StreamSettings),
Tag: i.Tag,
Sniffing: json.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"`
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module x-ui
go 1.16

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/fatih/color v1.11.0 // indirect
github.com/Workiva/go-datastructures v1.0.53
github.com/gin-contrib/sessions v0.0.3
github.com/gin-gonic/gin v1.7.1
github.com/go-ole/go-ole v1.2.5 // indirect
Expand All @@ -14,8 +14,9 @@ require (
github.com/pelletier/go-toml v1.9.1 // indirect
github.com/shirou/gopsutil v3.21.3+incompatible
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/xtls/xray-core v1.4.2
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/text v0.3.6
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.9
Expand Down
Loading

0 comments on commit 3cd25ce

Please sign in to comment.