560 lines
15 KiB
Go
560 lines
15 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"ddns-manager/db"
|
|
"ddns-manager/templates"
|
|
|
|
"github.com/cloudflare/cloudflare-go"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
_ "github.com/mattn/go-sqlite3" // SQLite driver
|
|
"github.com/robfig/cron/v3"
|
|
)
|
|
|
|
var (
|
|
api *cloudflare.API
|
|
scheduler *cron.Cron
|
|
lastIP string
|
|
jobID cron.EntryID
|
|
queries *db.Queries
|
|
)
|
|
|
|
func initDatabase() (*sql.DB, error) {
|
|
dbPath := os.Getenv("DB_PATH")
|
|
if dbPath == "" {
|
|
dbPath = "./ddns.db"
|
|
}
|
|
|
|
log.Printf("Using database path: %s", dbPath)
|
|
|
|
sqlDB, err := sql.Open("sqlite3", dbPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
|
|
if err := db.InitSchema(sqlDB); err != nil {
|
|
return nil, fmt.Errorf("failed to initialize schema: %w", err)
|
|
}
|
|
|
|
queries = db.New(sqlDB)
|
|
return sqlDB, nil
|
|
}
|
|
|
|
func initCloudflare(apiToken, zoneID string) error {
|
|
if apiToken == "" || zoneID == "" {
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
api, err = cloudflare.NewWithAPIToken(apiToken)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize Cloudflare API: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getCurrentIP() (string, error) {
|
|
resp, err := http.Get("https://api.ipify.org")
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to get current IP: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
ip, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to read IP response: %w", err)
|
|
}
|
|
|
|
return string(ip), nil
|
|
}
|
|
|
|
func getDNSRecords(zoneID string) ([]templates.DNSRecord, error) {
|
|
if api == nil {
|
|
return nil, fmt.Errorf("cloudflare API not initialized")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
rc := cloudflare.ZoneIdentifier(zoneID)
|
|
|
|
recs, _, err := api.ListDNSRecords(ctx, rc, cloudflare.ListDNSRecordsParams{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get DNS records: %w", err)
|
|
}
|
|
|
|
var records []templates.DNSRecord
|
|
for _, rec := range recs {
|
|
records = append(records, templates.DNSRecord{
|
|
ID: rec.ID,
|
|
Type: rec.Type,
|
|
Name: rec.Name,
|
|
Content: rec.Content,
|
|
TTL: rec.TTL,
|
|
Proxied: *rec.Proxied,
|
|
CreatedOn: rec.CreatedOn.Format(time.RFC3339),
|
|
})
|
|
}
|
|
|
|
return records, nil
|
|
}
|
|
|
|
func createDNSRecord(zoneID, domain, name, recordType, content string, ttl int, proxied bool) error {
|
|
if api == nil {
|
|
return fmt.Errorf("cloudflare API not initialized")
|
|
}
|
|
|
|
if !strings.HasSuffix(name, domain) && name != "@" {
|
|
name = name + "." + domain
|
|
}
|
|
|
|
if name == "@" {
|
|
name = domain
|
|
}
|
|
|
|
ctx := context.Background()
|
|
rc := cloudflare.ZoneIdentifier(zoneID)
|
|
|
|
_, err := api.CreateDNSRecord(ctx, rc, cloudflare.CreateDNSRecordParams{
|
|
Type: recordType,
|
|
Name: name,
|
|
Content: content,
|
|
TTL: ttl,
|
|
Proxied: &proxied,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create DNS record: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func updateDNSRecord(zoneID, id, name, recordType, content string, ttl int, proxied bool) error {
|
|
if api == nil {
|
|
return fmt.Errorf("cloudflare API not initialized")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
rc := cloudflare.ZoneIdentifier(zoneID)
|
|
|
|
_, err := api.UpdateDNSRecord(ctx, rc, cloudflare.UpdateDNSRecordParams{
|
|
ID: id,
|
|
Type: recordType,
|
|
Name: name,
|
|
Content: content,
|
|
TTL: ttl,
|
|
Proxied: &proxied,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update DNS record: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func deleteDNSRecord(zoneID, id string) error {
|
|
if api == nil {
|
|
return fmt.Errorf("cloudflare API not initialized")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
rc := cloudflare.ZoneIdentifier(zoneID)
|
|
|
|
err := api.DeleteDNSRecord(ctx, rc, id)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete DNS record: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func updateAllRecordsWithCurrentIP(zoneID string) error {
|
|
if api == nil {
|
|
return fmt.Errorf("cloudflare API not initialized")
|
|
}
|
|
|
|
currentIP, err := getCurrentIP()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if currentIP == lastIP {
|
|
log.Println("IP hasn't changed, no updates needed")
|
|
return nil
|
|
}
|
|
|
|
lastIP = currentIP
|
|
|
|
ctx := context.Background()
|
|
rc := cloudflare.ZoneIdentifier(zoneID)
|
|
|
|
records, _, err := api.ListDNSRecords(ctx, rc, cloudflare.ListDNSRecordsParams{
|
|
Type: "A",
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get DNS records: %w", err)
|
|
}
|
|
|
|
for _, rec := range records {
|
|
if rec.Content != currentIP {
|
|
proxied := rec.Proxied
|
|
_, err := api.UpdateDNSRecord(ctx, rc, cloudflare.UpdateDNSRecordParams{
|
|
ID: rec.ID,
|
|
Type: rec.Type,
|
|
Name: rec.Name,
|
|
Content: currentIP,
|
|
TTL: rec.TTL,
|
|
Proxied: proxied,
|
|
})
|
|
if err != nil {
|
|
log.Printf("Failed to update record %s: %v", rec.Name, err)
|
|
} else {
|
|
log.Printf("Updated record %s to %s", rec.Name, currentIP)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func scheduleUpdates(zoneID, updatePeriod string) error {
|
|
if jobID != 0 {
|
|
scheduler.Remove(jobID)
|
|
}
|
|
|
|
if updatePeriod == "" {
|
|
log.Println("Automatic updates disabled")
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
jobID, err = scheduler.AddFunc(updatePeriod, func() {
|
|
log.Println("Running scheduled IP update")
|
|
if err := updateAllRecordsWithCurrentIP(zoneID); err != nil {
|
|
log.Printf("Scheduled update failed: %v", err)
|
|
}
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to schedule updates: %w", err)
|
|
}
|
|
|
|
log.Printf("Scheduled IP updates with cron: %s", updatePeriod)
|
|
return nil
|
|
}
|
|
|
|
func getUpdateFrequencies() []templates.UpdateFrequency {
|
|
return []templates.UpdateFrequency{
|
|
{Label: "Every 1 minute", Value: "*/1 * * * *"},
|
|
{Label: "Every 5 minutes", Value: "*/5 * * * *"},
|
|
{Label: "Every 30 minutes", Value: "*/30 * * * *"},
|
|
{Label: "Hourly", Value: "0 * * * *"},
|
|
{Label: "Every 6 hours", Value: "0 */6 * * *"},
|
|
{Label: "Daily", Value: "0 0 * * *"},
|
|
{Label: "Never (manual only)", Value: ""},
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
sqlDB, err := initDatabase()
|
|
if err != nil {
|
|
log.Fatalf("Failed to initialize database: %v", err)
|
|
}
|
|
defer sqlDB.Close()
|
|
|
|
config, err := queries.GetConfig(context.Background())
|
|
if err != nil {
|
|
log.Printf("Warning: Failed to load configuration: %v", err)
|
|
config = db.Config{
|
|
Domain: "mz.uy",
|
|
UpdatePeriod: "0 */6 * * *",
|
|
}
|
|
}
|
|
|
|
if err := initCloudflare(config.ApiToken, config.ZoneID); err != nil {
|
|
log.Printf("Warning: Cloudflare initialization failed: %v", err)
|
|
}
|
|
|
|
scheduler = cron.New()
|
|
scheduler.Start()
|
|
defer scheduler.Stop()
|
|
|
|
if config.ApiToken != "" && config.ZoneID != "" && config.UpdatePeriod != "" {
|
|
if err := scheduleUpdates(config.ZoneID, config.UpdatePeriod); err != nil {
|
|
log.Printf("Warning: Failed to schedule updates: %v", err)
|
|
}
|
|
}
|
|
|
|
e := echo.New()
|
|
e.Use(middleware.Logger())
|
|
e.Use(middleware.Recover())
|
|
e.Static("/assets", "assets")
|
|
|
|
// Main page
|
|
e.GET("/", func(c echo.Context) error {
|
|
currentIP, _ := getCurrentIP()
|
|
|
|
var records []templates.DNSRecord
|
|
isConfigured := config.ApiToken != "" && config.ZoneID != ""
|
|
|
|
if isConfigured {
|
|
records, _ = getDNSRecords(config.ZoneID)
|
|
}
|
|
|
|
component := templates.Index(templates.IndexProps{
|
|
Title: "mz.uy DNS Manager",
|
|
IsConfigured: isConfigured,
|
|
CurrentIP: currentIP,
|
|
Config: templates.ConfigData{
|
|
ZoneID: config.ZoneID,
|
|
Domain: config.Domain,
|
|
UpdatePeriod: config.UpdatePeriod,
|
|
ApiToken: config.ApiToken,
|
|
},
|
|
Records: records,
|
|
UpdateFreqs: getUpdateFrequencies(),
|
|
})
|
|
return templates.Render(c.Response(), component)
|
|
})
|
|
|
|
// Refresh current IP
|
|
e.GET("/refresh-ip", func(c echo.Context) error {
|
|
ip, err := getCurrentIP()
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to get current IP")
|
|
return c.String(http.StatusInternalServerError, "Error")
|
|
}
|
|
return c.String(http.StatusOK, ip)
|
|
})
|
|
|
|
// Configuration
|
|
e.POST("/config", func(c echo.Context) error {
|
|
apiToken := c.FormValue("api_token")
|
|
zoneID := c.FormValue("zone_id")
|
|
domain := c.FormValue("domain")
|
|
updatePeriod := c.FormValue("update_period")
|
|
|
|
if apiToken == "" || zoneID == "" || domain == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "Please fill all required fields")
|
|
return c.String(http.StatusBadRequest, "Invalid input")
|
|
}
|
|
|
|
err := queries.UpsertConfig(context.Background(), db.UpsertConfigParams{
|
|
ApiToken: apiToken,
|
|
ZoneID: zoneID,
|
|
Domain: domain,
|
|
UpdatePeriod: updatePeriod,
|
|
})
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to save configuration")
|
|
return c.String(http.StatusInternalServerError, "Database error")
|
|
}
|
|
|
|
config.ApiToken = apiToken
|
|
config.ZoneID = zoneID
|
|
config.Domain = domain
|
|
config.UpdatePeriod = updatePeriod
|
|
|
|
if err := initCloudflare(config.ApiToken, config.ZoneID); err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to initialize Cloudflare client")
|
|
return c.String(http.StatusInternalServerError, "API error")
|
|
}
|
|
|
|
if err := scheduleUpdates(config.ZoneID, config.UpdatePeriod); err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to schedule updates")
|
|
return c.String(http.StatusInternalServerError, "Scheduler error")
|
|
}
|
|
|
|
c.Response().Header().Set("HX-Success-Message", "Configuration saved successfully")
|
|
return c.Redirect(http.StatusSeeOther, "/")
|
|
})
|
|
|
|
// Create DNS record
|
|
e.POST("/records", func(c echo.Context) error {
|
|
if config.ApiToken == "" || config.ZoneID == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "API not configured")
|
|
return c.String(http.StatusBadRequest, "Not configured")
|
|
}
|
|
|
|
name := c.FormValue("name")
|
|
recordType := c.FormValue("type")
|
|
content := c.FormValue("content")
|
|
ttlStr := c.FormValue("ttl")
|
|
proxied := c.FormValue("proxied") == "on"
|
|
useMyIP := c.FormValue("use_my_ip") == "on"
|
|
|
|
if name == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "Name is required")
|
|
return c.String(http.StatusBadRequest, "Invalid input")
|
|
}
|
|
|
|
ttl, err := strconv.Atoi(ttlStr)
|
|
if err != nil {
|
|
ttl = 1
|
|
}
|
|
|
|
if useMyIP {
|
|
currentIP, err := getCurrentIP()
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to get current IP")
|
|
return c.String(http.StatusInternalServerError, "IP error")
|
|
}
|
|
content = currentIP
|
|
}
|
|
|
|
if content == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "Content is required")
|
|
return c.String(http.StatusBadRequest, "Invalid input")
|
|
}
|
|
|
|
err = createDNSRecord(config.ZoneID, config.Domain, name, recordType, content, ttl, proxied)
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to create DNS record")
|
|
return c.String(http.StatusInternalServerError, "DNS error")
|
|
}
|
|
|
|
c.Response().Header().Set("HX-Success-Message", "DNS record created successfully")
|
|
|
|
// Return updated records table
|
|
records, _ := getDNSRecords(config.ZoneID)
|
|
currentIP, _ := getCurrentIP()
|
|
component := templates.DNSRecordsTable(records, currentIP)
|
|
return templates.Render(c.Response(), component)
|
|
})
|
|
|
|
// Update DNS record
|
|
e.PUT("/records/:id", func(c echo.Context) error {
|
|
if config.ApiToken == "" || config.ZoneID == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "API not configured")
|
|
return c.String(http.StatusBadRequest, "Not configured")
|
|
}
|
|
|
|
id := c.Param("id")
|
|
name := c.FormValue("name")
|
|
recordType := c.FormValue("type")
|
|
content := c.FormValue("content")
|
|
ttlStr := c.FormValue("ttl")
|
|
proxied := c.FormValue("proxied") == "on"
|
|
useMyIP := c.FormValue("use_my_ip") == "on"
|
|
|
|
ttl, err := strconv.Atoi(ttlStr)
|
|
if err != nil {
|
|
ttl = 1
|
|
}
|
|
|
|
if useMyIP {
|
|
currentIP, err := getCurrentIP()
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to get current IP")
|
|
return c.String(http.StatusInternalServerError, "IP error")
|
|
}
|
|
content = currentIP
|
|
}
|
|
|
|
// Convert name to full domain name if needed
|
|
fullName := name
|
|
if name != "@" && !strings.HasSuffix(name, config.Domain) {
|
|
fullName = name + "." + config.Domain
|
|
}
|
|
if name == "@" {
|
|
fullName = config.Domain
|
|
}
|
|
|
|
err = updateDNSRecord(config.ZoneID, id, fullName, recordType, content, ttl, proxied)
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to update DNS record")
|
|
return c.String(http.StatusInternalServerError, "DNS error")
|
|
}
|
|
|
|
c.Response().Header().Set("HX-Success-Message", "DNS record updated successfully")
|
|
|
|
// Return updated records table
|
|
records, _ := getDNSRecords(config.ZoneID)
|
|
currentIP, _ := getCurrentIP()
|
|
component := templates.DNSRecordsTable(records, currentIP)
|
|
return templates.Render(c.Response(), component)
|
|
})
|
|
|
|
// Delete DNS record
|
|
e.DELETE("/records/:id", func(c echo.Context) error {
|
|
if config.ApiToken == "" || config.ZoneID == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "API not configured")
|
|
return c.String(http.StatusBadRequest, "Not configured")
|
|
}
|
|
|
|
id := c.Param("id")
|
|
err := deleteDNSRecord(config.ZoneID, id)
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to delete DNS record")
|
|
return c.String(http.StatusInternalServerError, "DNS error")
|
|
}
|
|
|
|
c.Response().Header().Set("HX-Success-Message", "DNS record deleted successfully")
|
|
return c.String(http.StatusOK, "")
|
|
})
|
|
|
|
// Edit record form
|
|
e.GET("/edit-record/:id", func(c echo.Context) error {
|
|
if config.ApiToken == "" || config.ZoneID == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "API not configured")
|
|
return c.String(http.StatusBadRequest, "Not configured")
|
|
}
|
|
|
|
id := c.Param("id")
|
|
records, err := getDNSRecords(config.ZoneID)
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to load DNS records")
|
|
return c.String(http.StatusInternalServerError, "DNS error")
|
|
}
|
|
|
|
var record templates.DNSRecord
|
|
for _, r := range records {
|
|
if r.ID == id {
|
|
record = r
|
|
break
|
|
}
|
|
}
|
|
|
|
if record.ID == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "Record not found")
|
|
return c.String(http.StatusNotFound, "Not found")
|
|
}
|
|
|
|
component := templates.RecordForm("Edit DNS Record", id, config.Domain, record)
|
|
return templates.Render(c.Response(), component)
|
|
})
|
|
|
|
// Update all records with current IP
|
|
e.POST("/update-all-records", func(c echo.Context) error {
|
|
if config.ApiToken == "" || config.ZoneID == "" {
|
|
c.Response().Header().Set("HX-Error-Message", "API not configured")
|
|
return c.String(http.StatusBadRequest, "Not configured")
|
|
}
|
|
|
|
err := updateAllRecordsWithCurrentIP(config.ZoneID)
|
|
if err != nil {
|
|
c.Response().Header().Set("HX-Error-Message", "Failed to update records")
|
|
return c.String(http.StatusInternalServerError, "Update error")
|
|
}
|
|
|
|
c.Response().Header().Set("HX-Success-Message", "All A records updated with current IP")
|
|
|
|
// Return updated records table
|
|
records, _ := getDNSRecords(config.ZoneID)
|
|
currentIP, _ := getCurrentIP()
|
|
component := templates.DNSRecordsTable(records, currentIP)
|
|
return templates.Render(c.Response(), component)
|
|
})
|
|
|
|
log.Println("Starting server on http://localhost:3000")
|
|
log.Fatal(e.Start(":3000"))
|
|
}
|