-
Notifications
You must be signed in to change notification settings - Fork 1
[icxtunnel] add connect command and begin fleshing out routers #63
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,4 +19,5 @@ func Cmd() *cobra.Command { | |
|
|
||
| func init() { | ||
| alphaCmd.AddCommand(rateLimitCmd) | ||
| alphaCmd.AddCommand(tunnelCmd) | ||
| } | ||
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,215 @@ | ||
| package alpha | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "log/slog" | ||
| "net" | ||
| "net/netip" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "github.com/dpeckett/network" | ||
| "github.com/spf13/cobra" | ||
| "golang.org/x/sync/errgroup" | ||
|
|
||
| "github.com/apoxy-dev/apoxy/pkg/netstack" | ||
| "github.com/apoxy-dev/apoxy/pkg/tunnel/api" | ||
| "github.com/apoxy-dev/apoxy/pkg/tunnel/bifurcate" | ||
| "github.com/apoxy-dev/apoxy/pkg/tunnel/router" | ||
| ) | ||
|
|
||
| var ( | ||
| agentName string | ||
| tunnelName string | ||
| relayAddr string | ||
| token string | ||
| insecureSkipVerify bool | ||
| socksListenAddr string | ||
| pcapPath string | ||
| ) | ||
|
|
||
| var tunnelCmd = &cobra.Command{ | ||
| Use: "tunnel", | ||
| Short: "Manage tunnels", | ||
| Long: "Manage icx tunnels and connect to the remote Apoxy Edge fabric.", | ||
| } | ||
|
|
||
| var tunnelRunCmd = &cobra.Command{ | ||
| Use: "run", | ||
| Short: "Run a tunnel", | ||
| Long: "Create a secure tunnel to the remote Apoxy Edge fabric.", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| pc, err := net.ListenPacket("udp", ":0") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create UDP socket: %w", err) | ||
| } | ||
| defer pc.Close() | ||
|
|
||
| pcGeneve, pcQuic := bifurcate.Bifurcate(pc) | ||
| defer pcGeneve.Close() | ||
| defer pcQuic.Close() | ||
|
|
||
| apiURL := url.URL{ | ||
| Scheme: "https", | ||
| Host: relayAddr, | ||
| } | ||
| if insecureSkipVerify { | ||
| apiURL.Scheme = "http" | ||
| } | ||
|
|
||
| tlsConf := &tls.Config{ | ||
| InsecureSkipVerify: insecureSkipVerify, | ||
| } | ||
|
|
||
| client, err := api.NewClient(api.ClientOptions{ | ||
| BaseURL: apiURL.String(), | ||
| Agent: agentName, | ||
| TunnelName: tunnelName, | ||
| Token: token, | ||
| TLSConfig: tlsConf, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create tunnel API client: %w", err) | ||
| } | ||
| defer client.Close() | ||
|
|
||
| connectResp, err := client.Connect(cmd.Context()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to connect to tunnel relay: %w", err) | ||
| } | ||
|
|
||
| // Ensure we disconnect when the command exits | ||
| defer func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
|
|
||
| err := client.Disconnect(ctx, connectResp.ID) | ||
| cancel() | ||
| if err != nil { | ||
| slog.Error("Failed to disconnect from tunnel", slog.Any("error", err)) | ||
| } | ||
| }() | ||
|
|
||
| slog.Info("Connected to tunnel relay", slog.String("id", connectResp.ID), | ||
| slog.Int("vni", int(connectResp.VNI)), slog.Int("mtu", connectResp.MTU)) | ||
|
|
||
| var routerOpts []router.Option | ||
|
|
||
| if connectResp.DNS != nil { | ||
| resolveConf := &network.ResolveConfig{ | ||
| Nameservers: connectResp.DNS.Servers, | ||
| SearchDomains: connectResp.DNS.SearchDomains, | ||
| NDots: connectResp.DNS.NDots, | ||
| } | ||
| routerOpts = append(routerOpts, router.WithResolveConfig(resolveConf)) | ||
| } | ||
|
|
||
| if socksListenAddr != "" { | ||
| routerOpts = append(routerOpts, router.WithSocksListenAddr(socksListenAddr)) | ||
| } | ||
|
|
||
| if pcapPath != "" { | ||
| routerOpts = append(routerOpts, router.WithPcapPath(pcapPath)) | ||
| } | ||
|
|
||
| r, err := router.NewICXNetstackRouter(pcGeneve, connectResp.MTU, routerOpts...) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create ICX netstack router: %w", err) | ||
| } | ||
| defer r.Close() | ||
|
|
||
| remoteAddr, err := netip.ParseAddrPort(relayAddr) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse relay address: %w", err) | ||
| } | ||
|
|
||
| overlayAddrs, err := stringsToPrefixes(connectResp.Addresses) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse assigned addresses: %w", err) | ||
| } | ||
|
|
||
| if err := r.Handler.AddVirtualNetwork(connectResp.VNI, netstack.ToFullAddress(remoteAddr), overlayAddrs); err != nil { | ||
| return fmt.Errorf("failed to add virtual network to ICX handler: %w", err) | ||
| } | ||
|
|
||
| g, ctx := errgroup.WithContext(cmd.Context()) | ||
|
|
||
| g.Go(func() error { | ||
| // Rotate keys at half-life; retry with a short backoff on failure. | ||
| apply := func(k api.Keys) time.Duration { | ||
| // Apply new keys to the ICX handler. | ||
| if err := r.Handler.UpdateVirtualNetworkKeys(connectResp.VNI, k.Epoch, k.Recv, k.Send, k.ExpiresAt); err != nil { | ||
| slog.Error("Failed to apply new keys to router", slog.Any("error", err)) | ||
| } | ||
|
|
||
| // Compute next refresh: half of remaining lifetime. | ||
| remaining := time.Until(k.ExpiresAt) | ||
| next := remaining / 2 | ||
| // Clamp to a sensible minimum to avoid tight loops. | ||
| if next < 10*time.Second { | ||
| next = 10 * time.Second | ||
| } | ||
| return next | ||
| } | ||
|
|
||
| // Seed initial schedule from the keys we got on Connect. | ||
| next := apply(connectResp.Keys) | ||
|
|
||
| timer := time.NewTimer(next) | ||
| defer timer.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-timer.C: | ||
| // Try to rotate keys. | ||
| upd, err := client.UpdateKeys(ctx, connectResp.ID) | ||
| if err != nil { | ||
| slog.Warn("Key update failed; retrying soon", slog.Any("error", err)) | ||
| timer.Reset(5 * time.Second) | ||
| continue | ||
| } | ||
|
|
||
| slog.Info("Rotated tunnel keys", slog.Uint64("epoch", uint64(upd.Keys.Epoch))) | ||
| timer.Reset(apply(upd.Keys)) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| g.Go(func() error { | ||
| return r.Start(ctx) | ||
| }) | ||
|
|
||
| return g.Wait() | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| tunnelRunCmd.Flags().StringVarP(&agentName, "agent", "a", "", "The name of this agent.") | ||
| tunnelRunCmd.Flags().StringVarP(&tunnelName, "name", "n", "", "The name of the tunnel to connect to.") | ||
| tunnelRunCmd.Flags().StringVarP(&relayAddr, "relay-addr", "r", "", "The address of the tunnel relay to connect to.") | ||
| tunnelRunCmd.Flags().StringVarP(&token, "token", "k", "", "The token to use for authenticating with the tunnel relay.") | ||
| tunnelRunCmd.Flags().BoolVar(&insecureSkipVerify, "insecure-skip-verify", false, "Skip TLS certificate verification.") | ||
| tunnelRunCmd.Flags().StringVarP(&pcapPath, "pcap", "p", "", "Path to an optional packet capture file to write.") | ||
| tunnelRunCmd.Flags().StringVar(&socksListenAddr, "socks-addr", "localhost:1080", "Listen address for SOCKS proxy.") | ||
| cobra.CheckErr(tunnelRunCmd.MarkFlagRequired("agent")) | ||
| cobra.CheckErr(tunnelRunCmd.MarkFlagRequired("name")) | ||
| cobra.CheckErr(tunnelRunCmd.MarkFlagRequired("relay-addr")) | ||
| cobra.CheckErr(tunnelRunCmd.MarkFlagRequired("token")) | ||
|
|
||
| tunnelCmd.AddCommand(tunnelRunCmd) | ||
| } | ||
|
|
||
| func stringsToPrefixes(addrs []string) ([]netip.Prefix, error) { | ||
| prefixes := make([]netip.Prefix, 0, len(addrs)) | ||
| for _, addr := range addrs { | ||
| p, err := netip.ParsePrefix(addr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse address %q: %w", addr, err) | ||
| } | ||
| prefixes = append(prefixes, p) | ||
| } | ||
| return prefixes, nil | ||
| } | ||
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
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.
Where do we handle redundant connections? Inside client?
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.
You know I'd totally overlooked this, I had this broken mental model that one process -> one connection. But looking at the QUIC code I realize this was incorrect.
Thankfully icx internally has support for cryptokey routing (ala allowedips) so supporting multiple redundant connections for a single handler is really straight forward (just register a new virtual network for each with the assigned ip ranges).
Thankfully no need for seperate muxers.