bell-system/main.go

92 lines
2.2 KiB
Go

package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"time"
"github.com/icholy/digest"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"gopkg.in/yaml.v2"
)
type config struct {
Horn map[string]string `yaml:"horn"`
Times []string `yaml:"times"`
}
func (config *config) getConf() *config {
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
yamlFile, err := os.ReadFile("config.yaml")
if err != nil {
logger.Error("yamlFile.Get", logger.Args(":", err))
}
err = yaml.Unmarshal(yamlFile, config)
if err != nil {
logger.Error("Unmarshal", logger.Args(":", err))
}
logger.Info("Config loaded", logger.Args("Times", config.Times, "API URL", config.Horn["url"]))
return config
}
func main() {
pterm.Print("\n\n")
s, _ := pterm.DefaultBigText.WithLetters(
putils.LettersFromStringWithStyle("Bell ", pterm.FgRed.ToStyle()),
putils.LettersFromStringWithStyle("System", pterm.FgBlue.ToStyle())).
Srender()
pterm.DefaultCenter.Println(s)
pterm.DefaultCenter.Println("By Cody (" + pterm.LightBlue("https://oki.cx/tree") + ")")
pterm.DefaultCenter.Println("v1.1")
pterm.Print("\n")
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
var config config
config.getConf()
logger.Info("Bell system activated!")
for {
ctime := time.Now().Format("15:04:05")
for _, time := range config.Times {
if time == ctime {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{
Transport: &digest.Transport{
Username: config.Horn["username"],
Password: config.Horn["password"],
},
}
req, err := http.NewRequest("GET", config.Horn["url"], nil)
if err != nil {
logger.Error(fmt.Sprintf("%s", err))
}
resp, err := client.Do(req)
if err != nil {
logger.Error(fmt.Sprintf("%s", err))
}
if resp.StatusCode != 200 {
logger.Error(fmt.Sprintf("HTTP request failed: %s", resp.Status))
} else {
logger.Info(fmt.Sprintf("Sending HTTP request to %s", config.Horn["url"]), logger.Args("Reason", "Config File", "Time", ctime, "Status Code", resp.Status))
}
}
}
time.Sleep(time.Second)
}
}