Skip to content

Commit 16b9f88

Browse files
committed
feat(rem): auto-resolve pipeline from agent-id in rem update interval
Allow `rem update interval --agent-id <id> <ms>` without requiring --pipeline-id. The command queries PivotingContexts to find which pipeline the agent belongs to. If the agent exists on multiple pipelines, the user is prompted to disambiguate with --pipeline-id.
1 parent 62b7529 commit 16b9f88

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

client/command/pipeline/commands.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,14 @@ rem delete rem_test
223223
},
224224
Example: `~~~
225225
rem update interval --session-id 08d6c05a 5000
226+
rem update interval --agent-id uDM0BgG6 5000
226227
rem update interval --pipeline-id rem_graph_api_03 --agent-id uDM0BgG6 5000
227228
~~~`,
228229
}
229230
common.BindFlag(updateIntervalCmd, func(f *pflag.FlagSet) {
230231
f.String("session-id", "", "Session ID to reconfigure (resolves pipeline and agent automatically)")
231-
f.String("pipeline-id", "", "Pipeline name (used with --agent-id)")
232-
f.String("agent-id", "", "REM agent ID (used with --pipeline-id)")
232+
f.String("pipeline-id", "", "Pipeline name (required only when agent exists on multiple pipelines)")
233+
f.String("agent-id", "", "REM agent ID (pipeline is auto-resolved if unique)")
233234
})
234235
common.BindFlagCompletions(updateIntervalCmd, func(comp carapace.ActionMap) {
235236
comp["pipeline-id"] = common.RemPipelineCompleter(con)

client/command/pipeline/rem.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/chainreactors/malice-network/client/core"
1111
"github.com/chainreactors/malice-network/helper/cryptography"
1212
"github.com/chainreactors/malice-network/helper/third/rem"
13+
"github.com/chainreactors/malice-network/helper/utils/output"
1314
"github.com/chainreactors/tui"
1415
"github.com/spf13/cobra"
1516
)
@@ -153,8 +154,40 @@ func RemUpdateIntervalCmd(cmd *cobra.Command, con *core.Console) error {
153154
}
154155
}
155156

157+
// Resolve pipeline from agent-id by querying PivotingContexts
158+
if agentID != "" && pipelineID == "" {
159+
ctxs, err := con.Rpc.GetContexts(con.Context(), &clientpb.Context{
160+
Type: consts.ContextPivoting,
161+
})
162+
if err != nil {
163+
return fmt.Errorf("failed to query pivot contexts: %w", err)
164+
}
165+
pivots, err := output.ToContexts[*output.PivotingContext](ctxs.Contexts)
166+
if err != nil {
167+
return fmt.Errorf("failed to parse pivot contexts: %w", err)
168+
}
169+
var matched []string
170+
seen := make(map[string]struct{})
171+
for _, p := range pivots {
172+
if p.RemAgentID == agentID && p.Enable {
173+
if _, dup := seen[p.Pipeline]; !dup {
174+
seen[p.Pipeline] = struct{}{}
175+
matched = append(matched, p.Pipeline)
176+
}
177+
}
178+
}
179+
switch len(matched) {
180+
case 0:
181+
return fmt.Errorf("agent %s not found in any active pipeline", agentID)
182+
case 1:
183+
pipelineID = matched[0]
184+
default:
185+
return fmt.Errorf("agent %s found in multiple pipelines %v, please specify --pipeline-id", agentID, matched)
186+
}
187+
}
188+
156189
if pipelineID == "" || agentID == "" {
157-
return fmt.Errorf("either --session-id or both --pipeline-id and --agent-id are required")
190+
return fmt.Errorf("either --session-id, --agent-id, or both --pipeline-id and --agent-id are required")
158191
}
159192

160193
_, err = con.Rpc.RemAgentCtrl(con.Context(), &clientpb.REMAgent{

0 commit comments

Comments
 (0)