-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dynu | ||
|
||
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 dynu. | ||
URL = "https://api.dynu.com/nic/update?hostname=%s&password=%s&%s" | ||
) | ||
|
||
// DNSProvider struct. | ||
type DNSProvider struct { | ||
configuration *settings.Settings | ||
} | ||
|
||
// Init passes DNS settings and store it to provider instance. | ||
func (provider *DNSProvider) Init(conf *settings.Settings) { | ||
provider.configuration = conf | ||
} | ||
|
||
func (provider *DNSProvider) UpdateIP(domainName, subdomainName, ip string) error { | ||
hostname := subdomainName + "." + domainName | ||
client := utils.GetHTTPClient(provider.configuration) | ||
return provider.update(client, hostname, subdomainName, ip) | ||
} | ||
|
||
func (provider *DNSProvider) update(client *http.Client, hostname, subdomain string, currentIP string) error { | ||
var ip string | ||
if strings.ToUpper(provider.configuration.IPType) == utils.IPV4 { | ||
ip = fmt.Sprintf("myip=%s", currentIP) | ||
} else if strings.ToUpper(provider.configuration.IPType) == utils.IPV6 { | ||
ip = fmt.Sprintf("myipv6=%s", currentIP) | ||
} | ||
|
||
req, _ := http.NewRequest("GET", fmt.Sprintf( | ||
URL, | ||
hostname, | ||
utils.GetMD5Hash(provider.configuration.Password), | ||
ip), nil) | ||
|
||
if provider.configuration.UserAgent != "" { | ||
req.Header.Add("User-Agent", provider.configuration.UserAgent) | ||
} | ||
|
||
// update IP with HTTP GET request | ||
resp, err := client.Do(req) | ||
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) | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil || !strings.Contains(string(body), "good") { | ||
log.Error("Failed to update the IP", err) | ||
return err | ||
} | ||
|
||
log.Infof("IP updated to: %s", currentIP) | ||
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
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,13 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
) | ||
|
||
// GetMD5Hash returns the MD5 hash of the input string. | ||
func GetMD5Hash(input string) string { | ||
hasher := md5.New() | ||
hasher.Write([]byte(input)) | ||
return hex.EncodeToString(hasher.Sum(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
func TestGetMD5Hash(t *testing.T) { | ||
type args struct { | ||
input string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{"test1", args{"test"}, "098f6bcd4621d373cade4e832627b4f6"}, | ||
{"test2", args{"test2"}, "ad0234829205b9033196ba818f7a872b"}, | ||
{"test3", args{"test3"}, "8ad8757baa8564dc136c1e07507f4a98"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetMD5Hash(tt.args.input); got != tt.want { | ||
t.Errorf("GetMD5Hash() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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