From 15cfa17a5e66c521bdee9d6a359422d7d7172563 Mon Sep 17 00:00:00 2001 From: coward Date: Thu, 7 Mar 2024 17:32:38 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compoment/wireguard.go | 15 ---- {compoment => component}/captcha_store.go | 2 +- {compoment => component}/jwt.go | 2 +- component/wireguard.go | 33 ++++++++ http/api/captcha.go | 4 +- http/api/user.go | 8 +- logs/runtime-hyxc.log | 96 +++++++++++++++++++++++ middleware/authorization.go | 4 +- script/db_migrate.go | 9 +++ template/wg.conf | 25 ++++-- utils/template.go | 53 +++++++++++++ wg0.conf | 10 +++ 12 files changed, 228 insertions(+), 33 deletions(-) delete mode 100644 compoment/wireguard.go rename {compoment => component}/captcha_store.go (98%) rename {compoment => component}/jwt.go (99%) create mode 100644 component/wireguard.go create mode 100644 utils/template.go create mode 100644 wg0.conf diff --git a/compoment/wireguard.go b/compoment/wireguard.go deleted file mode 100644 index fab91ae..0000000 --- a/compoment/wireguard.go +++ /dev/null @@ -1,15 +0,0 @@ -package compoment - -type wireguard struct{} - -func Wireguard() wireguard { - return wireguard{} -} - -// Apply -// @description: 应用配置 -// @receiver wireguard -// @return err -func (wireguard) Apply() (err error) { - return nil -} diff --git a/compoment/captcha_store.go b/component/captcha_store.go similarity index 98% rename from compoment/captcha_store.go rename to component/captcha_store.go index caffe4b..705223e 100644 --- a/compoment/captcha_store.go +++ b/component/captcha_store.go @@ -1,4 +1,4 @@ -package compoment +package component import ( "context" diff --git a/compoment/jwt.go b/component/jwt.go similarity index 99% rename from compoment/jwt.go rename to component/jwt.go index 0149abf..6eb3309 100644 --- a/compoment/jwt.go +++ b/component/jwt.go @@ -1,4 +1,4 @@ -package compoment +package component import ( "context" diff --git a/component/wireguard.go b/component/wireguard.go new file mode 100644 index 0000000..9244695 --- /dev/null +++ b/component/wireguard.go @@ -0,0 +1,33 @@ +package component + +import ( + "gitee.ltd/lxh/logger/log" + "wireguard-dashboard/utils" +) + +type wireguard struct{} + +func Wireguard() wireguard { + return wireguard{} +} + +// Apply +// @description: 应用配置 +// @receiver wireguard +// @return err +func (wireguard) Apply(templateFilePath, configFilePath string, data any) (err error) { + + parseTemplate, err := utils.Template().Parse(templateFilePath) + if err != nil { + log.Errorf("解析模板信息失败") + return err + } + + err = utils.Template().Execute(parseTemplate, data, configFilePath) + if err != nil { + log.Errorf("应用配置失败: %v", err.Error()) + return err + } + + return nil +} diff --git a/http/api/captcha.go b/http/api/captcha.go index 471f615..bc19f82 100644 --- a/http/api/captcha.go +++ b/http/api/captcha.go @@ -3,7 +3,7 @@ package api import ( "github.com/gin-gonic/gin" "github.com/mojocn/base64Captcha" - "wireguard-dashboard/compoment" + "wireguard-dashboard/component" "wireguard-dashboard/utils" ) @@ -21,7 +21,7 @@ func (captcha) GenerateCaptcha(c *gin.Context) { math := base64Captcha.DriverMath{Height: 60, Width: 240, Fonts: []string{"ApothecaryFont.ttf", "3Dumb.ttf"}} mathDriver := math.ConvertFonts() - capt := base64Captcha.NewCaptcha(mathDriver, compoment.CaptchaStore{}) + capt := base64Captcha.NewCaptcha(mathDriver, component.CaptchaStore{}) id, base64Str, _, err := capt.Generate() if err != nil { diff --git a/http/api/user.go b/http/api/user.go index 90116b6..3ae283b 100644 --- a/http/api/user.go +++ b/http/api/user.go @@ -4,7 +4,7 @@ import ( "gitee.ltd/lxh/logger/log" "github.com/gin-gonic/gin" "wireguard-dashboard/client" - "wireguard-dashboard/compoment" + "wireguard-dashboard/component" "wireguard-dashboard/constant" "wireguard-dashboard/http/param" "wireguard-dashboard/model/entity" @@ -31,7 +31,7 @@ func (user) Login(c *gin.Context) { } // 校验验证码 - pass := compoment.CaptchaStore{}.Verify(p.CaptchaId, p.CaptchaAnswer, true) + pass := component.CaptchaStore{}.Verify(p.CaptchaId, p.CaptchaAnswer, true) if !pass { utils.GinResponse(c).FailedWithMsg("验证码错误") return @@ -56,7 +56,7 @@ func (user) Login(c *gin.Context) { } // 生成token - token, err := compoment.JWT().GenerateToken(user.Id) + token, err := component.JWT().GenerateToken(user.Id) if err != nil { utils.GinResponse(c).FailedWithMsg("登陆失败") return @@ -78,7 +78,7 @@ func (user) Logout(c *gin.Context) { utils.GinResponse(c).FailedWithMsg("你还没有登陆") return } - if err := compoment.JWT().Logout(data.(*entity.User).Id); err != nil { + if err := component.JWT().Logout(data.(*entity.User).Id); err != nil { log.Errorf("退出登陆失败: %v", err.Error()) utils.GinResponse(c).FailedWithMsg("退出登陆失败") return diff --git a/logs/runtime-hyxc.log b/logs/runtime-hyxc.log index 4230113..713f8b8 100644 --- a/logs/runtime-hyxc.log +++ b/logs/runtime-hyxc.log @@ -737,3 +737,99 @@ [2024-03-07 16:48:03.623] ERROR repository/system.go:28 no such table: t_setting -> SELECT * FROM `t_setting` WHERE code = "SERVER_SETTING" ORDER BY `t_setting`.`id` LIMIT 1 [2024-03-07 16:48:03.624] ERROR repository/system.go:59 no such table: t_setting -> INSERT INTO `t_setting` (`id`,`created_at`,`updated_at`,`code`,`data`,`describe`) VALUES ("34ef92b3423a4ac1a20535bafe2f759f","2024-03-07 16:48:03","2024-03-07 16:48:03","SERVER_SETTING","{""MTU"":1450,""configFilePath"":""/etc/wireguard/wg0.conf"",""dnsServer"":""10.10.10.1/24"",""endpointAddress"":""222.183.239.10"",""firewallMark"":"""",""persistentKeepalive"":15,""table"":""""}","服务端全局配置") [2024-03-07 16:48:03.624] ERROR script/db_migrate.go:30 no such table: t_setting +[2024-03-07 16:48:38.627] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_user" +[2024-03-07 16:48:38.640] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_user" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 16:48:38.641] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_user` LIMIT 1 +[2024-03-07 16:48:38.641] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 16:48:38.641] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_user" AND name = "t_user" +[2024-03-07 16:48:38.642] INFO sqlite@v1.5.5/migrator.go:413 [1.0448ms] [rows:0] CREATE TABLE `t_user__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`avatar` varchar(255) NOT NULL,`name` varchar(50) NOT NULL,`account` varchar(50) NOT NULL,`email` varchar(255),`password` varchar(255) NOT NULL,`is_admin` int(1) NOT NULL,`status` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) +[2024-03-07 16:48:38.643] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] INSERT INTO `t_user__temp`(`id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status`) SELECT `id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status` FROM `t_user` +[2024-03-07 16:48:38.643] INFO sqlite@v1.5.5/migrator.go:423 [522.6µs] [rows:1] DROP TABLE `t_user` +[2024-03-07 16:48:38.644] INFO sqlite@v1.5.5/migrator.go:423 [1.0629ms] [rows:1] ALTER TABLE `t_user__temp` RENAME TO `t_user` +[2024-03-07 16:48:38.646] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 16:48:38.646] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_user" AND name = "t_user" +[2024-03-07 16:48:38.647] INFO sqlite@v1.5.5/migrator.go:413 [523.7µs] [rows:1] CREATE TABLE `t_user__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`avatar` varchar(255) NOT NULL,`name` varchar(50) NOT NULL,`account` varchar(50) NOT NULL,`email` varchar(255),`password` varchar(255) NOT NULL,`is_admin` int(1) NOT NULL,`status` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) +[2024-03-07 16:48:38.647] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] INSERT INTO `t_user__temp`(`id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status`) SELECT `id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status` FROM `t_user` +[2024-03-07 16:48:38.647] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] DROP TABLE `t_user` +[2024-03-07 16:48:38.648] INFO sqlite@v1.5.5/migrator.go:423 [528µs] [rows:1] ALTER TABLE `t_user__temp` RENAME TO `t_user` +[2024-03-07 16:48:38.650] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_wg_server" +[2024-03-07 16:48:38.651] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_wg_server" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 16:48:38.651] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_wg_server` LIMIT 1 +[2024-03-07 16:48:38.651] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 16:48:38.651] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_server" AND name = "t_wg_server" +[2024-03-07 16:48:38.652] INFO sqlite@v1.5.5/migrator.go:413 [597.3µs] [rows:1] CREATE TABLE `t_wg_server__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`ip_scope` varchar(30) NOT NULL,`listen_port` int(10) NOT NULL,`private_key` text NOT NULL,`public_key` text NOT NULL,`post_up_script` text,`pre_down_script` text,`post_down_script` text,PRIMARY KEY (`id`)) +[2024-03-07 16:48:38.652] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] INSERT INTO `t_wg_server__temp`(`id`,`created_at`,`updated_at`,`ip_scope`,`listen_port`,`private_key`,`public_key`,`post_up_script`,`pre_down_script`,`post_down_script`) SELECT `id`,`created_at`,`updated_at`,`ip_scope`,`listen_port`,`private_key`,`public_key`,`post_up_script`,`pre_down_script`,`post_down_script` FROM `t_wg_server` +[2024-03-07 16:48:38.652] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] DROP TABLE `t_wg_server` +[2024-03-07 16:48:38.654] INFO sqlite@v1.5.5/migrator.go:423 [1.1141ms] [rows:0] ALTER TABLE `t_wg_server__temp` RENAME TO `t_wg_server` +[2024-03-07 16:48:38.655] INFO sqlite@v1.5.5/migrator.go:32 [521.3µs] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_wg_client" +[2024-03-07 16:48:38.655] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_wg_client" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 16:48:38.656] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_wg_client` LIMIT 1 +[2024-03-07 16:48:38.656] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 16:48:38.656] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_client" AND name = "t_wg_client" +[2024-03-07 16:48:38.657] INFO sqlite@v1.5.5/migrator.go:413 [1.0444ms] [rows:0] CREATE TABLE `t_wg_client__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`server_id` varchar(36) NOT NULL,`name` varchar(100) NOT NULL,`email` varchar(100),`subnet_range` varchar(255),`ip_allocation` varchar(30) NOT NULL,`allowed_ips` varchar(30) NOT NULL,`extra_allowed_ips` varchar(30),`endpoint` varchar(255),`use_server_dns` int(1),`enable_after_creation` int(1),`keys` text,`user_id` char(36) NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_t_wg_server_clients` FOREIGN KEY (`server_id`) REFERENCES `t_wg_server`(`id`)) +[2024-03-07 16:48:38.657] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] INSERT INTO `t_wg_client__temp`(`id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id`) SELECT `id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id` FROM `t_wg_client` +[2024-03-07 16:48:38.658] INFO sqlite@v1.5.5/migrator.go:423 [554.7µs] [rows:0] DROP TABLE `t_wg_client` +[2024-03-07 16:48:38.659] INFO sqlite@v1.5.5/migrator.go:423 [1.062ms] [rows:0] ALTER TABLE `t_wg_client__temp` RENAME TO `t_wg_client` +[2024-03-07 16:48:38.661] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 16:48:38.661] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_client" AND name = "t_wg_client" +[2024-03-07 16:48:38.662] INFO sqlite@v1.5.5/migrator.go:413 [504.9µs] [rows:0] CREATE TABLE `t_wg_client__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`server_id` varchar(36) NOT NULL,`name` varchar(100) NOT NULL,`email` varchar(100),`subnet_range` varchar(255),`ip_allocation` varchar(30) NOT NULL,`allowed_ips` varchar(30) NOT NULL,`extra_allowed_ips` varchar(30),`endpoint` varchar(255),`use_server_dns` int(1),`enable_after_creation` int(1),`keys` text,`user_id` char(36) NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_t_wg_server_clients` FOREIGN KEY (`server_id`) REFERENCES `t_wg_server`(`id`)) +[2024-03-07 16:48:38.662] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] INSERT INTO `t_wg_client__temp`(`id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id`) SELECT `id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id` FROM `t_wg_client` +[2024-03-07 16:48:38.663] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] DROP TABLE `t_wg_client` +[2024-03-07 16:48:38.665] INFO sqlite@v1.5.5/migrator.go:423 [1.1174ms] [rows:0] ALTER TABLE `t_wg_client__temp` RENAME TO `t_wg_client` +[2024-03-07 16:48:38.667] INFO sqlite@v1.5.5/migrator.go:219 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_client" AND (sql LIKE "%CONSTRAINT ""fk_t_wg_server_clients"" %" OR sql LIKE "%CONSTRAINT fk_t_wg_server_clients %" OR sql LIKE "%CONSTRAINT `fk_t_wg_server_clients`%" OR sql LIKE "%CONSTRAINT [fk_t_wg_server_clients]%" OR sql LIKE "%CONSTRAINT fk_t_wg_server_clients %") +[2024-03-07 16:48:38.667] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_setting" +[2024-03-07 16:48:38.669] INFO migrator/migrator.go:298 [2.7103ms] [rows:0] CREATE TABLE `t_setting` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`code` char(20) NOT NULL,`data` text NOT NULL,`describe` text,PRIMARY KEY (`id`)) +[2024-03-07 16:48:38.670] INFO script/db_migrate.go:57 [0s] [rows:1] SELECT count(*) FROM `t_user` WHERE is_admin = 1 +[2024-03-07 16:48:38.670] INFO script/db_migrate.go:94 [0s] [rows:1] SELECT count(*) FROM `t_wg_server` +[2024-03-07 16:48:39.590] ERROR repository/system.go:28 record not found -> SELECT * FROM `t_setting` WHERE code = "SERVER_SETTING" ORDER BY `t_setting`.`id` LIMIT 1 +[2024-03-07 16:48:39.593] INFO repository/system.go:59 [2.3012ms] [rows:1] INSERT INTO `t_setting` (`id`,`created_at`,`updated_at`,`code`,`data`,`describe`) VALUES ("82d85b9e4b0845bd9f710ce08f121db2","2024-03-07 16:48:39","2024-03-07 16:48:39","SERVER_SETTING","{""MTU"":1450,""configFilePath"":""/etc/wireguard/wg0.conf"",""dnsServer"":""10.10.10.1/24"",""endpointAddress"":""222.183.239.10"",""firewallMark"":"""",""persistentKeepalive"":15,""table"":""""}","服务端全局配置") +[2024-03-07 16:48:39.596] INFO repository/server.go:40 [2.3348ms] [rows:1] INSERT INTO `t_wg_server` (`id`,`created_at`,`updated_at`,`ip_scope`,`listen_port`,`private_key`,`public_key`,`post_up_script`,`pre_down_script`,`post_down_script`) VALUES ("578d7372a8904761b68631f49d896afa","2024-03-07 16:48:39","2024-03-07 16:48:39","10.10.10.1/24",51820,"gHdFnW5z2VaNBptvIg+4XoeWdanN7KFXnmoL5URnFnA=","yUsxEjnfPsYyAA0xQkfBUe+C1HZifw5u8wdJgJgSlzw=","","","") +[2024-03-07 17:03:26.828] ERROR repository/user.go:44 record not found -> SELECT * FROM `t_user` WHERE id = "21b09f1bbff54512bed815f991fae7f8" ORDER BY `t_user`.`id` LIMIT 1 +[2024-03-07 17:03:38.999] INFO repository/user.go:55 [507.7µs] [rows:1] SELECT * FROM `t_user` WHERE account = "Admin" ORDER BY `t_user`.`id` LIMIT 1 +[2024-03-07 17:03:49.604] INFO repository/user.go:44 [792.8µs] [rows:1] SELECT * FROM `t_user` WHERE id = "0dbdde3660934396b79a6bad002d82a8" ORDER BY `t_user`.`id` LIMIT 1 +[2024-03-07 17:03:49.605] INFO repository/server.go:26 [0s] [rows:1] SELECT `t_wg_server`.`id`,`t_wg_server`.`ip_scope`,`t_wg_server`.`listen_port`,`t_wg_server`.`private_key`,`t_wg_server`.`public_key`,`t_wg_server`.`post_up_script`,`t_wg_server`.`pre_down_script`,`t_wg_server`.`post_down_script` FROM `t_wg_server` ORDER BY `t_wg_server`.`id` LIMIT 1 +[2024-03-07 17:30:39.214] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_user" +[2024-03-07 17:30:39.227] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_user" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 17:30:39.227] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_user` LIMIT 1 +[2024-03-07 17:30:39.229] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 17:30:39.229] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_user" AND name = "t_user" +[2024-03-07 17:30:39.231] INFO sqlite@v1.5.5/migrator.go:413 [1.0734ms] [rows:0] CREATE TABLE `t_user__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`avatar` varchar(255) NOT NULL,`name` varchar(50) NOT NULL,`account` varchar(50) NOT NULL,`email` varchar(255),`password` varchar(255) NOT NULL,`is_admin` int(1) NOT NULL,`status` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) +[2024-03-07 17:30:39.231] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] INSERT INTO `t_user__temp`(`id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status`) SELECT `id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status` FROM `t_user` +[2024-03-07 17:30:39.231] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] DROP TABLE `t_user` +[2024-03-07 17:30:39.232] INFO sqlite@v1.5.5/migrator.go:423 [1.4392ms] [rows:1] ALTER TABLE `t_user__temp` RENAME TO `t_user` +[2024-03-07 17:30:39.234] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 17:30:39.234] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_user" AND name = "t_user" +[2024-03-07 17:30:39.235] INFO sqlite@v1.5.5/migrator.go:413 [1.0459ms] [rows:1] CREATE TABLE `t_user__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`avatar` varchar(255) NOT NULL,`name` varchar(50) NOT NULL,`account` varchar(50) NOT NULL,`email` varchar(255),`password` varchar(255) NOT NULL,`is_admin` int(1) NOT NULL,`status` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) +[2024-03-07 17:30:39.235] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] INSERT INTO `t_user__temp`(`id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status`) SELECT `id`,`created_at`,`updated_at`,`avatar`,`name`,`account`,`email`,`password`,`is_admin`,`status` FROM `t_user` +[2024-03-07 17:30:39.236] INFO sqlite@v1.5.5/migrator.go:423 [1.0513ms] [rows:1] DROP TABLE `t_user` +[2024-03-07 17:30:39.237] INFO sqlite@v1.5.5/migrator.go:423 [945.7µs] [rows:1] ALTER TABLE `t_user__temp` RENAME TO `t_user` +[2024-03-07 17:30:39.240] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_wg_server" +[2024-03-07 17:30:39.240] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_wg_server" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 17:30:39.240] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_wg_server` LIMIT 1 +[2024-03-07 17:30:39.240] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 17:30:39.240] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_server" AND name = "t_wg_server" +[2024-03-07 17:30:39.241] INFO sqlite@v1.5.5/migrator.go:413 [0s] [rows:1] CREATE TABLE `t_wg_server__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`ip_scope` varchar(30) NOT NULL,`listen_port` int(10) NOT NULL,`private_key` text NOT NULL,`public_key` text NOT NULL,`post_up_script` text,`pre_down_script` text,`post_down_script` text,PRIMARY KEY (`id`)) +[2024-03-07 17:30:39.242] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] INSERT INTO `t_wg_server__temp`(`id`,`created_at`,`updated_at`,`ip_scope`,`listen_port`,`private_key`,`public_key`,`post_up_script`,`pre_down_script`,`post_down_script`) SELECT `id`,`created_at`,`updated_at`,`ip_scope`,`listen_port`,`private_key`,`public_key`,`post_up_script`,`pre_down_script`,`post_down_script` FROM `t_wg_server` +[2024-03-07 17:30:39.242] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:1] DROP TABLE `t_wg_server` +[2024-03-07 17:30:39.243] INFO sqlite@v1.5.5/migrator.go:423 [1.1021ms] [rows:1] ALTER TABLE `t_wg_server__temp` RENAME TO `t_wg_server` +[2024-03-07 17:30:39.245] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_wg_client" +[2024-03-07 17:30:39.245] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_wg_client" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 17:30:39.245] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_wg_client` LIMIT 1 +[2024-03-07 17:30:39.246] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 17:30:39.246] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_client" AND name = "t_wg_client" +[2024-03-07 17:30:39.247] INFO sqlite@v1.5.5/migrator.go:413 [1.0534ms] [rows:1] CREATE TABLE `t_wg_client__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`server_id` varchar(36) NOT NULL,`name` varchar(100) NOT NULL,`email` varchar(100),`subnet_range` varchar(255),`ip_allocation` varchar(30) NOT NULL,`allowed_ips` varchar(30) NOT NULL,`extra_allowed_ips` varchar(30),`endpoint` varchar(255),`use_server_dns` int(1),`enable_after_creation` int(1),`keys` text,`user_id` char(36) NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_t_wg_server_clients` FOREIGN KEY (`server_id`) REFERENCES `t_wg_server`(`id`)) +[2024-03-07 17:30:39.248] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] INSERT INTO `t_wg_client__temp`(`id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id`) SELECT `id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id` FROM `t_wg_client` +[2024-03-07 17:30:39.248] INFO sqlite@v1.5.5/migrator.go:423 [522µs] [rows:0] DROP TABLE `t_wg_client` +[2024-03-07 17:30:39.250] INFO sqlite@v1.5.5/migrator.go:423 [1.3497ms] [rows:0] ALTER TABLE `t_wg_client__temp` RENAME TO `t_wg_client` +[2024-03-07 17:30:39.252] INFO sqlite@v1.5.5/migrator.go:79 [0s] [rows:1] PRAGMA foreign_keys +[2024-03-07 17:30:39.252] INFO sqlite@v1.5.5/migrator.go:368 [0s] [-] SELECT sql FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_client" AND name = "t_wg_client" +[2024-03-07 17:30:39.253] INFO sqlite@v1.5.5/migrator.go:413 [576.8µs] [rows:0] CREATE TABLE `t_wg_client__temp` (`id` varchar(36) NOT NULL,`created_at` datetime,`updated_at` datetime,`server_id` varchar(36) NOT NULL,`name` varchar(100) NOT NULL,`email` varchar(100),`subnet_range` varchar(255),`ip_allocation` varchar(30) NOT NULL,`allowed_ips` varchar(30) NOT NULL,`extra_allowed_ips` varchar(30),`endpoint` varchar(255),`use_server_dns` int(1),`enable_after_creation` int(1),`keys` text,`user_id` char(36) NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_t_wg_server_clients` FOREIGN KEY (`server_id`) REFERENCES `t_wg_server`(`id`)) +[2024-03-07 17:30:39.253] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] INSERT INTO `t_wg_client__temp`(`id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id`) SELECT `id`,`created_at`,`updated_at`,`server_id`,`name`,`email`,`subnet_range`,`ip_allocation`,`allowed_ips`,`extra_allowed_ips`,`endpoint`,`use_server_dns`,`enable_after_creation`,`keys`,`user_id` FROM `t_wg_client` +[2024-03-07 17:30:39.253] INFO sqlite@v1.5.5/migrator.go:423 [0s] [rows:0] DROP TABLE `t_wg_client` +[2024-03-07 17:30:39.255] INFO sqlite@v1.5.5/migrator.go:423 [1.1166ms] [rows:0] ALTER TABLE `t_wg_client__temp` RENAME TO `t_wg_client` +[2024-03-07 17:30:39.257] INFO sqlite@v1.5.5/migrator.go:219 [51.9µs] [-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "t_wg_client" AND (sql LIKE "%CONSTRAINT ""fk_t_wg_server_clients"" %" OR sql LIKE "%CONSTRAINT fk_t_wg_server_clients %" OR sql LIKE "%CONSTRAINT `fk_t_wg_server_clients`%" OR sql LIKE "%CONSTRAINT [fk_t_wg_server_clients]%" OR sql LIKE "%CONSTRAINT fk_t_wg_server_clients %") +[2024-03-07 17:30:39.257] INFO sqlite@v1.5.5/migrator.go:32 [0s] [-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="t_setting" +[2024-03-07 17:30:39.257] INFO migrator/migrator.go:73 [0s] [rows:1] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "t_setting" AND sql IS NOT NULL order by type = "table" desc +[2024-03-07 17:30:39.258] INFO sqlite@v1.5.5/migrator.go:125 [0s] [-] SELECT * FROM `t_setting` LIMIT 1 +[2024-03-07 17:30:39.258] INFO script/db_migrate.go:58 [0s] [rows:1] SELECT count(*) FROM `t_user` WHERE is_admin = 1 +[2024-03-07 17:30:39.258] INFO script/db_migrate.go:95 [524.9µs] [rows:1] SELECT count(*) FROM `t_wg_server` diff --git a/middleware/authorization.go b/middleware/authorization.go index 2e4e7f3..a6c8240 100644 --- a/middleware/authorization.go +++ b/middleware/authorization.go @@ -3,7 +3,7 @@ package middleware import ( "github.com/gin-gonic/gin" "strings" - "wireguard-dashboard/compoment" + "wireguard-dashboard/component" "wireguard-dashboard/constant" "wireguard-dashboard/repository" "wireguard-dashboard/utils" @@ -21,7 +21,7 @@ func Authorization() gin.HandlerFunc { return } - userClaims, err := compoment.JWT().ParseToken(token) + userClaims, err := component.JWT().ParseToken(token) if err != nil { utils.GinResponse(c).AuthorizationFailed() c.Abort() diff --git a/script/db_migrate.go b/script/db_migrate.go index cb4f911..81bb01a 100644 --- a/script/db_migrate.go +++ b/script/db_migrate.go @@ -5,6 +5,7 @@ import ( "gitee.ltd/lxh/logger/log" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "wireguard-dashboard/client" + "wireguard-dashboard/component" "wireguard-dashboard/constant" "wireguard-dashboard/model/entity" "wireguard-dashboard/repository" @@ -143,5 +144,13 @@ func (s Script) InitServer() error { return err } + // 数据库保存完毕,应用配置到配置文件当中 + err = component.Wireguard().Apply("E:\\Workspace\\Go\\wireguard-dashboard\\template\\wg.conf", + "E:\\Workspace\\Go\\wireguard-dashboard\\wg0.conf", nil) + if err != nil { + log.Errorf("应用配置文件失败: %v", err.Error()) + return err + } + return nil } diff --git a/template/wg.conf b/template/wg.conf index ee3f486..0afb7b5 100644 --- a/template/wg.conf +++ b/template/wg.conf @@ -1,9 +1,18 @@ [Interface] -Address = {{ .Address }} -ListenPort = {{ .ListenPort }} -PrivateKey = {{ .PrivateKey }} -MTU = {{ .MTU }} -PostUp = {{ .PostUp }} -PreDown = {{ .PreDown }} -PostDown = {{ .PostDown }} -Table = {{ .Table }} +Address = {{ .Server.Address }} +ListenPort = {{ .Server.ListenPort }} +PrivateKey = {{ .Server.PrivateKey }} +MTU = {{ .Server.MTU }} +PostUp = {{ .Server.PostUp }} +PreDown = {{ .Server.PreDown }} +PostDown = {{ .Server.PostDown }} +Table = {{ .Server.Table }} + +{{ range .Clients }} +[Peer] +PublicKey = {{ .Client.PublicKey }} +PresharedKey = {{ .Client.PresharedKey }} +AllowedIPs = {{ .Client.AllowedIPs }} +PersistentKeepalive = {{ .Client.PersistentKeepalive }} +Endpoint = {{ .Client.Endpoint }} +{{ end }} \ No newline at end of file diff --git a/utils/template.go b/utils/template.go new file mode 100644 index 0000000..27d9031 --- /dev/null +++ b/utils/template.go @@ -0,0 +1,53 @@ +package utils + +import ( + "gitee.ltd/lxh/logger/log" + "html/template" + "os" +) + +type templateUtils struct{} + +func Template() templateUtils { + return templateUtils{} +} + +// Parse +// @description: 解析指定模板文件 +// @receiver templateUtils +// @param filepath +// @return parseTemplate +// @return err +func (templateUtils) Parse(filepath string) (parseTemplate *template.Template, err error) { + file, err := os.ReadFile(filepath) + if err != nil { + return + } + + parseTemplate, err = template.New("wg0.conf").Parse(string(file)) + return +} + +// Execute +// @description: 序列化数据到文件中 +// @receiver templateUtils +// @param fromTemplate +// @param data +// @param filePath +// @return err +func (templateUtils) Execute(fromTemplate *template.Template, data any, filePath string) (err error) { + wg0Conf, err := os.Create(filePath) + if err != nil { + log.Errorf("创建文件[%s]失败: %v", filePath, err.Error()) + return + } + + defer func() { + if err = wg0Conf.Close(); err != nil { + log.Errorf("关闭文件[%s]失败: %v", filePath, err.Error()) + return + } + }() + + return fromTemplate.Execute(wg0Conf, data) +} diff --git a/wg0.conf b/wg0.conf new file mode 100644 index 0000000..484ad12 --- /dev/null +++ b/wg0.conf @@ -0,0 +1,10 @@ +[Interface] +Address = +ListenPort = +PrivateKey = +MTU = +PostUp = +PreDown = +PostDown = +Table = +