添加查看线程功能

This commit is contained in:
aixiao 2024-05-13 15:20:25 +08:00
parent ced52a63ce
commit c2c96465f1
5 changed files with 81 additions and 24 deletions

22
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -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
}

View File

@ -23,7 +23,7 @@ forward-tunnel: forward-tunnel.o
reverse-tunnel: reverse-tunnel.o reverse-tunnel: reverse-tunnel.o
$(CC) $(CFLAGS) -o $(reverse-tunnel) $^ $(SSH2_LIB) $(LIB) $(CC) $(CFLAGS) -o $(reverse-tunnel) $^ $(SSH2_LIB) $(LIB)
$(STRIP) reverse-tunnel : $(STRIP) reverse-tunnel
.c.o: .c.o:
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<

View File

@ -9,7 +9,7 @@ char *server_ssh_user = "aixiao";
char *server_ssh_passwd = "123456"; char *server_ssh_passwd = "123456";
const char *local_listenip = "0.0.0.0"; 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 */ char *remote_desthost = "0.0.0.0"; /* resolved by the server */
unsigned int remote_destport = 3306; unsigned int remote_destport = 3306;
@ -209,7 +209,6 @@ shutdown:
libssh2_exit(); libssh2_exit();
return NULL; return NULL;
} }
static char help_info(void) static char help_info(void)
@ -264,6 +263,51 @@ int nice_(int increment)
return setpriority(PRIO_PROCESS, getpid(), oldprio + 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) int main(int argc, char *argv[], char **env)
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
@ -271,17 +315,16 @@ int main(int argc, char *argv[], char **env)
socklen_t server_addr_len; socklen_t server_addr_len;
int sockopt = 1; int sockopt = 1;
int listensock = -1, forwardsock = -1; int listensock = -1, forwardsock = -1;
int daemon_ = 0; int _daemon = 0;
int opt; int opt;
char *p = NULL; char *p = NULL;
char *client_ip = NULL; char *client_ip = NULL;
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) {
case 'd': case 'd':
daemon_ = 1; _daemon = 1;
break; break;
case 'r': case 'r':
server_ssh_ip = strdup(optarg); server_ssh_ip = strdup(optarg);
@ -327,17 +370,7 @@ int main(int argc, char *argv[], char **env)
} }
} }
// 加密参数(账号密码) modif_argv(argc, argv);
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';
}
}
}
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);
@ -373,15 +406,16 @@ int main(int argc, char *argv[], char **env)
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));
// 后台运行 // 后台运行
if (daemon_ == 1) { if (_daemon == 1) {
if (daemon(1, 1)) { if (daemon(1, 1)) {
perror("daemon"); perror("daemon");
return -1; return -1;
} }
} }
// 进程优先级 // 进程优先级
if (-1 == (nice_(-20))) if (-1 == (nice_(-20)))
perror("nice_"); perror("nice_");
@ -408,6 +442,8 @@ int main(int argc, char *argv[], char **env)
// 多线程 // 多线程
while (1) { while (1) {
printf("threads: %d\n", get_threads());
server_addr_len = sizeof(server_addr); server_addr_len = sizeof(server_addr);
forwardsock = accept(listensock, (struct sockaddr *)&sin, &server_addr_len); forwardsock = accept(listensock, (struct sockaddr *)&sin, &server_addr_len);
if (forwardsock == -1) { if (forwardsock == -1) {
@ -420,7 +456,6 @@ int main(int argc, char *argv[], char **env)
client_ip = NULL; client_ip = NULL;
pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock); pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock);
} }
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);

View File

@ -1,5 +1,5 @@
#ifndef FORWARD_TUNNEL #ifndef FORWARD_TUNNEL_H
#define FORWARD_TUNNEL #define FORWARD_TUNNEL_H
#include <libssh2.h> #include <libssh2.h>
#include <sys/socket.h> #include <sys/socket.h>

View File

@ -46,7 +46,7 @@ enum {
AUTH_PUBLICKEY AUTH_PUBLICKEY
}; };
int forward_tunnel(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel) int forward_tunnel(LIBSSH2_SESSION * session, LIBSSH2_CHANNEL * channel)
{ {
int i, rc = 0; int i, rc = 0;
struct sockaddr_in sin; struct sockaddr_in sin;