Skip to content

Commit

Permalink
import windows scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Apr 16, 2020
1 parent a620458 commit f9d4161
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
30 changes: 20 additions & 10 deletions network/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package network
import (
"context"
"fmt"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"time"
)

func (network *Network) postConfigure(ctx context.Context, config *Config, tincBin string) error {
Expand All @@ -21,6 +23,7 @@ func (network *Network) postConfigure(ctx context.Context, config *Config, tincB
if iface.Name == config.Interface {
return nil
}
log.Println("Found interface:", iface.Name)
interfaces[iface.Name] = true
}

Expand All @@ -41,16 +44,23 @@ func (network *Network) postConfigure(ctx context.Context, config *Config, tincB
}

// find new interface
list, err = net.Interfaces()
if err != nil {
return err
}

var newInterface string
for _, iface := range list {
if !interfaces[iface.Name] {
newInterface = iface.Name
break
for newInterface == "" {
log.Println("Looking for a new interface")
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
}
list, err = net.Interfaces()
if err != nil {
return err
}
for _, iface := range list {
if !interfaces[iface.Name] {
newInterface = iface.Name
log.Println("New interface:", iface.Name)
break
}
}
}

Expand All @@ -59,7 +69,7 @@ func (network *Network) postConfigure(ctx context.Context, config *Config, tincB
}

// rename
cmd = exec.CommandContext(ctx, "netsh", "interface", "set", "name",
cmd = exec.CommandContext(ctx, "netsh", "interface", "set", "interface",
"name", "=", newInterface, "newname", "=", config.Interface)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand Down
6 changes: 3 additions & 3 deletions network/scripts_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package network
const scriptSuffix = ".bat"

const tincUpTxt = `
netsh interface ipv4 set address name="%INTERFACE%" static {{.Subnet}} store=persistent
netsh interface ipv4 set address name=%INTERFACE% static {{.Subnet}} store=persistent
`

const tincDownText = ``

const subnetUpText = `
{{.Executable}} subnet add && route add "$SUBNET" {{.Node.IP}}
{{.Executable}} subnet add && route add %SUBNET% {{.Node.IP}}
`

const subnetDownText = `
{{.Executable}} subnet remove && route delete "$SUBNET"
{{.Executable}} subnet remove && route delete %SUBNET%
`

func postProcessScript(filename string) error { return nil }
Expand Down
10 changes: 8 additions & 2 deletions tincd/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
"tinc-web-boot/network"
Expand Down Expand Up @@ -95,14 +96,19 @@ func (impl *netImpl) run(global context.Context) error {
return fmt.Errorf("configure: %w", err)
}

absDir, err := filepath.Abs(impl.definition.Root)
if err != nil {
return err
}

ctx, abort := context.WithCancel(global)
defer abort()

cmd := exec.CommandContext(ctx, impl.tincBin, "-D", "-d", "-d", "-d",
"--pidfile", impl.definition.Pidfile(),
"--logfile", impl.definition.Logfile(),
"-c", ".")
cmd.Dir = impl.definition.Root
"-c", absDir)
cmd.Dir = absDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
utils.SetCmdAttrs(cmd)
Expand Down

0 comments on commit f9d4161

Please sign in to comment.