-
Notifications
You must be signed in to change notification settings - Fork 21
/
SecLibGateway.php
321 lines (281 loc) · 5.91 KB
/
SecLibGateway.php
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php namespace Illuminate\Remote;
use Illuminate\Filesystem\Filesystem;
use Net_SFTP, Crypt_RSA, System_SSH_Agent;
class SecLibGateway implements GatewayInterface {
/**
* The host name of the server.
*
* @var string
*/
protected $host;
/**
* The SSH port on the server.
*
* @var int
*/
protected $port = 22;
/**
* The authentication credential set.
*
* @var array
*/
protected $auth;
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The SecLib connection instance.
*
* @var \Net_SFTP
*/
protected $connection;
/**
* Create a new gateway implementation.
*
* @param string $host
* @param array $auth
* @param \Illuminate\Filesystem\Filesystem $files
* @return void
*/
public function __construct($host, array $auth, Filesystem $files)
{
$this->auth = $auth;
$this->files = $files;
$this->setHostAndPort($host);
}
/**
* Set the host and port from a full host string.
*
* @param string $host
* @return void
*/
protected function setHostAndPort($host)
{
if ( ! str_contains($host, ':'))
{
$this->host = $host;
}
else
{
list($this->host, $this->port) = explode(':', $host);
$this->port = (int) $this->port;
}
}
/**
* Connect to the SSH server.
*
* @param string $username
* @return bool
*/
public function connect($username)
{
return $this->getConnection()->login($username, $this->getAuthForLogin());
}
/**
* Determine if the gateway is connected.
*
* @return bool
*/
public function connected()
{
return $this->getConnection()->isConnected();
}
/**
* Run a command against the server (non-blocking).
*
* @param string $command
* @return void
*/
public function run($command)
{
$this->getConnection()->exec($command, false);
}
/**
* Download the contents of a remote file.
*
* @param string $remote
* @param string $local
* @return void
*/
public function get($remote, $local)
{
$this->getConnection()->get($remote, $local);
}
/**
* Get the contents of a remote file.
*
* @param string $remote
* @return string
*/
public function getString($remote)
{
return $this->getConnection()->get($remote);
}
/**
* Upload a local file to the server.
*
* @param string $local
* @param string $remote
* @return void
*/
public function put($local, $remote)
{
$this->getConnection()->put($remote, $local, NET_SFTP_LOCAL_FILE);
}
/**
* Upload a string to to the given file on the server.
*
* @param string $remote
* @param string $contents
* @return void
*/
public function putString($remote, $contents)
{
$this->getConnection()->put($remote, $contents);
}
/**
* Get the next line of output from the server.
*
* @return string|null
*/
public function nextLine()
{
$value = $this->getConnection()->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
return $value === true ? null : $value;
}
/**
* Get the authentication object for login.
*
* @return \Crypt_RSA|\System_SSH_Agent|string
* @throws \InvalidArgumentException
*/
protected function getAuthForLogin()
{
if ($this->useAgent()) return $this->getAgent();
// If a "key" was specified in the auth credentials, we will load it into a
// secure RSA key instance, which will be used to connect to the servers
// in place of a password, and avoids the developer specifying a pass.
elseif ($this->hasRsaKey())
{
return $this->loadRsaKey($this->auth);
}
// If a plain password was set on the auth credentials, we will just return
// that as it can be used to connect to the server. This will be used if
// there is no RSA key and it gets specified in the credential arrays.
elseif (isset($this->auth['password']))
{
return $this->auth['password'];
}
throw new \InvalidArgumentException('Password / key is required.');
}
/**
* Determine if an RSA key is configured.
*
* @return bool
*/
protected function hasRsaKey()
{
$hasKey = (isset($this->auth['key']) && trim($this->auth['key']) != '');
return $hasKey || (isset($this->auth['keytext']) && trim($this->auth['keytext']) != '');
}
/**
* Load the RSA key instance.
*
* @param array $auth
* @return \Crypt_RSA
*/
protected function loadRsaKey(array $auth)
{
with($key = $this->getKey($auth))->loadKey($this->readRsaKey($auth));
return $key;
}
/**
* Read the contents of the RSA key.
*
* @param array $auth
* @return string
*/
protected function readRsaKey(array $auth)
{
if (isset($auth['key'])) return $this->files->get($auth['key']);
return $auth['keytext'];
}
/**
* Create a new RSA key instance.
*
* @param array $auth
* @return \Crypt_RSA
*/
protected function getKey(array $auth)
{
with($key = $this->getNewKey())->setPassword(array_get($auth, 'keyphrase'));
return $key;
}
/**
* Determine if the SSH Agent should provide an RSA key.
*
* @return bool
*/
protected function useAgent()
{
return isset($this->auth['agent']) && $this->auth['agent'] === true;
}
/**
* Get a new SSH Agent instance.
*
* @return \System_SSH_Agent
*/
public function getAgent()
{
return new System_SSH_Agent;
}
/**
* Get a new RSA key instance.
*
* @return \Crypt_RSA
*/
public function getNewKey()
{
return new Crypt_RSA;
}
/**
* Get the exit status of the last command.
*
* @return int|bool
*/
public function status()
{
return $this->getConnection()->getExitStatus();
}
/**
* Get the host used by the gateway.
*
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* Get the port used by the gateway.
*
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* Get the underlying Net_SFTP connection.
*
* @return \Net_SFTP
*/
public function getConnection()
{
if ($this->connection) return $this->connection;
return $this->connection = new Net_SFTP($this->host, $this->port);
}
}