Merge branch 'master' of github.com:/mfycheng/name-dyndns
This commit is contained in:
commit
0056049bf8
6 changed files with 43 additions and 41 deletions
44
api/api.go
44
api/api.go
|
@ -13,24 +13,24 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
productionUrl = "https://api.name.com/"
|
||||
devUrl = "https://api.dev.name.com/"
|
||||
productionURL = "https://api.name.com/"
|
||||
devURL = "https://api.dev.name.com/"
|
||||
)
|
||||
|
||||
// Contains details required to access the Name.com API.
|
||||
// API Contains details required to access the Name.com API.
|
||||
type API struct {
|
||||
baseUrl string
|
||||
baseURL string
|
||||
username string
|
||||
token string
|
||||
}
|
||||
|
||||
// A Name.com DNS record.
|
||||
// DNSRecord contains information about a Name.com DNS record.
|
||||
type DNSRecord struct {
|
||||
RecordId string `json:"record_id"`
|
||||
RecordID string `json:"record_id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
Ttl string `json:"ttl"`
|
||||
TTL string `json:"ttl"`
|
||||
CreateDate string `json:"create_date"`
|
||||
}
|
||||
|
||||
|
@ -39,21 +39,21 @@ type resultResponse struct {
|
|||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// Constructs a new Name.com API. If dev is true, then
|
||||
// NewNameAPI constructs a new Name.com API. If dev is true, then
|
||||
// the API uses the development API, instead of the production API.
|
||||
func NewNameAPI(username, token string, dev bool) API {
|
||||
a := API{username: username, token: token}
|
||||
|
||||
if dev {
|
||||
a.baseUrl = devUrl
|
||||
a.baseURL = devURL
|
||||
} else {
|
||||
a.baseUrl = productionUrl
|
||||
a.baseURL = productionURL
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Constructs a new Name.com API from a configuration.
|
||||
// NewAPIFromConfig constructs a new Name.com API from a configuration.
|
||||
func NewAPIFromConfig(c Config) API {
|
||||
return NewNameAPI(c.Username, c.Token, c.Dev)
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ func (api API) performRequest(method, url string, body io.Reader) (response []by
|
|||
return ioutil.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// Create a DNS record for a given domain. The name
|
||||
// CreateDNSRecord creates a DNS record for a given domain. The name
|
||||
// field in DNSRecord is in the format [hostname].[domainname]
|
||||
func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
||||
// We need to transform name -> hostname for JSON.
|
||||
|
@ -85,13 +85,13 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
|||
Hostname string `json:"hostname"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
Ttl string `json:"ttl"`
|
||||
TTL string `json:"ttl"`
|
||||
}
|
||||
|
||||
body.Hostname = record.Name
|
||||
body.Type = record.Type
|
||||
body.Content = record.Content
|
||||
body.Ttl = record.Ttl
|
||||
body.TTL = record.TTL
|
||||
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
|
@ -100,7 +100,7 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
|||
|
||||
resp, err := api.performRequest(
|
||||
"POST",
|
||||
fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/create/", domain),
|
||||
fmt.Sprintf("%s%s%s", api.baseURL, "api/dns/create/", domain),
|
||||
bytes.NewBuffer(b),
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -122,13 +122,13 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Deletes a DNS record for a given domain. The recordId can
|
||||
// DeleteDNSRecord deletes a DNS record for a given domain. The recordID can
|
||||
// be retreived from GetDNSRecords.
|
||||
func (api API) DeleteDNSRecord(domain, recordId string) error {
|
||||
func (api API) DeleteDNSRecord(domain, recordID string) error {
|
||||
var body struct {
|
||||
RecordId string `json:"record_id"`
|
||||
RecordID string `json:"record_id"`
|
||||
}
|
||||
body.RecordId = recordId
|
||||
body.RecordID = recordID
|
||||
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
|
@ -137,7 +137,7 @@ func (api API) DeleteDNSRecord(domain, recordId string) error {
|
|||
|
||||
resp, err := api.performRequest(
|
||||
"POST",
|
||||
fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/delete/", domain),
|
||||
fmt.Sprintf("%s%s%s", api.baseURL, "api/dns/delete/", domain),
|
||||
bytes.NewBuffer(b),
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -159,11 +159,11 @@ func (api API) DeleteDNSRecord(domain, recordId string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Returns a slice of DNS records associated with a given domain.
|
||||
// GetDNSRecords returns a slice of DNS records associated with a given domain.
|
||||
func (api API) GetDNSRecords(domain string) (records []DNSRecord, err error) {
|
||||
resp, err := api.performRequest(
|
||||
"GET",
|
||||
fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/list/", domain),
|
||||
fmt.Sprintf("%s%s%s", api.baseURL, "api/dns/list/", domain),
|
||||
nil,
|
||||
)
|
||||
|
||||
|
|
|
@ -5,11 +5,10 @@ import (
|
|||
"io/ioutil"
|
||||
)
|
||||
|
||||
// Each configuration should represent a specifc domain.
|
||||
// A domain can have multiple hostnames, and the hostnames
|
||||
// that should be automaticall updated are in Hostnames.
|
||||
// If the root domain is to be managed, an empty hostname
|
||||
// should be added to the Hostnames slice.
|
||||
// Config represents the configuration for a
|
||||
// specific domain. Each domain can have multiple
|
||||
// hostnames, including the root domain, where
|
||||
// hostname is an empty string.
|
||||
//
|
||||
// The interval is the polling time (in seconds) for
|
||||
// daemon mode.
|
||||
|
@ -22,7 +21,7 @@ type Config struct {
|
|||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// Loads configurations from a file. The configuration
|
||||
// LoadConfigs loads configurations from a file. The configuration
|
||||
// is stored as an array of JSON serialized Config structs.
|
||||
func LoadConfigs(path string) ([]Config, error) {
|
||||
var configs struct {
|
||||
|
|
|
@ -9,7 +9,7 @@ var expectedConfigs []Config
|
|||
|
||||
func init() {
|
||||
expectedConfigs = []Config{
|
||||
Config{
|
||||
{
|
||||
Username: "dev-account",
|
||||
Token: "asdasdasdasdasdad",
|
||||
Interval: 60,
|
||||
|
@ -17,7 +17,7 @@ func init() {
|
|||
Domain: "test.com",
|
||||
Hostnames: []string{"mail", "chat"},
|
||||
},
|
||||
Config{
|
||||
{
|
||||
Username: "production-account",
|
||||
Token: "123123123123",
|
||||
Interval: 3600,
|
||||
|
|
|
@ -26,9 +26,9 @@ func contains(c api.Config, val string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error {
|
||||
func updateDNSRecord(a api.API, domain, recordID string, newRecord api.DNSRecord) error {
|
||||
log.Logger.Printf("Deleting DNS record for %s.\n", newRecord.Name)
|
||||
err := a.DeleteDNSRecord(domain, newRecord.RecordId)
|
||||
err := a.DeleteDNSRecord(domain, newRecord.RecordID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -90,11 +90,11 @@ func runConfig(c api.Config, daemon bool) {
|
|||
log.Logger.Printf("Running update check for %s.", r.Name)
|
||||
if r.Content != ip {
|
||||
r.Content = ip
|
||||
err = updateDNSRecord(a, c.Domain, r.RecordId, r)
|
||||
err = updateDNSRecord(a, c.Domain, r.RecordID, r)
|
||||
if err != nil {
|
||||
log.Logger.Printf("Failed to update record %s [%s] with IP: %s\n\t%s\n", r.RecordId, r.Name, ip, err)
|
||||
log.Logger.Printf("Failed to update record %s [%s] with IP: %s\n\t%s\n", r.RecordID, r.Name, ip, err)
|
||||
} else {
|
||||
log.Logger.Printf("Updated record %s [%s] with IP: %s\n", r.RecordId, r.Name, ip)
|
||||
log.Logger.Printf("Updated record %s [%s] with IP: %s\n", r.RecordID, r.Name, ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -110,10 +110,13 @@ func runConfig(c api.Config, daemon bool) {
|
|||
}
|
||||
}
|
||||
|
||||
// For each domain, check if the host record matches
|
||||
// the current external IP. If it does not, it updates.
|
||||
// If daemon is true, then Run will run forever, polling at
|
||||
// an interval specified in each config.
|
||||
// Run will process each configuration in configs.
|
||||
// If daemon is true, then Run will run forever,
|
||||
// processing each configuration at its specified
|
||||
// interval.
|
||||
//
|
||||
// Each configuration represents a domain with
|
||||
// multiple hostnames.
|
||||
func Run(configs []api.Config, daemon bool) {
|
||||
for _, config := range configs {
|
||||
wg.Add(1)
|
||||
|
|
|
@ -28,7 +28,7 @@ func tryMirror(url string) (string, error) {
|
|||
return string(contents), nil
|
||||
}
|
||||
|
||||
// Retrieves the external facing IP Address.
|
||||
// GetExternalIP retrieves the external facing IP Address.
|
||||
// If multiple mirrors are provided in Urls,
|
||||
// it will try each one (in order), should
|
||||
// preceding mirrors fail.
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
// Global logger.
|
||||
var Logger *log.Logger
|
||||
|
||||
// Initialize the logger with a specific io.Writer.
|
||||
// Init intializes the logger with a specific io.Writer.
|
||||
// This function is generally called near startup.
|
||||
func Init(writer io.Writer) {
|
||||
Logger = log.New(writer, "", log.Ldate|log.Ltime)
|
||||
|
|
Loading…
Reference in a new issue