This commit is contained in:
aixiao 2024-01-03 09:19:09 +08:00
parent fd34266843
commit 1ea21b7bc6
4 changed files with 26 additions and 39 deletions

Binary file not shown.

View File

@ -1,6 +1,5 @@
#include "forward-tunnel.h" #include "forward-tunnel.h"
const char *keyfile1 = "/home/aixiao/.ssh/id_rsa.pub"; const char *keyfile1 = "/home/aixiao/.ssh/id_rsa.pub";
const char *keyfile2 = "/home/aixiao/.ssh/id_rsa"; const char *keyfile2 = "/home/aixiao/.ssh/id_rsa";
@ -52,7 +51,7 @@ void *forward_tunnel(void *sock_)
perror("socket"); perror("socket");
return NULL; return NULL;
} }
memset(&sin, 0, sizeof(sin)); memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(server_ssh_ip); sin.sin_addr.s_addr = inet_addr(server_ssh_ip);
@ -136,16 +135,16 @@ void *forward_tunnel(void *sock_)
while (1) { while (1) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(forwardsock, &fds); // 设置一个描述符 FD_SET(forwardsock, &fds); // 设置一个描述符
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 100000; tv.tv_usec = 100000;
rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
if (-1 == rc) { // 返回-1表示出错 if (-1 == rc) { // 返回-1表示出错
perror("select"); perror("select");
goto shutdown; goto shutdown;
} }
if (rc && FD_ISSET(forwardsock, &fds)) { // 测试某一个描述符返回值: 若fd在描述符集中则返回非0,否则返回0 if (rc && FD_ISSET(forwardsock, &fds)) { // 测试某一个描述符返回值: 若fd在描述符集中则返回非0,否则返回0
len = read(forwardsock, buffer, sizeof(buffer)); len = read(forwardsock, buffer, sizeof(buffer));
if (len < 0) { if (len < 0) {
perror("read"); perror("read");
@ -154,7 +153,7 @@ void *forward_tunnel(void *sock_)
fprintf(stderr, "The client at %s:%d disconnected!\n", shost, sport); fprintf(stderr, "The client at %s:%d disconnected!\n", shost, sport);
goto shutdown; goto shutdown;
} }
wr = 0; wr = 0;
while (wr < len) { while (wr < len) {
i = libssh2_channel_write(channel, buffer + wr, len - wr); i = libssh2_channel_write(channel, buffer + wr, len - wr);
@ -166,11 +165,11 @@ void *forward_tunnel(void *sock_)
fprintf(stderr, "libssh2_channel_write: %d\n", i); fprintf(stderr, "libssh2_channel_write: %d\n", i);
goto shutdown; goto shutdown;
} }
wr += i; wr += i;
} }
} }
while (1) { while (1) {
len = libssh2_channel_read(channel, buffer, sizeof(buffer)); len = libssh2_channel_read(channel, buffer, sizeof(buffer));
@ -180,7 +179,7 @@ void *forward_tunnel(void *sock_)
fprintf(stderr, "libssh2_channel_read: %d", (int)len); fprintf(stderr, "libssh2_channel_read: %d", (int)len);
goto shutdown; goto shutdown;
} }
wr = 0; wr = 0;
while (wr < len) { while (wr < len) {
i = write(forwardsock, buffer + wr, len - wr); i = write(forwardsock, buffer + wr, len - wr);
@ -188,7 +187,7 @@ void *forward_tunnel(void *sock_)
perror("write"); perror("write");
goto shutdown; goto shutdown;
} }
wr += i; wr += i;
} }
@ -199,8 +198,6 @@ void *forward_tunnel(void *sock_)
} }
} }
shutdown: shutdown:
close(forwardsock); close(forwardsock);
if (channel) if (channel)
@ -211,7 +208,6 @@ shutdown:
close(sock); close(sock);
libssh2_exit(); libssh2_exit();
return NULL; return NULL;
} }
@ -261,7 +257,7 @@ static char help_info(void)
return 0; return 0;
} }
int nice_( int increment) int nice_(int increment)
{ {
int oldprio = getpriority(PRIO_PROCESS, getpid()); int oldprio = getpriority(PRIO_PROCESS, getpid());
printf("%d\n", oldprio); printf("%d\n", oldprio);
@ -280,8 +276,7 @@ int main(int argc, char *argv[], char **env)
char *p = NULL; char *p = NULL;
char *client_ip = NULL; char *client_ip = NULL;
int i, j; int i, j;
char optstring[] = ":dr:s:p:l:u:e:h?"; char optstring[] = ":dr:s:p:l:u:e:h?";
while (-1 != (opt = getopt(argc, argv, optstring))) { while (-1 != (opt = getopt(argc, argv, optstring))) {
switch (opt) { switch (opt) {
@ -295,21 +290,20 @@ int main(int argc, char *argv[], char **env)
p = strchr(optarg, ':'); p = strchr(optarg, ':');
if (p != NULL) { if (p != NULL) {
remote_destport = atoi(p + 1); remote_destport = atoi(p + 1);
*p = '\0'; *p = '\0';
remote_desthost = optarg; remote_desthost = optarg;
} else { } else {
if (NULL == (p = strchr(optarg, '.'))) if (NULL == (p = strchr(optarg, '.'))) {
{ remote_destport = atoi(optarg);
remote_destport =atoi(optarg);
remote_desthost = "0.0.0.0"; remote_desthost = "0.0.0.0";
} else { } else {
help_info(); help_info();
exit(0); exit(0);
} }
} }
break; break;
case 'p': case 'p':
server_ssh_port = atoi(optarg); server_ssh_port = atoi(optarg);
@ -335,17 +329,15 @@ int main(int argc, char *argv[], char **env)
// 加密参数(账号密码) // 加密参数(账号密码)
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (0 == strcmp(argv[i], "-e") || 0 == strcmp(argv[i], "-u") || 0 == strcmp(argv[i], "-p")) if (0 == strcmp(argv[i], "-e") || 0 == strcmp(argv[i], "-u") || 0 == strcmp(argv[i], "-p")) {
{ for (j = strlen(argv[i + 1]) - 1; j >= 0; j--) {
for (j = strlen(argv[i+1]) - 1; j >= 0; j--) { argv[i + 1][j] = 'x';
argv[i+1][j] = 'x';
} }
} }
} }
printf("SSH Server: %s:%d, Local listen: %s:%d, User&Passwd: [%s]['%s']\n", server_ssh_ip, remote_destport, local_listenip, local_listenport, server_ssh_user, server_ssh_passwd); printf("SSH Server: %s:%d, Local listen: %s:%d, User&Passwd: [%s]['%s']\n", server_ssh_ip, remote_destport, local_listenip, local_listenport, server_ssh_user, server_ssh_passwd);
listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
@ -361,12 +353,12 @@ int main(int argc, char *argv[], char **env)
perror("inet_addr"); perror("inet_addr");
goto shutdown; goto shutdown;
} }
if (setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)) < 0) { if (setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)) < 0) {
perror("setsockopt"); perror("setsockopt");
return -1; return -1;
} }
memset(&server_addr, 0, sizeof(server_addr)); memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(local_listenport); server_addr.sin_port = htons(local_listenport);
@ -375,12 +367,12 @@ int main(int argc, char *argv[], char **env)
perror("bind"); perror("bind");
goto shutdown; goto shutdown;
} }
if (listen(listensock, 500) < 0) { if (listen(listensock, 500) < 0) {
perror("listen"); perror("listen");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "Waiting for TCP connection on %s:%d...\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); fprintf(stderr, "Waiting for TCP connection on %s:%d...\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
// 后台运行 // 后台运行
@ -390,11 +382,9 @@ int main(int argc, char *argv[], char **env)
return -1; return -1;
} }
} }
// 进程优先级 // 进程优先级
if (-1 == (nice_(-20))) if (-1 == (nice_(-20)))
perror("nice_"); perror("nice_");
// 多线程设置 // 多线程设置
pthread_t thread_id = 0; pthread_t thread_id = 0;
@ -406,7 +396,6 @@ int main(int argc, char *argv[], char **env)
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) { if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
printf("block sigpipe error\n"); printf("block sigpipe error\n");
} }
// 初始化线程属性 // 初始化线程属性
pthread_attr_init(&attr); pthread_attr_init(&attr);
@ -438,7 +427,6 @@ int main(int argc, char *argv[], char **env)
pthread_join(thread_id, NULL); pthread_join(thread_id, NULL);
pthread_exit(NULL); pthread_exit(NULL);
shutdown: shutdown:
close(forwardsock); close(forwardsock);
close(listensock); close(listensock);

View File

@ -19,7 +19,6 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/poll.h> #include <sys/poll.h>
#define MAX_EVENTS 1024 #define MAX_EVENTS 1024
@ -30,4 +29,4 @@
#define INADDR_NONE (in_addr_t)-1 #define INADDR_NONE (in_addr_t)-1
#endif #endif
#endif #endif

Binary file not shown.