Skip to content

Commit

Permalink
Fatal container exit if openvpn or unbound exits
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 13, 2020
1 parent 66667f9 commit ded635b
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 22 deletions.
24 changes: 19 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ func main() {
e.FatalOnError(err)
err = dnsConf.MakeUnboundConf(allSettings.DNS, uid, gid)
e.FatalOnError(err)
stream, err := dnsConf.Start(allSettings.DNS.VerbosityDetailsLevel)
stream, waitFn, err := dnsConf.Start(allSettings.DNS.VerbosityDetailsLevel)
e.FatalOnError(err)
go func() {
e.FatalOnError(waitFn())
}()
go streamMerger.Merge("unbound", stream)
dnsConf.UseDNSInternally(net.IP{127, 0, 0, 1}) // use Unbound
err = dnsConf.UseDNSSystemWide(net.IP{127, 0, 0, 1}) // use Unbound
Expand Down Expand Up @@ -133,16 +136,26 @@ func main() {
if allSettings.TinyProxy.Enabled {
err = tinyProxyConf.MakeConf(allSettings.TinyProxy.LogLevel, allSettings.TinyProxy.Port, allSettings.TinyProxy.User, allSettings.TinyProxy.Password, uid, gid)
e.FatalOnError(err)
stream, err := tinyProxyConf.Start()
stream, waitFn, err := tinyProxyConf.Start()
e.FatalOnError(err)
go func() {
if err := waitFn(); err != nil {
logger.Error(err)
}
}()
go streamMerger.Merge("tinyproxy", stream)
}

if allSettings.ShadowSocks.Enabled {
err = shadowsocksConf.MakeConf(allSettings.ShadowSocks.Port, allSettings.ShadowSocks.Password, uid, gid)
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)
go func() {
if err := waitFn(); err != nil {
logger.Error(err)
}
}()
go streamMerger.Merge("shadowsocks", stream)
}

Expand All @@ -161,12 +174,13 @@ func main() {
})
}

stream, err := ovpnConf.Start()
stream, waitFn, err := ovpnConf.Start()
e.FatalOnError(err)
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)
time.Sleep(100 * time.Millisecond) // wait for other processes to exit
return 0
})
e.FatalOnError(waitFn())
}
6 changes: 3 additions & 3 deletions internal/dns/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"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)
args := []string{"-d", "-c", string(constants.UnboundConf)}
if verbosityDetailsLevel > 0 {
args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel)))
}
// Only logs to stderr
_, stdout, _, err = c.commander.Start("unbound", args...)
return stdout, err
_, stdout, waitFn, err = c.commander.Start("unbound", args...)
return stdout, waitFn, err
}

func (c *configurator) Version() (version string, err error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/dns/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func Test_Start(t *testing.T) {
commander.On("Start", "unbound", "-d", "-c", string(constants.UnboundConf), "-vv").
Return(nil, nil, nil, nil).Once()
c := &configurator{commander: commander, logger: logger}
stdout, err := c.Start(2)
stdout, waitFn, err := c.Start(2)
assert.Nil(t, stdout)
assert.Nil(t, waitFn)
assert.NoError(t, err)
logger.AssertExpectations(t)
commander.AssertExpectations(t)
Expand Down
2 changes: 1 addition & 1 deletion internal/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Configurator interface {
MakeUnboundConf(settings settings.DNS, uid, gid int) (err error)
UseDNSInternally(IP net.IP)
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)
Version() (version string, err error)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/openvpn/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"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)
stdout, _, _, err = c.commander.Start("openvpn", "--config", string(constants.OpenVPNConf))
return stdout, err
stdout, _, waitFn, err = c.commander.Start("openvpn", "--config", string(constants.OpenVPNConf))
return stdout, waitFn, err
}

func (c *configurator) Version() (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/openvpn/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Configurator interface {
WriteAuthFile(user, password string, uid, gid int) error
CheckTUN() error
CreateTUN() error
Start() (stdout io.ReadCloser, err error)
Start() (stdout io.ReadCloser, waitFn func() error, err error)
}

type configurator struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/shadowsocks/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"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)
args := []string{
"-c", string(constants.ShadowsocksConf),
Expand All @@ -18,8 +18,8 @@ func (c *configurator) Start(server string, port uint16, password string, log bo
if log {
args = append(args, "-v")
}
stdout, _, _, err = c.commander.Start("ss-server", args...)
return stdout, err
stdout, _, waitFn, err = c.commander.Start("ss-server", args...)
return stdout, waitFn, err
}

// Version obtains the version of the installed shadowsocks server
Expand Down
2 changes: 1 addition & 1 deletion internal/shadowsocks/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const logPrefix = "shadowsocks configurator"
type Configurator interface {
Version() (string, 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 {
Expand Down
6 changes: 3 additions & 3 deletions internal/tinyproxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"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)
stdout, _, _, err = c.commander.Start("tinyproxy", "-d")
return stdout, err
stdout, _, waitFn, err = c.commander.Start("tinyproxy", "-d")
return stdout, waitFn, err
}

// Version obtains the version of the installed Tinyproxy server
Expand Down
2 changes: 1 addition & 1 deletion internal/tinyproxy/tinyproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const logPrefix = "tinyproxy configurator"
type Configurator interface {
Version() (string, 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 {
Expand Down

0 comments on commit ded635b

Please sign in to comment.