-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
Send ports forwarded to control server #2392
base: master
Are you sure you want to change the base?
Changes from 8 commits
ec3a983
8be9055
b1826bd
1d8e3e1
f18cdb8
8fbb43d
00dc345
52522df
a1e7f12
25fd6ff
32a7f1f
3b633e4
e9aaa97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -157,6 +157,20 @@ func (l *Loop) GetPortsForwarded() (ports []uint16) { | |||||||||||
return l.service.GetPortsForwarded() | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (l *Loop) SetPortsForwarded(ports []uint16) (err error) { | ||||||||||||
if l.service == nil { | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
err = l.service.SetPortsForwarded(l.runCtx, ports) | ||||||||||||
if err != nil { | ||||||||||||
l.logger.Error(err.Error()) | ||||||||||||
return err | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove the logs in the port forwarding code and let the calling layers log out the error if necessary (in this case in the control server code) - sorry if I might had changed my mind on this!
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, see e9aaa97 |
||||||||||||
} | ||||||||||||
|
||||||||||||
return nil | ||||||||||||
} | ||||||||||||
|
||||||||||||
func ptrTo[T any](value T) *T { | ||||||||||||
return &value | ||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -47,3 +47,45 @@ func (s *Service) GetPortsForwarded() (ports []uint16) { | |||||||
copy(ports, s.ports) | ||||||||
return ports | ||||||||
} | ||||||||
|
||||||||
func (s *Service) SetPortsForwarded(ctx context.Context, ports []uint16) (err error) { | ||||||||
for i, port := range s.ports { | ||||||||
err := s.portAllower.RemoveAllowedPort(ctx, port) | ||||||||
if err != nil { | ||||||||
for j := 0; j < i; j++ { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit you can now use the 'intrange' introduced in Go 1.23
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Fixed" with a1e7f12 |
||||||||
_ = s.portAllower.SetAllowedPort(ctx, s.ports[j], s.settings.Interface) | ||||||||
} | ||||||||
s.logger.Error(err.Error()) | ||||||||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return err | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove the log here and let the caller handle the error
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed with 25fd6ff |
||||||||
} | ||||||||
} | ||||||||
|
||||||||
for i, port := range ports { | ||||||||
err := s.portAllower.SetAllowedPort(ctx, port, s.settings.Interface) | ||||||||
if err != nil { | ||||||||
for j := 0; j < i; j++ { | ||||||||
_ = s.portAllower.RemoveAllowedPort(ctx, s.ports[j]) | ||||||||
} | ||||||||
for _, port := range s.ports { | ||||||||
_ = s.portAllower.SetAllowedPort(ctx, port, s.settings.Interface) | ||||||||
} | ||||||||
s.logger.Error(err.Error()) | ||||||||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return err | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove the log here and let the caller handle the error
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed with 25fd6ff |
||||||||
} | ||||||||
} | ||||||||
|
||||||||
err = s.writePortForwardedFile(ports) | ||||||||
if err != nil { | ||||||||
_ = s.cleanup() | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
s.portMutex.RLock() | ||||||||
defer s.portMutex.RUnlock() | ||||||||
s.ports = make([]uint16, len(ports)) | ||||||||
copy(s.ports, ports) | ||||||||
|
||||||||
s.logger.Info("updated: " + portsToString(s.ports)) | ||||||||
|
||||||||
return nil | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,19 +11,19 @@ import ( | |||||||||||
) | ||||||||||||
|
||||||||||||
func newOpenvpnHandler(ctx context.Context, looper VPNLooper, | ||||||||||||
pfGetter PortForwardedGetter, w warner) http.Handler { | ||||||||||||
portForwarding PortForwarding, w warner) http.Handler { | ||||||||||||
return &openvpnHandler{ | ||||||||||||
ctx: ctx, | ||||||||||||
looper: looper, | ||||||||||||
pf: pfGetter, | ||||||||||||
pf: portForwarding, | ||||||||||||
warner: w, | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
type openvpnHandler struct { | ||||||||||||
ctx context.Context //nolint:containedctx | ||||||||||||
looper VPNLooper | ||||||||||||
pf PortForwardedGetter | ||||||||||||
pf PortForwarding | ||||||||||||
warner warner | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -50,6 +50,8 @@ func (h *openvpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||||||||||
switch r.Method { | ||||||||||||
case http.MethodGet: | ||||||||||||
h.getPortForwarded(w) | ||||||||||||
case http.MethodPut: | ||||||||||||
h.setPortForwarded(w, r) | ||||||||||||
default: | ||||||||||||
errMethodNotSupported(w, r.Method) | ||||||||||||
} | ||||||||||||
|
@@ -141,3 +143,30 @@ func (h *openvpnHandler) getPortForwarded(w http.ResponseWriter) { | |||||||||||
w.WriteHeader(http.StatusInternalServerError) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (h *openvpnHandler) setPortForwarded(w http.ResponseWriter, r *http.Request) { | ||||||||||||
var data portsWrapper | ||||||||||||
|
||||||||||||
decoder := json.NewDecoder(r.Body) | ||||||||||||
if err := decoder.Decode(&data); err != nil { | ||||||||||||
http.Error(w, err.Error(), http.StatusBadRequest) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
if len(data.Ports) == 0 { | ||||||||||||
http.Error(w, "no port specified", http.StatusBadRequest) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could handle that as "remove forwarded ports"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simply removing the statement with 3b633e4 will clear forwarded ports, because we already remove the old port forwards when setting the new ones anyways. |
||||||||||||
|
||||||||||||
if err := h.pf.SetPortsForwarded(data.Ports); err != nil { | ||||||||||||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's log out as warning the error from the function call, and only say
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with 32a7f1f. |
||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
encoder := json.NewEncoder(w) | ||||||||||||
err := encoder.Encode(h.pf.GetPortsForwarded()) | ||||||||||||
if err != nil { | ||||||||||||
h.warner.Warn(err.Error()) | ||||||||||||
w.WriteHeader(http.StatusInternalServerError) | ||||||||||||
} | ||||||||||||
} |
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 could set the ports somehow, even if the service is not started. The ports could then be injected to the service when we create it. A bit of a futuristic approach about when we could do all kind of modifications live 😄
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.
Yeah, that might be beyond me for now. 😅
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.
No problem, let's keep this unresolved and I'll jump at implementing it later 😉