mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 20:40:26 +08:00
增加恢复模式
This commit is contained in:
17
internal/web/actions/default/recover/helper.go
Normal file
17
internal/web/actions/default/recover/helper.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package recover
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type Helper struct {
|
||||
}
|
||||
|
||||
func (this *Helper) BeforeAction(actionPtr actions.ActionWrapper) (goNext bool) {
|
||||
if !teaconst.IsRecoverMode {
|
||||
actionPtr.Object().RedirectURL("/")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
17
internal/web/actions/default/recover/index.go
Normal file
17
internal/web/actions/default/recover/index.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package recover
|
||||
|
||||
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
this.Show()
|
||||
}
|
||||
15
internal/web/actions/default/recover/init.go
Normal file
15
internal/web/actions/default/recover/init.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package recover
|
||||
|
||||
import "github.com/iwind/TeaGo"
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(new(Helper)).
|
||||
Prefix("/recover").
|
||||
Get("", new(IndexAction)).
|
||||
Post("/validateApi", new(ValidateApiAction)).
|
||||
Post("/updateHosts", new(UpdateHostsAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
192
internal/web/actions/default/recover/updateHosts.go
Normal file
192
internal/web/actions/default/recover/updateHosts.go
Normal file
@@ -0,0 +1,192 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package recover
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
)
|
||||
|
||||
type UpdateHostsAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdateHostsAction) RunPost(params struct {
|
||||
Protocol string
|
||||
Host string
|
||||
Port string
|
||||
NodeId string
|
||||
NodeSecret string
|
||||
|
||||
OldHosts []string
|
||||
NewHosts []string
|
||||
}) {
|
||||
if len(params.OldHosts) != len(params.NewHosts) {
|
||||
this.Fail("参数配置错误,请刷新页面后重试")
|
||||
}
|
||||
|
||||
client, err := rpc.NewRPCClient(&configs.APIConfig{
|
||||
RPC: struct {
|
||||
Endpoints []string `yaml:"endpoints"`
|
||||
}{
|
||||
Endpoints: []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port},
|
||||
},
|
||||
NodeId: params.NodeId,
|
||||
Secret: params.NodeSecret,
|
||||
})
|
||||
if err != nil {
|
||||
this.FailField("host", "测试API节点时出错,请检查配置,错误信息:"+err.Error())
|
||||
}
|
||||
_, err = client.APINodeRPC().FindCurrentAPINodeVersion(client.APIContext(0), &pb.FindCurrentAPINodeVersionRequest{})
|
||||
if err != nil {
|
||||
this.FailField("host", "无法连接此API节点,错误信息:"+err.Error())
|
||||
}
|
||||
|
||||
// 获取管理员节点信息
|
||||
apiTokensResp, err := client.APITokenRPC().FindAllEnabledAPITokens(client.APIContext(0), &pb.FindAllEnabledAPITokensRequest{Role: "admin"})
|
||||
if err != nil {
|
||||
this.Fail("读取管理员令牌失败:" + err.Error())
|
||||
}
|
||||
|
||||
var apiTokens = apiTokensResp.ApiTokens
|
||||
if len(apiTokens) == 0 {
|
||||
this.Fail("数据库中没有管理员令牌信息,请确认数据是否完整")
|
||||
}
|
||||
var adminAPIToken = apiTokens[0]
|
||||
|
||||
// API节点列表
|
||||
nodesResp, err := client.APINodeRPC().FindAllEnabledAPINodes(client.Context(0), &pb.FindAllEnabledAPINodesRequest{})
|
||||
if err != nil {
|
||||
this.Fail("获取API节点列表失败,错误信息:" + err.Error())
|
||||
}
|
||||
var endpoints = []string{}
|
||||
for _, node := range nodesResp.Nodes {
|
||||
if !node.IsOn {
|
||||
continue
|
||||
}
|
||||
|
||||
// http
|
||||
if len(node.HttpJSON) > 0 {
|
||||
for index, oldHost := range params.OldHosts {
|
||||
if len(params.NewHosts[index]) == 0 {
|
||||
continue
|
||||
}
|
||||
node.HttpJSON = bytes.ReplaceAll(node.HttpJSON, []byte("\""+oldHost+"\""), []byte("\""+params.NewHosts[index]+"\""))
|
||||
}
|
||||
}
|
||||
|
||||
// https
|
||||
if len(node.HttpsJSON) > 0 {
|
||||
for index, oldHost := range params.OldHosts {
|
||||
if len(params.NewHosts[index]) == 0 {
|
||||
continue
|
||||
}
|
||||
node.HttpsJSON = bytes.ReplaceAll(node.HttpsJSON, []byte("\""+oldHost+"\""), []byte("\""+params.NewHosts[index]+"\""))
|
||||
}
|
||||
}
|
||||
|
||||
// restHTTP
|
||||
if len(node.RestHTTPJSON) > 0 {
|
||||
for index, oldHost := range params.OldHosts {
|
||||
if len(params.NewHosts[index]) == 0 {
|
||||
continue
|
||||
}
|
||||
node.RestHTTPJSON = bytes.ReplaceAll(node.RestHTTPJSON, []byte("\""+oldHost+"\""), []byte("\""+params.NewHosts[index]+"\""))
|
||||
}
|
||||
}
|
||||
|
||||
// restHTTPS
|
||||
if len(node.RestHTTPSJSON) > 0 {
|
||||
for index, oldHost := range params.OldHosts {
|
||||
if len(params.NewHosts[index]) == 0 {
|
||||
continue
|
||||
}
|
||||
node.RestHTTPSJSON = bytes.ReplaceAll(node.RestHTTPSJSON, []byte("\""+oldHost+"\""), []byte("\""+params.NewHosts[index]+"\""))
|
||||
}
|
||||
}
|
||||
|
||||
// access addrs
|
||||
if len(node.AccessAddrsJSON) > 0 {
|
||||
for index, oldHost := range params.OldHosts {
|
||||
if len(params.NewHosts[index]) == 0 {
|
||||
continue
|
||||
}
|
||||
node.AccessAddrsJSON = bytes.ReplaceAll(node.AccessAddrsJSON, []byte("\""+oldHost+"\""), []byte("\""+params.NewHosts[index]+"\""))
|
||||
}
|
||||
|
||||
var addrs []*serverconfigs.NetworkAddressConfig
|
||||
err = json.Unmarshal(node.AccessAddrsJSON, &addrs)
|
||||
if err != nil {
|
||||
this.Fail("读取节点访问地址失败:" + err.Error())
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
err = addr.Init()
|
||||
if err != nil {
|
||||
// 暂时不提示错误
|
||||
continue
|
||||
}
|
||||
for _, a := range addr.FullAddresses() {
|
||||
if !lists.ContainsString(endpoints, a) {
|
||||
endpoints = append(endpoints, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存
|
||||
_, err = client.APINodeRPC().UpdateAPINode(client.Context(0), &pb.UpdateAPINodeRequest{
|
||||
NodeId: node.Id,
|
||||
Name: node.Name,
|
||||
Description: node.Description,
|
||||
HttpJSON: node.HttpJSON,
|
||||
HttpsJSON: node.HttpsJSON,
|
||||
AccessAddrsJSON: node.AccessAddrsJSON,
|
||||
IsOn: node.IsOn,
|
||||
RestIsOn: node.RestIsOn,
|
||||
RestHTTPJSON: node.RestHTTPJSON,
|
||||
RestHTTPSJSON: node.RestHTTPSJSON,
|
||||
})
|
||||
if err != nil {
|
||||
this.Fail("保存API节点信息失败:" + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// 修改api.yaml
|
||||
var apiConfig = &configs.APIConfig{
|
||||
RPC: struct {
|
||||
Endpoints []string `yaml:"endpoints"`
|
||||
}{
|
||||
Endpoints: endpoints,
|
||||
},
|
||||
NodeId: adminAPIToken.NodeId,
|
||||
Secret: adminAPIToken.Secret,
|
||||
}
|
||||
err = apiConfig.WriteFile(Tea.Root + "/configs/api.yaml")
|
||||
if err != nil {
|
||||
this.Fail("保存configs/api.yaml失败:" + err.Error())
|
||||
}
|
||||
|
||||
// 加载api.yaml
|
||||
rpcClient, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
this.Fail("初始化RPC失败:" + err.Error())
|
||||
}
|
||||
err = rpcClient.UpdateConfig(apiConfig)
|
||||
if err != nil {
|
||||
this.Fail("修改API配置失败:" + err.Error())
|
||||
}
|
||||
|
||||
// 退出恢复模式
|
||||
teaconst.IsRecoverMode = false
|
||||
|
||||
this.Success()
|
||||
}
|
||||
153
internal/web/actions/default/recover/validateApi.go
Normal file
153
internal/web/actions/default/recover/validateApi.go
Normal file
@@ -0,0 +1,153 @@
|
||||
package recover
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ValidateApiAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *ValidateApiAction) RunPost(params struct {
|
||||
Protocol string
|
||||
Host string
|
||||
Port string
|
||||
NodeId string
|
||||
NodeSecret string
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
params.NodeId = strings.Trim(params.NodeId, "\"' ")
|
||||
params.NodeSecret = strings.Trim(params.NodeSecret, "\"' ")
|
||||
|
||||
// 使用已有的API节点
|
||||
params.Must.
|
||||
Field("host", params.Host).
|
||||
Require("请输入主机地址").
|
||||
Field("port", params.Port).
|
||||
Require("请输入服务端口").
|
||||
Match(`^\d+$`, "服务端口只能是数字").
|
||||
Field("nodeId", params.NodeId).
|
||||
Require("请输入节点nodeId").
|
||||
Field("nodeSecret", params.NodeSecret).
|
||||
Require("请输入节点secret")
|
||||
client, err := rpc.NewRPCClient(&configs.APIConfig{
|
||||
RPC: struct {
|
||||
Endpoints []string `yaml:"endpoints"`
|
||||
}{
|
||||
Endpoints: []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port},
|
||||
},
|
||||
NodeId: params.NodeId,
|
||||
Secret: params.NodeSecret,
|
||||
})
|
||||
if err != nil {
|
||||
this.FailField("host", "测试API节点时出错,请检查配置,错误信息:"+err.Error())
|
||||
}
|
||||
_, err = client.APINodeRPC().FindCurrentAPINodeVersion(client.APIContext(0), &pb.FindCurrentAPINodeVersionRequest{})
|
||||
if err != nil {
|
||||
this.FailField("host", "无法连接此API节点,错误信息:"+err.Error())
|
||||
}
|
||||
|
||||
// API节点列表
|
||||
nodesResp, err := client.APINodeRPC().FindAllEnabledAPINodes(client.Context(0), &pb.FindAllEnabledAPINodesRequest{})
|
||||
if err != nil {
|
||||
this.Fail("获取API节点列表失败,错误信息:" + err.Error())
|
||||
}
|
||||
var hosts = []string{}
|
||||
for _, node := range nodesResp.Nodes {
|
||||
if !node.IsOn {
|
||||
continue
|
||||
}
|
||||
|
||||
// http
|
||||
if len(node.HttpJSON) > 0 {
|
||||
var config = &serverconfigs.HTTPProtocolConfig{}
|
||||
err = json.Unmarshal(node.HttpJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("读取节点HTTP信息失败:" + err.Error())
|
||||
}
|
||||
for _, listen := range config.Listen {
|
||||
if len(listen.Host) > 0 && !lists.ContainsString(hosts, listen.Host) {
|
||||
hosts = append(hosts, listen.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https
|
||||
if len(node.HttpsJSON) > 0 {
|
||||
var config = &serverconfigs.HTTPSProtocolConfig{}
|
||||
err = json.Unmarshal(node.HttpsJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("读取节点HTTPS信息失败:" + err.Error())
|
||||
}
|
||||
for _, listen := range config.Listen {
|
||||
if len(listen.Host) > 0 && !lists.ContainsString(hosts, listen.Host) {
|
||||
hosts = append(hosts, listen.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// restHTTP
|
||||
if len(node.RestHTTPJSON) > 0 {
|
||||
var config = &serverconfigs.HTTPProtocolConfig{}
|
||||
err = json.Unmarshal(node.RestHTTPJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("读取节点REST HTTP信息失败:" + err.Error())
|
||||
}
|
||||
for _, listen := range config.Listen {
|
||||
if len(listen.Host) > 0 && !lists.ContainsString(hosts, listen.Host) {
|
||||
hosts = append(hosts, listen.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// restHTTPS
|
||||
if len(node.RestHTTPSJSON) > 0 {
|
||||
var config = &serverconfigs.HTTPSProtocolConfig{}
|
||||
err = json.Unmarshal(node.RestHTTPSJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("读取节点REST HTTPS信息失败:" + err.Error())
|
||||
}
|
||||
for _, listen := range config.Listen {
|
||||
if len(listen.Host) > 0 && !lists.ContainsString(hosts, listen.Host) {
|
||||
hosts = append(hosts, listen.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// access addrs
|
||||
if len(node.AccessAddrsJSON) > 0 {
|
||||
var addrs []*serverconfigs.NetworkAddressConfig
|
||||
err = json.Unmarshal(node.AccessAddrsJSON, &addrs)
|
||||
if err != nil {
|
||||
this.Fail("读取节点访问地址失败:" + err.Error())
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
if len(addr.Host) > 0 && !lists.ContainsString(hosts, addr.Host) {
|
||||
hosts = append(hosts, addr.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Data["apiNode"] = maps.Map{
|
||||
"protocol": params.Protocol,
|
||||
"host": params.Host,
|
||||
"port": params.Port,
|
||||
"nodeId": params.NodeId,
|
||||
"nodeSecret": params.NodeSecret,
|
||||
"hosts": hosts,
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user