2020-07-08 13:14:39 +00:00
|
|
|
package openvpn
|
|
|
|
|
|
|
|
|
|
import (
|
2020-10-12 10:55:08 -04:00
|
|
|
"net/http"
|
2020-07-08 13:14:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2021-08-16 19:19:41 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/dns"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/firewall"
|
2021-07-23 20:41:45 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/loopstate"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2021-07-23 20:41:45 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/openvpn/state"
|
2021-07-28 08:35:44 -07:00
|
|
|
"github.com/qdm12/gluetun/internal/portforward"
|
2021-08-16 19:19:41 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/publicip"
|
2021-07-28 08:35:44 -07:00
|
|
|
"github.com/qdm12/gluetun/internal/routing"
|
2020-07-08 13:14:39 +00:00
|
|
|
"github.com/qdm12/golibs/logging"
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-24 19:54:15 +00:00
|
|
|
var _ Looper = (*Loop)(nil)
|
|
|
|
|
|
2020-07-08 13:14:39 +00:00
|
|
|
type Looper interface {
|
2021-07-24 18:56:42 +00:00
|
|
|
Runner
|
|
|
|
|
loopstate.Getter
|
|
|
|
|
loopstate.Applier
|
2021-07-24 19:49:50 +00:00
|
|
|
SettingsGetSetter
|
2021-07-24 18:56:42 +00:00
|
|
|
ServersGetterSetter
|
2020-07-08 13:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-24 19:14:49 +00:00
|
|
|
type Loop struct {
|
2021-07-23 20:41:45 +00:00
|
|
|
statusManager loopstate.Manager
|
|
|
|
|
state state.Manager
|
2020-07-11 21:03:55 +00:00
|
|
|
// Fixed parameters
|
2021-08-18 15:35:07 +00:00
|
|
|
username string
|
|
|
|
|
puid int
|
|
|
|
|
pgid int
|
|
|
|
|
buildInfo models.BuildInformation
|
|
|
|
|
versionInfo bool
|
2020-07-11 21:03:55 +00:00
|
|
|
// Configurators
|
2021-08-18 15:35:07 +00:00
|
|
|
conf Configurator
|
2021-07-28 08:35:44 -07:00
|
|
|
fw firewallConfigurer
|
2021-08-16 19:19:41 +00:00
|
|
|
routing routing.VPNGetter
|
2021-07-28 08:35:44 -07:00
|
|
|
portForward portforward.StartStopper
|
2021-08-16 19:19:41 +00:00
|
|
|
publicip publicip.Looper
|
|
|
|
|
dnsLooper dns.Looper
|
2020-07-11 21:03:55 +00:00
|
|
|
// Other objects
|
2021-08-16 19:19:41 +00:00
|
|
|
logger logging.Logger
|
|
|
|
|
client *http.Client
|
2021-07-16 21:20:34 +00:00
|
|
|
// Internal channels and values
|
2021-07-28 08:35:44 -07:00
|
|
|
stop <-chan struct{}
|
|
|
|
|
stopped chan<- struct{}
|
|
|
|
|
start <-chan struct{}
|
|
|
|
|
running chan<- models.LoopStatus
|
|
|
|
|
userTrigger bool
|
|
|
|
|
startPFCh chan struct{}
|
2021-07-16 21:20:34 +00:00
|
|
|
// Internal constant values
|
2021-07-18 03:17:48 +00:00
|
|
|
backoffTime time.Duration
|
2020-07-08 13:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-26 16:07:50 +00:00
|
|
|
type firewallConfigurer interface {
|
|
|
|
|
firewall.VPNConnectionSetter
|
|
|
|
|
firewall.PortAllower
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 15:36:12 -04:00
|
|
|
const (
|
2021-07-18 03:17:48 +00:00
|
|
|
defaultBackoffTime = 15 * time.Second
|
2021-05-04 15:36:12 -04:00
|
|
|
)
|
2020-12-30 17:22:54 +00:00
|
|
|
|
2021-08-17 15:44:11 +00:00
|
|
|
func NewLoop(openVPNSettings configuration.OpenVPN,
|
|
|
|
|
providerSettings configuration.Provider,
|
|
|
|
|
username string, puid, pgid int,
|
|
|
|
|
allServers models.AllServers, conf Configurator,
|
2021-08-16 19:19:41 +00:00
|
|
|
fw firewallConfigurer, routing routing.VPNGetter,
|
|
|
|
|
portForward portforward.StartStopper,
|
|
|
|
|
publicip publicip.Looper, dnsLooper dns.Looper,
|
|
|
|
|
logger logging.Logger, client *http.Client,
|
|
|
|
|
buildInfo models.BuildInformation, versionInfo bool) *Loop {
|
2021-07-16 21:20:34 +00:00
|
|
|
start := make(chan struct{})
|
|
|
|
|
running := make(chan models.LoopStatus)
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
|
|
2021-07-23 20:41:45 +00:00
|
|
|
statusManager := loopstate.New(constants.Stopped, start, running, stop, stopped)
|
2021-08-17 15:44:11 +00:00
|
|
|
state := state.New(statusManager, openVPNSettings, providerSettings, allServers)
|
2021-07-16 21:20:34 +00:00
|
|
|
|
2021-07-24 19:14:49 +00:00
|
|
|
return &Loop{
|
2021-08-18 15:35:07 +00:00
|
|
|
statusManager: statusManager,
|
|
|
|
|
state: state,
|
|
|
|
|
username: username,
|
|
|
|
|
puid: puid,
|
|
|
|
|
pgid: pgid,
|
|
|
|
|
buildInfo: buildInfo,
|
|
|
|
|
versionInfo: versionInfo,
|
|
|
|
|
conf: conf,
|
|
|
|
|
fw: fw,
|
|
|
|
|
routing: routing,
|
|
|
|
|
portForward: portForward,
|
|
|
|
|
publicip: publicip,
|
|
|
|
|
dnsLooper: dnsLooper,
|
|
|
|
|
logger: logger,
|
|
|
|
|
client: client,
|
|
|
|
|
start: start,
|
|
|
|
|
running: running,
|
|
|
|
|
stop: stop,
|
|
|
|
|
stopped: stopped,
|
|
|
|
|
userTrigger: true,
|
|
|
|
|
startPFCh: make(chan struct{}),
|
|
|
|
|
backoffTime: defaultBackoffTime,
|
2020-07-08 13:14:39 +00:00
|
|
|
}
|
|
|
|
|
}
|