Exemplo n.º 1
0
func (dyndns *Dyndns) GetResourceRecordSet(record string) string {
	svc := dyndns.getRoute53Client()

	params := &route53.ListResourceRecordSetsInput{
		HostedZoneId:          aws.String(dyndns.Config.ZoneId),
		MaxItems:              aws.String("20"),
		StartRecordIdentifier: aws.String("ResourceRecordSetIdentifier"),
		StartRecordName:       aws.String("DNSName"),
		StartRecordType:       aws.String("A"),
	}

	resp, err := svc.ListResourceRecordSets(params)
	utils.CheckErr(err)

	var recordResp *route53.ResourceRecordSet
	for _, rr := range resp.ResourceRecordSets {
		if aws.StringValue(rr.Type) != "A" {
			continue
		}

		rrName := aws.StringValue(rr.Name)
		rrName = strings.TrimSuffix(rrName, ".")

		if record == rrName {
			recordResp = rr
			break
		}
	}

	ip := *recordResp.ResourceRecords[0].Value
	return fmt.Sprintf("%s", ip)
}
Exemplo n.º 2
0
func LoadConfig(configFile string) *Config {
	// expand tilde to users home dir
	if strings.HasPrefix(configFile, "~") {
		u, _ := user.Current()
		configFile = strings.Replace(configFile, "~", u.HomeDir, 1)
	}

	log.Printf("config file is %s", configFile)

	// make sure the config file actually exists
	if _, err := os.Stat(configFile); os.IsNotExist(err) {
		utils.Fatalf("Error: couldn't load config from %s\n\n"+
			"If you haven't configured r53dyndns yet, place a "+
			"file that looks like this in ~/.r53dyndns:\n\n%s",
			configFile, SampleConfig())
	}

	// load config
	file, _ := os.Open(configFile)
	decoder := json.NewDecoder(file)
	config := Config{}
	if err := decoder.Decode(&config); err != nil {
		utils.Fatalf("error: %s", err)
	}

	// pre-flight check
	utils.CheckErr(config.PreFlight())

	return &config
}
Exemplo n.º 3
0
func main() {
	start_time := time.Now()
	var ip string

	flag.Parse()

	if *domain == "" {
		utils.Fatalf("Error: please supply the domain to update. Use -h for help.")
	}

	if !*verbose {
		log.SetOutput(ioutil.Discard)
	}

	log.Printf("starting Route 53 DNS update for %s", *domain)

	conf := config.LoadConfig(*configFile)
	r53 := dyndns.Dyndns{Config: conf}

	switch {
	case *staticIp != "":
		ip = *staticIp
		if !utils.ValidIP(ip) {
			utils.Fatalf("Error: %s is not a valid IP", ip)
		}
	case *interfaceIp != "":
		var err error
		ip, err = utils.GetInterfaceIP(*interfaceIp)
		utils.CheckErr(err)
	case *autoDetect:
		var err error
		ip, err = utils.GetExternalIP()
		utils.CheckErr(err)
	default:
		utils.Fatalf("Error: please supply one of -s, -a, -i. Use -h for help.")
	}

	// update record
	// XXX: only if it changed from the last run
	r53.UpsertDomain(*domain, ip)

	log.Printf("done - took %s", time.Since(start_time))
}
Exemplo n.º 4
0
func (dyndns *Dyndns) GetHostedZone() {
	svc := dyndns.getRoute53Client()

	params := &route53.GetHostedZoneInput{
		Id: aws.String(dyndns.Config.ZoneId),
	}
	resp, err := svc.GetHostedZone(params)
	utils.CheckErr(err)

	log.Printf(resp.String())
}
Exemplo n.º 5
0
func (dyndns *Dyndns) GetHostedZonesCount() {
	log.Printf("hello world")

	svc := dyndns.getRoute53Client()

	var params *route53.GetHostedZoneCountInput
	resp, err := svc.GetHostedZoneCount(params)
	utils.CheckErr(err)

	log.Printf("Zones: %d", *resp.HostedZoneCount)
}
Exemplo n.º 6
0
func (dyndns *Dyndns) UpsertDomain(domain string, ip string) {
	svc := dyndns.getRoute53Client()

	existingIp := dyndns.GetResourceRecordSet(domain)
	if existingIp == ip {
		log.Printf("existing IP is already set to %s", ip)
		return
	}

	log.Printf("updating %s with IP %s", domain, ip)

	params := &route53.ChangeResourceRecordSetsInput{
		ChangeBatch: &route53.ChangeBatch{
			Changes: []*route53.Change{
				{
					Action: aws.String(route53.ChangeActionUpsert),
					ResourceRecordSet: &route53.ResourceRecordSet{
						Name: aws.String(domain),
						Type: aws.String(route53.RRTypeA),
						ResourceRecords: []*route53.ResourceRecord{
							{
								Value: aws.String(ip),
							},
						},
						TTL: aws.Int64(60),
					},
				},
			},
			Comment: aws.String("Updated by r53dyndns"),
		},
		HostedZoneId: aws.String(dyndns.Config.ZoneId),
	}
	resp, err := svc.ChangeResourceRecordSets(params)
	utils.CheckErr(err)
	log.Println(resp)

	// force a log entry
	log.SetOutput(os.Stdout)
	log.Printf("external IP for %s changed from %s to %s", domain, existingIp, ip)
}