-
-
Notifications
You must be signed in to change notification settings - Fork 590
chore(restrictednet): add internal/restrictednet package
#3361
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
qdm12
wants to merge
20
commits into
master
Choose a base branch
from
restrictednet
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.
+1,007
−0
Open
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
aa781c6
initial
qdm12 fad8c98
Minor fixes
qdm12 a9a3664
imporatnt fix 1
qdm12 820689c
imporatnt fix 2
qdm12 c18c54c
Fix test to use a random port and not 443
qdm12 b48ba8c
review feedback
qdm12 2d2c371
pr review fixes
qdm12 8da913d
context aware connectSourceConnection
qdm12 e2256dd
moare fixes
qdm12 dd07205
add tests
qdm12 b5366b9
Change tests to be more integration oriented
qdm12 29186fe
Fix ordering in cleanup function
qdm12 69b4e5c
PR feedback fixes
qdm12 d28744e
pr review changes
qdm12 9af6aaf
PR feedback
qdm12 70d80f7
context aware connectFD
qdm12 b44c671
lint fix
qdm12 08dfd73
pr review feedback
qdm12 f6b2612
Merge branch 'master' into restrictednet
qdm12 106a4fd
Merge branch 'master' into restrictednet
qdm12 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
Some comments aren't visible on the classic Files Changed page.
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
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,80 @@ | ||
| package restrictednet | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "net/netip" | ||
| "strconv" | ||
|
|
||
| "github.com/qdm12/dns/v2/pkg/provider" | ||
| ) | ||
|
|
||
| // Client is a client for making restricted network requests, | ||
| // such as opening temporary firewall rules for HTTPS connections. | ||
| // It is not meant to be high performance, although it can be used for | ||
| // multiple requests and concurrently. | ||
| type Client struct { | ||
| outboundInterface string | ||
| ipv6Supported bool | ||
| firewall Firewall | ||
| dohServers []provider.DoHServer | ||
| } | ||
|
|
||
| func New(settings Settings) *Client { | ||
| if err := settings.validate(); err != nil { | ||
| panic(fmt.Sprintf("invalid settings: %v", err)) // programming error | ||
| } | ||
| dohServers := make([]provider.DoHServer, len(settings.UpstreamResolvers)) | ||
| for i, upstreamResolver := range settings.UpstreamResolvers { | ||
| dohServers[i] = upstreamResolver.DoH | ||
| } | ||
|
|
||
| return &Client{ | ||
| outboundInterface: settings.DefaultInterface, | ||
| ipv6Supported: *settings.IPv6Supported, | ||
| firewall: settings.Firewall, | ||
| dohServers: dohServers, | ||
| } | ||
| } | ||
|
|
||
| // OpenHTTPSByHostname opens an https connection through the firewall, | ||
| // to the hostname which in the format `host:port`. The returned cleanup | ||
| // function must be called to remove the temporary firewall rule and close connections. | ||
| // It first resolves the domain in hostname using DNS over HTTPS and then opens | ||
| // the restricted HTTPS connection to the resolved IP. | ||
|
qdm12 marked this conversation as resolved.
|
||
| func (c *Client) OpenHTTPSByHostname(ctx context.Context, hostname string) ( | ||
| httpClient *http.Client, cleanup func() error, err error, | ||
| ) { | ||
| host, portStr, err := net.SplitHostPort(hostname) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("splitting host and port: %w", err) | ||
| } | ||
| resolvedIPs, err := c.ResolveName(ctx, host) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("resolving name: %w", err) | ||
| } else if len(resolvedIPs) == 0 { | ||
| return nil, nil, fmt.Errorf("no IP address found for name %q", host) | ||
| } | ||
|
|
||
| portUint, err := strconv.ParseUint(portStr, 10, 16) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("parsing port: %w", err) | ||
| } | ||
| port := uint16(portUint) | ||
|
qdm12 marked this conversation as resolved.
|
||
|
|
||
| errs := make([]error, 0, len(resolvedIPs)) | ||
| for _, ip := range resolvedIPs { | ||
| addrPort := netip.AddrPortFrom(ip, port) | ||
| httpClient, cleanup, err := c.OpenHTTPS(ctx, host, addrPort) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("for %s: %w", ip, err)) | ||
| continue | ||
| } | ||
| return httpClient, cleanup, nil | ||
| } | ||
|
|
||
| return nil, nil, fmt.Errorf("opening HTTPS to %s: %w", hostname, errors.Join(errs...)) | ||
| } | ||
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,5 @@ | ||
| package restrictednet | ||
|
|
||
| func ptrTo[T any](value T) *T { | ||
| return &value | ||
| } |
Oops, something went wrong.
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.