-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Infomaniak as provider (#219)
Add support for Infomaniak(.com), follows the same schema as Google Domains
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package infomaniak | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/TimothyYe/godns/internal/settings" | ||
"github.com/TimothyYe/godns/internal/utils" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
// URL the API address for Infomaniak. | ||
URL = "https://%s:%[email protected]/nic/update?hostname=%s.%s&myip=%s" | ||
) | ||
|
||
// DNSProvider struct. | ||
type DNSProvider struct { | ||
configuration *settings.Settings | ||
} | ||
|
||
// Init passes DNS settings and store it to the provider instance. | ||
func (provider *DNSProvider) Init(conf *settings.Settings) { | ||
provider.configuration = conf | ||
} | ||
|
||
func (provider *DNSProvider) UpdateIP(domainName, subdomainName, ip string) error { | ||
return provider.updateIP(domainName, subdomainName, ip) | ||
} | ||
|
||
// 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, | ||
provider.configuration.Email, | ||
provider.configuration.Password, | ||
subDomain, | ||
domain, | ||
currentIP)) | ||
|
||
if err != nil { | ||
// handle error | ||
log.Error("Failed to update sub domain:", subDomain) | ||
return err | ||
} | ||
|
||
defer func(Body io.ReadCloser) { | ||
err := Body.Close() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}(resp.Body) | ||
|
||
if err != nil { | ||
log.Error("Err:", err.Error()) | ||
return err | ||
} | ||
|
||
body, _ := io.ReadAll(resp.Body) | ||
if resp.StatusCode != http.StatusOK { | ||
log.Errorf("Update IP failed: %s", string(body)) | ||
return fmt.Errorf("update IP failed: %s", string(body)) | ||
} | ||
|
||
if strings.Contains(string(body), "good") { | ||
log.Infof("Update IP success: %s", string(body)) | ||
} else if strings.Contains(string(body), "nochg") { | ||
log.Infof("IP not changed: %s", string(body)) | ||
} else { | ||
return fmt.Errorf("update IP failed: %s", string(body)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters