增加恢复模式

This commit is contained in:
GoEdgeLab
2021-07-20 17:15:17 +08:00
parent 320b325496
commit 064bf2016b
34 changed files with 948 additions and 285 deletions

View 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
}

View 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()
}

View 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()
})
}

View 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()
}

View 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()
}

View File

@@ -3,8 +3,7 @@ package setup
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"net"
"regexp"
"sort"
"strings"
)
type IndexAction struct {
@@ -16,31 +15,18 @@ func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct{}) {
// 当前服务器的IP
serverIPs := []string{}
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
netAddr, ok := addr.(*net.IPNet)
if !ok {
continue
var currentHost = this.Request.Host
if strings.Contains(this.Request.Host, ":") {
host, _, err := net.SplitHostPort(this.Request.Host)
if err == nil {
currentHost = host
}
serverIPs = append(serverIPs, netAddr.IP.String())
}
// 对IP进行排序我们希望IPv4排在前面而且希望127.0.0.1排在IPv4里的最后
sort.Slice(serverIPs, func(i, j int) bool {
ip1 := serverIPs[i]
if ip1 == "127.0.0.1" {
return false
}
if regexp.MustCompile(`^\d+\.\d+\.\d+.\d+$`).MatchString(ip1) {
return true
}
return false
})
this.Data["serverIPs"] = serverIPs
if net.ParseIP(currentHost) != nil {
this.Data["currentHost"] = currentHost
} else {
this.Data["currentHost"] = ""
}
this.Show()
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/TeaOSLab/EdgeAdmin/internal/nodes"
"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/systemconfigs"
"github.com/go-yaml/yaml"
@@ -68,14 +69,14 @@ func (this *InstallAction) RunPost(params struct {
_, err = os.Stat(apiNodeDir)
if err != nil {
if os.IsNotExist(err) {
this.Fail("在当前目录下找不到" + dir + "目录,请将" + dir + "目录上传或者重新下载解压")
this.Fail("在当前目录" + Tea.Root + "下找不到" + dir + "目录,请将" + dir + "目录上传或者重新下载解压")
}
this.Fail("无法检查" + dir + "目录,发生错误:" + err.Error())
}
}
// 保存数据库配置
dsn := dbMap.GetString("username") + ":" + dbMap.GetString("password") + "@tcp(" + dbMap.GetString("host") + ":" + dbMap.GetString("port") + ")/" + dbMap.GetString("database") + "?charset=utf8mb4&timeout=30s"
dsn := dbMap.GetString("username") + ":" + dbMap.GetString("password") + "@tcp(" + configutils.QuoteIP(dbMap.GetString("host")) + ":" + dbMap.GetString("port") + ")/" + dbMap.GetString("database") + "?charset=utf8mb4&timeout=30s"
dbConfig := &dbs.Config{
DBs: map[string]*dbs.DBConfig{
"prod": {
@@ -175,7 +176,7 @@ func (this *InstallAction) RunPost(params struct {
RPC: struct {
Endpoints []string `yaml:"endpoints"`
}{
Endpoints: []string{"http://" + apiNodeMap.GetString("newHost") + ":" + apiNodeMap.GetString("newPort")},
Endpoints: []string{"http://" + configutils.QuoteIP(apiNodeMap.GetString("newHost")) + ":" + apiNodeMap.GetString("newPort")},
},
NodeId: resultMap.GetString("adminNodeId"),
Secret: resultMap.GetString("adminNodeSecret"),
@@ -233,7 +234,7 @@ func (this *InstallAction) RunPost(params struct {
RPC: struct {
Endpoints []string `yaml:"endpoints"`
}{
Endpoints: []string{apiNodeMap.GetString("oldProtocol") + "://" + apiNodeMap.GetString("oldHost") + ":" + apiNodeMap.GetString("oldPort")},
Endpoints: []string{apiNodeMap.GetString("oldProtocol") + "://" + configutils.QuoteIP(apiNodeMap.GetString("oldHost")) + ":" + apiNodeMap.GetString("oldPort")},
},
NodeId: apiNodeMap.GetString("oldNodeId"),
Secret: apiNodeMap.GetString("oldNodeSecret"),

View File

@@ -4,6 +4,7 @@ import (
"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/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
@@ -62,7 +63,7 @@ func (this *ValidateApiAction) RunPost(params struct {
if net.ParseIP(params.NewHost) == nil {
this.FailField("newHost", "请输入正确的节点主机地址")
}
params.Must.
Field("newHost", params.NewHost).
Require("请输入节点主机地址")
@@ -86,7 +87,7 @@ func (this *ValidateApiAction) RunPost(params struct {
RPC: struct {
Endpoints []string `yaml:"endpoints"`
}{
Endpoints: []string{params.OldProtocol + "://" + params.OldHost + ":" + params.OldPort},
Endpoints: []string{params.OldProtocol + "://" + configutils.QuoteIP(params.OldHost) + ":" + params.OldPort},
},
NodeId: params.OldNodeId,
Secret: params.OldNodeSecret,

View File

@@ -2,11 +2,14 @@ package setup
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
stringutil "github.com/iwind/TeaGo/utils/string"
"net"
"regexp"
"strings"
)
@@ -27,7 +30,20 @@ func (this *ValidateDbAction) RunPost(params struct {
params.Must.
Field("host", params.Host).
Require("请输入主机地址").
Match(`^[\w\.-]+$`, "主机地址中不能包含特殊字符").
Expect(func() (message string, success bool) {
// 是否为IP
if net.ParseIP(params.Host) != nil {
success = true
return
}
if !regexp.MustCompile(`^[\w.-]+$`).MatchString(params.Host) {
message = "主机地址中不能包含特殊字符"
success = false
return
}
success = true
return
}).
Field("port", params.Port).
Require("请输入端口").
Match(`^\d+$`, "端口中只能包含数字").
@@ -41,7 +57,7 @@ func (this *ValidateDbAction) RunPost(params struct {
// 测试连接
db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
Driver: "mysql",
Dsn: params.Username + ":" + params.Password + "@tcp(" + params.Host + ":" + params.Port + ")/" + params.Database,
Dsn: params.Username + ":" + params.Password + "@tcp(" + configutils.QuoteIP(params.Host) + ":" + params.Port + ")/" + params.Database,
Prefix: "",
})
if err != nil {
@@ -58,7 +74,7 @@ func (this *ValidateDbAction) RunPost(params struct {
if strings.Contains(err.Error(), "Error 1049") {
db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
Driver: "mysql",
Dsn: params.Username + ":" + params.Password + "@tcp(" + params.Host + ":" + params.Port + ")/",
Dsn: params.Username + ":" + params.Password + "@tcp(" + configutils.QuoteIP(params.Host) + ":" + params.Port + ")/",
Prefix: "",
})