forked from Place1/wg-access-server
-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
119 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/coreos/go-iptables/iptables" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
func ConfigureRouting(wgIface string) error { | ||
// Networking configuration (ip links and route tables) | ||
// to ensure that network traffic in the VPN subnet | ||
// moves through the wireguard interface | ||
link, err := netlink.LinkByName(wgIface) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to find wireguard interface") | ||
} | ||
addr, err := netlink.ParseAddr("10.0.0.1/24") | ||
if err != nil { | ||
return errors.Wrap(err, "failed to parse subnet address") | ||
} | ||
if err := netlink.AddrAdd(link, addr); err != nil { | ||
logrus.Warn(errors.Wrap(err, "failed to add subnet to wireguard interface")) | ||
} | ||
if err := netlink.LinkSetUp(link); err != nil { | ||
logrus.Warn(errors.Wrap(err, "failed to bring wireguard interface up")) | ||
} | ||
return nil | ||
} | ||
|
||
func ConfigureForwarding(wgIface string, gatewayIface string) error { | ||
// Networking configuration (iptables) configuration | ||
// to ensure that traffic from clients the wireguard interface | ||
// is sent to the provided network interface | ||
ipt, err := iptables.New() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to init iptables") | ||
} | ||
if err := ipt.AppendUnique("filter", "FORWARD", "-s", "10.0.0.1/24", "-o", wgIface, "-j", "ACCEPT"); err != nil { | ||
return errors.Wrap(err, "failed to set ip tables rule") | ||
} | ||
if err := ipt.AppendUnique("filter", "FORWARD", "-s", "10.0.0.1/24", "-i", wgIface, "-j", "ACCEPT"); err != nil { | ||
return errors.Wrap(err, "failed to set ip tables rule") | ||
} | ||
if err := ipt.AppendUnique("nat", "POSTROUTING", "-s", "10.0.0.1/24", "-o", gatewayIface, "-j", "MASQUERADE"); err != nil { | ||
return errors.Wrap(err, "failed to set ip tables rule") | ||
} | ||
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,41 @@ | ||
package services | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func ExecUserWireGuard(wgcommand string, ifaceName string) error { | ||
logrus.Infof("using userspace wireguard implementation %s", wgcommand) | ||
|
||
// create the command to exec | ||
// if it's "boringtun" we'll provide some non-standard | ||
// flags to better support running within a docker container | ||
var cmd *exec.Cmd | ||
if wgcommand == "boringtun" { | ||
cmd = exec.Command( | ||
wgcommand, | ||
ifaceName, | ||
"--disable-drop-privileges=root", | ||
"--foreground", | ||
) | ||
} else { | ||
cmd = exec.Command( | ||
wgcommand, | ||
"-f", | ||
ifaceName, | ||
) | ||
} | ||
|
||
entry := logrus.NewEntry(logrus.New()).WithField("process", wgcommand) | ||
cmd.Stdout = entry.Writer() | ||
cmd.Stderr = entry.Writer() | ||
logrus.Infof("starting %s", cmd.String()) | ||
if err := cmd.Run(); err != nil { | ||
return errors.Wrap(err, "userspace wireguard exitted") | ||
} | ||
|
||
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