Fatal container exit if openvpn or unbound exits

This commit is contained in:
Quentin McGaw (desktop)
2020-02-13 13:23:22 +00:00
parent 66667f94e1
commit ded635bd56
10 changed files with 37 additions and 22 deletions

View File

@@ -102,8 +102,11 @@ func main() {
e.FatalOnError(err) e.FatalOnError(err)
err = dnsConf.MakeUnboundConf(allSettings.DNS, uid, gid) err = dnsConf.MakeUnboundConf(allSettings.DNS, uid, gid)
e.FatalOnError(err) e.FatalOnError(err)
stream, err := dnsConf.Start(allSettings.DNS.VerbosityDetailsLevel) stream, waitFn, err := dnsConf.Start(allSettings.DNS.VerbosityDetailsLevel)
e.FatalOnError(err) e.FatalOnError(err)
go func() {
e.FatalOnError(waitFn())
}()
go streamMerger.Merge("unbound", stream) go streamMerger.Merge("unbound", stream)
dnsConf.UseDNSInternally(net.IP{127, 0, 0, 1}) // use Unbound dnsConf.UseDNSInternally(net.IP{127, 0, 0, 1}) // use Unbound
err = dnsConf.UseDNSSystemWide(net.IP{127, 0, 0, 1}) // use Unbound err = dnsConf.UseDNSSystemWide(net.IP{127, 0, 0, 1}) // use Unbound
@@ -133,16 +136,26 @@ func main() {
if allSettings.TinyProxy.Enabled { if allSettings.TinyProxy.Enabled {
err = tinyProxyConf.MakeConf(allSettings.TinyProxy.LogLevel, allSettings.TinyProxy.Port, allSettings.TinyProxy.User, allSettings.TinyProxy.Password, uid, gid) err = tinyProxyConf.MakeConf(allSettings.TinyProxy.LogLevel, allSettings.TinyProxy.Port, allSettings.TinyProxy.User, allSettings.TinyProxy.Password, uid, gid)
e.FatalOnError(err) e.FatalOnError(err)
stream, err := tinyProxyConf.Start() stream, waitFn, err := tinyProxyConf.Start()
e.FatalOnError(err) e.FatalOnError(err)
go func() {
if err := waitFn(); err != nil {
logger.Error(err)
}
}()
go streamMerger.Merge("tinyproxy", stream) go streamMerger.Merge("tinyproxy", stream)
} }
if allSettings.ShadowSocks.Enabled { if allSettings.ShadowSocks.Enabled {
err = shadowsocksConf.MakeConf(allSettings.ShadowSocks.Port, allSettings.ShadowSocks.Password, uid, gid) err = shadowsocksConf.MakeConf(allSettings.ShadowSocks.Port, allSettings.ShadowSocks.Password, uid, gid)
e.FatalOnError(err) e.FatalOnError(err)
stream, err := shadowsocksConf.Start("0.0.0.0", allSettings.ShadowSocks.Port, allSettings.ShadowSocks.Password, allSettings.ShadowSocks.Log) stream, waitFn, err := shadowsocksConf.Start("0.0.0.0", allSettings.ShadowSocks.Port, allSettings.ShadowSocks.Password, allSettings.ShadowSocks.Log)
e.FatalOnError(err) e.FatalOnError(err)
go func() {
if err := waitFn(); err != nil {
logger.Error(err)
}
}()
go streamMerger.Merge("shadowsocks", stream) go streamMerger.Merge("shadowsocks", stream)
} }
@@ -161,12 +174,13 @@ func main() {
}) })
} }
stream, err := ovpnConf.Start() stream, waitFn, err := ovpnConf.Start()
e.FatalOnError(err) e.FatalOnError(err)
go streamMerger.Merge("openvpn", stream) go streamMerger.Merge("openvpn", stream)
signals.WaitForExit(func(signal string) int { go signals.WaitForExit(func(signal string) int {
logger.Warn("Caught OS signal %s, shutting down", signal) logger.Warn("Caught OS signal %s, shutting down", signal)
time.Sleep(100 * time.Millisecond) // wait for other processes to exit time.Sleep(100 * time.Millisecond) // wait for other processes to exit
return 0 return 0
}) })
e.FatalOnError(waitFn())
} }

View File

@@ -8,15 +8,15 @@ import (
"github.com/qdm12/private-internet-access-docker/internal/constants" "github.com/qdm12/private-internet-access-docker/internal/constants"
) )
func (c *configurator) Start(verbosityDetailsLevel uint8) (stdout io.ReadCloser, err error) { func (c *configurator) Start(verbosityDetailsLevel uint8) (stdout io.ReadCloser, waitFn func() error, err error) {
c.logger.Info("%s: starting unbound", logPrefix) c.logger.Info("%s: starting unbound", logPrefix)
args := []string{"-d", "-c", string(constants.UnboundConf)} args := []string{"-d", "-c", string(constants.UnboundConf)}
if verbosityDetailsLevel > 0 { if verbosityDetailsLevel > 0 {
args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel))) args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel)))
} }
// Only logs to stderr // Only logs to stderr
_, stdout, _, err = c.commander.Start("unbound", args...) _, stdout, waitFn, err = c.commander.Start("unbound", args...)
return stdout, err return stdout, waitFn, err
} }
func (c *configurator) Version() (version string, err error) { func (c *configurator) Version() (version string, err error) {

View File

@@ -20,8 +20,9 @@ func Test_Start(t *testing.T) {
commander.On("Start", "unbound", "-d", "-c", string(constants.UnboundConf), "-vv"). commander.On("Start", "unbound", "-d", "-c", string(constants.UnboundConf), "-vv").
Return(nil, nil, nil, nil).Once() Return(nil, nil, nil, nil).Once()
c := &configurator{commander: commander, logger: logger} c := &configurator{commander: commander, logger: logger}
stdout, err := c.Start(2) stdout, waitFn, err := c.Start(2)
assert.Nil(t, stdout) assert.Nil(t, stdout)
assert.Nil(t, waitFn)
assert.NoError(t, err) assert.NoError(t, err)
logger.AssertExpectations(t) logger.AssertExpectations(t)
commander.AssertExpectations(t) commander.AssertExpectations(t)

View File

@@ -19,7 +19,7 @@ type Configurator interface {
MakeUnboundConf(settings settings.DNS, uid, gid int) (err error) MakeUnboundConf(settings settings.DNS, uid, gid int) (err error)
UseDNSInternally(IP net.IP) UseDNSInternally(IP net.IP)
UseDNSSystemWide(IP net.IP) error UseDNSSystemWide(IP net.IP) error
Start(logLevel uint8) (stdout io.ReadCloser, err error) Start(logLevel uint8) (stdout io.ReadCloser, waitFn func() error, err error)
WaitForUnbound() (err error) WaitForUnbound() (err error)
Version() (version string, err error) Version() (version string, err error)
} }

View File

@@ -8,10 +8,10 @@ import (
"github.com/qdm12/private-internet-access-docker/internal/constants" "github.com/qdm12/private-internet-access-docker/internal/constants"
) )
func (c *configurator) Start() (stdout io.ReadCloser, err error) { func (c *configurator) Start() (stdout io.ReadCloser, waitFn func() error, err error) {
c.logger.Info("%s: starting openvpn", logPrefix) c.logger.Info("%s: starting openvpn", logPrefix)
stdout, _, _, err = c.commander.Start("openvpn", "--config", string(constants.OpenVPNConf)) stdout, _, waitFn, err = c.commander.Start("openvpn", "--config", string(constants.OpenVPNConf))
return stdout, err return stdout, waitFn, err
} }
func (c *configurator) Version() (string, error) { func (c *configurator) Version() (string, error) {

View File

@@ -17,7 +17,7 @@ type Configurator interface {
WriteAuthFile(user, password string, uid, gid int) error WriteAuthFile(user, password string, uid, gid int) error
CheckTUN() error CheckTUN() error
CreateTUN() error CreateTUN() error
Start() (stdout io.ReadCloser, err error) Start() (stdout io.ReadCloser, waitFn func() error, err error)
} }
type configurator struct { type configurator struct {

View File

@@ -8,7 +8,7 @@ import (
"github.com/qdm12/private-internet-access-docker/internal/constants" "github.com/qdm12/private-internet-access-docker/internal/constants"
) )
func (c *configurator) Start(server string, port uint16, password string, log bool) (stdout io.ReadCloser, err error) { func (c *configurator) Start(server string, port uint16, password string, log bool) (stdout io.ReadCloser, waitFn func() error, err error) {
c.logger.Info("%s: starting shadowsocks server", logPrefix) c.logger.Info("%s: starting shadowsocks server", logPrefix)
args := []string{ args := []string{
"-c", string(constants.ShadowsocksConf), "-c", string(constants.ShadowsocksConf),
@@ -18,8 +18,8 @@ func (c *configurator) Start(server string, port uint16, password string, log bo
if log { if log {
args = append(args, "-v") args = append(args, "-v")
} }
stdout, _, _, err = c.commander.Start("ss-server", args...) stdout, _, waitFn, err = c.commander.Start("ss-server", args...)
return stdout, err return stdout, waitFn, err
} }
// Version obtains the version of the installed shadowsocks server // Version obtains the version of the installed shadowsocks server

View File

@@ -13,7 +13,7 @@ const logPrefix = "shadowsocks configurator"
type Configurator interface { type Configurator interface {
Version() (string, error) Version() (string, error)
MakeConf(port uint16, password string, uid, gid int) (err error) MakeConf(port uint16, password string, uid, gid int) (err error)
Start(server string, port uint16, password string, log bool) (stdout io.ReadCloser, err error) Start(server string, port uint16, password string, log bool) (stdout io.ReadCloser, waitFn func() error, err error)
} }
type configurator struct { type configurator struct {

View File

@@ -6,10 +6,10 @@ import (
"strings" "strings"
) )
func (c *configurator) Start() (stdout io.ReadCloser, err error) { func (c *configurator) Start() (stdout io.ReadCloser, waitFn func() error, err error) {
c.logger.Info("%s: starting tinyproxy server", logPrefix) c.logger.Info("%s: starting tinyproxy server", logPrefix)
stdout, _, _, err = c.commander.Start("tinyproxy", "-d") stdout, _, waitFn, err = c.commander.Start("tinyproxy", "-d")
return stdout, err return stdout, waitFn, err
} }
// Version obtains the version of the installed Tinyproxy server // Version obtains the version of the installed Tinyproxy server

View File

@@ -14,7 +14,7 @@ const logPrefix = "tinyproxy configurator"
type Configurator interface { type Configurator interface {
Version() (string, error) Version() (string, error)
MakeConf(logLevel models.TinyProxyLogLevel, port uint16, user, password string, uid, gid int) error MakeConf(logLevel models.TinyProxyLogLevel, port uint16, user, password string, uid, gid int) error
Start() (stdout io.ReadCloser, err error) Start() (stdout io.ReadCloser, waitFn func() error, err error)
} }
type configurator struct { type configurator struct {