Skip to content

Commit 9f20f48

Browse files
committed
v2.0.0
1 parent 37b6aa2 commit 9f20f48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+9213
-5868
lines changed

README.md

Lines changed: 252 additions & 120 deletions
Large diffs are not rendered by default.

cmd/console/console.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package console
2+
3+
import (
4+
"kctl/cmd"
5+
"kctl/internal/console"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var (
12+
// 命令行参数
13+
target string
14+
port int
15+
tokenFile string
16+
tokenStr string
17+
proxy string
18+
)
19+
20+
// ConsoleCmd 是 console 子命令
21+
var ConsoleCmd = &cobra.Command{
22+
Use: "console",
23+
Aliases: []string{"c"},
24+
Short: "进入交互式控制台",
25+
Long: `进入交互式控制台,支持扫描、查询、执行等操作
26+
27+
特点:
28+
- 无文件落地:所有数据缓存在内存中,退出时自动清除
29+
- 一次扫描,多次查询:扫描结果缓存,避免重复扫描
30+
- 交互式操作:类似 MSF 的命令行界面,支持自动补全
31+
- 自动连接:进入时自动使用当前环境信息连接
32+
33+
在 Pod 内运行时会自动:
34+
- 检测 Kubelet IP(默认网关)
35+
- 读取 ServiceAccount Token
36+
- 使用内存数据库
37+
- 自动连接到 Kubelet
38+
39+
示例:
40+
# 进入交互式控制台(自动连接)
41+
kctl console
42+
43+
# 指定目标进入
44+
kctl console -t 10.0.0.1
45+
46+
# 在控制台中
47+
kctl [default]> scan
48+
kctl [default]> sa --admin
49+
kctl [default]> use kube-system/cluster-admin
50+
kctl [kube-system/cluster-admin ADMIN]> exec -- whoami`,
51+
Run: runConsole,
52+
}
53+
54+
func init() {
55+
cmd.RootCmd.AddCommand(ConsoleCmd)
56+
57+
// 添加命令行参数
58+
ConsoleCmd.Flags().StringVarP(&target, "target", "t", "", "Kubelet IP 地址")
59+
ConsoleCmd.Flags().IntVarP(&port, "port", "p", 10250, "Kubelet 端口")
60+
ConsoleCmd.Flags().StringVar(&tokenFile, "token-file", "", "Token 文件路径")
61+
ConsoleCmd.Flags().StringVar(&tokenStr, "token", "", "Token 字符串")
62+
ConsoleCmd.Flags().StringVar(&proxy, "proxy", "", "SOCKS5 代理地址")
63+
}
64+
65+
func runConsole(cmd *cobra.Command, args []string) {
66+
// 注册所有命令
67+
console.RegisterCommands()
68+
69+
// 创建控制台,传入命令行参数
70+
opts := console.Options{
71+
Target: target,
72+
Port: port,
73+
TokenFile: tokenFile,
74+
Token: tokenStr,
75+
Proxy: proxy,
76+
}
77+
78+
c, err := console.NewWithOptions(opts)
79+
if err != nil {
80+
log.Errorf("创建控制台失败: %v", err)
81+
return
82+
}
83+
defer c.Close()
84+
85+
// 运行控制台
86+
c.Run()
87+
}

0 commit comments

Comments
 (0)