Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

each google subdomain can have(?) or has it's own email and password #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ func (handler *Handler) updateDNS(domain *settings.Domain, ip string) error {
}
}
}
for _, designated := range domain.DelegatedSubDomains {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I know what does the DelegatedSubDomains used for?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, this part of the code is only available for the Google handler?

Copy link
Owner

@TimothyYe TimothyYe Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is needed to check the other providers. I'm just not sure if this change will affect the other providers. For example, is the config DelegatedSubDomains compulsory? Or for the other providers can just ignore it from the config file?


hostname := designated.DomainName + "." + domain.DomainName

lastIP, err := utils.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
if err != nil && (errors.Is(err, errEmptyResult) || errors.Is(err, errEmptyDomain)) {
log.Errorf("Failed to resolve DNS for domain: %s, error: %s", hostname, err)
continue
}

//check against the current known IP, if no change, skip update
if ip == lastIP {
log.Infof("IP is the same as cached one (%s). Skip update.", ip)
} else {

handler.Configuration.Email = designated.Email
handler.Configuration.Password = designated.Password
if err := handler.dnsProvider.UpdateIP(domain.DomainName, designated.DomainName, ip); err != nil {
return err
}

successMessage := fmt.Sprintf("%s.%s", designated.DomainName, domain.DomainName)
handler.notificationManager.Send(successMessage, ip)

// execute webhook when it is enabled
if handler.Configuration.Webhook.Enabled {
if err := lib.GetWebhook(handler.Configuration).Execute(hostname, ip); err != nil {
return err
}
}
}
}

return nil
}
8 changes: 6 additions & 2 deletions internal/provider/google/google_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func (provider *DNSProvider) UpdateIP(domainName, subdomainName, ip string) erro
// updateIP update subdomain with current IP.
func (provider *DNSProvider) updateIP(domain, subDomain, currentIP string) error {
client := utils.GetHTTPClient(provider.configuration)
resp, err := client.Get(fmt.Sprintf(URL,
request := fmt.Sprintf(URL,
provider.configuration.Email,
provider.configuration.Password,
subDomain,
domain,
currentIP))
currentIP)
resp, err := client.Get(request)

if err != nil {
// handle error
Expand Down Expand Up @@ -69,6 +70,9 @@ func (provider *DNSProvider) updateIP(domain, subDomain, currentIP string) error
log.Infof("Update IP success: %s", string(body))
} else if strings.Contains(string(body), "nochg") {
log.Infof("IP not changed: %s", string(body))
} else {
// google api can return StatusOK even if it fails
log.Errorf("Update IP failed: %s", string(body))
}

return nil
Expand Down
28 changes: 25 additions & 3 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ const (
extYML = "yml"
)

// DelagatedSubDomain
type DelegatedSubDomain struct {
DomainName string `json:"domain_name" yaml:"domain_name"`
Email string `json:"email" yaml:"email"`
Password string `json:"password" yaml:"password"`
}

// Domain struct.
type Domain struct {
DomainName string `json:"domain_name" yaml:"domain_name"`
SubDomains []string `json:"sub_domains" yaml:"sub_domains"`
DomainName string `json:"domain_name" yaml:"domain_name"`
SubDomains []string `json:"sub_domains" yaml:"sub_domains"`
DelegatedSubDomains []DelegatedSubDomain `json:"delegated_sub_domains" yaml:"delegated_sub_domains"`
}

// SlackNotify struct for Slack notification.
Expand Down Expand Up @@ -170,7 +178,21 @@ func loadSecretsFromFile(settings *Settings) error {
settings.PasswordFile,
settings.Password,
); err != nil {
return fmt.Errorf("failed to load password from file: %w", err)
any := false
for _, domain := range settings.Domains {
for _, sub_domain := range domain.DelegatedSubDomains {
if len(sub_domain.Password) == 0 {
name := fmt.Sprint("%s:%s", domain.DomainName, sub_domain.DomainName)
return fmt.Errorf("failed to find delegated sub domain password: %w", errors.New(name))
} else {
any = true
}

}
}
if !any {
return fmt.Errorf("failed to load password from file: %w", err)
}
}

if settings.LoginToken, err = readSecretFromFile(
Expand Down
21 changes: 20 additions & 1 deletion internal/utils/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,26 @@ func CheckSettings(config *settings.Settings) error {
return errors.New("login token cannot be empty")
}
case GOOGLE:
fallthrough
any := false
if config.Email == "" {
for _, domain := range config.Domains {
for _, sub_domain := range domain.DelegatedSubDomains {
if len(sub_domain.Email) == 0 {
name := fmt.Sprint("failed to find delegated sub domain email: %s:%s", domain.DomainName, sub_domain.DomainName)
return errors.New(name)
} else {
any = true
}

}
}
if !any {
return errors.New("email cannot be empty")
}
}
if config.Password == "" && any == false {
return errors.New("password cannot be empty")
}
case NOIP:
if config.Email == "" {
return errors.New("email cannot be empty")
Expand Down