mirror of
https://gitee.com/mirrors/AllinSSL.git
synced 2026-03-08 15:51:11 +08:00
修复ssh密钥登录、新增本地部署和部署到btwaf、新增百度云dns、新增可选择是否重复部署、修改初始化
This commit is contained in:
@@ -2,19 +2,22 @@ package deploy
|
||||
|
||||
import (
|
||||
"ALLinSSL/backend/internal/access"
|
||||
"ALLinSSL/backend/public"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type SSHConfig struct {
|
||||
User string
|
||||
Password string // 可选
|
||||
PrivateKey string // 可选
|
||||
PrivateKey string `json:"key"` // 可选
|
||||
Host string
|
||||
Port float64
|
||||
Port any
|
||||
}
|
||||
|
||||
type RemoteFile struct {
|
||||
@@ -45,7 +48,18 @@ func buildAuthMethods(password, privateKey string) ([]ssh.AuthMethod, error) {
|
||||
}
|
||||
|
||||
func writeMultipleFilesViaSSH(config SSHConfig, files []RemoteFile, preCmd, postCmd string) error {
|
||||
addr := fmt.Sprintf("%s:%d", config.Host, int(config.Port))
|
||||
var port string
|
||||
switch v := config.Port.(type) {
|
||||
case float64:
|
||||
port = strconv.Itoa(int(v))
|
||||
case string:
|
||||
port = v
|
||||
case int:
|
||||
port = strconv.Itoa(v)
|
||||
default:
|
||||
port = "22"
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%s", config.Host, port)
|
||||
|
||||
authMethods, err := buildAuthMethods(config.Password, config.PrivateKey)
|
||||
if err != nil {
|
||||
@@ -164,3 +178,60 @@ func DeploySSH(cfg map[string]any) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeployLocalhost(cfg map[string]any) error {
|
||||
cert, ok := cfg["certificate"].(map[string]any)
|
||||
if !ok {
|
||||
return fmt.Errorf("证书不存在")
|
||||
}
|
||||
// 设置证书
|
||||
keyPem, ok := cert["key"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("证书错误:key")
|
||||
}
|
||||
certPem, ok := cert["cert"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("证书错误:cert")
|
||||
}
|
||||
keyPath, ok := cfg["keyPath"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("参数错误:keyPath")
|
||||
}
|
||||
certPath, ok := cfg["certPath"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("参数错误:certPath")
|
||||
}
|
||||
beforeCmd, ok := cfg["beforeCmd"].(string)
|
||||
if ok {
|
||||
_, errout, err := public.ExecCommand(beforeCmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("前置命令执行失败: %v, %s", err, errout)
|
||||
}
|
||||
}
|
||||
|
||||
dir := filepath.Dir(certPath)
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
panic("创建证书保存目录失败: " + err.Error())
|
||||
}
|
||||
dir = filepath.Dir(keyPath)
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
panic("创建私钥保存目录失败: " + err.Error())
|
||||
}
|
||||
err := os.WriteFile(certPath, []byte(certPem), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("写入证书失败: %v", err)
|
||||
}
|
||||
err = os.WriteFile(keyPath, []byte(keyPem), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("写入私钥失败: %v", err)
|
||||
}
|
||||
|
||||
afterCmd, ok := cfg["afterCmd"].(string)
|
||||
if ok {
|
||||
_, errout, err := public.ExecCommand(afterCmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("后置命令执行失败: %v, %s", err, errout)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user