Changed Interval to use Scoends Instead of Minutes, and Now it only sends request when the IP changed
This commit is contained in:
parent
a06f35db99
commit
015755710a
Binary file not shown.
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"url": "https://triztech.pro/cpanelwebcall/TOKEN",
|
"update_url": "https://triztech.pro/cpanelwebcall/TOKEN",
|
||||||
"interval_minutes": 300
|
"check_interval_seconds": 30,
|
||||||
|
"ip_check_url": "https://api.ipify.org"
|
||||||
}
|
}
|
||||||
|
|||||||
71
main.go
71
main.go
@ -6,41 +6,55 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
URL string `json:"url"`
|
UpdateURL string `json:"update_url"`
|
||||||
IntervalMins int `json:"interval_minutes"`
|
CheckIntervalSecs int `json:"check_interval_seconds"`
|
||||||
|
IPCheckURL string `json:"ip_check_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// read config.json
|
|
||||||
func loadConfig(path string) (*Config, error) {
|
func loadConfig(path string) (*Config, error) {
|
||||||
file, err := ioutil.ReadFile(path)
|
data, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var cfg Config
|
var cfg Config
|
||||||
err = json.Unmarshal(file, &cfg)
|
err = json.Unmarshal(data, &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateDDNS(url string) {
|
func getPublicIP(ipURL string) (string, error) {
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(ipURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error calling DDNS URL:", err)
|
return "", err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(time.Now(), "DDNS updated successfully")
|
if err != nil {
|
||||||
} else {
|
return "", err
|
||||||
fmt.Println(time.Now(), "Failed to update DDNS, status code:", resp.StatusCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(string(body)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateDDNS(url string) error {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("failed, status code %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -50,13 +64,34 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(cfg.IntervalMins) * time.Minute)
|
//var lastIP string
|
||||||
|
lastIP, err := getPublicIP(cfg.IPCheckURL)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting intial public IP:", err)
|
||||||
|
lastIP = ""
|
||||||
|
} else {
|
||||||
|
fmt.Println(time.Now(), "Starting with current IP:", lastIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(time.Duration(cfg.CheckIntervalSecs) * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
// run immediately once
|
for {
|
||||||
updateDDNS(cfg.URL)
|
ip, err := getPublicIP(cfg.IPCheckURL)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting public IP:", err)
|
||||||
|
} else if ip != lastIP {
|
||||||
|
fmt.Println(time.Now(), "IP changed to", ip)
|
||||||
|
if err := updateDDNS(cfg.UpdateURL); err != nil {
|
||||||
|
fmt.Println("Failed to update DDNS:", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(time.Now(), "DDNS updated successfully")
|
||||||
|
lastIP = ip
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(time.Now(), "IP unchanged:", ip)
|
||||||
|
}
|
||||||
|
|
||||||
for range ticker.C {
|
<-ticker.C
|
||||||
updateDDNS(cfg.URL)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user