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"`
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM