[ Cmii ] [ Octopus ] - fix a lot of bugs
This commit is contained in:
@@ -4,7 +4,10 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"golang.org/x/net/proxy"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -511,8 +514,75 @@ func BasicConvertBufferToSlice(outputBuffer bytes.Buffer) (resultLog []string) {
|
||||
|
||||
}
|
||||
|
||||
// BasicDownloadFile 从目标地址下载文件到目的地,并检测文件是否下载成功
|
||||
func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog []string) {
|
||||
func BasicDownloadFile(downloadUrl string, socksProxyUrl string, proxyUser string, proxyPass string, desFile string) (downloadOk bool, resultLog []string) {
|
||||
// 解析下载URL
|
||||
_, err := url.Parse(downloadUrl)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error parsing download URL: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
out, err := os.Create(desFile)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error creating file: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// 创建HTTP客户端
|
||||
client := &http.Client{}
|
||||
|
||||
// 如果提供了代理URL
|
||||
if socksProxyUrl != "" {
|
||||
// 解析代理URL
|
||||
parsedProxyUrl, err := url.Parse(socksProxyUrl)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error parsing proxy URL: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 使用SOCKS5代理
|
||||
auth := &proxy.Auth{User: proxyUser, Password: proxyPass}
|
||||
dialer, err := proxy.SOCKS5("tcp", parsedProxyUrl.Host, auth, proxy.Direct)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error creating SOCKS5 dialer: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 设置HTTP和HTTPS代理
|
||||
httpTransport := &http.Transport{
|
||||
Dial: dialer.Dial,
|
||||
}
|
||||
client.Transport = httpTransport
|
||||
}
|
||||
|
||||
// 发送HTTP GET请求
|
||||
resp, err := client.Get(downloadUrl)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error making GET request: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查HTTP响应状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resultLog = append(resultLog, "Server returned HTTP status "+resp.Status)
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 将HTTP响应内容写入文件
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error writing to file: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
return true, resultLog
|
||||
}
|
||||
|
||||
// BasicDownloadFileByCurl 从目标地址下载文件到目的地,并检测文件是否下载成功
|
||||
func BasicDownloadFileByCurl(downloadUrl, desFile string) (downloadOk bool, resultLog []string) {
|
||||
|
||||
// wget or curl download
|
||||
var ok bool
|
||||
@@ -539,14 +609,14 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
|
||||
})
|
||||
|
||||
} else {
|
||||
sprintf := fmt.Sprintf("[BasicDownloadFile] - neither wget or curl exists ! can't download file [ %s ] from [ %s ]", desFile, downloadUrl)
|
||||
sprintf := fmt.Sprintf("[BasicDownloadFileByCurl] - neither wget or curl exists ! can't download file [ %s ] from [ %s ]", desFile, downloadUrl)
|
||||
log.Error(sprintf)
|
||||
return false, []string{
|
||||
sprintf,
|
||||
}
|
||||
}
|
||||
|
||||
errLog := fmt.Sprintf("[BasicDownloadFile] - download file [ %s ] from [ %s ] failed !", desFile, downloadUrl)
|
||||
errLog := fmt.Sprintf("[BasicDownloadFileByCurl] - download file [ %s ] from [ %s ] failed !", desFile, downloadUrl)
|
||||
if !ok {
|
||||
log.Error(errLog)
|
||||
return false, []string{
|
||||
@@ -558,7 +628,7 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
|
||||
if !existAndNotNull {
|
||||
return false, []string{
|
||||
errLog,
|
||||
"[BasicDownloadFile] - file not exist !",
|
||||
"[BasicDownloadFileByCurl] - file not exist !",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -598,7 +668,7 @@ func BasicDownloadFileWithProxy(downloadUrl, proxyUrl, desFile string) (download
|
||||
if !existAndNotNull {
|
||||
return false, []string{
|
||||
errLog,
|
||||
"[BasicDownloadFile] - file not exist !",
|
||||
"[BasicDownloadFileByCurl] - file not exist !",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user