Implements PostUp and PostDown commands using /bin/sh
This introduces PostUp and PostDown in dsnet. PostUp and PostDown allow the user to run arbitrary commands after the device is up or down. These are typically used to change the firewall rules via iptables. A working example would be ... "PostUp" : "iptables -A FORWARD -i dsnet -j ACCEPT; iptables -A FORWARD -o dsnet -j ACCEPT; iptables -t nat -A POSTROUTING -o ens2 -j MASQUERADE ", "PostDown" : "iptables -D FORWARD -i dsnet -j ACCEPT; iptables -D FORWARD -o dsnet -j ACCEPT; iptables -t nat -D POSTROUTING -o ens2 -j MASQUERADE ", ... All commands are executed by `/bin/sh` and no filtering or sandboxing is applied. Users of this should know what they are doing. Fixes https://github.com/naggie/dsnet/issues/16
This commit is contained in:
parent
ee8cb4c545
commit
e782db30e9
@ -66,6 +66,14 @@ with [hugo](https://gohugo.io/)
|
|||||||
network overview page. The shortcode file is included in this repository under
|
network overview page. The shortcode file is included in this repository under
|
||||||
`etc/`.
|
`etc/`.
|
||||||
|
|
||||||
|
"PostUp": ""
|
||||||
|
"PostDown": ""
|
||||||
|
|
||||||
|
Allows a user to specify commands to run after the device is up or down. This is
|
||||||
|
typcially a collection of `iptables` invocations. The commands are executed by
|
||||||
|
`/bin/sh`. *NOTE* These commands run as root, so make sure you check that they
|
||||||
|
are secure.
|
||||||
|
|
||||||
"PrivateKey": "uC+xz3v1mfjWBHepwiCgAmPebZcY+EdhaHAvqX2r7U8=",
|
"PrivateKey": "uC+xz3v1mfjWBHepwiCgAmPebZcY+EdhaHAvqX2r7U8=",
|
||||||
|
|
||||||
The server private key, automatically generated and very sensitive!
|
The server private key, automatically generated and very sensitive!
|
||||||
|
@ -73,6 +73,8 @@ Main (automatically generated) configuration example:
|
|||||||
"Networks": [],
|
"Networks": [],
|
||||||
"ReportFile": "/var/lib/dsnetreport.json",
|
"ReportFile": "/var/lib/dsnetreport.json",
|
||||||
"PrivateKey": "uC+xz3v1mfjWBHepwiCgAmPebZcY+EdhaHAvqX2r7U8=",
|
"PrivateKey": "uC+xz3v1mfjWBHepwiCgAmPebZcY+EdhaHAvqX2r7U8=",
|
||||||
|
"PostUp": "",
|
||||||
|
"PostDown" "",
|
||||||
"Peers": [
|
"Peers": [
|
||||||
{
|
{
|
||||||
"Hostname": "test",
|
"Hostname": "test",
|
||||||
|
3
add.go
3
add.go
@ -151,7 +151,6 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
|
|||||||
wgifSeed += int(b)
|
wgifSeed += int(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
t := template.Must(template.New("peerConf").Parse(peerConf))
|
t := template.Must(template.New("peerConf").Parse(peerConf))
|
||||||
err := t.Execute(os.Stdout, map[string]interface{}{
|
err := t.Execute(os.Stdout, map[string]interface{}{
|
||||||
"Peer": peer,
|
"Peer": peer,
|
||||||
@ -162,7 +161,7 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
|
|||||||
// vyatta requires an interface in range/format wg0-wg999
|
// vyatta requires an interface in range/format wg0-wg999
|
||||||
// deterministically choosing one in this range will probably allow use
|
// deterministically choosing one in this range will probably allow use
|
||||||
// of the config without a colliding interface name
|
// of the config without a colliding interface name
|
||||||
"Wgif": fmt.Sprintf("wg%d", wgifSeed % 999),
|
"Wgif": fmt.Sprintf("wg%d", wgifSeed%999),
|
||||||
})
|
})
|
||||||
check(err)
|
check(err)
|
||||||
}
|
}
|
||||||
|
@ -51,9 +51,11 @@ type DsnetConfig struct {
|
|||||||
// extra networks available, will be added to AllowedIPs
|
// extra networks available, will be added to AllowedIPs
|
||||||
Networks []JSONIPNet `validate:"required"`
|
Networks []JSONIPNet `validate:"required"`
|
||||||
// TODO Default subnets to route via VPN
|
// TODO Default subnets to route via VPN
|
||||||
ReportFile string `validate:"required"`
|
ReportFile string `validate:"required"`
|
||||||
PrivateKey JSONKey `validate:"required,len=44"`
|
PrivateKey JSONKey `validate:"required,len=44"`
|
||||||
Peers []PeerConfig `validate:"dive"`
|
PostUp string
|
||||||
|
PostDown string
|
||||||
|
Peers []PeerConfig `validate:"dive"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func MustLoadDsnetConfig() *DsnetConfig {
|
func MustLoadDsnetConfig() *DsnetConfig {
|
||||||
|
5
down.go
5
down.go
@ -7,6 +7,11 @@ import (
|
|||||||
func Down() {
|
func Down() {
|
||||||
conf := MustLoadDsnetConfig()
|
conf := MustLoadDsnetConfig()
|
||||||
DelLink(conf)
|
DelLink(conf)
|
||||||
|
RunPostDown(conf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunPostDown(conf *DsnetConfig) {
|
||||||
|
ShellOut(conf.PostDown, "PostDown")
|
||||||
}
|
}
|
||||||
|
|
||||||
func DelLink(conf *DsnetConfig) {
|
func DelLink(conf *DsnetConfig) {
|
||||||
|
@ -163,7 +163,7 @@ type PeerReport struct {
|
|||||||
// date peer was added to dsnet config
|
// date peer was added to dsnet config
|
||||||
Added time.Time
|
Added time.Time
|
||||||
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
|
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
|
||||||
IP net.IP
|
IP net.IP
|
||||||
IP6 net.IP
|
IP6 net.IP
|
||||||
// Last known external IP
|
// Last known external IP
|
||||||
ExternalIP net.IP
|
ExternalIP net.IP
|
||||||
|
5
up.go
5
up.go
@ -10,6 +10,11 @@ func Up() {
|
|||||||
conf := MustLoadDsnetConfig()
|
conf := MustLoadDsnetConfig()
|
||||||
CreateLink(conf)
|
CreateLink(conf)
|
||||||
ConfigureDevice(conf)
|
ConfigureDevice(conf)
|
||||||
|
RunPostUp(conf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunPostUp(conf *DsnetConfig) {
|
||||||
|
ShellOut(conf.PostUp, "PostUp")
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateLink(conf *DsnetConfig) {
|
func CreateLink(conf *DsnetConfig) {
|
||||||
|
12
util.go
12
util.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,6 +36,17 @@ func ExitFail(format string, a ...interface{}) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ShellOut(command string, name string) {
|
||||||
|
if command != "" {
|
||||||
|
fmt.Printf("Running %s commands:\n %s", name, command)
|
||||||
|
shell := exec.Command("/bin/sh", "-c", command)
|
||||||
|
err := shell.Run()
|
||||||
|
if err != nil {
|
||||||
|
ExitFail("%s '%s' failed", name, command, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ConfirmOrAbort(format string, a ...interface{}) {
|
func ConfirmOrAbort(format string, a ...interface{}) {
|
||||||
fmt.Fprintf(os.Stderr, format+" [y/n] ", a...)
|
fmt.Fprintf(os.Stderr, format+" [y/n] ", a...)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user