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

feat(digitalocean): get user IP over v4/v6 #123

Closed
Closed
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
66 changes: 49 additions & 17 deletions core/provider/digitalocean/ssh.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package digitalocean

import (
"time"
"context"
"crypto/rand"
"crypto/rsa"
Expand All @@ -10,6 +11,7 @@
"github.com/digitalocean/godo"
"golang.org/x/crypto/ssh"
"io"
"net"
"net/http"
"strings"
)
Expand Down Expand Up @@ -41,41 +43,71 @@
}

func getUserIPs(ctx context.Context) (ips []string, err error) {
res, err := http.Get("https://ifconfig.io")
Copy link
Contributor

Choose a reason for hiding this comment

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

ideally here, we try both ifconfig.io and ifconfig.co (different ASNs, so they'll get routed through different paths) and try both IPv4 and IPv6. we should also add the option in the provider to append a custom IP

ipv4s, err := getUserIPForNetwork(ctx, TCP4)

if err != nil {
return ips, err
}
errs := make([]error, 0)

defer res.Body.Close()
if err == nil {
ips = append(ips, ipv4s...)
} else {
errs = append(errs, err)
}

ifconfigIoIp, err := io.ReadAll(res.Body)
ipv6s, err := getUserIPForNetwork(ctx, TCP6)
if err == nil {
ips = append(ips, ipv6s...)
} else {
errs = append(errs, err)
}

if err != nil {
return ips, err
if len(ips) == 0 {
err = fmt.Errorf("failed to get user IP: %v", errs)

Check failure on line 64 in core/provider/digitalocean/ssh.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

ineffectual assignment to err (ineffassign)
Copy link
Contributor

Choose a reason for hiding this comment

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

looks loike this never gets returned?

}

ips = append(ips, strings.Trim(string(ifconfigIoIp), "\n"))
return ips, nil
}

res, err = http.Get("https://ifconfig.co")
type Network string

if err != nil {
return ips, err
const (
TCP6 Network = "tcp6"
TCP4 Network = "tcp4"
)

func getUserIPForNetwork(ctx context.Context, network Network) ([]string, error) {
client := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
dialer := net.Dialer{}
return dialer.DialContext(ctx, string(network), "https://ifconfig.io")
},
},
Timeout: 10 * time.Second,
}

defer res.Body.Close()
req, err := http.NewRequestWithContext(ctx, "GET", "https://ifconfig.io", nil)
if err != nil {
return nil, err
}

ifconfigCoIp, err := io.ReadAll(res.Body)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return ips, err
return nil, err
}

ips = append(ips, strings.Trim(string(ifconfigCoIp), "\n"))
ips := make([]string, 0)
ips = append(ips, strings.Trim(string(body), "\n"))

return removeDuplicateStr(ips), nil
return ips, nil
}


func (p *Provider) createSSHKey(ctx context.Context, pubKey string) (*godo.Key, error) {
req := &godo.KeyCreateRequest{PublicKey: pubKey, Name: fmt.Sprintf("%s-key", p.petriTag)}

Expand Down
Loading