Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements PostUp and PostDown commands using /bin/sh #25

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ with [hugo](https://gohugo.io/)
network overview page. The shortcode file is included in this repository under
`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=",

The server private key, automatically generated and very sensitive!
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Main (automatically generated) configuration example:
"Networks": [],
"ReportFile": "/var/lib/dsnetreport.json",
"PrivateKey": "uC+xz3v1mfjWBHepwiCgAmPebZcY+EdhaHAvqX2r7U8=",
"PostUp": "",
"PostDown" "",
"Peers": [
{
"Hostname": "test",
Expand Down
3 changes: 1 addition & 2 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
wgifSeed += int(b)
}


t := template.Must(template.New("peerConf").Parse(peerConf))
err := t.Execute(os.Stdout, map[string]interface{}{
"Peer": peer,
Expand All @@ -162,7 +161,7 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
// vyatta requires an interface in range/format wg0-wg999
// deterministically choosing one in this range will probably allow use
// of the config without a colliding interface name
"Wgif": fmt.Sprintf("wg%d", wgifSeed % 999),
"Wgif": fmt.Sprintf("wg%d", wgifSeed%999),
})
check(err)
}
8 changes: 5 additions & 3 deletions configtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ type DsnetConfig struct {
// extra networks available, will be added to AllowedIPs
Networks []JSONIPNet `validate:"required"`
// TODO Default subnets to route via VPN
ReportFile string `validate:"required"`
PrivateKey JSONKey `validate:"required,len=44"`
Peers []PeerConfig `validate:"dive"`
ReportFile string `validate:"required"`
PrivateKey JSONKey `validate:"required,len=44"`
PostUp string
PostDown string
Peers []PeerConfig `validate:"dive"`
}

func MustLoadDsnetConfig() *DsnetConfig {
Expand Down
5 changes: 5 additions & 0 deletions down.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
func Down() {
conf := MustLoadDsnetConfig()
DelLink(conf)
RunPostDown(conf)
}

func RunPostDown(conf *DsnetConfig) {
ShellOut(conf.PostDown, "PostDown")
}

func DelLink(conf *DsnetConfig) {
Expand Down
2 changes: 1 addition & 1 deletion reporttypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ type PeerReport struct {
// date peer was added to dsnet config
Added time.Time
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
IP net.IP
IP net.IP
IP6 net.IP
// Last known external IP
ExternalIP net.IP
Expand Down
5 changes: 5 additions & 0 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ func Up() {
conf := MustLoadDsnetConfig()
CreateLink(conf)
ConfigureDevice(conf)
RunPostUp(conf)
}

func RunPostUp(conf *DsnetConfig) {
ShellOut(conf.PostUp, "PostUp")
}

func CreateLink(conf *DsnetConfig) {
Expand Down
12 changes: 12 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)

Expand Down Expand Up @@ -35,6 +36,17 @@ func ExitFail(format string, a ...interface{}) {
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{}) {
fmt.Fprintf(os.Stderr, format+" [y/n] ", a...)

Expand Down