go将青龙面板里面的脚本文件都下载到本地
作者:互联网
纯粹练手用的,大家轻喷
青龙面板的脚本文件可以下载到本地,这样的话自己可以研究一下对应的脚本文件,能学到更多的知识,原理其实很简单,F12一下就知道了,青龙面板使用Request Headers里面放入Authorization,那么Token我们已经拿到了,然后获取到所有文件的名称,分级目录,太过于简单,直接上代码了
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
func main() {
recordbody := getData("http://yourIp:5600/api/scripts/files?t=")
var conf recordConfig
err := json.Unmarshal(recordbody, &conf)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("\r\n获取到的body code:%s \n", strconv.Itoa(conf.Code))
for _, val := range conf.Data {
if val.Children != nil {
for _, childval := range val.Children {
childbody := getData(fmt.Sprintf("http://yourIp:5600/api/scripts/%s?path=%s&t=", childval.Value, childval.Parent))
var jsconf jsConfig
err := json.Unmarshal(childbody, &jsconf)
if err != nil {
fmt.Println("error:", err)
}
downloadFile(strings.NewReader(string(jsconf.Data)), childval.Parent, childval.Value)
}
} else {
childbody := getData(fmt.Sprintf("http://yourIp:5600/api/scripts/%s?t=", val.Value))
var jsconf jsConfig
err := json.Unmarshal(childbody, &jsconf)
if err != nil {
fmt.Println("error:", err)
}
downloadFile(strings.NewReader(string(jsconf.Data)), "", val.Value)
}
}
fmt.Println("执行完毕")
}
func getData(urlstr string) []byte {
times := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
var bt bytes.Buffer
bt.WriteString(urlstr)
bt.WriteString(times)
fmt.Printf(bt.String())
fmt.Printf("\n")
client := &http.Client{}
req, _ := http.NewRequest("GET", bt.String(), nil)
req.Header.Add("Authorization", "Bearer yourToken")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
func downloadFile(body io.Reader, path string, name string) {
filepath := fmt.Sprintf("./%s", name)
// Create output file
if path != "" {
if _, err := os.Stat(path); os.IsNotExist(err) {
// 必须分成两步:先创建文件夹、再修改权限
os.Mkdir(path, 0777) //0777也可以os.ModePerm
os.Chmod(path, 0777)
}
filepath = fmt.Sprintf("./%s/%s", path, name)
}
out, err := os.Create(filepath)
if err != nil {
panic(err)
}
defer out.Close()
// copy stream
_, err = io.Copy(out, body)
if err != nil {
panic(err)
}
}
type jsConfig struct {
Code int `json:"code"`
Data string `json:"data"`
}
type recordConfig struct {
Code int `json:"code"`
Data []bodymsg `json:"data"`
}
type bodymsg struct {
Disabled bool `json:"disabled"`
Key string `json:"key"`
Mtime float32 `json:"mtime"`
Title string `json:"title"`
Value string `json:"value"`
Children []bodymsgchildren `json:"children"`
}
type bodymsgchildren struct {
Key string `json:"key"`
Mtime float32 `json:"mtime"`
Title string `json:"title"`
Value string `json:"value"`
Parent string `json:"parent"`
}
标签:string,err,nil,fmt,青龙,json,go,path,面板 来源: https://www.cnblogs.com/wangpengzong/p/15614177.html