-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
240 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
zcli.config.yml | ||
zcli.config.yml | ||
zcli.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/command/startVpn/handler_clean.go → src/command/startVpn/handler_clean_linux.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build linux | ||
|
||
package startVpn | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// +build darwin | ||
|
||
package startVpn | ||
|
||
import "os/exec" | ||
|
||
func (h *Handler) cleanVpn() error { | ||
|
||
var err error | ||
|
||
cmd := "ps aux | grep wireguard | grep -v grep | awk '{print $2}' | xargs sudo kill" | ||
|
||
_, err = h.sudoers.RunCommand(exec.Command("bash", "-c", cmd)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// +build darwin | ||
|
||
package startVpn | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/zerops-io/zcli/src/service/sudoers" | ||
|
||
"github.com/google/uuid" | ||
"github.com/zerops-io/zcli/src/zeropsVpnProtocol" | ||
) | ||
|
||
func (h *Handler) setVpn(selectedVpnAddress, privateKey string, response *zeropsVpnProtocol.StartVpnResponse) error { | ||
var err error | ||
|
||
output, err := h.sudoers.RunCommand(exec.Command("wireguard-go", "utun")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
re := regexp.MustCompile(`INFO: \((.*)\)`) | ||
submatches := re.FindSubmatch(output) | ||
if len(submatches) != 2 { | ||
return errors.New("vpn interface not found") | ||
} | ||
|
||
interfaceName := string(submatches[1]) | ||
|
||
{ | ||
privateKeyName := uuid.New().String() | ||
tempPrivateKeyFile := path.Join(os.TempDir(), privateKeyName) | ||
|
||
fmt.Println(tempPrivateKeyFile) | ||
err = ioutil.WriteFile(tempPrivateKeyFile, []byte(privateKey), 0755) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = h.sudoers.RunCommand(exec.Command("wg", "set", interfaceName, "private-key", tempPrivateKeyFile)) | ||
if err != nil { | ||
return err | ||
} | ||
err = os.Remove(tempPrivateKeyFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
_, err = h.sudoers.RunCommand(exec.Command("wg", "set", interfaceName, "listen-port", wireguardPort)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientIp := zeropsVpnProtocol.FromProtoIP(response.GetVpn().GetAssignedClientIp()) | ||
serverIp := zeropsVpnProtocol.FromProtoIP(response.GetVpn().GetServerIp()) | ||
vpnRange := zeropsVpnProtocol.FromProtoIPRange(response.GetVpn().GetVpnIpRange()) | ||
|
||
args := []string{ | ||
"set", interfaceName, | ||
"peer", response.GetVpn().GetServerPublicKey(), | ||
"allowed-ips", vpnRange.String(), | ||
"endpoint", selectedVpnAddress + ":" + strconv.Itoa(int(response.GetVpn().GetPort())), | ||
"persistent-keepalive", "25", | ||
} | ||
_, err = h.sudoers.RunCommand(exec.Command("wg", args...)) | ||
if err != nil { | ||
if !errors.Is(err, sudoers.IpAlreadySetErr) { | ||
panic(err) | ||
} | ||
} | ||
|
||
_, err = h.sudoers.RunCommand(exec.Command("ifconfig", interfaceName, "inet6", clientIp.String(), "mtu", "1420")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = h.sudoers.RunCommand(exec.Command("route", "add", "-inet6", vpnRange.String(), serverIp.String())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.logger.Debug("assigned client address: " + clientIp.String()) | ||
h.logger.Debug("assigned vpn server: " + selectedVpnAddress + ":" + strconv.Itoa(int(response.GetVpn().GetPort()))) | ||
h.logger.Debug("server public key: " + response.GetVpn().GetServerPublicKey()) | ||
h.logger.Debug("serverIp address: " + serverIp.String()) | ||
h.logger.Debug("vpnRange: " + vpnRange.String()) | ||
|
||
h.storage.Data.ServerIp = serverIp.String() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build linux | ||
|
||
package stopVpn | ||
|
||
import ( | ||
"errors" | ||
"os/exec" | ||
|
||
"github.com/zerops-io/zcli/src/service/sudoers" | ||
) | ||
|
||
func (h *Handler) cleanVpn() error { | ||
|
||
var err error | ||
|
||
_, err = h.sudoers.RunCommand(exec.Command("ip", "link", "del", "dev", "wg0")) | ||
if err != nil { | ||
if !errors.Is(err, sudoers.CannotFindDeviceErr) { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// +build darwin | ||
|
||
package stopVpn | ||
|
||
import "os/exec" | ||
|
||
func (h *Handler) cleanVpn() error { | ||
|
||
var err error | ||
|
||
cmd := "ps aux | grep wireguard | grep -v grep | awk '{print $2}' | xargs sudo kill" | ||
|
||
_, err = h.sudoers.RunCommand(exec.Command("bash", "-c", cmd)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmdRunner | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
var IpAlreadySetErr = errors.New("RTNETLINK answers: File exists") | ||
var CannotFindDeviceErr = errors.New(`Cannot find device "wg0"`) | ||
var OperationNotPermitted = errors.New(`Operation not permitted`) | ||
|
||
func Run(cmd *exec.Cmd) ([]byte, error) { | ||
output := &bytes.Buffer{} | ||
errOutput := &bytes.Buffer{} | ||
cmd.Stdout = output | ||
cmd.Stderr = errOutput | ||
|
||
if err := cmd.Run(); err != nil { | ||
|
||
|
||
if errOutput.Len() > 0 { | ||
errOutputString := string(errOutput.Bytes()[0 : errOutput.Len()-1]) | ||
|
||
if strings.Contains(errOutputString, OperationNotPermitted.Error()) { | ||
return nil, OperationNotPermitted | ||
} | ||
|
||
for _, e := range []error{IpAlreadySetErr, CannotFindDeviceErr} { | ||
if errOutputString == e.Error() { | ||
return nil, e | ||
} | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
return output.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.