package main

import (
	"encoding/base64"
	"io"
	"net"
	"net/http"
	"os/exec"
	"strings"
	"time"
)

func main() {
	//POST参数 mac="0x"+base64加密(mac地址)+&product=产品名称+&user=?
	mac := GetMac()
	url := "https://www.360xyz.cn/api/app-login"
	kvString := "mac=0x" + base64.StdEncoding.EncodeToString([]byte(mac)) + "&product=电脑定时故障&user=webserver"
	//fmt.Println(kvString)
	//设置定时器为100秒
	timer := time.NewTimer(60 * time.Second)
	//fmt.Println("定时开始")
	for {
		<-timer.C
		//100秒后执行这条
		re, err1 := Post(url, kvString)
		if err1 == nil {
			//fmt.Println(re)
			if re != "登陆成功" {
				arg := []string{"-s", "-t", "0"}
				cmd := exec.Command("shutdown", arg...)
				cmd.CombinedOutput()
			}
		} else {
			timer.Reset(60 * time.Second)
		}
	}

}
func GetMac() string {
	netInterfaces, err := net.Interfaces()
	if err != nil {
		return ""
	}
	for _, i2 := range netInterfaces {
		//fmt.Println(i2.Name)
		mac := i2.HardwareAddr
		//fmt.Println(mac)
		return mac.String()
	}
	return ""
}
func Post(url string, KVstring string) (string, error) {
	var err error
	myhttp := &http.Client{}
	myhttp.Timeout = time.Duration(3 * time.Second)
	resp, err1 := myhttp.Post(url, "application/x-www-form-urlencoded; charset=UTF-8", strings.NewReader(KVstring))
	if err1 != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err2 := io.ReadAll(resp.Body)
	if err2 != nil {
		return "", err
	}
	return string(body), nil
}
