-
Notifications
You must be signed in to change notification settings - Fork 243
feat: add remote IP address to connection stats sidebar #944
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
Merged
+613
−24
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a54d4c
feat: add remote IP address to connection stats sidebar
ym 7d2fe79
feat: add public IP card to settings network page
ym d6d113e
fix: remoteIPaddress can't be null
ym 1097dee
fix: clean-up cancel function
ym e9df46b
chore(public-ip): use interface state to set public IP ready state
ym 1671a24
cosm(public-ip): update public IP card to show last updated time
ym 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
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
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,160 @@ | ||
| package myip | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/jetkvm/kvm/pkg/nmlite/link" | ||
| ) | ||
|
|
||
| func (ps *PublicIPState) request(ctx context.Context, url string, family int) ([]byte, error) { | ||
| client := ps.httpClient(family) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "GET", url, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating request: %w", err) | ||
| } | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error sending request: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error reading response body: %w", err) | ||
| } | ||
|
|
||
| return body, err | ||
| } | ||
|
|
||
| // checkCloudflare uses cdn-cgi/trace to get the public IP address | ||
| func (ps *PublicIPState) checkCloudflare(ctx context.Context, family int) (*PublicIP, error) { | ||
| u, err := url.JoinPath(ps.cloudflareEndpoint, "/cdn-cgi/trace") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error joining path: %w", err) | ||
| } | ||
|
|
||
| body, err := ps.request(ctx, u, family) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| values := make(map[string]string) | ||
| for line := range strings.SplitSeq(string(body), "\n") { | ||
| key, value, ok := strings.Cut(line, "=") | ||
| if !ok { | ||
| continue | ||
| } | ||
| values[key] = value | ||
| } | ||
|
|
||
| ps.lastUpdated = time.Now() | ||
| if ts, ok := values["ts"]; ok { | ||
| if ts, err := strconv.ParseFloat(ts, 64); err == nil { | ||
| ps.lastUpdated = time.Unix(int64(ts), 0) | ||
| } | ||
| } | ||
|
|
||
| ipStr, ok := values["ip"] | ||
| if !ok { | ||
| return nil, fmt.Errorf("no IP address found") | ||
| } | ||
|
|
||
| ip := net.ParseIP(ipStr) | ||
| if ip == nil { | ||
| return nil, fmt.Errorf("invalid IP address: %s", ipStr) | ||
| } | ||
|
|
||
| return &PublicIP{ | ||
| IPAddress: ip, | ||
| LastUpdated: ps.lastUpdated, | ||
| }, nil | ||
| } | ||
|
|
||
| // checkAPI uses the API endpoint to get the public IP address | ||
| func (ps *PublicIPState) checkAPI(_ context.Context, _ int) (*PublicIP, error) { | ||
| return nil, fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| // checkIPs checks both IPv4 and IPv6 public IP addresses in parallel | ||
| // and updates the IPAddresses slice with the results | ||
| func (ps *PublicIPState) checkIPs(ctx context.Context, checkIPv4, checkIPv6 bool) error { | ||
| var wg sync.WaitGroup | ||
| var mu sync.Mutex | ||
| var ips []PublicIP | ||
| var errors []error | ||
|
|
||
| checkFamily := func(family int, familyName string) { | ||
| wg.Add(1) | ||
| go func(f int, name string) { | ||
| defer wg.Done() | ||
|
|
||
| ip, err := ps.checkIPForFamily(ctx, f) | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| if err != nil { | ||
| errors = append(errors, fmt.Errorf("%s check failed: %w", name, err)) | ||
| return | ||
| } | ||
| if ip != nil { | ||
| ips = append(ips, *ip) | ||
| } | ||
| }(family, familyName) | ||
| } | ||
|
|
||
| if checkIPv4 { | ||
| checkFamily(link.AfInet, "IPv4") | ||
| } | ||
|
|
||
| if checkIPv6 { | ||
| checkFamily(link.AfInet6, "IPv6") | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| if len(ips) > 0 { | ||
| ps.mu.Lock() | ||
| defer ps.mu.Unlock() | ||
|
|
||
| ps.addresses = ips | ||
| ps.lastUpdated = time.Now() | ||
| } | ||
|
|
||
| if len(errors) > 0 && len(ips) == 0 { | ||
| return errors[0] | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (ps *PublicIPState) checkIPForFamily(ctx context.Context, family int) (*PublicIP, error) { | ||
| if ps.apiEndpoint != "" { | ||
| ip, err := ps.checkAPI(ctx, family) | ||
| if err == nil && ip != nil { | ||
| return ip, nil | ||
| } | ||
| } | ||
|
|
||
| if ps.cloudflareEndpoint != "" { | ||
| ip, err := ps.checkCloudflare(ctx, family) | ||
| if err == nil && ip != nil { | ||
| return ip, nil | ||
| } | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("all IP check methods failed for family %d", family) | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We silently discard any failure to parse
tsbut that's probably okay...