router_linux: extract process runner routines into runner.go.
These will probably be useful across platforms. They're not really Linux-specific at all. Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>pull/418/head
parent
34c30eaea0
commit
8a6bd21baf
|
@ -6,13 +6,11 @@ package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/go-iptables/iptables"
|
"github.com/coreos/go-iptables/iptables"
|
||||||
|
@ -68,14 +66,6 @@ type netfilterRunner interface {
|
||||||
DeleteChain(table, chain string) error
|
DeleteChain(table, chain string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// commandRunner abstracts helpers to run OS commands. It exists
|
|
||||||
// purely to swap out osCommandRunner (below) with a fake runner in
|
|
||||||
// tests.
|
|
||||||
type commandRunner interface {
|
|
||||||
run(...string) error
|
|
||||||
output(...string) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type linuxRouter struct {
|
type linuxRouter struct {
|
||||||
logf func(fmt string, args ...interface{})
|
logf func(fmt string, args ...interface{})
|
||||||
tunname string
|
tunname string
|
||||||
|
@ -113,72 +103,6 @@ func newUserspaceRouterAdvanced(logf logger.Logf, tunname string, netfilter netf
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type osCommandRunner struct{}
|
|
||||||
|
|
||||||
func errCode(err error) int {
|
|
||||||
if err == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
var e *exec.ExitError
|
|
||||||
if ok := errors.As(err, &e); ok {
|
|
||||||
return e.ExitCode()
|
|
||||||
}
|
|
||||||
s := err.Error()
|
|
||||||
if strings.HasPrefix(s, "exitcode:") {
|
|
||||||
code, err := strconv.Atoi(s[9:])
|
|
||||||
if err == nil {
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -42
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o osCommandRunner) run(args ...string) error {
|
|
||||||
_, err := o.output(args...)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o osCommandRunner) output(args ...string) ([]byte, error) {
|
|
||||||
if len(args) == 0 {
|
|
||||||
return nil, errors.New("cmd: no argv[0]")
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("running %q failed: %w\n%s", strings.Join(args, " "), err, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type runGroup struct {
|
|
||||||
OkCode int // an error code that is acceptable, other than 0, if any
|
|
||||||
Runner commandRunner // the runner that actually runs our commands
|
|
||||||
ErrAcc error // first error encountered, if any
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRunGroup(okCode int, runner commandRunner) *runGroup {
|
|
||||||
return &runGroup{
|
|
||||||
OkCode: okCode,
|
|
||||||
Runner: runner,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rg *runGroup) Output(args ...string) []byte {
|
|
||||||
b, err := rg.Runner.output(args...)
|
|
||||||
if rg.ErrAcc == nil && err != nil && errCode(err) != rg.OkCode {
|
|
||||||
rg.ErrAcc = err
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rg *runGroup) Run(args ...string) {
|
|
||||||
err := rg.Runner.run(args...)
|
|
||||||
if rg.ErrAcc == nil && err != nil && errCode(err) != rg.OkCode {
|
|
||||||
rg.ErrAcc = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *linuxRouter) Up() error {
|
func (r *linuxRouter) Up() error {
|
||||||
if err := r.delLegacyNetfilter(); err != nil {
|
if err := r.delLegacyNetfilter(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// commandRunner abstracts helpers to run OS commands. It exists
|
||||||
|
// purely to swap out osCommandRunner (below) with a fake runner in
|
||||||
|
// tests.
|
||||||
|
type commandRunner interface {
|
||||||
|
run(...string) error
|
||||||
|
output(...string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type osCommandRunner struct{}
|
||||||
|
|
||||||
|
func errCode(err error) int {
|
||||||
|
if err == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var e *exec.ExitError
|
||||||
|
if ok := errors.As(err, &e); ok {
|
||||||
|
return e.ExitCode()
|
||||||
|
}
|
||||||
|
s := err.Error()
|
||||||
|
if strings.HasPrefix(s, "exitcode:") {
|
||||||
|
code, err := strconv.Atoi(s[9:])
|
||||||
|
if err == nil {
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -42
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o osCommandRunner) run(args ...string) error {
|
||||||
|
_, err := o.output(args...)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o osCommandRunner) output(args ...string) ([]byte, error) {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return nil, errors.New("cmd: no argv[0]")
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("running %q failed: %w\n%s", strings.Join(args, " "), err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type runGroup struct {
|
||||||
|
OkCode int // an error code that is acceptable, other than 0, if any
|
||||||
|
Runner commandRunner // the runner that actually runs our commands
|
||||||
|
ErrAcc error // first error encountered, if any
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRunGroup(okCode int, runner commandRunner) *runGroup {
|
||||||
|
return &runGroup{
|
||||||
|
OkCode: okCode,
|
||||||
|
Runner: runner,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rg *runGroup) Output(args ...string) []byte {
|
||||||
|
b, err := rg.Runner.output(args...)
|
||||||
|
if rg.ErrAcc == nil && err != nil && errCode(err) != rg.OkCode {
|
||||||
|
rg.ErrAcc = err
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rg *runGroup) Run(args ...string) {
|
||||||
|
err := rg.Runner.run(args...)
|
||||||
|
if rg.ErrAcc == nil && err != nil && errCode(err) != rg.OkCode {
|
||||||
|
rg.ErrAcc = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue