-
Notifications
You must be signed in to change notification settings - Fork 961
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
base: main
Are you sure you want to change the base?
Conversation
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.
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.
fb745d4
to
7512819
Compare
Definitely, I've removed the protobuf changes from this PR. |
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.
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.
d94b25e
to
86d37e2
Compare
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.
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()) |
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.
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 { |
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.
func (m *manager) Stop() error { | |
func (m *manager) Close() error { |
so it implements io.Closer
} | ||
|
||
// Forwarder handles a single port forward. | ||
type Forwarder interface { |
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.
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" | ||
|
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.
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
key := fmt.Sprintf("%s:%s:%d", spec.Network, spec.ListenHost, spec.ListenPort) |
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.
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
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.
Perhaps /
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.