Skip to content

chore: move port forwarding out of cli package #19092

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
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

ibetitsmike
Copy link
Contributor

This PR is the initial work needed for hosting the port forwarding capabilities both from the vpn tunnel package and cli instead of having the logic duplicated.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are adding new fields to a protobuf so we need to bump the corresponding api minor version in vpn/version.go. Convention here is also to add a comment detailing the version history.

@ibetitsmike ibetitsmike changed the title chore: port forwarding moved out of cli package chore: move port forwarding out of cli package Jul 30, 2025
@ibetitsmike ibetitsmike force-pushed the mike/port-forwarding branch from fb745d4 to 7512819 Compare July 30, 2025 07:59
Copy link
Contributor Author

Definitely, I've removed the protobuf changes from this PR.

@ibetitsmike ibetitsmike requested a review from deansheather July 30, 2025 09:39
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look OK to me, some non-blocking comments below.

In the case of returning an interface, the caller still has the option to use either the interface or the struct, or define their own interface.

@github-actions github-actions bot added the stale This issue is like stale bread. label Aug 12, 2025
@ibetitsmike ibetitsmike force-pushed the mike/port-forwarding branch from d94b25e to 86d37e2 Compare August 13, 2025 08:16
Copy link
Member

@deansheather deansheather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the API could be shrunk down to:

// remove Forwarder as a public type

type Manager interface {
  Add(spec Spec) error // don't return forwarder
  Remove(spec Spec) error
  List() []Spec // don't return forwarders
  Close() error
}


// Test if we can actually bind to the port before adding the forwarder
listenAddress := netip.AddrPortFrom(spec.ListenHost, spec.ListenPort)
testListener, err := m.opts.Listener.Listen(spec.Network, listenAddress.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add() listens but doesn't start forwarding? IMO the Start() API should just be removed in favor of Add() always creating the listener and starting the forwarding goroutine

return nil
}

func (m *manager) Stop() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (m *manager) Stop() error {
func (m *manager) Close() error {

so it implements io.Closer

}

// Forwarder handles a single port forward.
type Forwarder interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The forwarder interface doesn't seem to do much except track a goroutine. All of it's methods could probably just be added as private methods onto the manager implementation IMO.

"golang.org/x/xerrors"

"cdr.dev/slog"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

m.mu.Lock()
defer m.mu.Unlock()

key := fmt.Sprintf("%s:%s:%d", spec.Network, spec.ListenHost, spec.ListenPort)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key generation should be added as a method on the spec struct. Probably should also use a different character as a separator since : shows up in IPv6 addresses

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps /

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale This issue is like stale bread.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants