2021-07-23 20:41:45 +00:00
|
|
|
package openvpn
|
|
|
|
|
|
2021-07-23 20:46:57 +00:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2021-07-24 18:56:42 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/openvpn/state"
|
2021-07-23 20:46:57 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/provider"
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-24 18:56:42 +00:00
|
|
|
type PortForwadedGetter = state.PortForwardedGetter
|
|
|
|
|
|
2021-07-24 19:14:49 +00:00
|
|
|
func (l *Loop) GetPortForwarded() (port uint16) {
|
2021-07-23 20:41:45 +00:00
|
|
|
return l.state.GetPortForwarded()
|
|
|
|
|
}
|
2021-07-23 20:46:57 +00:00
|
|
|
|
2021-07-24 18:56:42 +00:00
|
|
|
type PortForwader interface {
|
|
|
|
|
PortForward(vpnGatewayIP net.IP)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 19:14:49 +00:00
|
|
|
func (l *Loop) PortForward(vpnGateway net.IP) { l.portForwardSignals <- vpnGateway }
|
2021-07-23 20:46:57 +00:00
|
|
|
|
|
|
|
|
// portForward is a blocking operation which may or may not be infinite.
|
|
|
|
|
// You should therefore always call it in a goroutine.
|
2021-07-24 19:14:49 +00:00
|
|
|
func (l *Loop) portForward(ctx context.Context,
|
2021-07-23 20:46:57 +00:00
|
|
|
providerConf provider.Provider, client *http.Client, gateway net.IP) {
|
|
|
|
|
settings := l.state.GetSettings()
|
|
|
|
|
if !settings.Provider.PortForwarding.Enabled {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncState := func(port uint16) (pfFilepath string) {
|
|
|
|
|
l.state.SetPortForwarded(port)
|
|
|
|
|
settings := l.state.GetSettings()
|
|
|
|
|
return settings.Provider.PortForwarding.Filepath
|
|
|
|
|
}
|
|
|
|
|
providerConf.PortForward(ctx, client, l.pfLogger,
|
|
|
|
|
gateway, l.fw, syncState)
|
|
|
|
|
}
|