This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
/
generic.go
260 lines (243 loc) · 6.94 KB
/
generic.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package connector
import (
"bytes"
"encoding/base64"
"errors"
"io"
"strings"
"sync"
"time"
"github.com/limanmys/render-engine/postgresql"
"golang.org/x/text/encoding/unicode"
"github.com/acarl005/stripansi"
"golang.org/x/crypto/ssh"
)
//GetConnection GetConnection
func GetConnection(userID string, serverID string, IPAddress string) (*Connection, error) {
var val Connection
if val2, ok := ActiveConnections[userID+serverID]; ok {
val = val2
} else {
res := val.CreateShell(userID, serverID, IPAddress)
if !res {
return &val, errors.New("cannot connect to server")
}
}
val.LastConnection = time.Now()
ActiveConnections[userID+serverID] = val
return &val, nil
}
//CreateShell CreateShell
func (val *Connection) CreateShell(userID string, serverID string, IPAddress string) bool {
username, password, keyPort, keyObject := postgresql.GetServerKey(userID, serverID)
val.password = password
val.IpAddr = IPAddress
val.Port = keyPort
if keyObject.Type == "ssh" {
connection, err := InitShellWithPassword(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.SSH = connection
} else if keyObject.Type == "ssh_certificate" {
connection, err := InitShellWithCertificate(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.SSH = connection
} else if keyObject.Type == "winrm" {
connection, err := InitWinRMShell(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.WinRM = connection
val.WindowsPath = strings.TrimSpace(val.Run("echo $env:TEMP")) + "\\"
val.WindowsLetter = val.WindowsPath[0:1]
val.WindowsPath = val.WindowsPath[3:]
} else if keyObject.Type == "winrm_insecure" {
connection, err := InitInsecureWinRMShell(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.WinRM = connection
val.WindowsPath = strings.TrimSpace(val.Run("echo $env:TEMP")) + "\\"
val.WindowsLetter = val.WindowsPath[0:1]
val.WindowsPath = val.WindowsPath[3:]
} else {
return false
}
return true
}
//CreateFileConnection CreateFileConnection
func (val *Connection) CreateFileConnection(userID string, serverID string, IPAddress string) bool {
username, password, keyPort, keyObject := postgresql.GetServerKey(userID, serverID)
val.password = password
val.IpAddr = IPAddress
val.Port = keyPort
if keyObject.Type == "ssh" || keyObject.Type == "ssh_certificate" {
if val.SFTP != nil {
return true
}
flag := val.CreateShell(userID, serverID, IPAddress)
if !flag {
return false
}
val.SFTP = OpenSFTPConnection(val.SSH)
} else if keyObject.Type == "winrm" {
if val.SMB != nil {
return true
}
temp, err := OpenSMBConnection(IPAddress, username, password)
if err != nil {
return false
}
val.SMB = temp
} else if keyObject.Type == "winrm_insecure" {
if val.SMB != nil {
return true
}
temp, err := OpenSMBConnection(IPAddress, username, password)
if err != nil {
return false
}
val.SMB = temp
} else {
return false
}
return true
}
//CreateShellRaw CreateShellRaw
func (val *Connection) CreateShellRaw(connectionType string, username string, password string, IPAddress string, keyPort string) bool {
val.password = password
val.IpAddr = IPAddress
val.Port = keyPort
if connectionType == "ssh" {
connection, err := InitShellWithPassword(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.SSH = connection
} else if connectionType == "ssh_certificate" {
connection, err := InitShellWithCertificate(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.SSH = connection
} else if connectionType == "winrm" {
connection, err := InitWinRMShell(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.WinRM = connection
} else if connectionType == "winrm_insecure" {
connection, err := InitInsecureWinRMShell(username, password, IPAddress, keyPort)
if err != nil {
return false
}
val.WinRM = connection
} else {
return false
}
return true
}
var mutex = &sync.Mutex{}
func checkOutput(in io.Writer, output *bytes.Buffer, val Connection) bool {
mutex.Lock()
defer mutex.Unlock()
if output != nil && output.Len() > 0 {
if output.String() == "liman-pass-sudo" {
_, _ = in.Write([]byte(val.password + "\n"))
return true
} else {
return true
}
}
return false
}
//Run Run through ssh
func (val Connection) Run(command string) string {
if val.SSH != nil {
sess, err := val.SSH.NewSession()
if err != nil {
return err.Error()
}
defer sess.Close()
modes := ssh.TerminalModes{
ssh.ECHO: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
err = sess.RequestPty("dumb", 1000, 1000, modes)
if err != nil {
return err.Error()
}
stdoutB := new(bytes.Buffer)
sess.Stdout = stdoutB
in, err := sess.StdinPipe()
if err != nil {
return err.Error()
}
if strings.Contains(command, "liman-pass-sudo") {
endChan := make(chan struct{})
defer close(endChan)
go func(in io.Writer, output *bytes.Buffer, endChan chan struct{}) {
For:
for {
select {
case <-endChan:
break For
default:
if checkOutput(in, output, val) {
break For
}
time.Sleep(500)
}
}
}(in, stdoutB, endChan)
}
sess.Run("(" + command + ") 2> /dev/null")
return stripansi.Strip(strings.TrimSpace(strings.Replace(stdoutB.String(), "liman-pass-sudo", "", 1)))
} else if val.WinRM != nil {
command = "$ProgressPreference = 'SilentlyContinue';" + command
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
encoded, _ := encoder.String(command)
command = base64.StdEncoding.EncodeToString([]byte(encoded))
stdout, stderr, _, err := val.WinRM.RunWithString("powershell.exe -encodedCommand "+command, "")
if err != nil {
return err.Error()
}
return strings.TrimSpace(stdout) + strings.TrimSpace(stderr)
}
return "Cannot run command!"
}
//Put Put
func (val Connection) Put(localPath string, remotePath string) bool {
if val.SFTP != nil {
return PutFileSFTP(val.SFTP, localPath, remotePath)
} else if val.SMB != nil {
return PutFileSMB(val.SMB, localPath, remotePath, val.WindowsLetter)
}
return false
}
//Get Get
func (val Connection) Get(localPath string, remotePath string) bool {
if val.SFTP != nil {
return GetFileSFTP(val.SFTP, localPath, remotePath)
} else if val.SMB != nil {
return GetFileSMB(val.SMB, localPath, remotePath, val.WindowsLetter)
}
return false
}
//VerifyAuth VerifyAuth
func VerifyAuth(username string, password string, ipAddress string, port string, keyType string) bool {
if keyType == "ssh" {
return VerifySSH(username, password, ipAddress, port)
} else if keyType == "ssh_certificate" {
return VerifySSHCertificate(username, password, ipAddress, port)
} else if keyType == "winrm" {
return VerifyWinRM(username, password, ipAddress, port)
} else if keyType == "winrm_insecure" {
return VerifyInsecureWinRM(username, password, ipAddress, port)
}
return true
}