chore(restrictednet): add internal/restrictednet package#3361
Open
qdm12 wants to merge 20 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Introduces a new internal/restrictednet package intended to perform DNS-over-HTTPS name resolution and HTTPS requests while temporarily opening highly-restrictive outbound firewall rules (scoped by source IP/port and destination IP/port), supporting the “restricted internet when VPN tunnel is down” workflow referenced in #3358.
Changes:
- Add
internal/restrictednetclient APIs for DoH-based name resolution and for opening temporary HTTPS egress rules. - Extend the firewall abstraction and iptables implementation with
AcceptOutputFromIPPortToIPPort. - Add unit tests and mock generation for the new package, plus update agent guidance in
AGENTS.md.
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/restrictednet/client.go | Adds Client constructor and OpenHTTPSByDomain helper wiring DoH resolution to HTTPS rule opening. |
| internal/restrictednet/https.go | Implements HTTPS client creation with bound dialing and temporary firewall rule add/remove. |
| internal/restrictednet/resolve.go | Implements DoH POST queries and parsing DNS answers into netip.Addr results. |
| internal/restrictednet/client_test.go | Adds unit test coverage for OpenHTTPS firewall rule add/remove behavior. |
| internal/restrictednet/resolve_test.go | Adds unit test coverage for parsing DNS answers into netip.Addr. |
| internal/restrictednet/interfaces.go | Defines the Firewall interface used by restrictednet. |
| internal/restrictednet/mocks_generate_test.go | Adds go:generate directive for GoMock generation. |
| internal/restrictednet/mocks_test.go | Adds generated GoMock for the restrictednet Firewall interface. |
| internal/firewall/wrappers.go | Exposes a new wrapper method on firewall.Config. |
| internal/firewall/interfaces.go | Extends the internal firewall implementation interface with the new accept rule method. |
| internal/firewall/iptables/iptables.go | Adds iptables/ip6tables rule generation for source+dest IP/port-specific output acceptance. |
| AGENTS.md | Updates repository agent/coding guidance with additional preferences. |
Files not reviewed (1)
- internal/restrictednet/mocks_test.go: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+195
to
+198
| if message.Attributes.Src.To4() == nil { | ||
| return netip.AddrFrom16([16]byte(message.Attributes.Src)), nil | ||
| } | ||
| return netip.AddrFrom4([4]byte(message.Attributes.Src)), nil |
Comment on lines
+139
to
+142
| response, err := httpClient.Do(request) | ||
| if err != nil { | ||
| return nil, err | ||
| } |
Comment on lines
+51
to
+58
| bitsIndex := fd / 64 //nolint:mnd | ||
| if bitsIndex >= len(unix.FdSet{}.Bits) { | ||
| return fmt.Errorf("fd %d exceeds unix.Select FdSet capacity", fd) | ||
| } | ||
| wset := &unix.FdSet{} | ||
| wset.Bits[bitsIndex] |= 1 << (uint64(fd) % 64) //nolint:gosec,mnd | ||
| eset := &unix.FdSet{} | ||
| eset.Bits[bitsIndex] |= 1 << (uint64(fd) % 64) //nolint:gosec,mnd |
Comment on lines
+153
to
+157
| file := os.NewFile(uintptr(fd), "") | ||
| if file == nil { | ||
| closeFD(fd) | ||
| return nil, fmt.Errorf("creating socket file") | ||
| } |
| "net/netip" | ||
| "testing" | ||
|
|
||
| "github.com/golang/mock/gomock" |
Comment on lines
+10
to
+12
| func closeFD(fd int) { | ||
| panic("not implemented") | ||
| } |
Comment on lines
+70
to
+72
| - name: Run integration tests in test container | ||
| run: | | ||
| docker run --rm --entrypoint go test-container test -tags=integration ./internal/restrictednet |
Comment on lines
+191
to
+203
| func parseDestinationPort(portStr string) (port uint16, err error) { | ||
| portUint, err := strconv.ParseUint(portStr, 10, 16) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| const maxPortUint = 65535 | ||
| switch { | ||
| case portUint == 0: | ||
| return 0, errors.New("port cannot be 0") | ||
| case portUint > maxPortUint: | ||
| return 0, fmt.Errorf("port cannot be greater than %d", maxPortUint) | ||
| } |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
See #3358