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

Add LoopiaSE as provider #197

Merged
merged 1 commit into from
Mar 1, 2023
Merged
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- [Scaleway](#scaleway)
- [Linode](#linode)
- [Strato](#strato)
- [LoopiaSE](#loopiase)
- [Notifications](#notifications)
- [Email](#email)
- [Telegram](#telegram)
Expand Down Expand Up @@ -87,6 +88,7 @@
| [Scaleway][Scaleway] | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| [Linode][linode] | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| [Strato][strato] | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: |
| [LoopiaSE][loopiase] | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: |


[cloudflare]: https://cloudflare.com
Expand All @@ -101,6 +103,7 @@
[Scaleway]: https://www.scaleway.com/
[Linode]: https://www.linode.com
[Strato]: https://strato.de
[LoopiaSE]: https://www.loopia.se/
Tip: You can follow this [issue](https://github.com/TimothyYe/godns/issues/76) to view the current status of DDNS for root domains.

## Supported Platforms
Expand Down Expand Up @@ -582,6 +585,36 @@ More Info: [German](https://www.strato.de/faq/hosting/so-einfach-richten-sie-dyn
```
</details>

#### LoopiaSE

For LoopiaSE, you need to provide username & password, and config all the domains & subdomains.
More info: [Swedish](https://support.loopia.se/wiki/om-dyndns-stodet/)

<details>
<summary>Example</summary>

```json
{
"provider": "LoopiaSE",
"email": "Your_Username",
"password": "Your_Password",
"domains": [{
"domain_name": "example.com",
"sub_domains": ["www","test"]
},{
"domain_name": "example2.com",
"sub_domains": ["www","test"]
}
],
"resolver": "8.8.8.8",
"ip_urls": ["https://api.ip.sb/ip"],
"ip_type": "IPv4",
"interval": 300,
"socks5_proxy": ""
}
```
</details>

### Notifications

GoDNS can send a notification each time the IP changes.
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/TimothyYe/godns/internal/provider/google"
"github.com/TimothyYe/godns/internal/provider/he"
"github.com/TimothyYe/godns/internal/provider/linode"
"github.com/TimothyYe/godns/internal/provider/loopiase"
"github.com/TimothyYe/godns/internal/provider/noip"
"github.com/TimothyYe/godns/internal/provider/scaleway"
"github.com/TimothyYe/godns/internal/provider/strato"
Expand Down Expand Up @@ -47,6 +48,8 @@ func GetProvider(conf *settings.Settings) (IDNSProvider, error) {
provider = &linode.DNSProvider{}
case utils.STRATO:
provider = &strato.DNSProvider{}
case utils.LOOPIASE:
provider = &loopiase.DNSProvider{}
default:
return nil, fmt.Errorf("Unknown provider '%s'", conf.Provider)
}
Expand Down
76 changes: 76 additions & 0 deletions internal/provider/loopiase/loopiase_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package loopiase

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 LoopiaSE.
URL = "https://%s:%[email protected]/?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
}
2 changes: 2 additions & 0 deletions internal/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
LINODE = "Linode"
// STRATO for Strato.
STRATO = "Strato"
// LOOPIASE for LoopiaSE.
LOOPIASE = "LoopiaSE"
// IPV4 for IPV4 mode.
IPV4 = "IPV4"
// IPV6 for IPV6 mode.
Expand Down
4 changes: 4 additions & 0 deletions internal/utils/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func CheckSettings(config *settings.Settings) error {
if config.Password == "" {
return errors.New("password cannot be empty")
}
case LOOPIASE:
if config.Password == "" {
return errors.New("password cannot be empty")
}
default:
message := fmt.Sprintf("'%s' is not a supported DNS provider", config.Provider)
return errors.New(message)
Expand Down