mirror of
https://github.com/hufrea/byedpi.git
synced 2025-07-05 21:44:02 +00:00
More portable send_fake
This commit is contained in:
parent
53cca29834
commit
90365fa67d
1
Makefile
1
Makefile
@ -2,6 +2,7 @@ TARGET = ciadpi
|
|||||||
|
|
||||||
CPPFLAGS = -D_XOPEN_SOURCE=600
|
CPPFLAGS = -D_XOPEN_SOURCE=600
|
||||||
CFLAGS += -I. -std=c99 -Wall -Wno-unused -O2
|
CFLAGS += -I. -std=c99 -Wall -Wno-unused -O2
|
||||||
|
LDFLAGS += -lrt
|
||||||
WIN_LDFLAGS = -lws2_32 -lmswsock
|
WIN_LDFLAGS = -lws2_32 -lmswsock
|
||||||
|
|
||||||
SRC = packets.c main.c conev.c proxy.c desync.c mpool.c extend.c
|
SRC = packets.c main.c conev.c proxy.c desync.c mpool.c extend.c
|
||||||
|
47
desync.c
47
desync.c
@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "desync.h"
|
#include "desync.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -9,13 +10,13 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <sys/sendfile.h>
|
#include <sys/sendfile.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <linux/tcp.h>
|
#include <linux/tcp.h>
|
||||||
|
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
@ -23,7 +24,6 @@
|
|||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <windows.h>
|
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <mswsock.h>
|
#include <mswsock.h>
|
||||||
#endif
|
#endif
|
||||||
@ -114,12 +114,13 @@ void wait_send(int sfd)
|
|||||||
#define wait_send_if_support(sfd) // :(
|
#define wait_send_if_support(sfd) // :(
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifndef _WIN32
|
||||||
ssize_t send_fake(int sfd, char *buffer,
|
ssize_t send_fake(int sfd, char *buffer,
|
||||||
int cnt, long pos, int fa, struct desync_params *opt)
|
int cnt, long pos, int fa, struct desync_params *opt)
|
||||||
{
|
{
|
||||||
struct sockaddr_in6 addr = {};
|
struct sockaddr_in6 addr = {};
|
||||||
socklen_t addr_size = sizeof(addr);
|
socklen_t addr_size = sizeof(addr);
|
||||||
|
#ifdef __linux__
|
||||||
if (opt->md5sig) {
|
if (opt->md5sig) {
|
||||||
if (getpeername(sfd,
|
if (getpeername(sfd,
|
||||||
(struct sockaddr *)&addr, &addr_size) < 0) {
|
(struct sockaddr *)&addr, &addr_size) < 0) {
|
||||||
@ -127,6 +128,7 @@ ssize_t send_fake(int sfd, char *buffer,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
struct packet pkt;
|
struct packet pkt;
|
||||||
if (opt->fake_data.data) {
|
if (opt->fake_data.data) {
|
||||||
pkt = opt->fake_data;
|
pkt = opt->fake_data;
|
||||||
@ -135,12 +137,19 @@ ssize_t send_fake(int sfd, char *buffer,
|
|||||||
pkt = cnt != IS_HTTP ? fake_tls : fake_http;
|
pkt = cnt != IS_HTTP ? fake_tls : fake_http;
|
||||||
}
|
}
|
||||||
size_t psz = pkt.size;
|
size_t psz = pkt.size;
|
||||||
|
#ifdef __linux__
|
||||||
int ffd = memfd_create("name", 0);
|
int ffd = memfd_create("name", 0);
|
||||||
if (ffd < 0) {
|
if (ffd < 0) {
|
||||||
uniperror("memfd_create");
|
uniperror("memfd_create");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
int ffd = shm_open("name", O_RDWR | O_CREAT, 0);
|
||||||
|
if (ffd < 0) {
|
||||||
|
uniperror("shm_open");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
char *p = 0;
|
char *p = 0;
|
||||||
ssize_t len = -1;
|
ssize_t len = -1;
|
||||||
|
|
||||||
@ -160,6 +169,8 @@ ssize_t send_fake(int sfd, char *buffer,
|
|||||||
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
|
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
if (opt->md5sig) {
|
if (opt->md5sig) {
|
||||||
struct tcp_md5sig md5 = {
|
struct tcp_md5sig md5 = {
|
||||||
.tcpm_keylen = 5
|
.tcpm_keylen = 5
|
||||||
@ -172,19 +183,37 @@ ssize_t send_fake(int sfd, char *buffer,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (opt->ip_options && fa == AF_INET
|
if (opt->ip_options && fa == AF_INET
|
||||||
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
|
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
|
||||||
opt->ip_options, opt->ip_options_len) < 0) {
|
opt->ip_options, opt->ip_options_len) < 0) {
|
||||||
uniperror("setsockopt IP_OPTIONS");
|
uniperror("setsockopt IP_OPTIONS");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#ifdef __linux__
|
||||||
len = sendfile(sfd, ffd, 0, pos);
|
len = sendfile(sfd, ffd, 0, pos);
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
uniperror("sendfile");
|
uniperror("sendfile");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
wait_send(sfd);
|
wait_send(sfd);
|
||||||
|
#else
|
||||||
|
// Emulate sendfile with reading ffd to buffer on other unix systems
|
||||||
|
char* buffer2 = calloc(sizeof(char), pos+1);
|
||||||
|
int n = read(ffd, buffer2, pos);
|
||||||
|
if (n < 0) {
|
||||||
|
uniperror("read");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
len = send(sfd, buffer2, pos, 0);
|
||||||
|
if (len < 0) {
|
||||||
|
uniperror("send");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
wait_send(sfd);
|
||||||
|
// should be already copied to socket so safe to free
|
||||||
|
free(buffer2);
|
||||||
|
#endif
|
||||||
memcpy(p, buffer, pos);
|
memcpy(p, buffer, pos);
|
||||||
|
|
||||||
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
||||||
@ -196,6 +225,7 @@ ssize_t send_fake(int sfd, char *buffer,
|
|||||||
uniperror("setsockopt IP_OPTIONS");
|
uniperror("setsockopt IP_OPTIONS");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#ifdef __linux__
|
||||||
if (opt->md5sig) {
|
if (opt->md5sig) {
|
||||||
struct tcp_md5sig md5 = {
|
struct tcp_md5sig md5 = {
|
||||||
.tcpm_keylen = 0
|
.tcpm_keylen = 0
|
||||||
@ -208,15 +238,14 @@ ssize_t send_fake(int sfd, char *buffer,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (p) munmap(p, pos);
|
if (p) munmap(p, pos);
|
||||||
close(ffd);
|
close(ffd);
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
#endif
|
#else
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
OVERLAPPED ov = {};
|
OVERLAPPED ov = {};
|
||||||
|
|
||||||
ssize_t send_fake(int sfd, char *buffer,
|
ssize_t send_fake(int sfd, char *buffer,
|
||||||
|
2
params.h
2
params.h
@ -15,7 +15,7 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__linux__) || defined(_WIN32)
|
#if defined(__linux__) || defined(__NetBSD__) || defined(_WIN32)
|
||||||
#define FAKE_SUPPORT 1
|
#define FAKE_SUPPORT 1
|
||||||
#define TIMEOUT_SUPPORT 1
|
#define TIMEOUT_SUPPORT 1
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user