First
This commit is contained in:
commit
a06f35db99
25
Makefile
Normal file
25
Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
APP_NAME = DDNSUpdater
|
||||
BIN_DIR = bin
|
||||
VERSION = 0.1.0
|
||||
|
||||
all: build run
|
||||
|
||||
build:
|
||||
@if not exist $(BIN_DIR) mkdir $(BIN_DIR)
|
||||
go build -ldflags "-s -w -X main.version=$(VERSION)" -o $(BIN_DIR)\$(APP_NAME).exe .
|
||||
|
||||
run:
|
||||
go run .
|
||||
|
||||
clean:
|
||||
@if exist $(BIN_DIR) rmdir /s /q $(BIN_DIR)
|
||||
|
||||
windows:
|
||||
@if not exist $(BIN_DIR) mkdir $(BIN_DIR)
|
||||
set GOOS=windows&& set GOARCH=amd64&& go build -o $(BIN_DIR)\$(APP_NAME).exe .
|
||||
|
||||
tidy:
|
||||
go mod tidy
|
||||
|
||||
fmt:
|
||||
go fmt ./...
|
||||
BIN
bin/DDNSUpdater.exe
Normal file
BIN
bin/DDNSUpdater.exe
Normal file
Binary file not shown.
4
config.json
Normal file
4
config.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"url": "https://triztech.pro/cpanelwebcall/TOKEN",
|
||||
"interval_minutes": 300
|
||||
}
|
||||
62
main.go
Normal file
62
main.go
Normal file
@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string `json:"url"`
|
||||
IntervalMins int `json:"interval_minutes"`
|
||||
}
|
||||
|
||||
// read config.json
|
||||
func loadConfig(path string) (*Config, error) {
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cfg Config
|
||||
err = json.Unmarshal(file, &cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func updateDDNS(url string) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Println("Error calling DDNS URL:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
fmt.Println(time.Now(), "DDNS updated successfully")
|
||||
} else {
|
||||
fmt.Println(time.Now(), "Failed to update DDNS, status code:", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg, err := loadConfig("config.json")
|
||||
if err != nil {
|
||||
fmt.Println("Failed to load config:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Duration(cfg.IntervalMins) * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
// run immediately once
|
||||
updateDDNS(cfg.URL)
|
||||
|
||||
for range ticker.C {
|
||||
updateDDNS(cfg.URL)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user