Compare commits

...

2 Commits

Author SHA1 Message Date
54e087b2e1 up 2024-07-01 23:25:13 +07:00
8d5d5ca34a update 2024-07-01 23:24:53 +07:00
8 changed files with 92 additions and 6 deletions

BIN
PushDXCluster.exe Normal file

Binary file not shown.

47
config.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
Gotify struct {
URL string `yaml:"url"`
Token string `yanl:"token"`
} `yaml:"gotify"`
Cluster struct {
Host string `yaml:"host"`
Call string `yaml:"call"`
} `yaml:"cluster"`
}
func NewConfig(configPath string) (*Config, error) {
config := &Config{}
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func ValidateConfigPath(path string) error {
s, err := os.Stat(path)
if err != nil {
return err
}
if s.IsDir() {
return fmt.Errorf("'%s' is a directory, not a normal file", path)
}
return nil
}

View File

@ -3,5 +3,5 @@ gotify:
token: ALaGS4MVMWTEMcP token: ALaGS4MVMWTEMcP
cluster: cluster:
host: dxc.nc7j.com:23 host: arc.jg1vgx.net:7000
call: XV9Q call: XV9Q

2
go.mod
View File

@ -1,3 +1,5 @@
module gitea.rouggy.com/PushDXCluster module gitea.rouggy.com/PushDXCluster
go 1.21.4 go 1.21.4
require gopkg.in/yaml.v2 v2.4.0

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

43
main.go
View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -33,6 +34,26 @@ type ClusterMessage struct {
Time string Time string
} }
func ParseFlags() (string, error) {
// String that contains the configured configuration path
var configPath string
// Set up a CLI flag called "-config" to allow users
// to supply the configuration file
flag.StringVar(&configPath, "config", "./config.yml", "path to config file")
// Actually parse the flags
flag.Parse()
// Validate the path first
if err := ValidateConfigPath(configPath); err != nil {
return "", err
}
// Return the configuration path
return configPath, nil
}
// Message structure for Gotify // Message structure for Gotify
type GotifyMessage struct { type GotifyMessage struct {
Title string `json:"title"` Title string `json:"title"`
@ -57,7 +78,7 @@ func readDXExpeFile(filename string) string {
} }
// Function to send message to Gotify // Function to send message to Gotify
func sendToGotify(title string, sMess ClusterMessage, priority int) { func sendToGotify(title string, sMess ClusterMessage, priority int, cfg Config) {
message := fmt.Sprintf("DX: %s\nFrom: %s\nFreq: %s\nMode: %s\nReport: %s\nTime: %s", sMess.DX, sMess.From, sMess.Freq, sMess.Mode, sMess.Report, sMess.Time) message := fmt.Sprintf("DX: %s\nFrom: %s\nFreq: %s\nMode: %s\nReport: %s\nTime: %s", sMess.DX, sMess.From, sMess.Freq, sMess.Mode, sMess.Report, sMess.Time)
@ -73,13 +94,13 @@ func sendToGotify(title string, sMess ClusterMessage, priority int) {
return return
} }
req, err := http.NewRequest("POST", gotifyURL, bytes.NewBuffer(jsonData)) req, err := http.NewRequest("POST", cfg.Gotify.URL, bytes.NewBuffer(jsonData))
if err != nil { if err != nil {
log.Println("Error creating request:", err) log.Println("Error creating request:", err)
return return
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer ALaGS4MVMWTEMcP") req.Header.Add("Authorization", "Bearer "+cfg.Gotify.Token)
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
@ -125,10 +146,22 @@ func SanitizeClusterMessage(message string) ClusterMessage {
} }
func main() { func main() {
// Generate our config based on the config supplied
// by the user in the flags
cfgPath, err := ParseFlags()
if err != nil {
log.Fatal(err)
}
cfg, err := NewConfig(cfgPath)
if err != nil {
log.Fatal(err)
}
fmt.Println("PushDXCluster v0.1") fmt.Println("PushDXCluster v0.1")
for { for {
// Connect to the Telnet server // Connect to the Telnet server
conn, err := net.Dial("tcp", "ve7cc.net:23") conn, err := net.Dial("tcp", cfg.Cluster.Host)
if err != nil { if err != nil {
log.Printf("Failed to connect to Telnet server: %v", err) log.Printf("Failed to connect to Telnet server: %v", err)
time.Sleep(5 * time.Second) // Wait before retrying time.Sleep(5 * time.Second) // Wait before retrying
@ -187,7 +220,7 @@ func main() {
sMess := SanitizeClusterMessage(message) sMess := SanitizeClusterMessage(message)
log.Printf("Received message: %s", message) log.Printf("Received message: %s", message)
if sMess.DX != "" && sMess.From == "XV9Q" && strings.Contains(DX, sMess.DX) { if sMess.DX != "" && sMess.From == "XV9Q" && strings.Contains(DX, sMess.DX) {
sendToGotify("Spot", sMess, 5) sendToGotify("Spot", sMess, 5, *cfg)
} }
} }

BIN
rsrc_windows_386.syso Normal file

Binary file not shown.

BIN
rsrc_windows_amd64.syso Normal file

Binary file not shown.