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

feat(config): wildcard lan interface name support #729

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
158 changes: 79 additions & 79 deletions cmd/internal/su.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,100 +6,100 @@
package internal

import (
"fmt"
"github.com/sirupsen/logrus"
"os"
"os/exec"
"fmt"
"github.com/sirupsen/logrus"
"os"
"os/exec"
)

func AutoSu() {
if os.Geteuid() == 0 {
return
}
path, arg := trySudo()
if path == "" {
path, arg = tryDoas()
}
if path == "" {
path, arg = tryPolkit()
}
if os.Geteuid() == 0 {
return
}
path, arg := trySudo()
if path == "" {
path, arg = tryDoas()
}
if path == "" {
path, arg = tryPolkit()
}

if path == "" {
return
}
logrus.Infof("use [ %s ] to elevate privileges to run [ %s ]", path, os.Args[0])
p, err := os.StartProcess(path, append(arg, os.Args...), &os.ProcAttr{
Files: []*os.File{
os.Stdin,
os.Stdout,
os.Stderr,
},
})
if err != nil {
logrus.Fatal(err)
}
stat, err := p.Wait()
if err != nil {
os.Exit(1)
}
os.Exit(stat.ExitCode())
if path == "" {
return
}
logrus.Infof("use [ %s ] to elevate privileges to run [ %s ]", path, os.Args[0])
p, err := os.StartProcess(path, append(arg, os.Args...), &os.ProcAttr{
Files: []*os.File{
os.Stdin,
os.Stdout,
os.Stderr,
},
})
if err != nil {
logrus.Fatal(err)
}
stat, err := p.Wait()
if err != nil {
os.Exit(1)
}
os.Exit(stat.ExitCode())
}

func trySudo() (path string, arg []string) {
pathSudo, err := exec.LookPath("sudo")
if err != nil || !isExistAndExecutable(pathSudo) {
return "", nil
}
// https://github.com/WireGuard/wireguard-tools/blob/71799a8f6d1450b63071a21cad6ed434b348d3d5/src/wg-quick/linux.bash#L85
return pathSudo, []string{
pathSudo,
"-E",
"-p",
fmt.Sprintf("Please enter the password for %%u to continue: "),
"--",
}
pathSudo, err := exec.LookPath("sudo")
if err != nil || !isExistAndExecutable(pathSudo) {
return "", nil
}
// https://github.com/WireGuard/wireguard-tools/blob/71799a8f6d1450b63071a21cad6ed434b348d3d5/src/wg-quick/linux.bash#L85
return pathSudo, []string{
pathSudo,
"-E",
"-p",
fmt.Sprintf("Please enter the password for %%u to continue: "),
"--",
}
}

func tryDoas() (path string, arg []string) {
// https://man.archlinux.org/man/doas.1
var err error
path, err = exec.LookPath("doas")
if err != nil {
return "", nil
}
return path, []string{path, "-u", "root"}
// https://man.archlinux.org/man/doas.1
var err error
path, err = exec.LookPath("doas")
if err != nil {
return "", nil
}
return path, []string{path, "-u", "root"}
}

func tryPolkit() (path string, arg []string) {
// https://github.com/systemd/systemd/releases/tag/v256
// introduced run0 which is a polkit wrapper.
var possible = []string{"run0", "pkexec"}
for _, v := range possible {
path, err := exec.LookPath(v)
if err != nil {
continue
}
if isExistAndExecutable(path) {
switch v {
case "run0":
return path, []string{path}
case "pkexec":
return path, []string{path, "--keep-cwd", "--user", "root"}
}
}
}
return "", nil
// https://github.com/systemd/systemd/releases/tag/v256
// introduced run0 which is a polkit wrapper.
var possible = []string{"run0", "pkexec"}
for _, v := range possible {
path, err := exec.LookPath(v)
if err != nil {
continue
}
if isExistAndExecutable(path) {
switch v {
case "run0":
return path, []string{path}
case "pkexec":
return path, []string{path, "--keep-cwd", "--user", "root"}
}
}
}
return "", nil
}

func isExistAndExecutable(path string) bool {
if path == "" {
return false
}
if path == "" {
return false
}

st, err := os.Stat(path)
if err == nil {
// https://stackoverflow.com/questions/60128401/how-to-check-if-a-file-is-executable-in-go
return st.Mode()&0o111 == 0o111
}
return false
st, err := os.Stat(path)
if err == nil {
// https://stackoverflow.com/questions/60128401/how-to-check-if-a-file-is-executable-in-go
return st.Mode()&0o111 == 0o111
}
return false
}
2 changes: 1 addition & 1 deletion cmd/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
Use: "reload [pid]",
Short: "To reload config file without interrupt connections.",
Run: func(cmd *cobra.Command, args []string) {
internal.AutoSu()
internal.AutoSu()
if len(args) == 0 {
_pid, err := os.ReadFile(PidFilePath)
if err != nil {
Expand Down
54 changes: 54 additions & 0 deletions common/netutils/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package netutils

import (
"fmt"
"regexp"
"strings"
)

func IsInterfaceNameIsWildcard(ifname string) bool {
return strings.ContainsAny(ifname, "*+")
}

type InterfaceMather interface {
Match(ifname string) bool
}

type simpleInterfaceMatcher struct {
pattern string
}

func (m *simpleInterfaceMatcher) Match(ifname string) bool {
return m.pattern == ifname
}

type wildcardInterfaceMatcher struct {
pattern string
re *regexp.Regexp
}

func (m *wildcardInterfaceMatcher) Match(ifname string) bool {
return m.pattern == ifname || m.re.MatchString(ifname)
}

func NewInterfaceMatcher(pattern string) (InterfaceMather, error) {
if !IsInterfaceNameIsWildcard(pattern) {
return &simpleInterfaceMatcher{pattern}, nil
}

return newWildcardInterfaceMatcher(pattern)
}

func newWildcardInterfaceMatcher(pattern string) (*wildcardInterfaceMatcher, error) {
regexPattern := "^" +
strings.ReplaceAll(
strings.ReplaceAll(pattern, "*", ".*"),
"+", ".+") +
"$"

regex, err := regexp.Compile(regexPattern)
if err != nil {
return nil, fmt.Errorf("failed to create interface matcher for ifname pattern %s: %w", pattern, err)
}
return &wildcardInterfaceMatcher{pattern, regex}, nil
}
2 changes: 1 addition & 1 deletion control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func NewControlPlane(
}
global.LanInterface = common.Deduplicate(global.LanInterface)
for _, ifname := range global.LanInterface {
if err = core.bindLan(ifname, global.AutoConfigKernelParameter); err != nil {
if err = core.bindLanWildcard(ifname, global.AutoConfigKernelParameter); err != nil {
return nil, fmt.Errorf("bindLan: %v: %w", ifname, err)
}
}
Expand Down
Loading
Loading