优化
This commit is contained in:
parent
fd34266843
commit
1ea21b7bc6
BIN
forward-tunnel
BIN
forward-tunnel
Binary file not shown.
@ -1,6 +1,5 @@
|
||||
#include "forward-tunnel.h"
|
||||
|
||||
|
||||
const char *keyfile1 = "/home/aixiao/.ssh/id_rsa.pub";
|
||||
const char *keyfile2 = "/home/aixiao/.ssh/id_rsa";
|
||||
|
||||
@ -52,7 +51,7 @@ void *forward_tunnel(void *sock_)
|
||||
perror("socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
memset(&sin, 0, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = inet_addr(server_ssh_ip);
|
||||
@ -136,16 +135,16 @@ void *forward_tunnel(void *sock_)
|
||||
|
||||
while (1) {
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(forwardsock, &fds); // 设置一个描述符
|
||||
FD_SET(forwardsock, &fds); // 设置一个描述符
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100000;
|
||||
rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
|
||||
if (-1 == rc) { // 返回-1表示出错
|
||||
if (-1 == rc) { // 返回-1表示出错
|
||||
perror("select");
|
||||
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));
|
||||
if (len < 0) {
|
||||
perror("read");
|
||||
@ -154,7 +153,7 @@ void *forward_tunnel(void *sock_)
|
||||
fprintf(stderr, "The client at %s:%d disconnected!\n", shost, sport);
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
wr = 0;
|
||||
while (wr < len) {
|
||||
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);
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
wr += i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
while (1) {
|
||||
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);
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
wr = 0;
|
||||
while (wr < len) {
|
||||
i = write(forwardsock, buffer + wr, len - wr);
|
||||
@ -188,7 +187,7 @@ void *forward_tunnel(void *sock_)
|
||||
perror("write");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
wr += i;
|
||||
}
|
||||
|
||||
@ -199,8 +198,6 @@ void *forward_tunnel(void *sock_)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
shutdown:
|
||||
close(forwardsock);
|
||||
if (channel)
|
||||
@ -211,7 +208,6 @@ shutdown:
|
||||
close(sock);
|
||||
libssh2_exit();
|
||||
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
@ -261,7 +257,7 @@ static char help_info(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nice_( int increment)
|
||||
int nice_(int increment)
|
||||
{
|
||||
int oldprio = getpriority(PRIO_PROCESS, getpid());
|
||||
printf("%d\n", oldprio);
|
||||
@ -280,8 +276,7 @@ int main(int argc, char *argv[], char **env)
|
||||
char *p = NULL;
|
||||
char *client_ip = NULL;
|
||||
int i, j;
|
||||
|
||||
|
||||
|
||||
char optstring[] = ":dr:s:p:l:u:e:h?";
|
||||
while (-1 != (opt = getopt(argc, argv, optstring))) {
|
||||
switch (opt) {
|
||||
@ -295,21 +290,20 @@ int main(int argc, char *argv[], char **env)
|
||||
p = strchr(optarg, ':');
|
||||
if (p != NULL) {
|
||||
remote_destport = atoi(p + 1);
|
||||
|
||||
|
||||
*p = '\0';
|
||||
remote_desthost = optarg;
|
||||
|
||||
|
||||
} else {
|
||||
if (NULL == (p = strchr(optarg, '.')))
|
||||
{
|
||||
remote_destport =atoi(optarg);
|
||||
if (NULL == (p = strchr(optarg, '.'))) {
|
||||
remote_destport = atoi(optarg);
|
||||
remote_desthost = "0.0.0.0";
|
||||
} else {
|
||||
help_info();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
case 'p':
|
||||
server_ssh_port = atoi(optarg);
|
||||
@ -335,17 +329,15 @@ int main(int argc, char *argv[], char **env)
|
||||
|
||||
// 加密参数(账号密码)
|
||||
for (i = 1; i < argc; i++) {
|
||||
|
||||
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--) {
|
||||
argv[i+1][j] = 'x';
|
||||
|
||||
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--) {
|
||||
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);
|
||||
|
||||
listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
@ -361,12 +353,12 @@ int main(int argc, char *argv[], char **env)
|
||||
perror("inet_addr");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
if (setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)) < 0) {
|
||||
perror("setsockopt");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(local_listenport);
|
||||
@ -375,12 +367,12 @@ int main(int argc, char *argv[], char **env)
|
||||
perror("bind");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
if (listen(listensock, 500) < 0) {
|
||||
perror("listen");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 进程优先级
|
||||
if (-1 == (nice_(-20)))
|
||||
perror("nice_");
|
||||
|
||||
|
||||
// 多线程设置
|
||||
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) {
|
||||
printf("block sigpipe error\n");
|
||||
}
|
||||
|
||||
// 初始化线程属性
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
@ -438,7 +427,6 @@ int main(int argc, char *argv[], char **env)
|
||||
pthread_join(thread_id, NULL);
|
||||
pthread_exit(NULL);
|
||||
|
||||
|
||||
shutdown:
|
||||
close(forwardsock);
|
||||
close(listensock);
|
||||
|
@ -19,7 +19,6 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
#include <sys/poll.h>
|
||||
|
||||
#define MAX_EVENTS 1024
|
||||
@ -30,4 +29,4 @@
|
||||
#define INADDR_NONE (in_addr_t)-1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
BIN
forward-tunnel.o
BIN
forward-tunnel.o
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user