-
Notifications
You must be signed in to change notification settings - Fork 10
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
11 changed files
with
298 additions
and
58 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
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
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,60 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
package wg | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os/exec" | ||
"text/template" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/zeropsio/zcli/src/i18n" | ||
"github.com/zeropsio/zerops-go/dto/output" | ||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||
) | ||
|
||
func CheckWgInstallation() error { | ||
_, err := exec.LookPath("wg-quick") | ||
if err != nil { | ||
return errors.New(i18n.T(i18n.VpnWgQuickIsNotInstalled)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem) error { | ||
data, err := defaultTemplateData(privateKey, vpnSettings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return template.Must(template.New("wg template").Parse(vpnTmpl)).Execute(f, data) | ||
} | ||
|
||
func UpCmd(ctx context.Context, filePath string) (err *exec.Cmd) { | ||
return exec.CommandContext(ctx, "wg-quick", "up", filePath) | ||
} | ||
|
||
func DownCmd(ctx context.Context, filePath string) (err *exec.Cmd) { | ||
return exec.CommandContext(ctx, "wg-quick", "down", filePath) | ||
} | ||
|
||
var vpnTmpl = ` | ||
[Interface] | ||
PrivateKey = {{.PrivateKey}} | ||
Address = {{if .AssignedIpv4Address}}{{.AssignedIpv4Address}}/32{{end}}, {{.AssignedIpv6Address}}/128 | ||
PostUp = echo "nameserver {{.Ipv4NetworkGateway}}" > /etc/resolver/zerops | ||
PostDown = rm /etc/resolver/zerops | ||
[Peer] | ||
PublicKey = {{.PublicKey}} | ||
AllowedIPs = {{if .ProjectIpv4Network}}{{.ProjectIpv4Network}},{{end}} {{.ProjectIpv6Network}}, {{if .Ipv4Network}}{{.Ipv4Network}}, {{end}}{{.Ipv6Network}} | ||
Endpoint = {{.ProjectIpv4SharedEndpoint}} | ||
PersistentKeepalive = 5 | ||
` |
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,60 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package wg | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os/exec" | ||
"text/template" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/zeropsio/zcli/src/i18n" | ||
"github.com/zeropsio/zerops-go/dto/output" | ||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||
) | ||
|
||
func CheckWgInstallation() error { | ||
_, err := exec.LookPath("wg-quick") | ||
if err != nil { | ||
return errors.New(i18n.T(i18n.VpnWgQuickIsNotInstalled)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.ProjectVpnItem) error { | ||
data, err := defaultTemplateData(privateKey, vpnSettings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return template.Must(template.New("wg template").Parse(vpnTmpl)).Execute(f, data) | ||
} | ||
|
||
func UpCmd(ctx context.Context, filePath string) (err *exec.Cmd) { | ||
return exec.CommandContext(ctx, "wg-quick", "up", filePath) | ||
} | ||
|
||
func DownCmd(ctx context.Context, filePath string) (err *exec.Cmd) { | ||
return exec.CommandContext(ctx, "wg-quick", "down", filePath) | ||
} | ||
|
||
var vpnTmpl = ` | ||
[Interface] | ||
PrivateKey = {{.PrivateKey}} | ||
Address = {{if .AssignedIpv4Address}}{{.AssignedIpv4Address}}/32{{end}}, {{.AssignedIpv6Address}}/128 | ||
PostUp = resolvectl dns %i {{.Ipv4NetworkGateway}} | ||
PostUp = resolvectl domain %i zerops | ||
[Peer] | ||
PublicKey = {{.PublicKey}} | ||
AllowedIPs = {{if .ProjectIpv4Network}}{{.ProjectIpv4Network}},{{end}} {{.ProjectIpv6Network}}, {{if .Ipv4Network}}{{.Ipv4Network}}, {{end}}{{.Ipv6Network}} | ||
Endpoint = {{.ProjectIpv4SharedEndpoint}} | ||
PersistentKeepalive = 5 | ||
` |
Oops, something went wrong.