ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

go将青龙面板里面的脚本文件都下载到本地

2021-11-28 01:31:44  阅读:182  来源: 互联网

标签:string err nil fmt 青龙 json go path 面板


纯粹练手用的,大家轻喷
青龙面板的脚本文件可以下载到本地,这样的话自己可以研究一下对应的脚本文件,能学到更多的知识,原理其实很简单,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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有