diff --git a/internal/const/plus.go b/internal/const/plus.go new file mode 100644 index 00000000..0c606765 --- /dev/null +++ b/internal/const/plus.go @@ -0,0 +1,6 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package teaconst + +// IsPlus 是否为企业版 +var IsPlus = false diff --git a/internal/nodes/admin_node.go b/internal/nodes/admin_node.go index 37d27698..aa85806b 100644 --- a/internal/nodes/admin_node.go +++ b/internal/nodes/admin_node.go @@ -87,7 +87,7 @@ func (this *AdminNode) Run() { Start() } -// 实现守护进程 +// Daemon 实现守护进程 func (this *AdminNode) Daemon() { path := os.TempDir() + "/edge-admin.sock" isDebug := lists.ContainsString(os.Args, "debug") @@ -132,7 +132,7 @@ func (this *AdminNode) Daemon() { } } -// 安装系统服务 +// InstallSystemService 安装系统服务 func (this *AdminNode) InstallSystemService() error { shortName := teaconst.SystemdServiceName @@ -149,7 +149,7 @@ func (this *AdminNode) InstallSystemService() error { return nil } -// 添加子PID +// AddSubPID 添加子PID func (this *AdminNode) AddSubPID(pid int) { this.subPIDs = append(this.subPIDs, pid) } diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index 1254f4de..8f20efc6 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -207,7 +207,7 @@ func (this *RPCClient) HTTPRewriteRuleRPC() pb.HTTPRewriteRuleServiceClient { return pb.NewHTTPRewriteRuleServiceClient(this.pickConn()) } -// 访问日志 +// HTTPAccessLogRPC 访问日志 func (this *RPCClient) HTTPAccessLogRPC() pb.HTTPAccessLogServiceClient { return pb.NewHTTPAccessLogServiceClient(this.pickConn()) } @@ -328,7 +328,11 @@ func (this *RPCClient) NodeTaskRPC() pb.NodeTaskServiceClient { return pb.NewNodeTaskServiceClient(this.pickConn()) } -// 构造Admin上下文 +func (this *RPCClient) AuthorityKeyRPC() pb.AuthorityKeyServiceClient { + return pb.NewAuthorityKeyServiceClient(this.pickConn()) +} + +// Context 构造Admin上下文 func (this *RPCClient) Context(adminId int64) context.Context { ctx := context.Background() m := maps.Map{ @@ -351,7 +355,7 @@ func (this *RPCClient) Context(adminId int64) context.Context { return ctx } -// 构造API上下文 +// APIContext 构造API上下文 func (this *RPCClient) APIContext(apiNodeId int64) context.Context { ctx := context.Background() m := maps.Map{ @@ -374,7 +378,7 @@ func (this *RPCClient) APIContext(apiNodeId int64) context.Context { return ctx } -// 修改配置 +// UpdateConfig 修改配置 func (this *RPCClient) UpdateConfig(config *configs.APIConfig) error { this.apiConfig = config return this.init() diff --git a/internal/tasks/task_authority.go b/internal/tasks/task_authority.go new file mode 100644 index 00000000..d6afd22f --- /dev/null +++ b/internal/tasks/task_authority.go @@ -0,0 +1,68 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package tasks + +import ( + teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const" + "github.com/TeaOSLab/EdgeAdmin/internal/events" + "github.com/TeaOSLab/EdgeAdmin/internal/rpc" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/Tea" + "github.com/iwind/TeaGo/logs" + "time" +) + +func init() { + events.On(events.EventStart, func() { + task := NewAuthorityTask() + go task.Start() + }) +} + +type AuthorityTask struct { +} + +func NewAuthorityTask() *AuthorityTask { + return &AuthorityTask{} +} + +func (this *AuthorityTask) Start() { + ticker := time.NewTicker(10 * time.Minute) + if Tea.IsTesting() { + // 快速测试 + ticker = time.NewTicker(1 * time.Minute) + } + + // 初始化的时候先获取一次 + timeout := time.NewTimer(5 * time.Second) + <-timeout.C + err := this.Loop() + if err != nil { + logs.Println("[TASK][AuthorityTask]" + err.Error()) + } + + // 定时获取 + for range ticker.C { + err := this.Loop() + if err != nil { + logs.Println("[TASK][AuthorityTask]" + err.Error()) + } + } +} + +func (this *AuthorityTask) Loop() error { + rpcClient, err := rpc.SharedRPC() + if err != nil { + return err + } + resp, err := rpcClient.AuthorityKeyRPC().ReadAuthorityKey(rpcClient.Context(0), &pb.ReadAuthorityKeyRequest{}) + if err != nil { + return err + } + if resp.AuthorityKey != nil { + teaconst.IsPlus = true + } else { + teaconst.IsPlus = false + } + return nil +} diff --git a/internal/web/actions/default/settings/authority/index.go b/internal/web/actions/default/settings/authority/index.go new file mode 100644 index 00000000..8bf85536 --- /dev/null +++ b/internal/web/actions/default/settings/authority/index.go @@ -0,0 +1,51 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package server + +import ( + teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/maps" + timeutil "github.com/iwind/TeaGo/utils/time" +) + +type IndexAction struct { + actionutils.ParentAction +} + +func (this *IndexAction) Init() { + this.Nav("", "", "") +} + +func (this *IndexAction) RunGet(params struct{}) { + keyResp, err := this.RPC().AuthorityKeyRPC().ReadAuthorityKey(this.AdminContext(), &pb.ReadAuthorityKeyRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + var keyMap maps.Map = nil + teaconst.IsPlus = false + if keyResp.AuthorityKey != nil { + if len(keyResp.AuthorityKey.MacAddresses) == 0 { + keyResp.AuthorityKey.MacAddresses = []string{} + } + + isActive := len(keyResp.AuthorityKey.DayTo) > 0 && keyResp.AuthorityKey.DayTo >= timeutil.Format("Y-m-d") + if isActive { + teaconst.IsPlus = true + } + + keyMap = maps.Map{ + "dayFrom": keyResp.AuthorityKey.DayFrom, + "dayTo": keyResp.AuthorityKey.DayTo, + "macAddresses": keyResp.AuthorityKey.MacAddresses, + "hostname": keyResp.AuthorityKey.Hostname, + "isExpired": !isActive, + "updatedTime": timeutil.FormatTime("Y-m-d H:i:s", keyResp.AuthorityKey.UpdatedAt), + } + } + this.Data["key"] = keyMap + + this.Show() +} diff --git a/internal/web/actions/default/settings/authority/init.go b/internal/web/actions/default/settings/authority/init.go new file mode 100644 index 00000000..77e2f586 --- /dev/null +++ b/internal/web/actions/default/settings/authority/init.go @@ -0,0 +1,19 @@ +package server + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/configloaders" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/settingutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/helpers" + "github.com/iwind/TeaGo" +) + +func init() { + TeaGo.BeforeStart(func(server *TeaGo.Server) { + server. + Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeSetting)). + Helper(settingutils.NewAdvancedHelper("authority")). + Prefix("/settings/authority"). + Get("", new(IndexAction)). + EndAll() + }) +} diff --git a/internal/web/actions/default/settings/settingutils/advanced_helper.go b/internal/web/actions/default/settings/settingutils/advanced_helper.go index 37fdd6df..cab9b8f7 100644 --- a/internal/web/actions/default/settings/settingutils/advanced_helper.go +++ b/internal/web/actions/default/settings/settingutils/advanced_helper.go @@ -2,6 +2,7 @@ package settingutils import ( "github.com/TeaOSLab/EdgeAdmin/internal/configloaders" + teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/iwind/TeaGo/actions" ) @@ -34,7 +35,10 @@ func (this *AdvancedHelper) BeforeAction(actionPtr actions.ActionWrapper) (goNex tabbar.Add("API节点", "", "/api", "", this.tab == "apiNodes") tabbar.Add("用户节点", "", "/settings/userNodes", "", this.tab == "userNodes") tabbar.Add("日志数据库", "", "/db", "", this.tab == "dbNodes") - tabbar.Add("监控节点", "", "/settings/monitorNodes", "", this.tab == "monitorNodes") + if teaconst.IsPlus { + tabbar.Add("监控节点", "", "/settings/monitorNodes", "", this.tab == "monitorNodes") + } + tabbar.Add("企业版认证", "", "/settings/authority", "", this.tab == "authority") //tabbar.Add("备份", "", "/settings/backup", "", this.tab == "backup") } diff --git a/internal/web/helpers/user_must_auth.go b/internal/web/helpers/user_must_auth.go index d0d5e5ed..da472ed4 100644 --- a/internal/web/helpers/user_must_auth.go +++ b/internal/web/helpers/user_must_auth.go @@ -234,6 +234,7 @@ func (this *userMustAuth) modules(adminId int64) []maps.Map { "name": "通知媒介", "url": "/admins/recipients", "code": "recipients", + "isOn": teaconst.IsPlus, }, }, }, diff --git a/internal/web/import.go b/internal/web/import.go index ebf60ec0..260e53df 100644 --- a/internal/web/import.go +++ b/internal/web/import.go @@ -84,6 +84,7 @@ import ( _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/websocket" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/stat" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings" + _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/authority" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/backup" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/database" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/ip-library" diff --git a/web/views/@default/@layout.html b/web/views/@default/@layout.html index 0eeaaf00..ec08d40f 100644 --- a/web/views/@default/@layout.html +++ b/web/views/@default/@layout.html @@ -85,7 +85,7 @@
| 状态 | ++ 已认证 + | +
| 开始日期 | +{{key.dayFrom}} | +
| 结束日期 | +{{key.dayTo}}
+
+ 已过期,请尽快更新认证信息。
+
+ |
+
| 主机名 | +{{key.hostname}}
+ 认证服务所在服务器主机名。 + |
+
| 主机MAC地址 | +
+ {{address}}
+ 认证服务所在服务器的MAC地址。 + |
+
数据更新于{{key.updatedTime}}。
+