-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtunnel.cc
More file actions
executable file
·229 lines (207 loc) · 6.89 KB
/
Copy pathtunnel.cc
File metadata and controls
executable file
·229 lines (207 loc) · 6.89 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
/**
* @package node-ssh
* @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax.org>
* @author Gabor Krizsanits <gabor AT ajax DOT org>
* @license http://github.com/ajaxorg/node-ssh/blob/master/LICENSE MIT License
*/
#include "tunnel.h"
#include <node_buffer.h>
#include <string.h>
Persistent<FunctionTemplate> Tunnel::constructor_template;
/***********************************************
* Tunnel class
************************************************/
Tunnel::Tunnel(const Arguments& args)
: SSHBase(args)
, m_ssh_channel(NULL)
, m_remote_host(NULL)
, m_remote_port(NULL)
, m_src_host(NULL)
, m_local_port(NULL)
{
m_rdata = (char*) malloc (SSH_READ_BUFFER_SIZE);
}
Tunnel::~Tunnel()
{
freeSessions();
resetData();
if (m_rdata)
free(m_rdata);
}
Handle<Value> Tunnel::New(const Arguments &args)
{
HandleScope scope;
Tunnel *tunnel = new Tunnel(args);
tunnel->Wrap(args.This());
return args.This();
}
void Tunnel::resetData()
{
SSHBase::resetData();
m_size = m_pos = m_done = 0;
}
void Tunnel::freeSessions()
{
if (m_ssh_channel) {
ssh_channel_close(m_ssh_channel);
ssh_channel_free(m_ssh_channel);
m_ssh_channel = NULL;
}
SSHBase::freeSessions();
}
int Tunnel::startConnect(eio_req *req)
{
// ssh connection:
Tunnel* pthis = (Tunnel*) req->data;
if (SSHBase::startConnect(req)<0) {
return 0;
}
// create channel for the tunneling
pthis->m_ssh_channel = ssh_channel_new(pthis->m_ssh_session);
int res = channel_open_forward(pthis->m_ssh_channel,
pthis->m_remote_host, pthis->m_remote_port,
pthis->m_src_host, pthis->m_local_port);
if (res != SSH_OK) {
snprintf(pthis->m_error, SSH_MAX_ERROR,
"Error opening a channel for port forwarding: %s\n",
ssh_get_error(pthis->m_ssh_session));
ssh_channel_free(pthis->m_ssh_channel);
}
return 0;
}
// sending data
int Tunnel::startWrite(eio_req *req)
{
Tunnel* pthis = (Tunnel*) req->data;
int res = ssh_channel_write(pthis->m_ssh_channel,pthis->m_wdata,pthis->m_size);
if (res < pthis->m_size) {
snprintf(pthis->m_error, SSH_MAX_ERROR,
"Error writing channel: %s\n",
ssh_get_error(pthis->m_ssh_session));
}
return 0;
}
// pollling the channel and reading the incoming data
int Tunnel::startRead(eio_req *req)
{
Tunnel* pthis = (Tunnel*) req->data;
// poll the channel
int available = ssh_channel_poll(pthis->m_ssh_channel, 0);
if (available) {
// if something is available for read, reading it
int res = ssh_channel_read(pthis->m_ssh_channel,
pthis->m_rdata,SSH_MIN(available, SSH_READ_BUFFER_SIZE),0);
if (res<0) {
snprintf(pthis->m_error, SSH_MAX_ERROR,
"Error reading channel: %s\n",
ssh_get_error(pthis->m_ssh_session));
pthis->m_size = 0;
}
else {
pthis->m_size = res;
}
}
return 0;
}
int Tunnel::onRead(eio_req *req)
{
Tunnel* pthis = (Tunnel*) req->data;
HandleScope scope;
Handle<Value> argv[2];
argv[0] = String::New(pthis->m_error);
// read attempt is done, let's emitt an event, with the error and data
// (if there were any)
if (pthis->m_size)
argv[1] = createBuffer(pthis->m_rdata, pthis->m_size);
pthis->Emit(callback_symbol, pthis->m_size ? 2 : 1, argv);
ev_unref(EV_DEFAULT_UC);
return 0;
}
void Tunnel::Initialize(Handle<Object>& target)
{
HandleScope scope;
static int inited = 0;
if (!inited) {
// init of the constructor function
Local<FunctionTemplate> t = FunctionTemplate::New(New);
constructor_template = Persistent<FunctionTemplate>::New(t);
// inherit it from EventEmitter
constructor_template->Inherit(EventEmitter::constructor_template);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
// setting class name
constructor_template->SetClassName(String::NewSymbol("Tunnel"));
// setting methods on the prototype (exporting c++ functions)
NODE_SET_PROTOTYPE_METHOD(constructor_template, "init", init);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "connect", connect);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setPubKey", SSHBase::setPubKey);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setPrvKey", SSHBase::setPrvKey);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "interrupt", SSHBase::interrupt);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "read", read);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", write);
callback_symbol = NODE_PSYMBOL("callback");
inited = true;
}
// add the Tunnel constructor function as a property to the exported object
// (see init.cc)
target->Set(
String::NewSymbol("Tunnel"),
constructor_template->GetFunction()
);
}
// fetching all the data that is needed for the ssh port forwarding session
Handle<Value> Tunnel::init(const Arguments &args)
{
HandleScope scope;
Tunnel *pthis = ObjectWrap::Unwrap<Tunnel>(args.This());
pthis->freeSessions();
pthis->m_ssh_session = ssh_new();
if (!pthis->m_ssh_session)
return False();
Local<Object> opt = Local<Object>::Cast(args[0]);
setOption(pthis->m_ssh_session, opt, "host", SSH_OPTIONS_HOST);
setOption(pthis->m_ssh_session, opt, "port", SSH_OPTIONS_PORT_STR);
setOption(pthis->m_ssh_session, opt, "user", SSH_OPTIONS_USER);
setMember(pthis->m_remote_host, opt, "remoteHost");
setMember(pthis->m_remote_port, opt, "remotePort");
setMember(pthis->m_src_host, opt, "srcHost");
if (!pthis->m_src_host)
pthis->m_src_host = "localhost";
setMember(pthis->m_local_port, opt, "localPort");
if (!pthis->m_local_port)
pthis->m_local_port = 5555;
long timeout = 10;
ssh_options_set(pthis->m_ssh_session, SSH_OPTIONS_TIMEOUT, (void*) &timeout);
//ssh_options_set(pthis->m_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, (void*) new int(SSH_LOG_PACKET));
return True();
}
Handle<Value> Tunnel::connect(const Arguments &args)
{
HandleScope scope;
Tunnel *pthis = ObjectWrap::Unwrap<Tunnel>(args.This());
pthis->resetData();
eio_custom(startConnect, EIO_PRI_DEFAULT, onDone, pthis);
ev_ref(EV_DEFAULT_UC);
return True();
}
Handle<Value> Tunnel::write(const Arguments &args)
{
HandleScope scope;
Tunnel *pthis = ObjectWrap::Unwrap<Tunnel>(args.This());
pthis->resetData();
Local<Object> buffer_obj = args[0]->ToObject();
pthis->m_size = Buffer::Length(buffer_obj);
pthis->m_wdata = (char*) malloc (pthis->m_size);
memcpy(pthis->m_wdata, Buffer::Data(buffer_obj), pthis->m_size);
eio_custom(startWrite, EIO_PRI_DEFAULT, onDone, pthis);
ev_ref(EV_DEFAULT_UC);
return True();
}
Handle<Value> Tunnel::read(const Arguments &args)
{
HandleScope scope;
Tunnel *pthis = ObjectWrap::Unwrap<Tunnel>(args.This());
pthis->resetData();
eio_custom(startRead, EIO_PRI_DEFAULT, onRead, pthis);
ev_ref(EV_DEFAULT_UC);
return True();
}