mirror of
https://gitee.com/mirrors/AllinSSL.git
synced 2026-03-10 00:31:10 +08:00
代码同步
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
@@ -50,11 +51,12 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if providerConfig["url"][len(providerConfig["url"])-1:] != "/" {
|
||||
providerConfig["url"] += "/"
|
||||
parsedURL, err := url.Parse(providerConfig["url"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, providerConfig["url"]+requestUrl, bytes.NewBuffer(jsonData))
|
||||
baseURL := fmt.Sprintf("%s://%s/", parsedURL.Scheme, parsedURL.Host)
|
||||
req, err := http.NewRequest(method, baseURL+requestUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
// fmt.Println(err)
|
||||
return nil, err
|
||||
|
||||
@@ -41,14 +41,17 @@ func RequestBt(data *url.Values, method, providerID, requestUrl string) (map[str
|
||||
}
|
||||
timestamp := time.Now().Unix()
|
||||
token := generateSignature(fmt.Sprintf("%d", timestamp), providerConfig["api_key"])
|
||||
if providerConfig["url"][len(providerConfig["url"])-1:] != "/" {
|
||||
providerConfig["url"] += "/"
|
||||
}
|
||||
|
||||
data.Set("request_time", fmt.Sprintf("%d", timestamp))
|
||||
data.Set("request_token", token)
|
||||
|
||||
req, err := http.NewRequest(method, providerConfig["url"]+requestUrl, strings.NewReader(data.Encode()))
|
||||
parsedURL, err := url.Parse(providerConfig["url"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
baseURL := fmt.Sprintf("%s://%s/", parsedURL.Scheme, parsedURL.Host)
|
||||
|
||||
req, err := http.NewRequest(method, baseURL+requestUrl, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -112,7 +115,7 @@ func DeployBt(cfg map[string]any) error {
|
||||
data.Set("cert_type", "1")
|
||||
data.Set("privateKey", keyPem)
|
||||
data.Set("certPem", certPem)
|
||||
_, err := RequestBt(&data, "POST", providerID, "/config?action=SetPanelSSL")
|
||||
_, err := RequestBt(&data, "POST", providerID, "config?action=SetPanelSSL")
|
||||
if err != nil {
|
||||
return fmt.Errorf("证书部署失败: %v", err)
|
||||
}
|
||||
@@ -150,7 +153,7 @@ func DeployBtSite(cfg map[string]any) error {
|
||||
data.Set("key", keyPem)
|
||||
data.Set("csr", certPem)
|
||||
data.Set("siteName", siteName)
|
||||
_, err := RequestBt(&data, "POST", providerID, "/site?action=SetSSL")
|
||||
_, err := RequestBt(&data, "POST", providerID, "site?action=SetSSL")
|
||||
if err != nil {
|
||||
return fmt.Errorf("证书部署失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -37,8 +37,9 @@ func Deploy(cfg map[string]any, logger *public.Logger) error {
|
||||
case "aliyun-cdn":
|
||||
logger.Debug("部署到阿里云CDN...")
|
||||
return DeployAliCdn(cfg)
|
||||
// case "aliyun-oss":
|
||||
|
||||
case "aliyun-oss":
|
||||
logger.Debug("部署到阿里云OSS...")
|
||||
return DeployOss(cfg)
|
||||
default:
|
||||
return fmt.Errorf("不支持的部署: %s", providerName)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type SSHConfig struct {
|
||||
Password string // 可选
|
||||
PrivateKey string // 可选
|
||||
Host string
|
||||
Port string
|
||||
Port float64
|
||||
}
|
||||
|
||||
type RemoteFile struct {
|
||||
@@ -24,7 +24,7 @@ type RemoteFile struct {
|
||||
|
||||
func buildAuthMethods(password, privateKey string) ([]ssh.AuthMethod, error) {
|
||||
var methods []ssh.AuthMethod
|
||||
|
||||
|
||||
if privateKey != "" {
|
||||
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
|
||||
if err != nil {
|
||||
@@ -32,71 +32,71 @@ func buildAuthMethods(password, privateKey string) ([]ssh.AuthMethod, error) {
|
||||
}
|
||||
methods = append(methods, ssh.PublicKeys(signer))
|
||||
}
|
||||
|
||||
|
||||
if password != "" {
|
||||
methods = append(methods, ssh.Password(password))
|
||||
}
|
||||
|
||||
|
||||
if len(methods) == 0 {
|
||||
return nil, fmt.Errorf("no authentication methods provided")
|
||||
}
|
||||
|
||||
|
||||
return methods, nil
|
||||
}
|
||||
|
||||
func writeMultipleFilesViaSSH(config SSHConfig, files []RemoteFile, preCmd, postCmd string) error {
|
||||
addr := fmt.Sprintf("%s:%s", config.Host, config.Port)
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", config.Host, int(config.Port))
|
||||
|
||||
authMethods, err := buildAuthMethods(config.Password, config.PrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: config.User,
|
||||
Auth: authMethods,
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||
}
|
||||
|
||||
|
||||
client, err := ssh.Dial("tcp", addr, sshConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to dial: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return fmt.Errorf("会话创建失败: %v", err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
|
||||
var script bytes.Buffer
|
||||
|
||||
|
||||
if preCmd != "" {
|
||||
script.WriteString(preCmd + " && ")
|
||||
}
|
||||
|
||||
|
||||
for i, file := range files {
|
||||
if i > 0 {
|
||||
script.WriteString(" && ")
|
||||
}
|
||||
|
||||
|
||||
dirCmd := fmt.Sprintf("mkdir -p $(dirname %q)", file.Path)
|
||||
writeCmd := fmt.Sprintf("printf %%s '%s' > %s", file.Content, file.Path)
|
||||
|
||||
|
||||
script.WriteString(dirCmd + " && " + writeCmd)
|
||||
}
|
||||
|
||||
|
||||
if postCmd != "" {
|
||||
script.WriteString(" && " + postCmd)
|
||||
}
|
||||
|
||||
|
||||
cmd := script.String()
|
||||
|
||||
|
||||
if err := session.Run(cmd); err != nil {
|
||||
return fmt.Errorf("运行出错: %v", err)
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -127,17 +127,17 @@ func DeploySSH(cfg map[string]any) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("参数错误:keyPath")
|
||||
}
|
||||
certPath, ok := cfg["keyPath"].(string)
|
||||
certPath, ok := cfg["certPath"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("参数错误:certPath")
|
||||
}
|
||||
beforeCmd, ok := cfg["beforeCmd"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("参数错误:beforeCmd")
|
||||
beforeCmd = ""
|
||||
}
|
||||
afterCmd, ok := cfg["afterCmd"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("参数错误:afterCmd")
|
||||
afterCmd = ""
|
||||
}
|
||||
providerData, err := access.GetAccess(providerID)
|
||||
if err != nil {
|
||||
@@ -155,8 +155,8 @@ func DeploySSH(cfg map[string]any) error {
|
||||
}
|
||||
// 自动创建多级目录
|
||||
files := []RemoteFile{
|
||||
{Path: keyPath, Content: certPem},
|
||||
{Path: certPath, Content: keyPem},
|
||||
{Path: certPath, Content: certPem},
|
||||
{Path: keyPath, Content: keyPem},
|
||||
}
|
||||
err = writeMultipleFilesViaSSH(providerConfig, files, beforeCmd, afterCmd)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user