-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.go
113 lines (99 loc) · 2.79 KB
/
jobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package jobs
/*
Sliver Implant Framework
Copyright (C) 2019 Bishop Fox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import (
"context"
"fmt"
"sort"
"github.com/starkzarn/glod/client/command/settings"
"github.com/starkzarn/glod/client/console"
"github.com/starkzarn/glod/protobuf/clientpb"
"github.com/starkzarn/glod/protobuf/commonpb"
"github.com/desertbit/grumble"
"github.com/jedib0t/go-pretty/v6/table"
)
// JobsCmd - Manage server jobs (listeners, etc)
func JobsCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
if ctx.Flags.Int("kill") != -1 {
jobKill(uint32(ctx.Flags.Int("kill")), con)
} else if ctx.Flags.Bool("kill-all") {
killAllJobs(con)
} else {
jobs, err := con.Rpc.GetJobs(context.Background(), &commonpb.Empty{})
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
// Convert to a map
activeJobs := map[uint32]*clientpb.Job{}
for _, job := range jobs.Active {
activeJobs[job.ID] = job
}
if 0 < len(activeJobs) {
PrintJobs(activeJobs, con)
} else {
con.PrintInfof("No active jobs\n")
}
}
}
// PrintJobs - Prints a list of active jobs
func PrintJobs(jobs map[uint32]*clientpb.Job, con *console.SliverConsoleClient) {
tw := table.NewWriter()
tw.SetStyle(settings.GetTableStyle(con))
tw.AppendHeader(table.Row{
"ID",
"Name",
"Protocol",
"Port",
"Stage Profile",
})
var keys []int
for _, job := range jobs {
keys = append(keys, int(job.ID))
}
sort.Ints(keys)
for _, k := range keys {
job := jobs[uint32(k)]
tw.AppendRow(table.Row{
fmt.Sprintf("%d", job.ID),
job.Name,
job.Protocol,
fmt.Sprintf("%d", job.Port),
job.ProfileName,
})
}
con.Printf("%s\n", tw.Render())
}
func jobKill(jobID uint32, con *console.SliverConsoleClient) {
con.PrintInfof("Killing job #%d ...\n", jobID)
jobKill, err := con.Rpc.KillJob(context.Background(), &clientpb.KillJobReq{
ID: jobID,
})
if err != nil {
con.PrintErrorf("%s\n", err)
} else {
con.PrintInfof("Successfully killed job #%d\n", jobKill.ID)
}
}
func killAllJobs(con *console.SliverConsoleClient) {
jobs, err := con.Rpc.GetJobs(context.Background(), &commonpb.Empty{})
if err != nil {
con.PrintErrorf("%s\n", err)
return
}
for _, job := range jobs.Active {
jobKill(job.ID, con)
}
}