提交 ba72317f authored 作者: blu's avatar blu

feature: reverse debug tunnel

上级 7eab710a
...@@ -547,8 +547,13 @@ private: ...@@ -547,8 +547,13 @@ private:
return -1; return -1;
} }
thread th; thread th;
auto port = createReverseTun(tunCfg["host"], tunCfg["port"], tunCfg["user"], tunCfg["password"], th); string host = tunCfg["host"], user = tunCfg["user"], password = tunCfg["password"];
spdlog::info("evdaemon {} created reverse tun to {}:{}", devSn, tunCfg["host"].get<string>(), port); int port = tunCfg["port"];
th = thread([host, port, user, password](){
createReverseTun(host, port, user, password);
});
spdlog::info("evdaemon {} created reverse tun to {}:{}", devSn, tunCfg["host"].get<string>(), tunCfg["port"].get<int>());
th.detach(); th.detach();
return ret; return ret;
......
...@@ -35,7 +35,7 @@ enum { ...@@ -35,7 +35,7 @@ enum {
}; };
int createReverseTun(string host, int port, string user, string _password, thread &thWorker) int createReverseTun(string host, int port, string user, string _password)
{ {
int remote_listenport = -1; int remote_listenport = -1;
const char *server_ip = host.c_str(); const char *server_ip = host.c_str();
...@@ -56,7 +56,6 @@ int createReverseTun(string host, int port, string user, string _password, threa ...@@ -56,7 +56,6 @@ int createReverseTun(string host, int port, string user, string _password, threa
fd_set fds; fd_set fds;
struct timeval tv; struct timeval tv;
ssize_t len, wr; ssize_t len, wr;
char buf[10240]; // 10K
string msg = "Fingerprint: "; string msg = "Fingerprint: ";
int sock = -1, forwardsock = -1; int sock = -1, forwardsock = -1;
...@@ -211,75 +210,79 @@ int createReverseTun(string host, int port, string user, string _password, threa ...@@ -211,75 +210,79 @@ int createReverseTun(string host, int port, string user, string _password, threa
/* Must use non-blocking IO hereafter due to the current libssh2 API */ /* Must use non-blocking IO hereafter due to the current libssh2 API */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
thWorker = thread([&]() { bool hasError = false;
bool hasError = false; char buf[10240]; // 10K
while(1) { while(1) {
FD_ZERO(&fds); if(hasError) {
FD_SET(forwardsock, &fds); break;
tv.tv_sec = 0; }
tv.tv_usec = 100000; FD_ZERO(&fds);
rc = select(forwardsock + 1, &fds, nullptr, nullptr, &tv); FD_SET(forwardsock, &fds);
if(-1 == rc) { tv.tv_sec = 0;
spdlog::error("tun failed to select"); tv.tv_usec = 100000;
rc = select(forwardsock + 1, &fds, nullptr, nullptr, &tv);
if(-1 == rc) {
spdlog::error("tun failed to select");
break;
}
if(rc && FD_ISSET(forwardsock, &fds)) {
len = recv(forwardsock, buf, sizeof(buf), 0);
if(len < 0) {
spdlog::error("tun failed to read");
break; break;
} }
else if(0 == len) {
if(rc && FD_ISSET(forwardsock, &fds)) { spdlog::info("tun the local server at {}:{} disconnected!\n",
len = recv(forwardsock, buf, sizeof(buf), 0); local_destip, local_destport);
if(len < 0) { break;
spdlog::error("tun failed to read"); }
break; wr = 0;
} do {
else if(0 == len) { i = libssh2_channel_write(channel, buf, len);
spdlog::info("tun the local server at {}:{} disconnected!\n", if(i < 0) {
local_destip, local_destport); spdlog::error("tun failed to libssh2_channel_write: {}", i);
hasError = true;
break; break;
} }
wr = 0; wr += i;
do {
i = libssh2_channel_write(channel, buf, len);
if(i < 0) {
spdlog::error("tun failed to libssh2_channel_write: {}", i);
hasError = true;
break;
}
wr += i;
}
while(i > 0 && wr < len);
} }
while(i > 0 && wr < len);
}
while(1) { while(1) {
len = libssh2_channel_read(channel, buf, sizeof(buf)); len = libssh2_channel_read(channel, buf, sizeof(buf));
if(LIBSSH2_ERROR_EAGAIN == len) if(LIBSSH2_ERROR_EAGAIN == len)
break; break;
else if(len < 0) { else if(len < 0) {
spdlog::error("tun failed libssh2_channel_read: {}", (int)len); spdlog::error("tun failed libssh2_channel_read: {}", (int)len);
hasError = true;
break;
}
wr = 0;
while(wr < len) {
i = send(forwardsock, buf + wr, len - wr, 0);
if(i <= 0) {
spdlog::error("tun failed to reverse write");
hasError = true; hasError = true;
break; break;
} }
wr = 0; wr += i;
while(wr < len) { }
i = send(forwardsock, buf + wr, len - wr, 0);
if(i <= 0) {
spdlog::error("tun failed to reverse write");
hasError = true;
break;
}
wr += i;
}
if(libssh2_channel_eof(channel)) { if(libssh2_channel_eof(channel)) {
spdlog::error("tun the remote client at {}:{} disconnected!", spdlog::info("tun the remote client at {}:{} disconnected!",
remote_listenhost, remote_listenport); remote_listenhost, remote_listenport);
break; hasError = true;
} break;
} }
} }
}
closeFun(); closeFun();
});
return remote_listenport; return 0;//remote_listenport;
} }
#endif #endif
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论