Merge pull request #3 from dovecoteescapee/android

Add LOG for Android
This commit is contained in:
hufrea 2024-02-24 23:35:43 +03:00 committed by GitHub
commit 97fb56955d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 50 additions and 31 deletions

View File

@ -19,7 +19,7 @@ struct poolhd *init_pool(int count)
#ifndef NOEPOLL #ifndef NOEPOLL
int efd = epoll_create(count); int efd = epoll_create(count);
if (efd < 0) { if (efd < 0) {
perror("epoll_create"); uniperror("epoll_create");
free(pool); free(pool);
return 0; return 0;
} }
@ -149,7 +149,7 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *type)
if (i < 0) { if (i < 0) {
i = (epoll_wait(pool->efd, pool->pevents, pool->max, -1) - 1); i = (epoll_wait(pool->efd, pool->pevents, pool->max, -1) - 1);
if (i < 0) { if (i < 0) {
perror("epoll_wait"); uniperror("epoll_wait");
return 0; return 0;
} }
} }
@ -171,7 +171,7 @@ int mod_etype(struct poolhd *pool, struct eval *val, int type, char add)
val->events = ev.events; val->events = ev.events;
int s = epoll_ctl(pool->efd, EPOLL_CTL_MOD, val->fd, &ev); int s = epoll_ctl(pool->efd, EPOLL_CTL_MOD, val->fd, &ev);
if (s) if (s)
perror("epoll_ctl"); uniperror("epoll_ctl");
return s; return s;
} }

View File

@ -75,7 +75,7 @@ int fake_attack(int sfd, char *buffer,
int ffd = memfd_create("name", O_RDWR); int ffd = memfd_create("name", O_RDWR);
if (ffd < 0) { if (ffd < 0) {
perror("memfd_create"); uniperror("memfd_create");
return -1; return -1;
} }
char *p = 0; char *p = 0;
@ -83,12 +83,12 @@ int fake_attack(int sfd, char *buffer,
while (status) { while (status) {
if (ftruncate(ffd, pos) < 0) { if (ftruncate(ffd, pos) < 0) {
perror("ftruncate"); uniperror("ftruncate");
break; break;
} }
p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0); p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0);
if (p == MAP_FAILED) { if (p == MAP_FAILED) {
perror("mmap"); uniperror("mmap");
p = 0; p = 0;
break; break;
} }
@ -163,6 +163,7 @@ int desync(int sfd, char *buffer, size_t bfsize,
} }
if (type == IS_HTTP && params.mod_http) { if (type == IS_HTTP && params.mod_http) {
LOG(LOG_S, "modify HTTP: n=%ld\n", n);
if (mod_http(buffer, n, params.mod_http)) { if (mod_http(buffer, n, params.mod_http)) {
fprintf(stderr, "mod http error\n"); fprintf(stderr, "mod http error\n");
return -1; return -1;
@ -176,6 +177,7 @@ int desync(int sfd, char *buffer, size_t bfsize,
else if (o < 0) { else if (o < 0) {
o += n; o += n;
} }
LOG(LOG_S, "tlsrec: pos=%d, n=%ld\n", o, n);
n = part_tls(buffer, bfsize, n, o); n = part_tls(buffer, bfsize, n, o);
} }
@ -188,7 +190,7 @@ int desync(int sfd, char *buffer, size_t bfsize,
else if (pos < 0) { else if (pos < 0) {
pos += n; pos += n;
} }
LOG(LOG_L, "split pos: %d, n: %ld\n", pos, n); LOG(LOG_L, "split-pos=%d, n=%ld\n", pos, n);
if (params.custom_ttl) { if (params.custom_ttl) {
if (setttl(sfd, params.def_ttl, fa) < 0) { if (setttl(sfd, params.def_ttl, fa) < 0) {

30
error.h
View File

@ -2,7 +2,10 @@
#include <errno.h> #include <errno.h>
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#endif
#ifdef ANDROID_APP
#include <android/log.h>
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
@ -14,11 +17,17 @@
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#define uniperror(str) \ #define uniperror(str) \
fprintf(stderr, "%s: %d\n", str, WSAGetLastError()) fprintf(stderr, "%s: %d\n", str, WSAGetLastError())
#else #else
#define uniperror(str) \ #ifdef ANDROID_APP
#define uniperror(str) \
__android_log_print(ANDROID_LOG_ERROR, "proxy", \
"%s: %s\n", str, strerror(errno))
#else
#define uniperror(str) \
perror(str) perror(str)
#endif
#endif #endif
inline const int unie(int e) inline const int unie(int e)
@ -39,3 +48,18 @@ inline const int unie(int e)
#endif #endif
return e; return e;
} }
#ifdef ANDROID_APP
#define LOG_E ANDROID_LOG_ERROR
#define LOG_S ANDROID_LOG_DEBUG
#define LOG_L ANDROID_LOG_VERBOSE
#define LOG(s, str, ...) \
__android_log_print(s, "proxy", str, ##__VA_ARGS__)
#else
#define LOG_E -1
#define LOG_S 1
#define LOG_L 2
#define LOG(s, str, ...) \
if (params.debug >= s) \
fprintf(stderr, str, ##__VA_ARGS__)
#endif

View File

@ -43,10 +43,3 @@ struct packet {
}; };
extern struct packet fake_tls; extern struct packet fake_tls;
extern struct packet fake_http; extern struct packet fake_http;
#define LOG_S 1
#define LOG_L 2
#define LOG(s, str, ...) \
if (params.debug >= s) printf(str, ##__VA_ARGS__)

24
proxy.c
View File

@ -81,7 +81,7 @@ static inline int nb_socket(int domain, int type)
#else #else
#ifndef __linux__ #ifndef __linux__
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
perror("fcntl"); uniperror("fcntl");
close(fd); close(fd);
return -1; return -1;
} }
@ -202,7 +202,7 @@ int s4_get_addr(char *buff, size_t n,
return -1; return -1;
} }
if (resolve(id_end + 1, len, dst)) { if (resolve(id_end + 1, len, dst)) {
fprintf(stderr, "not resolved: %.*s\n", len, id_end + 1); LOG(LOG_E, "not resolved: %.*s\n", len, id_end + 1);
return -1; return -1;
} }
} }
@ -224,11 +224,11 @@ int s5_get_addr(char *buffer, ssize_t n,
(r->atp == S_ATP_ID ? r->id.len + S_SIZE_ID : (r->atp == S_ATP_ID ? r->id.len + S_SIZE_ID :
(r->atp == S_ATP_I6 ? S_SIZE_I6 : 0))); (r->atp == S_ATP_I6 ? S_SIZE_I6 : 0)));
if (n < o) { if (n < o) {
fprintf(stderr, "ss: bad request\n"); LOG(LOG_E, "ss: bad request\n");
return S_ER_GEN; return S_ER_GEN;
} }
if (r->cmd != S_CMD_CONN) { if (r->cmd != S_CMD_CONN) {
fprintf(stderr, "ss: unsupported cmd: 0x%x\n", r->cmd); LOG(LOG_E, "ss: unsupported cmd: 0x%x\n", r->cmd);
return S_ER_CMD; return S_ER_CMD;
} }
switch (r->atp) { switch (r->atp) {
@ -243,7 +243,7 @@ int s5_get_addr(char *buffer, ssize_t n,
} }
if (r->id.len < 3 || if (r->id.len < 3 ||
resolve(r->id.domain, r->id.len, addr)) { resolve(r->id.domain, r->id.len, addr)) {
fprintf(stderr, "not resolved: %.*s\n", r->id.len, r->id.domain); LOG(LOG_E, "not resolved: %.*s\n", r->id.len, r->id.domain);
return S_ER_HOST; return S_ER_HOST;
} }
break; break;
@ -272,7 +272,7 @@ int create_conn(struct poolhd *pool,
map_fix(&addr, 0); map_fix(&addr, 0);
} }
if (addr.sa.sa_family != params.baddr.sin6_family) { if (addr.sa.sa_family != params.baddr.sin6_family) {
fprintf(stderr, "different addresses family\n"); LOG(LOG_E, "different addresses family\n");
return -1; return -1;
} }
int sfd = nb_socket(addr.sa.sa_family, SOCK_STREAM); int sfd = nb_socket(addr.sa.sa_family, SOCK_STREAM);
@ -350,7 +350,7 @@ static inline int on_request(struct poolhd *pool, struct eval *val,
return 0; return 0;
} }
if (n < S_SIZE_MIN) { if (n < S_SIZE_MIN) {
fprintf(stderr, "ss: request to small\n"); LOG(LOG_E, "ss: request to small\n");
return -1; return -1;
} }
int s5e = s5_get_addr(buffer, n, &dst); int s5e = s5_get_addr(buffer, n, &dst);
@ -377,7 +377,7 @@ static inline int on_request(struct poolhd *pool, struct eval *val,
} }
} }
else { else {
fprintf(stderr, "ss: invalid version: 0x%x (%lu)\n", *buffer, n); LOG(LOG_E, "ss: invalid version: 0x%x (%lu)\n", *buffer, n);
return -1; return -1;
} }
val->type = EV_IGNORE; val->type = EV_IGNORE;
@ -411,7 +411,7 @@ static inline int on_accept(struct poolhd *pool, struct eval *val)
uniperror("ioctlsocket"); uniperror("ioctlsocket");
#else #else
if (fcntl(c, F_SETFL, O_NONBLOCK) < 0) { if (fcntl(c, F_SETFL, O_NONBLOCK) < 0) {
perror("fcntl"); uniperror("fcntl");
#endif #endif
close(c); close(c);
continue; continue;
@ -603,11 +603,11 @@ int big_loop(int srvfd)
continue; continue;
default: default:
fprintf(stderr, "???\n"); LOG(LOG_E, "???\n");
NOT_EXIT = 0; NOT_EXIT = 0;
} }
} }
fprintf(stderr, "exit\n"); LOG(LOG_E, "exit\n");
free(buffer); free(buffer);
destroy_pool(pool); destroy_pool(pool);
return 0; return 0;
@ -618,7 +618,7 @@ int listener(struct sockaddr_ina srv)
{ {
#ifdef SIGPIPE #ifdef SIGPIPE
if (signal(SIGPIPE, SIG_IGN)) if (signal(SIGPIPE, SIG_IGN))
perror("signal SIGPIPE!"); uniperror("signal SIGPIPE!");
#endif #endif
signal(SIGINT, on_cancel); signal(SIGINT, on_cancel);