2020-04-12 20:05:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2020-10-12 10:55:08 -04:00
|
|
|
"net"
|
2020-08-30 14:48:57 +00:00
|
|
|
"net/http"
|
2020-12-29 00:55:31 +00:00
|
|
|
nativeos "os"
|
2020-04-29 01:27:42 +00:00
|
|
|
"os/signal"
|
2020-07-08 23:36:02 +00:00
|
|
|
"sync"
|
2020-04-29 01:27:42 +00:00
|
|
|
"syscall"
|
2020-04-12 20:05:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
2021-01-02 18:31:39 +00:00
|
|
|
"github.com/qdm12/dns/pkg/unbound"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/alpine"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/cli"
|
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"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/dns"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/firewall"
|
2020-10-27 03:28:25 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/healthcheck"
|
2020-10-31 21:50:31 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/httpproxy"
|
2020-07-26 12:07:06 +00:00
|
|
|
gluetunLogging "github.com/qdm12/gluetun/internal/logging"
|
2020-11-04 14:07:04 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/openvpn"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/publicip"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/routing"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/server"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/shadowsocks"
|
2020-08-25 19:38:50 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/storage"
|
2020-12-29 01:02:47 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/unix"
|
2020-09-12 14:04:54 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/updater"
|
2020-08-30 14:48:57 +00:00
|
|
|
versionpkg "github.com/qdm12/gluetun/internal/version"
|
2020-04-12 20:05:28 +00:00
|
|
|
"github.com/qdm12/golibs/logging"
|
2021-01-02 01:57:00 +00:00
|
|
|
"github.com/qdm12/golibs/os"
|
|
|
|
|
"github.com/qdm12/golibs/os/user"
|
2021-02-06 11:05:50 -05:00
|
|
|
"github.com/qdm12/golibs/params"
|
2021-01-02 18:31:39 +00:00
|
|
|
"github.com/qdm12/updated/pkg/dnscrypto"
|
2020-04-12 20:05:28 +00:00
|
|
|
)
|
|
|
|
|
|
2020-08-29 19:14:52 +00:00
|
|
|
//nolint:gochecknoglobals
|
2020-11-07 22:31:20 +00:00
|
|
|
var (
|
|
|
|
|
version = "unknown"
|
|
|
|
|
commit = "unknown"
|
|
|
|
|
buildDate = "an unknown date"
|
|
|
|
|
)
|
2020-08-29 19:14:52 +00:00
|
|
|
|
2020-05-02 14:48:18 +00:00
|
|
|
func main() {
|
2020-12-29 01:21:54 +00:00
|
|
|
buildInfo := models.BuildInformation{
|
|
|
|
|
Version: version,
|
|
|
|
|
Commit: commit,
|
|
|
|
|
BuildDate: buildDate,
|
|
|
|
|
}
|
2021-01-04 01:40:07 +00:00
|
|
|
|
2020-06-02 23:03:18 +00:00
|
|
|
ctx := context.Background()
|
2021-01-04 01:40:07 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
2021-02-25 23:51:29 +00:00
|
|
|
logger := logging.New(logging.StdLog)
|
2021-01-04 01:40:07 +00:00
|
|
|
|
2020-12-29 00:55:31 +00:00
|
|
|
args := nativeos.Args
|
|
|
|
|
os := os.New()
|
2020-12-29 01:16:53 +00:00
|
|
|
osUser := user.New()
|
2020-12-29 01:21:54 +00:00
|
|
|
unix := unix.New()
|
2020-12-29 18:24:03 +00:00
|
|
|
cli := cli.New()
|
2021-01-04 01:40:07 +00:00
|
|
|
|
|
|
|
|
errorCh := make(chan error)
|
|
|
|
|
go func() {
|
|
|
|
|
errorCh <- _main(ctx, buildInfo, args, logger, os, osUser, unix, cli)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
signalsCh := make(chan nativeos.Signal, 1)
|
|
|
|
|
signal.Notify(signalsCh,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGTERM,
|
|
|
|
|
nativeos.Interrupt,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case signal := <-signalsCh:
|
|
|
|
|
logger.Warn("Caught OS signal %s, shutting down", signal)
|
|
|
|
|
case err := <-errorCh:
|
|
|
|
|
close(errorCh)
|
2021-01-04 13:41:56 +00:00
|
|
|
if err == nil { // expected exit such as healthcheck
|
|
|
|
|
nativeos.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
logger.Error(err)
|
2021-01-04 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
|
|
const shutdownGracePeriod = 5 * time.Second
|
|
|
|
|
timer := time.NewTimer(shutdownGracePeriod)
|
|
|
|
|
select {
|
|
|
|
|
case <-errorCh:
|
|
|
|
|
if !timer.Stop() {
|
|
|
|
|
<-timer.C
|
|
|
|
|
}
|
|
|
|
|
logger.Info("Shutdown successful")
|
|
|
|
|
case <-timer.C:
|
|
|
|
|
logger.Warn("Shutdown timed out")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nativeos.Exit(1)
|
2020-06-02 23:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 00:55:31 +00:00
|
|
|
//nolint:gocognit,gocyclo
|
2021-01-30 17:29:27 +00:00
|
|
|
func _main(ctx context.Context, buildInfo models.BuildInformation,
|
2021-01-04 01:40:07 +00:00
|
|
|
args []string, logger logging.Logger, os os.OS, osUser user.OSUser, unix unix.Unix,
|
|
|
|
|
cli cli.CLI) error {
|
2020-06-02 23:03:18 +00:00
|
|
|
if len(args) > 1 { // cli operation
|
|
|
|
|
switch args[1] {
|
|
|
|
|
case "healthcheck":
|
2021-01-30 17:29:27 +00:00
|
|
|
return cli.HealthCheck(ctx)
|
2020-06-13 10:43:47 -04:00
|
|
|
case "clientkey":
|
2021-01-04 01:40:07 +00:00
|
|
|
return cli.ClientKey(args[2:], os.OpenFile)
|
2020-07-13 23:43:26 +00:00
|
|
|
case "openvpnconfig":
|
2021-01-04 01:40:07 +00:00
|
|
|
return cli.OpenvpnConfig(os)
|
2020-08-28 08:17:04 -04:00
|
|
|
case "update":
|
2021-01-30 17:29:27 +00:00
|
|
|
return cli.Update(ctx, args[2:], os)
|
2020-06-02 23:03:18 +00:00
|
|
|
default:
|
2021-01-04 01:40:07 +00:00
|
|
|
return fmt.Errorf("command %q is unknown", args[1])
|
2020-04-12 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-30 17:29:27 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-05-02 13:05:09 +00:00
|
|
|
defer cancel()
|
2020-07-23 02:15:37 +00:00
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
const clientTimeout = 15 * time.Second
|
|
|
|
|
httpClient := &http.Client{Timeout: clientTimeout}
|
2020-04-12 20:05:28 +00:00
|
|
|
// Create configurators
|
2020-12-29 01:16:53 +00:00
|
|
|
alpineConf := alpine.NewConfigurator(os.OpenFile, osUser)
|
2020-12-29 01:02:47 +00:00
|
|
|
ovpnConf := openvpn.NewConfigurator(logger, os, unix)
|
2021-01-02 18:31:39 +00:00
|
|
|
dnsCrypto := dnscrypto.New(httpClient, "", "")
|
2021-01-02 20:39:24 +00:00
|
|
|
const cacertsPath = "/etc/ssl/certs/ca-certificates.crt"
|
|
|
|
|
dnsConf := unbound.NewConfigurator(logger, os.OpenFile, dnsCrypto,
|
|
|
|
|
"/etc/unbound", "/usr/sbin/unbound", cacertsPath)
|
2020-10-22 18:55:28 -04:00
|
|
|
routingConf := routing.NewRouting(logger)
|
2020-12-29 00:55:31 +00:00
|
|
|
firewallConf := firewall.NewConfigurator(logger, routingConf, os.OpenFile)
|
2020-04-12 20:05:28 +00:00
|
|
|
|
2020-11-04 14:07:04 +00:00
|
|
|
fmt.Println(gluetunLogging.Splash(buildInfo))
|
2020-07-14 00:17:31 +00:00
|
|
|
|
2020-05-02 14:48:18 +00:00
|
|
|
printVersions(ctx, logger, map[string]func(ctx context.Context) (string, error){
|
2020-10-31 21:50:31 -04:00
|
|
|
"OpenVPN": ovpnConf.Version,
|
|
|
|
|
"Unbound": dnsConf.Version,
|
|
|
|
|
"IPtables": firewallConf.Version,
|
2020-05-02 14:48:18 +00:00
|
|
|
})
|
2020-04-12 20:05:28 +00:00
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
var allSettings configuration.Settings
|
2021-02-25 23:51:29 +00:00
|
|
|
err := allSettings.Read(params.NewEnv(), os, logger.NewChild(logging.SetPrefix("configuration: ")))
|
2020-08-27 22:59:58 +00:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-27 22:59:58 +00:00
|
|
|
}
|
2020-04-12 20:05:28 +00:00
|
|
|
logger.Info(allSettings.String())
|
|
|
|
|
|
2020-12-29 00:55:31 +00:00
|
|
|
if err := os.MkdirAll("/tmp/gluetun", 0644); err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-12-29 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
if err := os.MkdirAll("/gluetun", 0644); err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-12-29 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-25 19:38:50 -04:00
|
|
|
// TODO run this in a loop or in openvpn to reload from file without restarting
|
2020-12-29 17:49:38 +00:00
|
|
|
storage := storage.New(logger, os, constants.ServersData)
|
|
|
|
|
allServers, err := storage.SyncServers(constants.GetAllServers())
|
2020-08-25 19:38:50 -04:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-08 22:13:59 +00:00
|
|
|
// Should never change
|
2020-12-29 16:44:35 +00:00
|
|
|
puid, pgid := allSettings.System.PUID, allSettings.System.PGID
|
2020-07-08 22:13:59 +00:00
|
|
|
|
2020-12-27 00:36:39 +00:00
|
|
|
const defaultUsername = "nonrootuser"
|
2020-12-29 16:44:35 +00:00
|
|
|
nonRootUsername, err := alpineConf.CreateUser(defaultUsername, puid)
|
2020-08-27 22:59:58 +00:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-27 22:59:58 +00:00
|
|
|
}
|
2020-12-29 16:44:35 +00:00
|
|
|
if nonRootUsername != defaultUsername {
|
|
|
|
|
logger.Info("using existing username %s corresponding to user id %d", nonRootUsername, puid)
|
|
|
|
|
}
|
2020-12-29 00:55:31 +00:00
|
|
|
|
2020-12-29 16:44:35 +00:00
|
|
|
if err := os.Chown("/etc/unbound", puid, pgid); err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-27 22:59:58 +00:00
|
|
|
}
|
2020-04-12 20:05:28 +00:00
|
|
|
|
2020-07-13 02:14:56 +00:00
|
|
|
if allSettings.Firewall.Debug {
|
|
|
|
|
firewallConf.SetDebug()
|
|
|
|
|
routingConf.SetDebug()
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 00:35:53 +00:00
|
|
|
defaultInterface, defaultGateway, err := routingConf.DefaultRoute()
|
|
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-07-20 00:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-10 03:08:20 +10:00
|
|
|
localNetworks, err := routingConf.LocalNetworks()
|
2020-07-20 00:35:53 +00:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-07-20 00:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-29 19:23:44 -04:00
|
|
|
defaultIP, err := routingConf.DefaultIP()
|
|
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-10-29 19:23:44 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-10 03:08:20 +10:00
|
|
|
firewallConf.SetNetworkInformation(defaultInterface, defaultGateway, localNetworks, defaultIP)
|
2020-07-20 00:35:53 +00:00
|
|
|
|
2020-10-24 18:05:11 -04:00
|
|
|
if err := routingConf.Setup(); err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-10-24 18:05:11 -04:00
|
|
|
}
|
|
|
|
|
defer func() {
|
2020-10-31 21:50:31 -04:00
|
|
|
routingConf.SetVerbose(false)
|
2020-10-24 18:05:11 -04:00
|
|
|
if err := routingConf.TearDown(); err != nil {
|
|
|
|
|
logger.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2020-10-29 19:23:44 -04:00
|
|
|
if err := firewallConf.SetOutboundSubnets(ctx, allSettings.Firewall.OutboundSubnets); err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-10-29 19:23:44 -04:00
|
|
|
}
|
|
|
|
|
if err := routingConf.SetOutboundRoutes(allSettings.Firewall.OutboundSubnets); err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-10-29 19:23:44 -04:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 20:05:28 +00:00
|
|
|
if err := ovpnConf.CheckTUN(); err != nil {
|
|
|
|
|
logger.Warn(err)
|
|
|
|
|
err = ovpnConf.CreateTUN()
|
2020-08-27 22:59:58 +00:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-27 22:59:58 +00:00
|
|
|
}
|
2020-04-12 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 01:49:05 +00:00
|
|
|
tunnelReadyCh := make(chan struct{})
|
2020-09-12 19:17:19 +00:00
|
|
|
defer close(tunnelReadyCh)
|
2020-04-12 20:05:28 +00:00
|
|
|
|
2020-07-11 21:03:55 +00:00
|
|
|
if allSettings.Firewall.Enabled {
|
|
|
|
|
err := firewallConf.SetEnabled(ctx, true) // disabled by default
|
2020-08-27 22:59:58 +00:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-27 22:59:58 +00:00
|
|
|
}
|
2020-07-11 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 02:07:13 +00:00
|
|
|
for _, vpnPort := range allSettings.Firewall.VPNInputPorts {
|
|
|
|
|
err = firewallConf.SetAllowedPort(ctx, vpnPort, string(constants.TUN))
|
2020-08-27 22:59:58 +00:00
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-08-27 22:59:58 +00:00
|
|
|
}
|
2020-07-20 02:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 09:22:28 -04:00
|
|
|
for _, port := range allSettings.Firewall.InputPorts {
|
|
|
|
|
err = firewallConf.SetAllowedPort(ctx, port, defaultInterface)
|
|
|
|
|
if err != nil {
|
2021-01-04 01:40:07 +00:00
|
|
|
return err
|
2020-10-18 09:22:28 -04:00
|
|
|
}
|
2020-10-24 18:05:11 -04:00
|
|
|
} // TODO move inside firewall?
|
2020-10-18 09:22:28 -04:00
|
|
|
|
2020-07-23 02:15:37 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
2021-05-04 15:36:12 -04:00
|
|
|
healthy := make(chan bool)
|
2020-07-23 02:15:37 +00:00
|
|
|
|
2020-12-29 16:44:35 +00:00
|
|
|
openvpnLooper := openvpn.NewLooper(allSettings.OpenVPN, nonRootUsername, puid, pgid, allServers,
|
2021-05-04 15:36:12 -04:00
|
|
|
ovpnConf, firewallConf, routingConf, logger, httpClient, os.OpenFile, tunnelReadyCh, healthy, cancel)
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
2020-07-08 13:14:39 +00:00
|
|
|
// wait for restartOpenvpn
|
2020-07-15 01:34:46 +00:00
|
|
|
go openvpnLooper.Run(ctx, wg)
|
2020-04-30 23:41:57 +00:00
|
|
|
|
2020-12-19 20:10:34 -05:00
|
|
|
updaterLooper := updater.NewLooper(allSettings.Updater,
|
|
|
|
|
allServers, storage, openvpnLooper.SetServers, httpClient, logger)
|
2020-09-12 14:04:54 -04:00
|
|
|
wg.Add(1)
|
|
|
|
|
// wait for updaterLooper.Restart() or its ticket launched with RunRestartTicker
|
|
|
|
|
go updaterLooper.Run(ctx, wg)
|
|
|
|
|
|
2021-01-02 18:31:39 +00:00
|
|
|
unboundLooper := dns.NewLooper(dnsConf, allSettings.DNS, httpClient,
|
2021-01-26 04:17:22 +00:00
|
|
|
logger, nonRootUsername, puid, pgid)
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
2020-09-12 19:17:19 +00:00
|
|
|
// wait for unboundLooper.Restart or its ticker launched with RunRestartTicker
|
2021-01-29 00:06:55 +00:00
|
|
|
go unboundLooper.Run(ctx, wg)
|
2020-06-16 00:11:22 +00:00
|
|
|
|
2020-12-28 01:51:55 +00:00
|
|
|
publicIPLooper := publicip.NewLooper(
|
2020-12-29 16:44:35 +00:00
|
|
|
httpClient, logger, allSettings.PublicIP, puid, pgid, os)
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
|
|
|
|
go publicIPLooper.Run(ctx, wg)
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go publicIPLooper.RunRestartTicker(ctx, wg)
|
2020-07-08 22:33:28 +00:00
|
|
|
|
2020-11-21 01:26:02 +00:00
|
|
|
httpProxyLooper := httpproxy.NewLooper(logger, allSettings.HTTPProxy)
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
2020-10-31 21:50:31 -04:00
|
|
|
go httpProxyLooper.Run(ctx, wg)
|
2020-07-08 23:20:33 +00:00
|
|
|
|
2020-12-30 21:43:45 +00:00
|
|
|
shadowsocksLooper := shadowsocks.NewLooper(allSettings.ShadowSocks, logger)
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
2020-07-15 01:34:46 +00:00
|
|
|
go shadowsocksLooper.Run(ctx, wg)
|
2020-07-08 23:29:40 +00:00
|
|
|
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
2021-01-29 00:06:55 +00:00
|
|
|
go routeReadyEvents(ctx, wg, buildInfo, tunnelReadyCh,
|
2020-09-12 19:17:19 +00:00
|
|
|
unboundLooper, updaterLooper, publicIPLooper, routingConf, logger, httpClient,
|
|
|
|
|
allSettings.VersionInformation, allSettings.OpenVPN.Provider.PortForwarding.Enabled, openvpnLooper.PortForward,
|
|
|
|
|
)
|
2020-10-17 22:07:15 +00:00
|
|
|
controlServerAddress := fmt.Sprintf("0.0.0.0:%d", allSettings.ControlServer.Port)
|
2020-10-17 22:21:20 +00:00
|
|
|
controlServerLogging := allSettings.ControlServer.Log
|
2020-10-20 02:45:28 +00:00
|
|
|
httpServer := server.New(controlServerAddress, controlServerLogging,
|
2020-12-27 21:06:00 +00:00
|
|
|
logger, buildInfo, openvpnLooper, unboundLooper, updaterLooper, publicIPLooper)
|
2020-09-12 14:34:15 -04:00
|
|
|
wg.Add(1)
|
2020-07-08 23:36:02 +00:00
|
|
|
go httpServer.Run(ctx, wg)
|
2020-07-08 13:14:39 +00:00
|
|
|
|
2020-10-27 03:28:25 +00:00
|
|
|
healthcheckServer := healthcheck.NewServer(
|
|
|
|
|
constants.HealthcheckAddress, logger)
|
|
|
|
|
wg.Add(1)
|
2021-05-04 15:36:12 -04:00
|
|
|
go healthcheckServer.Run(ctx, healthy, wg)
|
2020-10-27 03:28:25 +00:00
|
|
|
|
2020-12-19 20:10:34 -05:00
|
|
|
// Start openvpn for the first time in a blocking call
|
|
|
|
|
// until openvpn is launched
|
|
|
|
|
_, _ = openvpnLooper.SetStatus(constants.Running) // TODO option to disable with variable
|
2020-07-08 13:14:39 +00:00
|
|
|
|
2021-01-04 01:40:07 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
|
|
2020-07-05 20:05:38 +00:00
|
|
|
if allSettings.OpenVPN.Provider.PortForwarding.Enabled {
|
|
|
|
|
logger.Info("Clearing forwarded port status file %s", allSettings.OpenVPN.Provider.PortForwarding.Filepath)
|
2021-02-06 18:31:14 +00:00
|
|
|
if err := os.Remove(allSettings.OpenVPN.Provider.PortForwarding.Filepath); err != nil {
|
2020-04-19 20:10:48 +00:00
|
|
|
logger.Error(err)
|
|
|
|
|
}
|
2020-04-29 01:27:42 +00:00
|
|
|
}
|
2020-05-02 14:48:18 +00:00
|
|
|
|
2021-04-19 14:27:38 +00:00
|
|
|
if allSettings.Firewall.Enabled {
|
|
|
|
|
const enable = false
|
|
|
|
|
err := firewallConf.SetEnabled(context.Background(), enable)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 01:40:07 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
return nil
|
2020-05-02 14:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
func printVersions(ctx context.Context, logger logging.Logger,
|
|
|
|
|
versionFunctions map[string]func(ctx context.Context) (string, error)) {
|
|
|
|
|
const timeout = 5 * time.Second
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
2020-05-02 14:48:18 +00:00
|
|
|
defer cancel()
|
|
|
|
|
for name, f := range versionFunctions {
|
|
|
|
|
version, err := f(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
|
|
|
|
} else {
|
|
|
|
|
logger.Info("%s version: %s", name, version)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 01:21:54 +00:00
|
|
|
func routeReadyEvents(ctx context.Context, wg *sync.WaitGroup, buildInfo models.BuildInformation,
|
2021-01-29 00:06:55 +00:00
|
|
|
tunnelReadyCh <-chan struct{},
|
2020-09-12 19:17:19 +00:00
|
|
|
unboundLooper dns.Looper, updaterLooper updater.Looper, publicIPLooper publicip.Looper,
|
|
|
|
|
routing routing.Routing, logger logging.Logger, httpClient *http.Client,
|
2020-10-12 10:55:08 -04:00
|
|
|
versionInformation, portForwardingEnabled bool, startPortForward func(vpnGateway net.IP)) {
|
2020-09-12 19:17:19 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
tickerWg := &sync.WaitGroup{}
|
|
|
|
|
// for linters only
|
|
|
|
|
var restartTickerContext context.Context
|
|
|
|
|
var restartTickerCancel context.CancelFunc = func() {}
|
2021-01-29 00:06:55 +00:00
|
|
|
first := true
|
2020-09-12 19:17:19 +00:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
restartTickerCancel() // for linters only
|
|
|
|
|
tickerWg.Wait()
|
|
|
|
|
return
|
|
|
|
|
case <-tunnelReadyCh: // blocks until openvpn is connected
|
2021-01-29 00:06:55 +00:00
|
|
|
vpnDestination, err := routing.VPNDestinationIP()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Warn(err)
|
|
|
|
|
} else {
|
|
|
|
|
logger.Info("VPN routing IP address: %s", vpnDestination)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-30 20:36:19 +00:00
|
|
|
if unboundLooper.GetSettings().Enabled {
|
|
|
|
|
_, _ = unboundLooper.SetStatus(constants.Running)
|
|
|
|
|
}
|
2021-01-29 00:06:55 +00:00
|
|
|
|
2020-09-12 19:17:19 +00:00
|
|
|
restartTickerCancel() // stop previous restart tickers
|
|
|
|
|
tickerWg.Wait()
|
|
|
|
|
restartTickerContext, restartTickerCancel = context.WithCancel(ctx)
|
2021-01-29 00:06:55 +00:00
|
|
|
|
|
|
|
|
// Runs the Public IP getter job once
|
|
|
|
|
_, _ = publicIPLooper.SetStatus(constants.Running)
|
|
|
|
|
if !versionInformation {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if first {
|
|
|
|
|
first = false
|
|
|
|
|
message, err := versionpkg.GetMessage(ctx, buildInfo, httpClient)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
|
|
|
|
} else {
|
|
|
|
|
logger.Info(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 14:20:57 +00:00
|
|
|
//nolint:gomnd
|
|
|
|
|
tickerWg.Add(2)
|
2020-09-12 19:17:19 +00:00
|
|
|
go unboundLooper.RunRestartTicker(restartTickerContext, tickerWg)
|
|
|
|
|
go updaterLooper.RunRestartTicker(restartTickerContext, tickerWg)
|
2020-10-12 10:55:08 -04:00
|
|
|
if portForwardingEnabled {
|
2020-11-10 13:35:49 +00:00
|
|
|
// vpnGateway required only for PIA
|
2020-11-05 02:10:34 +00:00
|
|
|
vpnGateway, err := routing.VPNLocalGatewayIP()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
2020-09-12 19:17:19 +00:00
|
|
|
}
|
2020-11-05 02:10:34 +00:00
|
|
|
logger.Info("VPN gateway IP address: %s", vpnGateway)
|
|
|
|
|
startPortForward(vpnGateway)
|
2020-09-12 19:17:19 +00:00
|
|
|
}
|
2020-05-02 14:48:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|