diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..949932a --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,22 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "windowsSdkVersion": "10.0.22621.0", + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-gcc-x64", + "configurationProvider": "ms-vscode.makefile-tools" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/Makefile b/Makefile index 588fafe..0e4a757 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ forward-tunnel: forward-tunnel.o reverse-tunnel: reverse-tunnel.o $(CC) $(CFLAGS) -o $(reverse-tunnel) $^ $(SSH2_LIB) $(LIB) - $(STRIP) reverse-tunnel + : $(STRIP) reverse-tunnel .c.o: $(CC) $(CFLAGS) -c $< diff --git a/forward-tunnel.c b/forward-tunnel.c index f29e171..c7ed723 100644 --- a/forward-tunnel.c +++ b/forward-tunnel.c @@ -9,7 +9,7 @@ char *server_ssh_user = "aixiao"; char *server_ssh_passwd = "123456"; const char *local_listenip = "0.0.0.0"; -unsigned int local_listenport = 3009; +unsigned int local_listenport = 3306; char *remote_desthost = "0.0.0.0"; /* resolved by the server */ unsigned int remote_destport = 3306; @@ -209,7 +209,6 @@ shutdown: libssh2_exit(); return NULL; - } static char help_info(void) @@ -264,6 +263,51 @@ int nice_(int increment) return setpriority(PRIO_PROCESS, getpid(), oldprio + increment); } +int modif_argv(int argc, char *argv[]) +{ + int i, j; + + // 加密参数(账号密码) + 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'; + } + } + + } + + return 0; +} + +int get_threads() +{ + + + pid_t pid; + char line[256]; + char path[256]; + int threads = 0; + + pid = getpid(); + snprintf(path, sizeof(path), "/proc/%d/status", pid); + FILE *file = fopen(path, "r"); + if (file == NULL) { + perror("Failed to open file"); + return EXIT_FAILURE; + } + + while (fgets(line, sizeof(line), file)) { + if (strncmp(line, "Threads:", 8) == 0) { + sscanf(line, "Threads:\t%d", &threads); + break; + } + } + + return threads; +} + int main(int argc, char *argv[], char **env) { struct sockaddr_in sin; @@ -271,17 +315,16 @@ int main(int argc, char *argv[], char **env) socklen_t server_addr_len; int sockopt = 1; int listensock = -1, forwardsock = -1; - int daemon_ = 0; + int _daemon = 0; int opt; 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) { case 'd': - daemon_ = 1; + _daemon = 1; break; case 'r': server_ssh_ip = strdup(optarg); @@ -327,17 +370,7 @@ 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'; - } - } - - } - + modif_argv(argc, argv); 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); @@ -373,15 +406,16 @@ int main(int argc, char *argv[], char **env) 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)); // 后台运行 - if (daemon_ == 1) { + if (_daemon == 1) { if (daemon(1, 1)) { perror("daemon"); return -1; } } + // 进程优先级 if (-1 == (nice_(-20))) perror("nice_"); @@ -408,6 +442,8 @@ int main(int argc, char *argv[], char **env) // 多线程 while (1) { + printf("threads: %d\n", get_threads()); + server_addr_len = sizeof(server_addr); forwardsock = accept(listensock, (struct sockaddr *)&sin, &server_addr_len); if (forwardsock == -1) { @@ -420,7 +456,6 @@ int main(int argc, char *argv[], char **env) client_ip = NULL; pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock); - } pthread_attr_destroy(&attr); @@ -437,6 +472,6 @@ shutdown: free(server_ssh_user); if (server_ssh_passwd) free(server_ssh_passwd); - + return 0; } diff --git a/forward-tunnel.h b/forward-tunnel.h index 5c87cc0..6325841 100644 --- a/forward-tunnel.h +++ b/forward-tunnel.h @@ -1,5 +1,5 @@ -#ifndef FORWARD_TUNNEL -#define FORWARD_TUNNEL +#ifndef FORWARD_TUNNEL_H +#define FORWARD_TUNNEL_H #include #include diff --git a/reverse-tunnel.c b/reverse-tunnel.c index 0f7cb16..39d1903 100644 --- a/reverse-tunnel.c +++ b/reverse-tunnel.c @@ -46,7 +46,7 @@ enum { AUTH_PUBLICKEY }; -int forward_tunnel(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel) +int forward_tunnel(LIBSSH2_SESSION * session, LIBSSH2_CHANNEL * channel) { int i, rc = 0; struct sockaddr_in sin;