Skip to content
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
26 changes: 26 additions & 0 deletions services/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/mark3labs/mcp-go/mcp"
"github.com/rs/zerolog"
"path/filepath"
"strings"
)

var (
Expand Down Expand Up @@ -155,6 +156,31 @@ func (cs *CommandServer) handleExecuteCommand(ctx context.Context, request mcp.C
}, nil
}

// isAllowedCommand checks if the command is allowed based on the configuration.
func (cs *CommandServer) isAllowedCommand(command string) bool {

// 检查命令是否在允许的列表中
for _, allowed := range cs.config.AllowedCommands {
if strings.HasPrefix(command, allowed) {
return true
}
}

// 如果命令包含管道符,进一步检查每个子命令
if strings.Contains(command, "|") {
parts := strings.Split(command, "|")
for _, part := range parts {
part = strings.TrimSpace(part)
if !cs.isAllowedCommand(part) {
return false
}
}
return true
}

return false
}

// Config returns the configuration of the service as a string.
func (cs *CommandServer) Config() string {
cfg, err := json.Marshal(cs.config)
Expand Down
26 changes: 0 additions & 26 deletions services/command_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"errors"
"os/exec"
"strings"
"time"
)

Expand All @@ -48,28 +47,3 @@ func (cs *CommandServer) executeCommand(command string) (string, error) {

return string(output), nil
}

// isAllowedCommand checks if the command is allowed based on the configuration.
func (cs *CommandServer) isAllowedCommand(command string) bool {

// 检查命令是否在允许的列表中
for _, allowed := range cs.config.AllowedCommands {
if strings.HasPrefix(command, allowed) {
return true
}
}

// 如果命令包含管道符,进一步检查每个子命令
if strings.Contains(command, "|") {
parts := strings.Split(command, "|")
for _, part := range parts {
part = strings.TrimSpace(part)
if !cs.isAllowedCommand(part) {
return false
}
}
return true
}

return false
}
Loading