优化编译判断
This commit is contained in:
parent
32b7b5bda0
commit
fd34266843
19
Makefile
19
Makefile
@ -2,20 +2,27 @@ CROSS_COMPILE ?=
|
|||||||
CC := $(CROSS_COMPILE)gcc
|
CC := $(CROSS_COMPILE)gcc
|
||||||
STRIP := $(CROSS_COMPILE)strip
|
STRIP := $(CROSS_COMPILE)strip
|
||||||
CFLAGS = -Wall -g -O3
|
CFLAGS = -Wall -g -O3
|
||||||
LIB = -lssh2 -pthread -static
|
LIB = -lssh2 -pthread
|
||||||
OBJ = tunnel
|
forward-tunnel = forward-tunnel
|
||||||
|
reverse-tunnel = reverse-tunnel
|
||||||
SSH2_LIB := $(shell pkg-config --static --libs --cflags libssh2)
|
|
||||||
|
|
||||||
|
ifeq ($(shell uname -o), GNU/Linux)
|
||||||
|
ifeq ($(shell lsb_release -si), Debian)
|
||||||
|
LIB += -static
|
||||||
|
SSH2_LIB := $(shell pkg-config --static --libs --cflags libssh2)
|
||||||
|
else
|
||||||
|
SSH2_LIB := $(shell pkg-config --libs --cflags libssh2)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
all:forward-tunnel reverse-tunnel
|
all:forward-tunnel reverse-tunnel
|
||||||
|
|
||||||
forward-tunnel: forward-tunnel.o
|
forward-tunnel: forward-tunnel.o
|
||||||
$(CC) $(CFLAGS) -o forward-tunnel $^ $(SSH2_LIB) $(LIB)
|
$(CC) $(CFLAGS) -o $(forward-tunnel) $^ $(SSH2_LIB) $(LIB)
|
||||||
$(STRIP) forward-tunnel
|
$(STRIP) forward-tunnel
|
||||||
|
|
||||||
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:
|
||||||
|
BIN
forward-tunnel
BIN
forward-tunnel
Binary file not shown.
@ -21,11 +21,9 @@ enum {
|
|||||||
AUTH_PUBLICKEY
|
AUTH_PUBLICKEY
|
||||||
};
|
};
|
||||||
|
|
||||||
void *forward_tunnel(void *p)
|
void *forward_tunnel(void *sock_)
|
||||||
{
|
{
|
||||||
|
int forwardsock = *(int *)sock_;
|
||||||
int forwardsock = *(int *)p;
|
|
||||||
|
|
||||||
int rc, i, auth = AUTH_NONE;
|
int rc, i, auth = AUTH_NONE;
|
||||||
struct sockaddr_in sin;
|
struct sockaddr_in sin;
|
||||||
const char *fingerprint;
|
const char *fingerprint;
|
||||||
@ -220,6 +218,7 @@ shutdown:
|
|||||||
|
|
||||||
static char help_info(void)
|
static char help_info(void)
|
||||||
{
|
{
|
||||||
|
int l;
|
||||||
static const char name[] = "STunnel";
|
static const char name[] = "STunnel";
|
||||||
static const char subject[] = "SSH forward tunnel";
|
static const char subject[] = "SSH forward tunnel";
|
||||||
static const struct {
|
static const struct {
|
||||||
@ -252,7 +251,6 @@ static char help_info(void)
|
|||||||
fprintf(stderr, "Version: %s\n", author.version);
|
fprintf(stderr, "Version: %s\n", author.version);
|
||||||
fprintf(stderr, "%s\n", usage);
|
fprintf(stderr, "%s\n", usage);
|
||||||
|
|
||||||
int l;
|
|
||||||
for (l = 0; s_help[l]; l++) {
|
for (l = 0; s_help[l]; l++) {
|
||||||
fprintf(stderr, "%s\n", s_help[l]);
|
fprintf(stderr, "%s\n", s_help[l]);
|
||||||
}
|
}
|
||||||
@ -263,7 +261,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,6 +278,8 @@ int main(int argc, char *argv[], char **env)
|
|||||||
int daemon_ = 0;
|
int daemon_ = 0;
|
||||||
int opt;
|
int opt;
|
||||||
char *p = NULL;
|
char *p = 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?";
|
||||||
@ -333,7 +333,7 @@ int main(int argc, char *argv[], char **env)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int i, j;
|
// 加密参数(账号密码)
|
||||||
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"))
|
||||||
@ -346,7 +346,6 @@ int main(int argc, char *argv[], char **env)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
@ -384,9 +383,7 @@ int main(int argc, char *argv[], char **env)
|
|||||||
|
|
||||||
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");
|
||||||
@ -394,11 +391,12 @@ int main(int argc, char *argv[], char **env)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 进程优先级
|
||||||
if (-1 == (nice(-20))) // 进程优先级
|
if (-1 == (nice_(-20)))
|
||||||
perror("nice");
|
perror("nice_");
|
||||||
|
|
||||||
|
|
||||||
|
// 多线程设置
|
||||||
pthread_t thread_id = 0;
|
pthread_t thread_id = 0;
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
struct sched_param param;
|
struct sched_param param;
|
||||||
@ -409,8 +407,6 @@ int main(int argc, char *argv[], char **env)
|
|||||||
printf("block sigpipe error\n");
|
printf("block sigpipe error\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 初始化线程属性
|
// 初始化线程属性
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
|
|
||||||
@ -421,7 +417,7 @@ int main(int argc, char *argv[], char **env)
|
|||||||
param.sched_priority = 50;
|
param.sched_priority = 50;
|
||||||
pthread_attr_setschedparam(&attr, ¶m);
|
pthread_attr_setschedparam(&attr, ¶m);
|
||||||
|
|
||||||
|
// 多线程
|
||||||
while (1) {
|
while (1) {
|
||||||
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);
|
||||||
@ -430,8 +426,9 @@ int main(int argc, char *argv[], char **env)
|
|||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
getsockname(forwardsock, (struct sockaddr *)&sin, &server_addr_len);
|
getsockname(forwardsock, (struct sockaddr *)&sin, &server_addr_len);
|
||||||
char *ip = inet_ntoa(sin.sin_addr);
|
client_ip = inet_ntoa(sin.sin_addr);
|
||||||
printf("Client IP address: %s\n", ip);
|
printf("Client IP address: %s\n", client_ip);
|
||||||
|
client_ip = NULL;
|
||||||
|
|
||||||
pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock);
|
pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock);
|
||||||
|
|
||||||
|
BIN
forward-tunnel.o
BIN
forward-tunnel.o
Binary file not shown.
BIN
reverse-tunnel
BIN
reverse-tunnel
Binary file not shown.
Loading…
Reference in New Issue
Block a user