-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (103 loc) · 2.44 KB
/
main.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
114
115
116
117
118
package main
import (
"flag"
"html/template"
"log"
"os"
"os/exec"
"runtime"
"github.com/cncws/jms-rdp/client"
)
const (
version = "0.1.0"
helpTemplate = `
JumpServer RDP Launcher v{{.}}
Support: MacOS(arm64), Windows(amd64)
Usage:
jms-rdp -url URL -ak AK -sk SK -account ACCOUNT IP
必需参数
-url 服务器地址
-ak Access Key,在 Web 页面 API Key 列表获取
-sk Secret Key,在 Web 页面 API Key 列表获取
-account 登陆账号
可选参数
-fullscreen 是否全屏
`
)
var (
serverURL string
accessKey, secretKey string
account string
fullscreen string
cli *client.JmsClient
)
func init() {
log.SetFlags(0)
if len(os.Args) == 1 {
tpl, _ := template.New("help").Parse(helpTemplate)
tpl.Execute(os.Stdout, version)
os.Exit(0)
}
flag.StringVar(&serverURL, "url", "", "server url")
flag.StringVar(&accessKey, "ak", "", "access key")
flag.StringVar(&secretKey, "sk", "", "secret key")
flag.StringVar(&account, "account", "", "登陆账号")
flag.StringVar(&fullscreen, "fullscreen", "1", "是否全屏")
flag.Parse()
if serverURL == "" {
log.Fatal("需设置 -url 参数")
}
if accessKey == "" {
log.Fatal("需设置 -ak 参数")
}
if secretKey == "" {
log.Fatal("需设置 -sk 参数")
}
if account == "" {
log.Fatal("需设置 -account 参数")
}
cli = client.NewJmsClient(serverURL, accessKey, secretKey, 3)
}
func handleRDP(assetID string) error {
log.Println("下载连接令牌...")
token, err := cli.GenRDPToken(assetID, account)
if err != nil {
return err
}
log.Println("✅", token)
log.Println("下载 RDP 文件...")
file, err := cli.DownRDP(token, fullscreen)
if err != nil {
return err
}
log.Println("✅", file)
log.Println("打开远程桌面...")
switch runtime.GOOS {
case "darwin":
cmd := exec.Command("open", file)
return cmd.Start()
case "windows":
cmd := exec.Command("mstsc.exe", file)
return cmd.Start()
default:
log.Println("❌尚未支持 ", runtime.GOOS)
}
return nil
}
func main() {
ips := flag.Args()
if len(ips) == 0 {
log.Fatal("❌未指定连接的资产 IP")
}
log.Println("查询资产 ID...")
id, protocols := cli.QueryIDByIP(ips[0])
log.Println("✅", id, "supports", protocols)
for _, p := range protocols {
switch p {
case "rdp":
handleRDP(id)
return
}
}
log.Fatal("❌未支持协议", protocols)
}