-
-
Notifications
You must be signed in to change notification settings - Fork 96
(WIP) feat(registry): add support for rdap/whois resolving #163
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
ezrizhu
wants to merge
2
commits into
natesales:main
Choose a base branch
from
ezrizhu:feat/rdap-whois
base: main
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,84 @@ | ||
| package registry | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/openrdap/rdap" | ||
| ) | ||
|
|
||
| // queryRDAP performs an RDAP lookup using github.com/openrdap/rdap. | ||
| // When opts.RDAPServer or a non-IANA RIR is set, the server URL is supplied | ||
| // directly on the request, bypassing bootstrap. | ||
| func queryRDAP(ctx context.Context, opts Options) (string, error) { | ||
| kind := Classify(opts.Target) | ||
|
|
||
| req, err := buildRDAPRequest(kind, opts.Target) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| base := opts.RDAPServer | ||
| if base == "" { | ||
| base = rirRDAPBase(opts.RIR) | ||
| } | ||
| if base != "" { | ||
| serverURL, err := url.Parse(base) | ||
| if err != nil { | ||
| return "", fmt.Errorf("registry: parse rdap server %q: %w", base, err) | ||
| } | ||
| req = req.WithServer(serverURL) | ||
| } | ||
| req = req.WithContext(ctx) | ||
| req.Timeout = opts.Timeout | ||
|
|
||
| client := &rdap.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return "", fmt.Errorf("registry: rdap: %w", err) | ||
| } | ||
| if resp == nil || resp.Object == nil { | ||
| return "", fmt.Errorf("registry: rdap: empty response") | ||
| } | ||
|
|
||
| return formatRDAP(resp, opts.Format) | ||
| } | ||
|
|
||
| func buildRDAPRequest(kind Kind, target string) (*rdap.Request, error) { | ||
| switch kind { | ||
| case KindDomain: | ||
| return &rdap.Request{Type: rdap.DomainRequest, Query: target}, nil | ||
| case KindIPv4, KindIPv6: | ||
| return &rdap.Request{Type: rdap.IPRequest, Query: target}, nil | ||
| case KindASN: | ||
| num := strings.TrimPrefix(strings.ToUpper(target), "AS") | ||
| return &rdap.Request{Type: rdap.AutnumRequest, Query: num}, nil | ||
| case KindNICHandle: | ||
| return &rdap.Request{Type: rdap.EntityRequest, Query: target}, nil | ||
| } | ||
| return nil, fmt.Errorf("registry: cannot classify rdap target %q", target) | ||
| } | ||
|
|
||
| func formatRDAP(resp *rdap.Response, format string) (string, error) { | ||
| switch strings.ToLower(format) { | ||
| case "json", "raw": | ||
| b, err := json.MarshalIndent(resp.Object, "", " ") | ||
| if err != nil { | ||
| return "", fmt.Errorf("registry: marshal rdap: %w", err) | ||
| } | ||
| return string(b), nil | ||
| } | ||
|
|
||
| if pretty := formatRDAPPretty(resp); pretty != "" { | ||
| return pretty, nil | ||
| } | ||
|
|
||
| b, err := json.MarshalIndent(resp.Object, "", " ") | ||
| if err != nil { | ||
| return "", fmt.Errorf("registry: marshal rdap: %w", err) | ||
| } | ||
| return string(b), nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: RDAP formatter ignores
yamland returns pretty/JSON output despite CLI advertising YAML support.Prompt for AI agents