-
-
Notifications
You must be signed in to change notification settings - Fork 241
Add TransIP provider #921
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
Open
LucaScorpion
wants to merge
8
commits into
qdm12:master
Choose a base branch
from
LucaScorpion:transip
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add TransIP provider #921
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1dbbbb2
Start work on transip provider, wip update func
LucaScorpion e0d7857
Fix lookupIPsResilient
LucaScorpion 59ee28c
Read private key, create access token
LucaScorpion 52663a4
Set user agent
LucaScorpion 7f780f5
Fix duplicate token labels, working update call
LucaScorpion 8d2a970
Working create or update logic
LucaScorpion 66e4838
Add docs
LucaScorpion cd592e4
Use encoding/pem package instead of custom string manipulation
LucaScorpion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| /data | ||
| /data | ||
| .idea |
This file contains hidden or 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 hidden or 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,42 @@ | ||
| # TransIP | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Example | ||
|
|
||
| ```json | ||
| { | ||
| "settings": [ | ||
| { | ||
| "provider": "transip", | ||
| "domain": "example.com", | ||
| "ip_version": "ipv4", | ||
| "ipv6_suffix": "", | ||
| "username": "username", | ||
| "key": "-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ### Compulsory parameters | ||
|
|
||
| - `"domain"` is the domain to update. It can be `example.com` (root domain), `sub.example.com` (subdomain of `example.com`) or `*.example.com` for the wildcard. | ||
| - `"username"` | ||
| - `"key"` | ||
|
|
||
| ### Optional parameters | ||
|
|
||
| - `"ip_version"` can be `ipv4` (A records), or `ipv6` (AAAA records) or `ipv4 or ipv6` (update one of the two, depending on the public ip found). It defaults to `ipv4 or ipv6`. | ||
| - `"ipv6_suffix"` is the IPv6 interface identifier suffix to use. It can be for example `0:0:0:0:72ad:8fbb:a54e:bedd/64`. If left empty, it defaults to no suffix and the raw public IPv6 address obtained is used in the record updating. | ||
|
|
||
| ## Domain setup | ||
|
|
||
| 1. Log in to the [TransIP control panel](https://www.transip.nl/cp/). | ||
| 2. Under your account, go to the [API settings](https://www.transip.nl/cp/account/api/). | ||
| 3. Enable the API.\ | ||
|  | ||
| 4. Add a key pair. Make sure to uncheck the checkbox to only accept IP addresses from the whitelist.\ | ||
|  | ||
| 5. Copy your private key, and store it in your config file.\ | ||
|  |
This file contains hidden or 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 hidden or 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 hidden or 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,285 @@ | ||
| package transip | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "crypto" | ||
| "crypto/rsa" | ||
| "crypto/sha512" | ||
| "crypto/x509" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/qdm12/ddns-updater/internal/models" | ||
| "github.com/qdm12/ddns-updater/internal/provider/constants" | ||
| "github.com/qdm12/ddns-updater/internal/provider/errors" | ||
| "github.com/qdm12/ddns-updater/internal/provider/headers" | ||
| "github.com/qdm12/ddns-updater/internal/provider/utils" | ||
| "github.com/qdm12/ddns-updater/pkg/publicip/ipversion" | ||
| "net/http" | ||
| "net/netip" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| type Provider struct { | ||
| domain string | ||
| owner string | ||
| ipVersion ipversion.IPVersion | ||
| ipv6Suffix netip.Prefix | ||
| username string | ||
| key string | ||
| } | ||
|
|
||
| func New(data json.RawMessage, domain, owner string, | ||
| ipVersion ipversion.IPVersion, ipv6Suffix netip.Prefix) ( | ||
| p *Provider, err error, | ||
| ) { | ||
| extraSettings := struct { | ||
| Username string `json:"username"` | ||
| Key string `json:"key"` | ||
| }{} | ||
| err = json.Unmarshal(data, &extraSettings) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = validateSettings(domain, extraSettings.Username, extraSettings.Key) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("validating provider specific settings: %w", err) | ||
| } | ||
|
|
||
| return &Provider{ | ||
| domain: domain, | ||
| owner: owner, | ||
| ipVersion: ipVersion, | ||
| ipv6Suffix: ipv6Suffix, | ||
| username: extraSettings.Username, | ||
| key: extraSettings.Key, | ||
| }, nil | ||
| } | ||
|
|
||
| func validateSettings(domain, username string, key string) (err error) { | ||
| err = utils.CheckDomain(domain) | ||
| if err != nil { | ||
| return fmt.Errorf("%w: %w", errors.ErrDomainNotValid, err) | ||
| } | ||
|
|
||
| if username == "" { | ||
| return errors.ErrUsernameNotSet | ||
| } | ||
|
|
||
| if key == "" { | ||
| return errors.ErrKeyNotSet | ||
| } | ||
|
|
||
| if _, err := parsePrivateKey(key); err != nil { | ||
| return fmt.Errorf("%w: %w", errors.ErrKeyNotValid, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *Provider) String() string { | ||
| return utils.ToString(p.domain, p.owner, constants.TransIP, p.ipVersion) | ||
| } | ||
|
|
||
| func (p *Provider) Domain() string { | ||
| return p.domain | ||
| } | ||
|
|
||
| func (p *Provider) Owner() string { | ||
| return p.owner | ||
| } | ||
|
|
||
| func (p *Provider) IPVersion() ipversion.IPVersion { | ||
| return p.ipVersion | ||
| } | ||
|
|
||
| func (p *Provider) IPv6Suffix() netip.Prefix { | ||
| return p.ipv6Suffix | ||
| } | ||
|
|
||
| func (p *Provider) Proxied() bool { | ||
| return false | ||
| } | ||
|
|
||
| func (p *Provider) BuildDomainName() string { | ||
| return utils.BuildDomainName(p.owner, p.domain) | ||
| } | ||
|
|
||
| func (p *Provider) HTML() models.HTMLRow { | ||
| return models.HTMLRow{ | ||
| Domain: fmt.Sprintf("<a href=\"http://%s\">%s</a>", p.BuildDomainName(), p.BuildDomainName()), | ||
| Owner: p.Owner(), | ||
| Provider: "<a href=\"https://www.transip.nl/\">TransIP</a>", | ||
| IPVersion: p.ipVersion.String(), | ||
| } | ||
| } | ||
|
|
||
| func parsePrivateKey(keyString string) (*rsa.PrivateKey, error) { | ||
| // Remove the begin and end markers, remove whitespace, and trim. | ||
| pemData := strings.ReplaceAll(keyString, "\n", "") | ||
| pemData = strings.ReplaceAll(pemData, "-----BEGIN PRIVATE KEY-----", "") | ||
| pemData = strings.ReplaceAll(pemData, "-----END PRIVATE KEY-----", "") | ||
| pemData = strings.TrimSpace(pemData) | ||
|
|
||
| decodedKey, err := base64.StdEncoding.DecodeString(pemData) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| key, err := x509.ParsePKCS8PrivateKey(decodedKey) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if rsaKey, ok := key.(*rsa.PrivateKey); ok { | ||
| return rsaKey, nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("not an RSA private key") | ||
| } | ||
|
|
||
| func (p *Provider) createAccessToken(ctx context.Context, client *http.Client) (string, error) { | ||
| requestBody, err := json.Marshal(map[string]any{ | ||
| "login": p.username, | ||
| "nonce": strconv.FormatInt(time.Now().UnixNano(), 10), | ||
| "global_key": true, | ||
| "read_only": false, | ||
| "label": fmt.Sprintf("ddns-updater %d", time.Now().Unix()), | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("json encoding request body: %w", err) | ||
| } | ||
|
|
||
| request, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.transip.nl/v6/auth", bytes.NewReader(requestBody)) | ||
| if err != nil { | ||
| return "", fmt.Errorf("creating http request: %w", err) | ||
| } | ||
| headers.SetUserAgent(request) | ||
| headers.SetContentType(request, "application/json") | ||
|
|
||
| privateKey, err := parsePrivateKey(p.key) | ||
| if err != nil { | ||
| return "", fmt.Errorf("parsing private key: %w", err) | ||
| } | ||
|
|
||
| // Sign the request body, put the signature in a header. | ||
| hashedBody := sha512.Sum512(requestBody) | ||
| signature, err := rsa.SignPKCS1v15(nil, privateKey, crypto.SHA512, hashedBody[:]) | ||
| if err != nil { | ||
| return "", fmt.Errorf("signing request: %w", err) | ||
| } | ||
| request.Header.Set("Signature", base64.StdEncoding.EncodeToString(signature)) | ||
|
|
||
| response, err := client.Do(request) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer response.Body.Close() | ||
|
|
||
| if response.StatusCode != http.StatusCreated { | ||
| return "", fmt.Errorf("%w: %d: %s", | ||
| errors.ErrHTTPStatusNotValid, response.StatusCode, utils.BodyToSingleLine(response.Body)) | ||
| } | ||
|
|
||
| var result struct { | ||
| Token string `json:"token"` | ||
| } | ||
| err = json.NewDecoder(response.Body).Decode(&result) | ||
| if err != nil { | ||
| return "", fmt.Errorf("json decoding response body: %w", err) | ||
| } | ||
|
|
||
| return result.Token, nil | ||
| } | ||
|
|
||
| func (p *Provider) Update(ctx context.Context, client *http.Client, ip netip.Addr) (newIP netip.Addr, err error) { | ||
| recordType := constants.A | ||
| if ip.Is6() { | ||
| recordType = constants.AAAA | ||
| } | ||
|
|
||
| token, err := p.createAccessToken(ctx, client) | ||
| if err != nil { | ||
| return netip.Addr{}, err | ||
| } | ||
|
|
||
| dnsApiUrl := fmt.Sprintf("https://api.transip.nl/v6/domains/%s/dns", p.domain) | ||
|
|
||
| // List the existing DNS entries. | ||
| request, err := http.NewRequestWithContext(ctx, http.MethodGet, dnsApiUrl, nil) | ||
| if err != nil { | ||
| return netip.Addr{}, fmt.Errorf("creating http request: %w", err) | ||
| } | ||
| headers.SetUserAgent(request) | ||
| headers.SetAuthBearer(request, token) | ||
| response, err := client.Do(request) | ||
| if err != nil { | ||
| return netip.Addr{}, err | ||
| } | ||
| defer response.Body.Close() | ||
| if response.StatusCode != http.StatusOK { | ||
| return netip.Addr{}, fmt.Errorf("%w: %d: %s", | ||
| errors.ErrHTTPStatusNotValid, response.StatusCode, utils.BodyToSingleLine(response.Body)) | ||
| } | ||
| var entries struct { | ||
| DnsEntries []dnsEntry `json:"dnsEntries"` | ||
| } | ||
| err = json.NewDecoder(response.Body).Decode(&entries) | ||
| if err != nil { | ||
| return netip.Addr{}, fmt.Errorf("json decoding response body: %w", err) | ||
| } | ||
|
|
||
| // Construct the basis of the new/updated entry. | ||
| updatedEntry := dnsEntry{ | ||
| Name: p.owner, | ||
| Expire: 300, | ||
| Type: recordType, | ||
| Content: ip.String(), | ||
| } | ||
| postOrPatch := http.MethodPost | ||
|
|
||
| // Check if there is a matching entry, based on the name and type. | ||
| for _, entry := range entries.DnsEntries { | ||
| if entry.Name == p.owner && entry.Type == recordType { | ||
| postOrPatch = http.MethodPatch | ||
| updatedEntry.Expire = entry.Expire | ||
| } | ||
| } | ||
|
|
||
| // Create or update the entry. | ||
| requestBody, err := json.Marshal(map[string]any{ | ||
| "dnsEntry": updatedEntry, | ||
| }) | ||
| if err != nil { | ||
| return netip.Addr{}, fmt.Errorf("json encoding request body: %w", err) | ||
| } | ||
| request, err = http.NewRequestWithContext(ctx, postOrPatch, dnsApiUrl, bytes.NewReader(requestBody)) | ||
| if err != nil { | ||
| return netip.Addr{}, fmt.Errorf("creating http request: %w", err) | ||
| } | ||
| headers.SetUserAgent(request) | ||
| headers.SetContentType(request, "application/json") | ||
| headers.SetAuthBearer(request, token) | ||
| response, err = client.Do(request) | ||
| if err != nil { | ||
| return netip.Addr{}, err | ||
| } | ||
| defer response.Body.Close() | ||
| if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusCreated { | ||
| return netip.Addr{}, fmt.Errorf("%w: %d: %s", | ||
| errors.ErrHTTPStatusNotValid, response.StatusCode, utils.BodyToSingleLine(response.Body)) | ||
| } | ||
|
|
||
| return ip, nil | ||
| } | ||
|
|
||
| type dnsEntry struct { | ||
| Name string `json:"name"` | ||
| Expire int `json:"expire"` | ||
| Type string `json:"type"` | ||
| Content string `json:"content"` | ||
| } | ||
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.