Exemplo n.º 1
0
func BuildUpdateURL(user *url.Userinfo, host Host) (string, error) {
	family, err := godnsupdater.FamilyFromString(host.AddressFamily)
	if err != nil {
		return "", err
	}

	address, err := godnsupdater.GetInterfaceIP(host.Interface, family)
	if err != nil {
		return "", err
	}

	qv := url.Values{}
	qv.Set("content-type", "json")
	qv.Set("h", host.Name)
	qv.Set("ip", address)

	endpoint, err := url.Parse(familyEndpoints[family])
	if err != nil {
		return "", err
	}

	endpoint.RawQuery = qv.Encode()
	endpoint.User = user

	return endpoint.String(), nil
}
Exemplo n.º 2
0
func main() {
	flag.Parse()
	log.Println("gogogandidns - Gandi Dynamic DNS updater by Diego Elio Pettenò <*****@*****.**>")

	if *apiFilePath == "" {
		log.Fatalf("Missing value for -api_file")
	}

	if *ifaceName == "" {
		log.Fatalf("Missing value for -iface")
	}

	family, err := godnsupdater.FamilyFromString(*addrFamily)
	if err != nil {
		log.Fatalf("Invalid value for -family: %v", err)
	}

	if *zoneId == 0 {
		log.Fatalf("Missing value for -zone")
	}

	if *recordName == "" {
		log.Fatalf("Missing value for -record")
	}

	api, err := getApiKey(*apiFilePath)
	if err != nil {
		log.Fatal(err)
	}

	address, err := godnsupdater.GetInterfaceIP(*ifaceName, family)
	if err != nil {
		log.Fatal(err)
	}

	env := client.Production
	if *useTestingEnvironment {
		env = client.Testing
	}
	c := client.New(api, env)

	newVersion, err := cloneLatestZone(c, *zoneId)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("New version %v created for zone %v", newVersion, *zoneId)

	recordType := godnsupdater.DnsTypeByFamily[family]

	// Get the list of current entries, filter on the name and type of record, as delete all the records of the same type already present.
	r := record.New(c)
	rInfos, err := r.List(*zoneId, newVersion)
	if err != nil {
		log.Fatal(err)
	}
	for _, rInfo := range rInfos {
		if rInfo.Name == *recordName && rInfo.Type == recordType {
			log.Printf("Removing record \"%v\" with ID %v", rInfo.Name, rInfo.Id)
			ok, err := r.Delete(*zoneId, newVersion, rInfo.Id)
			if err != nil {
				log.Fatal(err)
			}
			if !ok {
				log.Fatalf("Deleting record failed, but no error returned.")
			}
		}
	}

	addArgs := record.RecordAdd{
		Zone:    *zoneId,
		Version: newVersion,
		Name:    *recordName,
		Type:    recordType,
		Value:   address,
		Ttl:     300,
	}
	rInfo, err := r.Add(addArgs)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("New record \"%v\" created with ID %v", rInfo.Name, rInfo.Id)

	zv := version.New(c)
	ok, err := zv.Set(*zoneId, newVersion)
	if err != nil {
		log.Fatal(err)
	}
	if !ok {
		log.Fatalf("Setting new version live failed, but no error returned.")
	}
	log.Printf("Version %v set live for zone %v", newVersion, *zoneId)
}