Skip to content
Snippets Groups Projects
Verified Commit d4f013fe authored by meskio's avatar meskio :tent:
Browse files

[feat] do a proper openvpn process management

parent 9f6a017b
No related branches found
No related tags found
1 merge request!17WIP: [feat] pure go bitmask vpn implemenation
......@@ -34,32 +34,63 @@ var bitmaskRootPaths = []string{
"/snap/bin/riseup-vpn.bitmask-root",
}
func openvpnStart(flags ...string) error {
type launcher struct {
openvpnCh chan []string
}
func newLauncher() *launcher {
l := launcher{make(chan []string, 1)}
go l.openvpnRunner()
return &l
}
func (l *launcher) openvpnStart(flags ...string) error {
log.Println("openvpn start: ", flags)
arg := []string{"openvpn", "start", getOpenvpnPath()}
arg = append(arg, flags...)
// TODO: check errors somehow instead of fire and forget
go runBitmaskRoot(arg...)
l.openvpnCh <- arg
return nil
}
func openvpnStop() error {
func (l *launcher) openvpnStop() error {
l.openvpnCh <- nil
log.Println("openvpn stop")
return runBitmaskRoot("openvpn", "stop")
}
func firewallStart(gateways []string) error {
func (l *launcher) firewallStart(gateways []string) error {
log.Println("firewall start")
arg := []string{"firewall", "start"}
arg = append(arg, gateways...)
return runBitmaskRoot(arg...)
}
func firewallStop() error {
func (l *launcher) firewallStop() error {
log.Println("firewall stop")
return runBitmaskRoot("firewall", "stop")
}
func (l *launcher) openvpnRunner(arg ...string) {
running := false
runOpenvpn := func(arg []string) {
for running {
err := runBitmaskRoot(arg...)
if err != nil {
log.Printf("An error ocurred running openvpn: %v", err)
}
}
}
for arg := range l.openvpnCh {
if arg == nil {
running = false
} else {
running = true
go runOpenvpn(arg)
}
}
}
func runBitmaskRoot(arg ...string) error {
bitmaskRoot, err := bitmaskRootPath()
if err != nil {
......
......@@ -28,6 +28,7 @@ type Bitmask struct {
tempdir string
statusCh chan string
managementClient *openvpn.MgmtClient
launch *launcher
}
// Init the connection to bitmask
......@@ -37,7 +38,8 @@ func Init() (*Bitmask, error) {
if err != nil {
return nil, err
}
b := Bitmask{tempdir, statusCh, nil}
launch := newLauncher()
b := Bitmask{tempdir, statusCh, nil, launch}
err = b.StopVPN()
if err != nil {
......
......@@ -33,7 +33,7 @@ var gateways = []string{
// StartVPN for provider
func (b *Bitmask) StartVPN(provider string) error {
// TODO: openvpn args are hardcoded
err := firewallStart(gateways)
err := b.launch.firewallStart(gateways)
if err != nil {
return err
}
......@@ -44,16 +44,16 @@ func (b *Bitmask) StartVPN(provider string) error {
}
certPemPath := b.getCertPemPath()
arg = append(arg, "--client", "--tls-client", "--remote-cert-tls", "server", "--tls-cipher", "DHE-RSA-AES128-SHA", "--cipher", "AES-128-CBC", "--tun-ipv6", "--auth", "SHA1", "--keepalive", "10 30", "--management-client", "--management", openvpnManagementAddr+" "+openvpnManagementPort, "--ca", b.getCaCertPath(), "--cert", certPemPath, "--key", certPemPath)
return openvpnStart(arg...)
return b.launch.openvpnStart(arg...)
}
// StopVPN or cancel
func (b *Bitmask) StopVPN() error {
err := firewallStop()
err := b.launch.firewallStop()
if err != nil {
return err
}
return openvpnStop()
return b.launch.openvpnStop()
}
// GetStatus returns the VPN status
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment