-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathxshell2windterm.go
More file actions
149 lines (131 loc) · 3.73 KB
/
xshell2windterm.go
File metadata and controls
149 lines (131 loc) · 3.73 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"flag"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/google/uuid"
)
type Session struct {
UUID string `json:"session.uuid"`
Group string `json:"session.group"`
Label string `json:"session.label"`
Port int `json:"session.port"`
Protocol string `json:"session.protocol"`
Target string `json:"session.target"`
IdentityFilePath string `json:"ssh.identityFilePath"`
Icon string `json:"session.icon"`
AutoLogin string `json:"session.autoLogin"`
KeepAlive int `json:"session.keepAlive"`
EnableTcpKeepAlive bool `json:"session.tcpKeepAlive"`
}
func main() {
xshellSessionDIR := flag.String("i", "", "Xshell session directory")
windTermUserFile := flag.String("o", "user.sessions", "WindTerm user file")
showHelp := flag.Bool("h", false, "Show help")
flag.Parse()
if *showHelp {
flag.Usage()
return
}
if *xshellSessionDIR == "" {
log.Print("Xshell session directory is required")
flag.Usage()
return
}
windTerm := make([]Session, 0)
xshFiles := scanSessionDir(*xshellSessionDIR)
for _, xshFile := range xshFiles {
log.Printf("Processing %s", xshFile)
session, err := handleXshFile(xshFile)
if err != nil {
log.Printf("Fail to generate session info for %s: %v", xshFile, err)
}
session.UUID = NewUUID()
session.Label = getLable(xshFile)
session.Group = getGroup(*xshellSessionDIR, xshFile)
session.Icon = "session::square-mediumorchid"
session.Protocol = "SSH"
session.EnableTcpKeepAlive = true
session.KeepAlive = 60
windTerm = append(windTerm, session)
}
saveToJson(windTerm, *windTermUserFile)
}
func NewUUID() string {
uuid, _ := uuid.NewRandom()
return uuid.String()
}
func scanSessionDir(sessionDir string) []string {
var sessionFiles []string
err := filepath.Walk(sessionDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".xsh") {
sessionFiles = append(sessionFiles, path)
}
return nil
})
if err != nil {
log.Fatal(err)
}
return sessionFiles
}
func handleXshFile(xshFile string) (Session, error) {
session := Session{}
bytes, err := os.ReadFile(xshFile)
if err != nil {
log.Printf("Fail to read %s: %v", xshFile, err)
return session, err
}
data := strings.Split(string(bytes), "\n")
var ip, user string
for _, line := range data {
r := strings.ReplaceAll(line, " ", "")
r = strings.ReplaceAll(r, "\u0000", "")
r = strings.ReplaceAll(r, "\r", "")
if strings.HasPrefix(r, "Host=") {
ip = strings.TrimPrefix(r, "Host=")
}
if strings.HasPrefix(r, "UserName=") {
user = strings.TrimPrefix(r, "UserName=")
}
if strings.HasPrefix(r, "Port=") {
port := strings.TrimPrefix(r, "Port=")
session.Port, _ = strconv.Atoi(port)
}
if strings.HasPrefix(r, "UserKey=") {
session.IdentityFilePath = strings.TrimPrefix(r, "UserKey=")
}
}
session.Target = user + "@" + ip
return session, nil
}
func getLable(xshFile string) string {
filename := filepath.Base(xshFile)
return strings.TrimSuffix(filename, ".xsh")
}
func getGroup(basePath, xshFile string) string {
group := filepath.Dir(xshFile)
group = strings.ReplaceAll(group, basePath, "")
group = strings.ReplaceAll(group, "\\", ">")
return strings.TrimPrefix(group, ">")
}
func saveToJson(session []Session, targetFile string) {
f, err := os.Create(targetFile)
if err != nil {
log.Fatalf("Fail to create %s: %v", targetFile, err)
}
defer f.Close()
bytes, _ := json.MarshalIndent(session, "", " ")
f.Write(bytes)
log.Printf("Saved %s", targetFile)
log.Printf("Note: You need to manually update the identity file path in %s", targetFile)
}