Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
samlm0 committed Mar 6, 2024
1 parent a8f8e09 commit 8a21e9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 6 additions & 1 deletion backend/fakeshell/commands/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (
"github.com/spf13/cobra"
)

func AddExecureableAsCommand(cmd *cobra.Command, command string) {
func AddExecureableAsCommand(cmd *cobra.Command, command string, argFilter func(args []string) ([]string, error)) {

cmdDefine := &cobra.Command{
Use: command,
Run: func(cmd *cobra.Command, args []string) {
args, err := argFilter(args)
if err != nil {
cmd.Println(err)
return
}
c := exec.Command(command, args...)
c.Env = os.Environ()
c.Env = append(c.Env, "TERM=xterm-256color")
Expand Down
25 changes: 24 additions & 1 deletion backend/fakeshell/menu.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fakeshell

import (
"errors"
"fmt"
"os/exec"
"regexp"

"github.com/reeflective/console"
"github.com/samlm0/als/v2/config"
Expand All @@ -27,7 +29,24 @@ func defineMenuCommands(a *console.Console) console.Commands {
"mtr": config.Config.FeatureMTR,
}

argsFilter := map[string]func([]string) ([]string, error){
"ping": func(args []string) ([]string, error) {
var re = regexp.MustCompile(`(?m)^-?f$|^-\S+f\S*$`)
for _, str := range args {
if len(re.FindAllString(str, -1)) != 0 {
return []string{}, errors.New("dangerous flag detected, stop running")
}
}
return args, nil
},
}

hasNotFound := false

argsPassthough := func(args []string) ([]string, error) {
return args, nil
}

for command, feature := range features {
if feature {
_, err := exec.LookPath(command)
Expand All @@ -38,7 +57,11 @@ func defineMenuCommands(a *console.Console) console.Commands {
hasNotFound = true
continue
}
commands.AddExecureableAsCommand(rootCmd, command)
filter, ok := argsFilter[command]
if !ok {
filter = argsPassthough
}
commands.AddExecureableAsCommand(rootCmd, command, filter)
}
}

Expand Down

0 comments on commit 8a21e9a

Please sign in to comment.