forked from zhanggf2010/v2ray-ssrpanel-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
111 lines (91 loc) · 1.98 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ssrpanel
import (
"time"
"github.com/jinzhu/gorm"
)
type UserModel struct {
ID uint
VmessID string
Email string `gorm:"column:username"`
Port int
}
func (*UserModel) TableName() string {
return "user"
}
type UserTrafficLog struct {
ID uint `gorm:"primary_key"`
UserID uint
Uplink uint64 `gorm:"column:u"`
Downlink uint64 `gorm:"column:d"`
NodeID uint
Rate float64
Traffic string
LogTime int64
}
func (l *UserTrafficLog) BeforeCreate(scope *gorm.Scope) error {
l.LogTime = time.Now().Unix()
return nil
}
type NodeOnlineLog struct {
ID uint `gorm:"primary_key"`
NodeID uint
OnlineUser int
LogTime int64
}
func (*NodeOnlineLog) TableName() string {
return "ss_node_online_log"
}
func (l *NodeOnlineLog) BeforeCreate(scope *gorm.Scope) error {
l.LogTime = time.Now().Unix()
return nil
}
type NodeIP struct {
ID uint `gorm:"primary_key"`
NodeID uint
UserID uint
Port int
IPList string `gorm:"column:ip"`
CreatedAt int64
}
func (*NodeIP) TableName() string {
return "ss_node_ip"
}
func (n *NodeIP) BeforeCreate(scope *gorm.Scope) error {
n.CreatedAt = time.Now().Unix()
return nil
}
type NodeInfo struct {
ID uint `gorm:"primary_key"`
NodeID uint
Uptime time.Duration
Load string
LogTime int64
}
func (*NodeInfo) TableName() string {
return "ss_node_info"
}
func (l *NodeInfo) BeforeCreate(scope *gorm.Scope) error {
l.LogTime = time.Now().Unix()
return nil
}
type Node struct {
ID uint `gorm:"primary_key"`
TrafficRate float64
}
func (*Node) TableName() string {
return "ss_node"
}
type DB struct {
DB *gorm.DB
RetryTimes int64
}
func (db *DB) GetAllUsers() ([]UserModel, error) {
users := make([]UserModel, 0)
err := db.DB.Select("id, vmess_id, username, port").Where("enable = 1 AND u + d < transfer_enable").Find(&users).Error
return users, err
}
func (db *DB) GetNode(id uint) (*Node, error) {
node := Node{}
err := db.DB.First(&node, id).Error
return &node, err
}