forked from illuminate/remote
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecLibGateway.php
More file actions
259 lines (227 loc) · 4.67 KB
/
SecLibGateway.php
File metadata and controls
259 lines (227 loc) · 4.67 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
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
<?php namespace Illuminate\Remote;
use Net_SFTP, Crypt_RSA;
use Illuminate\Filesystem\Filesystem;
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
* @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 void
*/
public function connect($username)
{
$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);
}
/**
* 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|string
*/
protected function getAuthForLogin()
{
// 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.
if ($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()
{
return (isset($this->auth['key']) && trim($this->auth['key']) != '');
}
/**
* Load the RSA key instance.
*
* @param array $auth
* @return \Crypt_RSA
*/
protected function loadRsaKey(array $auth)
{
with($key = $this->getKey($auth))->loadKey($this->files->get($auth['key']));
return $key;
}
/**
* 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;
}
/**
* 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);
}
}