diff options
author | Ruben Pollan <meskio@sindominio.net> | 2018-06-08 10:26:58 +0200 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2018-06-20 12:18:03 +0200 |
commit | fcc7514ec5f1b35068b1033d8ac4278c45043a80 (patch) | |
tree | 0ad4d5f4d650ab2a01879e4f31faedf848c39287 /bitmask_go/launcher_linux.go | |
parent | 11094e0f58e1f28f5333a91f3c4129b56ad154e6 (diff) |
[feat] pure go bitmask vpn implemenation
- Resolves: #42
Diffstat (limited to 'bitmask_go/launcher_linux.go')
-rw-r--r-- | bitmask_go/launcher_linux.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/bitmask_go/launcher_linux.go b/bitmask_go/launcher_linux.go new file mode 100644 index 0000000..05398a6 --- /dev/null +++ b/bitmask_go/launcher_linux.go @@ -0,0 +1,92 @@ +// +build linux +// Copyright (C) 2018 LEAP +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +package bitmask + +import ( + "errors" + "log" + "os" + "os/exec" +) + +const ( + systemOpenvpnPath = "/usr/sbin/openvpn" + snapOpenvpnPath = "/snap/bin/riseup-vpn.openvpn" +) + +var bitmaskRootPaths = []string{ + "/usr/sbin/bitmask-root", + "/usr/local/sbin/bitmask-root", + "/snap/bin/riseup-vpn.bitmask-root", +} + +func 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...) + return nil +} + +func openvpnStop() error { + log.Println("openvpn stop") + return runBitmaskRoot("openvpn", "stop") +} + +func firewallStart(gateways []string) error { + log.Println("firewall start") + arg := []string{"firewall", "start"} + arg = append(arg, gateways...) + return runBitmaskRoot(arg...) +} + +func firewallStop() error { + log.Println("firewall stop") + return runBitmaskRoot("firewall", "stop") +} + +func runBitmaskRoot(arg ...string) error { + bitmaskRoot, err := bitmaskRootPath() + if err != nil { + return err + } + arg = append([]string{bitmaskRoot}, arg...) + + cmd := exec.Command("pkexec", arg...) + err = cmd.Run() + if err != nil { + return err + } + return nil +} + +func bitmaskRootPath() (string, error) { + for _, path := range bitmaskRootPaths { + if _, err := os.Stat(path); !os.IsNotExist(err) { + return path, nil + } + } + return "", errors.New("No bitmask-root found") +} + +func getOpenvpnPath() string { + if os.Getenv("SNAP") != "" { + return snapOpenvpnPath + } + return systemOpenvpnPath +} |