文章

XXL-JOB golang trigger调用

背景

xxl-job本身支持python等脚本创建调度任务,但是有时候也需要主动触发,所以抓包写了一个golang小程序,可以通过手机快捷指令快速调用xxl-job上的任务。

Show Code

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

const host = "http://127.0.0.1:8080"
const username = "admin"
const password = "admin"

func main() {
	http.HandleFunc("/changeIp", func(writer http.ResponseWriter, request *http.Request) {
		queryParams := request.URL.Query()

		// 检查是否包含test参数
		if _, exists := queryParams["test"]; !exists {
			// 如果不包含bo1sd参数,返回权限不足
			writer.Write([]byte("权限不足"))
			return
		}
		currentTime := time.Now()
		// 打印当前时间
		fmt.Println("调用一次,当前时间:", currentTime.Format("2006-01-02 15:04:05"))
		cookie := getLoginCookie()
		res := oneCall(cookie, 30)
		writer.Write([]byte(res))
	})

	err := http.ListenAndServe(":8118", nil)
	if err != nil {
		fmt.Println(err)
	}

}

func oneCall(cookie string, id int) string {

	url := host + "/jobinfo/trigger"
	method := "POST"

	payload := strings.NewReader(fmt.Sprintf(`id=%d&executorParam=&addressList=`, id))

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println(err)
		return ""
	}
	req.Header.Add("accept", "application/json, text/javascript, */*; q=0.01")
	req.Header.Add("accept-language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7")
	req.Header.Add("cache-control", "no-cache")
	req.Header.Add("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
	req.Header.Add("cookie", cookie)
	req.Header.Add("origin", "https://xxl-job.boommanpro.com")
	req.Header.Add("pragma", "no-cache")
	req.Header.Add("priority", "u=1, i")
	req.Header.Add("referer", "https://xxl-job.boommanpro.com/jobinfo?jobGroup=3")
	req.Header.Add("sec-ch-ua", "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"")
	req.Header.Add("sec-ch-ua-mobile", "?0")
	req.Header.Add("sec-ch-ua-platform", "\"macOS\"")
	req.Header.Add("sec-fetch-dest", "empty")
	req.Header.Add("sec-fetch-mode", "cors")
	req.Header.Add("sec-fetch-site", "same-origin")
	req.Header.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
	req.Header.Add("x-requested-with", "XMLHttpRequest")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return string(body)
}

func getLoginCookie() string {

	url := host + "/login"
	method := "POST"

	payload := strings.NewReader(fmt.Sprintf(`userName=%s&password=%s&ifRemember=on`, username, password))

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println(err)
		return ""
	}
	req.Header.Add("accept", "*/*")
	req.Header.Add("accept-language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7")
	req.Header.Add("cache-control", "no-cache")
	req.Header.Add("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
	req.Header.Add("origin", "https://xxl-job.boommanpro.com")
	req.Header.Add("pragma", "no-cache")
	req.Header.Add("priority", "u=1, i")
	req.Header.Add("referer", "https://xxl-job.boommanpro.com/toLogin")
	req.Header.Add("sec-ch-ua", "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"")
	req.Header.Add("sec-ch-ua-mobile", "?0")
	req.Header.Add("sec-ch-ua-platform", "\"macOS\"")
	req.Header.Add("sec-fetch-dest", "empty")
	req.Header.Add("sec-fetch-mode", "cors")
	req.Header.Add("sec-fetch-site", "same-origin")
	req.Header.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
	req.Header.Add("x-requested-with", "XMLHttpRequest")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	defer res.Body.Close()

	return res.Header.Get("set-cookie")
}