:toda:
This commit is contained in:
25
service/base.go
Normal file
25
service/base.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package service
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
func Paginate(current, size int64) func(db *gorm.DB) *gorm.DB {
|
||||
// 如果页码是-1,就不分页
|
||||
if current == -1 {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
return db
|
||||
}
|
||||
}
|
||||
// 分页
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if current == 0 {
|
||||
current = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
// 计算偏移量
|
||||
offset := (current - 1) * size
|
||||
// 返回组装结果
|
||||
return db.Offset(int(offset)).Limit(int(size))
|
||||
}
|
||||
}
|
171
service/client.go
Normal file
171
service/client.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
gdb "wireguard-ui/global/client"
|
||||
"wireguard-ui/http/param"
|
||||
"wireguard-ui/http/vo"
|
||||
"wireguard-ui/model"
|
||||
"wireguard-ui/utils"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func Client() client {
|
||||
return client{
|
||||
gdb.DB,
|
||||
}
|
||||
}
|
||||
|
||||
// SaveClient
|
||||
// @description: 新增/编辑客户端
|
||||
// @receiver s
|
||||
// @param p
|
||||
// @param loginUser
|
||||
// @return error
|
||||
func (s client) SaveClient(p param.SaveClient, loginUser *vo.User) error {
|
||||
serverConf, err := Setting().GetWGServerForConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 对客户端IP做格式校验
|
||||
for _, cip := range p.IpAllocation {
|
||||
if !utils.Network().IPContains(serverConf.Address, cip) {
|
||||
return fmt.Errorf("客户端IP[%s]不符合定义", cip)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理一下endpoint
|
||||
if p.Endpoint == "" {
|
||||
globalConf, err := Setting().GetWGSetForConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.Endpoint = fmt.Sprintf("%s:%d", globalConf.EndpointAddress, serverConf.ListenPort)
|
||||
}
|
||||
|
||||
keys, _ := json.Marshal(p.Keys)
|
||||
|
||||
ent := &model.Client{
|
||||
Base: model.Base{
|
||||
Id: p.Id,
|
||||
},
|
||||
Name: p.Name,
|
||||
Email: p.Email,
|
||||
SubnetRange: p.SubnetRange,
|
||||
IpAllocation: strings.Join(p.IpAllocation, ","),
|
||||
AllowedIps: strings.Join(p.AllowedIps, ","),
|
||||
ExtraAllowedIps: strings.Join(p.ExtraAllowedIps, ","),
|
||||
Endpoint: p.Endpoint,
|
||||
UseServerDns: *p.UseServerDns,
|
||||
Keys: string(keys),
|
||||
UserId: loginUser.Id,
|
||||
Enabled: *p.Enabled,
|
||||
OfflineMonitoring: *p.OfflineMonitoring,
|
||||
}
|
||||
|
||||
// 编辑
|
||||
if p.Id != "" {
|
||||
return s.Model(&model.Client{}).Select("id", "name", "email", "subnet_range",
|
||||
"ip_allocation", "allowed_ips",
|
||||
"extra_allowed_ips", "endpoint",
|
||||
"use_server_dns", "keys", "user_id", "enabled",
|
||||
"offline_monitoring").
|
||||
Where("id = ?", ent.Id).Updates(&ent).Error
|
||||
}
|
||||
|
||||
// 如果是新增,判断这个客户端IP是否已经存在过了
|
||||
var count int64
|
||||
if err := s.Model(&model.Client{}).Where("ip_allocation = ?", strings.Join(p.IpAllocation, ",")).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return errors.New("当前IP客户端已经存在")
|
||||
}
|
||||
|
||||
// 新增
|
||||
return s.Model(&model.Client{}).Create(&ent).Error
|
||||
}
|
||||
|
||||
// Delete
|
||||
// @description: 删除
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @return error
|
||||
func (s client) Delete(id string) error {
|
||||
return s.Model(&model.Client{}).Where("id = ?", id).Delete(&model.Client{}).Error
|
||||
}
|
||||
|
||||
// List
|
||||
// @description: 客户端分页列表
|
||||
// @receiver s
|
||||
// @param p
|
||||
// @return data
|
||||
// @return total
|
||||
// @return err
|
||||
func (s client) List(p param.ClientList) (data []vo.ClientItem, total int64, err error) {
|
||||
sel := s.Table("t_client as tc").Scopes(Paginate(p.Current, p.Size)).
|
||||
Joins("LEFT JOIN t_user as tu ON tu.id = tc.user_id").
|
||||
Select("tc.id,tc.name,tc.email,tc.ip_allocation as ip_allocation_str,"+
|
||||
"tc.allowed_ips as allowed_ips_str,tc.extra_allowed_ips as extra_allowed_ips_str,"+
|
||||
"tc.endpoint,tc.use_server_dns,tc.keys as keys_str,tu.nickname as create_user,"+
|
||||
"tc.enabled,tc.offline_monitoring,"+
|
||||
"tc.created_at", "tc.updated_at")
|
||||
if p.Enabled != nil {
|
||||
sel.Where("tc.enabled = ?", *p.Enabled)
|
||||
}
|
||||
if p.Name != "" {
|
||||
sel.Where("tc.name like ?", "%"+p.Name+"%")
|
||||
}
|
||||
if p.Email != "" {
|
||||
sel.Where("tc.email like ?", "%"+p.Email+"%")
|
||||
}
|
||||
if p.IpAllocation != "" {
|
||||
sel.Where("tc.ip_allocation like ?", "%"+p.IpAllocation+"%")
|
||||
}
|
||||
err = sel.Order("tc.created_at DESC").Find(&data).Limit(-1).Offset(-1).Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i, v := range data {
|
||||
if v.KeysStr != "" {
|
||||
_ = json.Unmarshal([]byte(v.KeysStr), &data[i].Keys)
|
||||
}
|
||||
if v.IpAllocationStr != "" {
|
||||
data[i].IpAllocation = strings.Split(v.IpAllocationStr, ",")
|
||||
}
|
||||
if v.AllowedIpsStr != "" {
|
||||
data[i].AllowedIps = strings.Split(v.AllowedIpsStr, ",")
|
||||
} else {
|
||||
data[i].AllowedIps = []string{}
|
||||
}
|
||||
if v.ExtraAllowedIpsStr != "" {
|
||||
data[i].ExtraAllowedIps = strings.Split(v.ExtraAllowedIpsStr, ",")
|
||||
} else {
|
||||
data[i].ExtraAllowedIps = []string{}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetByID
|
||||
// @description: 通过ID获取客户端
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @return data
|
||||
// @return err
|
||||
func (s client) GetByID(id string) (data *model.Client, err error) {
|
||||
err = s.Model(&model.Client{}).Where("id = ?", id).Take(&data).Error
|
||||
return
|
||||
}
|
74
service/setting.go
Normal file
74
service/setting.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gorm.io/gorm"
|
||||
gdb "wireguard-ui/global/client"
|
||||
"wireguard-ui/model"
|
||||
"wireguard-ui/template/render_data"
|
||||
)
|
||||
|
||||
type setting struct{ *gorm.DB }
|
||||
|
||||
func Setting() setting {
|
||||
return setting{gdb.DB}
|
||||
}
|
||||
|
||||
func (s setting) SetData(data *model.Setting) error {
|
||||
// 判断code是否已经存在
|
||||
var count int64
|
||||
if err := s.Model(&model.Setting{}).Where("code = ?", data.Code).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 存在就更新,反之新增
|
||||
if count > 0 {
|
||||
return s.Save(&data).Error
|
||||
}
|
||||
|
||||
return s.Create(&data).Error
|
||||
}
|
||||
|
||||
// GetWGSetForConfig
|
||||
// @description: 获取全局配置
|
||||
// @receiver s
|
||||
// @return data
|
||||
// @return err
|
||||
func (s setting) GetWGSetForConfig() (data *render_data.ServerSetting, err error) {
|
||||
var rs *model.Setting
|
||||
if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SETTING").Take(&rs).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(rs.Data), &data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetWGServerForConfig
|
||||
// @description: 获取wireguard服务端配置为了渲染数据
|
||||
// @receiver s
|
||||
// @return render_data
|
||||
// @return err
|
||||
func (s setting) GetWGServerForConfig() (data *render_data.Server, err error) {
|
||||
var rs *model.Setting
|
||||
if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SERVER").Take(&rs).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(rs.Data), &data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取一下全局的服务配置
|
||||
gs, err := s.GetWGSetForConfig()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data.MTU = gs.MTU
|
||||
data.Table = gs.Table
|
||||
return
|
||||
}
|
126
service/user.go
Normal file
126
service/user.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
gdb "wireguard-ui/global/client"
|
||||
"wireguard-ui/global/constant"
|
||||
"wireguard-ui/http/param"
|
||||
"wireguard-ui/http/vo"
|
||||
"wireguard-ui/model"
|
||||
"wireguard-ui/utils"
|
||||
)
|
||||
|
||||
type user struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func User() user {
|
||||
return user{
|
||||
gdb.DB,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateUser
|
||||
// @description: 创建用户
|
||||
// @receiver s
|
||||
// @param user
|
||||
// @return err
|
||||
func (s user) CreateUser(user *model.User) (err error) {
|
||||
// 更新
|
||||
if user.Id != "" {
|
||||
updates := map[string]any{
|
||||
"nickname": user.Nickname,
|
||||
"avatar": user.Avatar,
|
||||
"contact": user.Contact,
|
||||
"is_admin": user.IsAdmin,
|
||||
"status": user.Status,
|
||||
}
|
||||
|
||||
return s.Model(&model.User{}).Where("id = ?", user.Id).Updates(&updates).Error
|
||||
}
|
||||
|
||||
defaultPassword := utils.Password().GenerateHashPassword("admin123")
|
||||
if user.Password == "" { // 没有密码给一个默认密码
|
||||
user.Password = defaultPassword
|
||||
} else {
|
||||
user.Password = utils.Password().GenerateHashPassword(user.Password)
|
||||
}
|
||||
|
||||
// 没有头像就生成一个头像
|
||||
if user.Avatar == "" {
|
||||
user.Avatar, _ = utils.Avatar().GenerateAvatar(true)
|
||||
}
|
||||
|
||||
// 创建
|
||||
return s.Create(&user).Error
|
||||
}
|
||||
|
||||
// Delete
|
||||
// @description: 删除用户
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @return error
|
||||
func (s user) Delete(id string) error {
|
||||
return s.Model(&model.User{}).Where("id = ?", id).Delete(&model.User{}).Error
|
||||
}
|
||||
|
||||
// List
|
||||
// @description: 用户列表
|
||||
// @receiver s
|
||||
// @param p
|
||||
// @return data
|
||||
// @return count
|
||||
// @return err
|
||||
func (s user) List(p param.Page) (data []vo.UserItem, count int64, err error) {
|
||||
if err = s.Model(&model.User{}).
|
||||
Scopes(Paginate(p.Current, p.Size)).
|
||||
Order("created_at DESC").
|
||||
Find(&data).
|
||||
Offset(-1).Limit(-1).
|
||||
Count(&count).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserByAccount
|
||||
// @description: 根据账户获取用户信息
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @return data
|
||||
// @return err
|
||||
func (s user) GetUserByAccount(account string) (data vo.User, err error) {
|
||||
err = s.Model(&model.User{}).Where("account = ?", account).Take(&data).Error
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserById
|
||||
// @description: 根据id获取用户信息
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @return data
|
||||
// @return err
|
||||
func (s user) GetUserById(id string) (data vo.User, err error) {
|
||||
err = s.Model(&model.User{}).Where("id = ?", id).Take(&data).Error
|
||||
return
|
||||
}
|
||||
|
||||
// Status
|
||||
// @description: 修改用户状态
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @return error
|
||||
func (s user) Status(id string, state constant.Status) error {
|
||||
return s.Model(&model.User{}).Where("id = ?", id).Update("status", state).Error
|
||||
}
|
||||
|
||||
// ChangePassword
|
||||
// @description: 修改密码
|
||||
// @receiver s
|
||||
// @param id
|
||||
// @param password
|
||||
// @return error
|
||||
func (s user) ChangePassword(id, password string) error {
|
||||
return s.Model(&model.User{}).Where("id = ?", id).Update("password", utils.Password().GenerateHashPassword(password)).Error
|
||||
}
|
Reference in New Issue
Block a user