Repository navigation
feat: add active network policy probe #205
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
Merged
Changes from all commits
Commits
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
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,106 @@ | ||
| package netprobe | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "strconv" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| ExitSuccess = 0 | ||
| ExitDialFailed = 10 | ||
| ExitDNSFailed = 20 | ||
| ExitInvalidArgs = 30 | ||
| ExitServerFailed = 40 | ||
| defaultDialTimeout = 5 * time.Second | ||
| defaultServeTimeout = 2 * time.Minute | ||
| ) | ||
|
|
||
| // Run executes the minimal TCP server or client used by the active policy probe. | ||
| func Run(args []string, stdout, stderr io.Writer) int { | ||
| if len(args) == 0 { | ||
| _, _ = fmt.Fprintln(stderr, "usage: netprobe serve|dial") | ||
| return ExitInvalidArgs | ||
| } | ||
| switch args[0] { | ||
| case "serve": | ||
| return runServeCommand(args[1:], stderr) | ||
| case "dial": | ||
| return runDialCommand(args[1:], stdout, stderr) | ||
| default: | ||
| _, _ = fmt.Fprintf(stderr, "unknown netprobe command %q\n", args[0]) | ||
| return ExitInvalidArgs | ||
| } | ||
| } | ||
|
|
||
| func runServeCommand(args []string, stderr io.Writer) int { | ||
| flags := flag.NewFlagSet("serve", flag.ContinueOnError) | ||
| flags.SetOutput(stderr) | ||
| address := flags.String("address", ":18443", "TCP listen address") | ||
| if err := flags.Parse(args); err != nil || flags.NArg() != 0 { | ||
| return ExitInvalidArgs | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), defaultServeTimeout) | ||
| defer cancel() | ||
| if err := serve(ctx, *address); err != nil { | ||
| _, _ = fmt.Fprintln(stderr, err) | ||
| return ExitServerFailed | ||
| } | ||
| return ExitSuccess | ||
| } | ||
|
|
||
| func serve(ctx context.Context, address string) error { | ||
| listener, err := net.Listen("tcp", address) | ||
| if err != nil { | ||
| return fmt.Errorf("listen: %w", err) | ||
| } | ||
| defer listener.Close() | ||
|
|
||
| go func() { | ||
| <-ctx.Done() | ||
| _ = listener.Close() | ||
| }() | ||
| for { | ||
| connection, err := listener.Accept() | ||
| if err != nil { | ||
| if ctx.Err() != nil { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("accept: %w", err) | ||
| } | ||
| _ = connection.Close() | ||
| } | ||
| } | ||
|
|
||
| func runDialCommand(args []string, stdout, stderr io.Writer) int { | ||
| if len(args) != 2 { | ||
| _, _ = fmt.Fprintln(stderr, "usage: netprobe dial <host> <port>") | ||
| return ExitInvalidArgs | ||
| } | ||
| if _, err := strconv.ParseUint(args[1], 10, 16); err != nil { | ||
| _, _ = fmt.Fprintf(stderr, "invalid TCP port %q\n", args[1]) | ||
| return ExitInvalidArgs | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), defaultDialTimeout) | ||
| defer cancel() | ||
| return dial(ctx, args[0], args[1], stdout, stderr) | ||
| } | ||
|
|
||
| func dial(ctx context.Context, host, port string, stdout, stderr io.Writer) int { | ||
| if _, err := net.DefaultResolver.LookupHost(ctx, host); err != nil { | ||
| _, _ = fmt.Fprintf(stderr, "resolve %s: %v\n", host, err) | ||
| return ExitDNSFailed | ||
| } | ||
| connection, err := (&net.Dialer{}).DialContext(ctx, "tcp", net.JoinHostPort(host, port)) | ||
| if err != nil { | ||
| _, _ = fmt.Fprintf(stderr, "dial %s:%s: %v\n", host, port, err) | ||
| return ExitDialFailed | ||
| } | ||
| defer connection.Close() | ||
| _, _ = fmt.Fprintln(stdout, "connected") | ||
| return ExitSuccess | ||
| } |
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,50 @@ | ||
| package netprobe | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "net" | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestRunDial(t *testing.T) { | ||
| listener, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer listener.Close() | ||
| go func() { | ||
| connection, err := listener.Accept() | ||
| if err == nil { | ||
| _ = connection.Close() | ||
| } | ||
| }() | ||
|
|
||
| port := strconv.Itoa(listener.Addr().(*net.TCPAddr).Port) | ||
| var stdout, stderr bytes.Buffer | ||
| if exitCode := Run([]string{"dial", "127.0.0.1", port}, &stdout, &stderr); exitCode != ExitSuccess { | ||
| t.Fatalf("Run(dial) = %d, stderr=%q", exitCode, stderr.String()) | ||
| } | ||
| if stdout.String() != "connected\n" { | ||
| t.Fatalf("stdout = %q, want connected", stdout.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestDialReturnsDNSFailure(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
| defer cancel() | ||
| if exitCode := dial(ctx, "does-not-exist.invalid", "18443", &bytes.Buffer{}, &bytes.Buffer{}); exitCode != ExitDNSFailed { | ||
| t.Fatalf("dial() = %d, want %d", exitCode, ExitDNSFailed) | ||
| } | ||
| } | ||
|
|
||
| func TestRunRejectsInvalidArguments(t *testing.T) { | ||
| if exitCode := Run(nil, &bytes.Buffer{}, &bytes.Buffer{}); exitCode != ExitInvalidArgs { | ||
| t.Fatalf("Run() = %d, want %d", exitCode, ExitInvalidArgs) | ||
| } | ||
| if exitCode := Run([]string{"dial", "localhost"}, &bytes.Buffer{}, &bytes.Buffer{}); exitCode != ExitInvalidArgs { | ||
| t.Fatalf("Run(dial) = %d, want %d", exitCode, ExitInvalidArgs) | ||
| } | ||
| } |
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.