Skip to content
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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

jagaimoworks
Copy link

@jagaimoworks jagaimoworks commented Aug 3, 2024

First timer here. This is a somewhat working implementation of #2369. Hit me with the improvements I can take it 😅

I say somewhat working because the removal of ports from the firewall suffers from #2334 and therefore does not reliably work right now.

The way it works right now is by sending a http PUT request with a body like {ports: [1234, 3456]} to /v1/openvpn/portforwarded.

Copy link
Owner

@qdm12 qdm12 left a comment

Choose a reason for hiding this comment

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

That's great, thanks for the PR 💯 !
I will wait to fix the iptables removals (to create less user frustration and duplicate issues) after v3.39.0 gets released, to merge this though.

Comment on lines 165 to 168
err := l.service.SetPortsForwarded(l.runCtx, ports)
if err != nil {
l.logger.Error(err.Error())
}
Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps we should return an error here to let the http client know it failed for xyz reason 🤔
And possibly log it as well, as it is now.

Copy link
Author

Choose a reason for hiding this comment

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

Commit f18cdb8 addresses this. It would probably suffice to let control server respond with a more generic error, since the original error message already gets logged anyways. What do you think?

Copy link
Owner

Choose a reason for hiding this comment

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

I suggested exactly that 😄 I should had read unresolved conversations better!

Comment on lines +161 to +163
if l.service == nil {
return
}
Copy link
Owner

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 😄

Copy link
Author

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. 😅

Copy link
Owner

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 😉

internal/portforward/service/service.go Outdated Show resolved Hide resolved
internal/portforward/service/service.go Outdated Show resolved Hide resolved
internal/portforward/service/service.go Outdated Show resolved Hide resolved
internal/server/interfaces.go Outdated Show resolved Hide resolved
internal/server/openvpn.go Outdated Show resolved Hide resolved
internal/server/openvpn.go Outdated Show resolved Hide resolved
internal/server/openvpn.go Outdated Show resolved Hide resolved
@qdm12 qdm12 added Status: 🔴 Blocked Blocked by another issue or pull request Status: 🔒 After next release Will be done after the next release labels Aug 3, 2024
@qdm12 qdm12 removed the Status: 🔒 After next release Will be done after the next release label Aug 9, 2024
@qdm12
Copy link
Owner

qdm12 commented Aug 9, 2024

(Sort of) blocked by #1785

@qdm12 qdm12 added Status: 🔴 Blocked Blocked by another issue or pull request Status: 🟡 Nearly resolved This might be resolved or is about to be resolved and removed Status: 🔴 Blocked Blocked by another issue or pull request labels Aug 17, 2024
@qdm12
Copy link
Owner

qdm12 commented Aug 23, 2024

Blocked by #2238 as well.

@andy3469
Copy link

andy3469 commented Nov 9, 2024

Hello!
Any news on this PR ?

for i, port := range s.ports {
err := s.portAllower.RemoveAllowedPort(ctx, port)
if err != nil {
for j := 0; j < i; j++ {
Copy link
Owner

Choose a reason for hiding this comment

The 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
for j := 0; j < i; j++ {
for j := range i {

Copy link
Author

Choose a reason for hiding this comment

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

"Fixed" with a1e7f12

Comment on lines 58 to 59
s.logger.Error(err.Error())
return err
Copy link
Owner

Choose a reason for hiding this comment

The 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
s.logger.Error(err.Error())
return err
return fmt.Errorf("removing allowed port: %w", err)

Copy link
Author

Choose a reason for hiding this comment

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

Fixed with 25fd6ff

Comment on lines 72 to 73
s.logger.Error(err.Error())
return err
Copy link
Owner

Choose a reason for hiding this comment

The 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
s.logger.Error(err.Error())
return err
return fmt.Errorf("setting allowed port: %w", err)

Copy link
Author

Choose a reason for hiding this comment

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

Fixed with 25fd6ff

Comment on lines 156 to 159
if len(data.Ports) == 0 {
http.Error(w, "no port specified", http.StatusBadRequest)
return
}
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe we could handle that as "remove forwarded ports"?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Comment on lines 161 to 162
if err := h.pf.SetPortsForwarded(data.Ports); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Copy link
Owner

Choose a reason for hiding this comment

The 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 "failed setting forwarded ports" in the http response (for the sake of not exposing too much details to the http caller who might not control the gluetun instance)

Suggested change
if err := h.pf.SetPortsForwarded(data.Ports); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
if err := h.pf.SetPortsForwarded(data.Ports); err != nil {
h.warner.Warn(fmt.Sprintf("failed setting forwarded ports: %s", err))
http.Error(w, "failed setting forwarded ports", http.StatusInternalServerError)

Copy link
Author

Choose a reason for hiding this comment

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

Done with 32a7f1f.
Also, I assumed that if we want to limit detail exposure through http responses we should give the json deconding logic just above the same treatment.

Comment on lines +161 to +163
if l.service == nil {
return
}
Copy link
Owner

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 😉

Comment on lines 166 to 168
if err != nil {
l.logger.Error(err.Error())
return err
Copy link
Owner

Choose a reason for hiding this comment

The 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
if err != nil {
l.logger.Error(err.Error())
return err
if err != nil {
return err

Copy link
Author

Choose a reason for hiding this comment

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

No worries, see e9aaa97

@qdm12
Copy link
Owner

qdm12 commented Nov 22, 2024

@jagaimoworks By the way:

  • Great work! ❤️
  • Sorry for the delay re-reviewing this 🕐
  • Just a few minor comments ✅

@qdm12
Copy link
Owner

qdm12 commented Nov 22, 2024

And @andy3469 I'm curious, what do you plan to use this PR for 😃?

@andy3469
Copy link

And @andy3469 I'm curious, what do you plan to use this PR for 😃?

I plan to use it with https://github.com/dhruvinsh/ws-ephemeral as a way to update the port every week.
I will do a PR on the other project once this one is up and running 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: 🔴 Blocked Blocked by another issue or pull request Status: 🟡 Nearly resolved This might be resolved or is about to be resolved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants