mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 23:59:41 +08:00
Major code refactoring
- sockmapping rewritten from stratch to minimilse polling. poll() is now only called if blocking is actually expected, splice pipes are now polled if splice fails, buffers flushing is much more accurate. - logging code moved to separate files - signal masks added to client threads to prevent unneeded interruptions - bandwidth limitation will not delay the thread after client or server shutdown
This commit is contained in:
1 parent
3dd1969657
commit
eb829b062b
17 files changed
+724
-586
No files matched your search
+1
-1
@@ -369,7 +369,7 @@ int connectwithpoll(SOCKET sock, struct sockaddr *sa, SASIZETYPE size, int to){
|
||||
unsigned long ul = 1;
|
||||
ioctlsocket(sock, FIONBIO, &ul);
|
||||
#else
|
||||
fcntl(sock,F_SETFL,O_NONBLOCK);
|
||||
fcntl(sock,F_SETFL, O_NONBLOCK | fcntl(sock,F_GETFL));
|
||||
#endif
|
||||
if(so._connect(sock,sa,size)) {
|
||||
if(errno != EAGAIN && errno != EINPROGRESS) return (13);
|
||||
|
||||
+1
-1
@@ -198,7 +198,7 @@ CLEANRET:
|
||||
if((ip && type == 0x01) || type == 0x1c){
|
||||
myinet_ntop(type == 0x01? AF_INET:AF_INET6, addr, (char *)buf+strlen((char *)buf), 64);
|
||||
}
|
||||
(*param->srv->logfunc)(param, buf);
|
||||
dolog(param, buf);
|
||||
}
|
||||
if(bbuf)myfree(bbuf);
|
||||
if(host)myfree(host);
|
||||
|
||||
+2
-2
@@ -295,7 +295,7 @@ void * ftpprchild(struct clientparam* param) {
|
||||
sasize = sizeof(param->sincr);
|
||||
if(so._getpeername(param->ctrlsock, (struct sockaddr *)¶m->sincr, &sasize)){RETURN(819);}
|
||||
if(req && (param->statscli64 || param->statssrv64)){
|
||||
(*param->srv->logfunc)(param, (unsigned char *)req);
|
||||
dolog(param, (unsigned char *)req);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,7 +316,7 @@ CLEANRET:
|
||||
sasize = sizeof(param->sincr);
|
||||
so._getpeername(param->ctrlsock, (struct sockaddr *)¶m->sincr, &sasize);
|
||||
if(param->res != 0 || param->statscli64 || param->statssrv64 ){
|
||||
(*param->srv->logfunc)(param, (unsigned char *)((req && (param->res > 802))? req:NULL));
|
||||
dolog(param, (unsigned char *)((req && (param->res > 802))? req:NULL));
|
||||
}
|
||||
if(req) myfree(req);
|
||||
if(buf) myfree(buf);
|
||||
|
||||
@@ -1,18 +1,38 @@
|
||||
/*
|
||||
3APA3A simpliest proxy server
|
||||
(c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
|
||||
(c) 2002-2020 by Vladimir Dubrovin <3proxy@3proxy.ru>
|
||||
|
||||
please read License Agreement
|
||||
|
||||
*/
|
||||
|
||||
|
||||
pthread_mutex_t log_mutex;
|
||||
|
||||
|
||||
#include "proxy.h"
|
||||
pthread_mutex_t log_mutex;
|
||||
int havelog = 0;
|
||||
|
||||
|
||||
struct clientparam logparam;
|
||||
struct srvparam logsrv;
|
||||
|
||||
|
||||
|
||||
void dolog(struct clientparam * param, const unsigned char *s){
|
||||
static int init = 0;
|
||||
|
||||
if(param)param->srv->logfunc(param, s);
|
||||
else {
|
||||
if(!init){
|
||||
srvinit(&logsrv, &logparam);
|
||||
init = 1;
|
||||
}
|
||||
logstdout(&logparam, s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void clearstat(struct clientparam * param) {
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
+2
-2
@@ -49,9 +49,9 @@ CLEANRET:
|
||||
|
||||
if(param->hostname&¶m->extusername) {
|
||||
sprintf((char *)buf, "%.128s@%.128s%c%hu", param->extusername, param->hostname, (*SAPORT(¶m->sinsr)==110)?0:':', ntohs(*SAPORT(¶m->sinsr)));
|
||||
(*param->srv->logfunc)(param, buf);
|
||||
dolog(param, buf);
|
||||
}
|
||||
else (*param->srv->logfunc)(param, NULL);
|
||||
else dolog(param, NULL);
|
||||
if(param->clisock != INVALID_SOCKET) {
|
||||
if ((param->res > 0 && param->res < 100) || (param->res > 611 && param->res <700)) socksend(param->clisock, (unsigned char *)"-ERR\r\n", 6,conf.timeouts[STRING_S]);
|
||||
}
|
||||
|
||||
+1
-1
@@ -152,7 +152,7 @@ static void logurl(struct clientparam * param, char * buf, char * req, int ftp){
|
||||
strcpy(se, sb);
|
||||
}
|
||||
}
|
||||
if(param->res != 555 && param->res != 508)(*param->srv->logfunc)(param, (unsigned char *)(req?buf:NULL));
|
||||
if(param->res != 555 && param->res != 508)dolog(param, (unsigned char *)(req?buf:NULL));
|
||||
}
|
||||
|
||||
void decodeurl(unsigned char *s, int allowcr){
|
||||
|
||||
+4
-4
@@ -151,8 +151,7 @@ extern int timetoexit;
|
||||
|
||||
extern struct extparam conf;
|
||||
|
||||
int sockmap(struct clientparam * param, int timeo);
|
||||
int splicemap(struct clientparam * param, int timeo);
|
||||
int sockmap(struct clientparam * param, int timeo, int usesplice);
|
||||
int socksend(SOCKET sock, unsigned char * buf, int bufsize, int to);
|
||||
int socksendto(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
|
||||
int sockrecvfrom(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
|
||||
@@ -168,6 +167,7 @@ int sockgetlinebuf(struct clientparam * param, DIRECTION which, unsigned char *
|
||||
|
||||
|
||||
|
||||
void dolog(struct clientparam * param, const unsigned char *s);
|
||||
int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
|
||||
int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
|
||||
extern FILE * stdlog;
|
||||
@@ -312,9 +312,9 @@ extern struct datatype datatypes[64];
|
||||
extern struct commands commandhandlers[];
|
||||
|
||||
#ifdef WITHSPLICE
|
||||
#define mapsocket(a,b) (a->srv->usesplice && !a->ndatfilterssrv && !a->ndatfilterscli && !a->npredatfilters?splicemap(a,b):sockmap(a,b))
|
||||
#define mapsocket(a,b) ((a->srv->usesplice && !a->ndatfilterssrv && !a->ndatfilterscli)?sockmap(a,b,1):sockmap(a,b,0))
|
||||
#else
|
||||
#define mapsocket(a,b) sockmap(a,b)
|
||||
#define mapsocket(a,b) sockmap(a,b, 0)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
+36
-25
@@ -23,27 +23,27 @@ void * threadfunc (void *p) {
|
||||
fds.revents = 0;
|
||||
for(i=5+(param->srv->maxchild>>10); i; i--){
|
||||
if(so._poll(&fds, 1, 1000*CONNBACK_TO)!=1){
|
||||
param->srv->logfunc(param, (unsigned char *)"Connect back not received, check connback client");
|
||||
dolog(param, (unsigned char *)"Connect back not received, check connback client");
|
||||
i = 0;
|
||||
break;
|
||||
}
|
||||
param->remsock = so._accept(param->srv->cbsock, (struct sockaddr*)¶m->sinsr, &size);
|
||||
if(param->remsock == INVALID_SOCKET) {
|
||||
param->srv->logfunc(param, (unsigned char *)"Connect back accept() failed");
|
||||
dolog(param, (unsigned char *)"Connect back accept() failed");
|
||||
continue;
|
||||
}
|
||||
#ifndef WITHMAIN
|
||||
param->req = param->sinsr;
|
||||
if(param->srv->acl) param->res = checkACL(param);
|
||||
if(param->res){
|
||||
param->srv->logfunc(param, (unsigned char *)"Connect back ACL failed");
|
||||
dolog(param, (unsigned char *)"Connect back ACL failed");
|
||||
so._closesocket(param->remsock);
|
||||
param->remsock = INVALID_SOCKET;
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if(socksendto(param->remsock, (struct sockaddr*)¶m->sinsr, (unsigned char *)"C", 1, CONNBACK_TO) != 1){
|
||||
param->srv->logfunc(param, (unsigned char *)"Connect back sending command failed");
|
||||
dolog(param, (unsigned char *)"Connect back sending command failed");
|
||||
so._closesocket(param->remsock);
|
||||
param->remsock = INVALID_SOCKET;
|
||||
continue;
|
||||
@@ -57,6 +57,13 @@ void * threadfunc (void *p) {
|
||||
freeparam(param);
|
||||
}
|
||||
else {
|
||||
|
||||
#ifndef _WIN32
|
||||
sigset_t mask;
|
||||
sigfillset(&mask);
|
||||
pthread_sigmask(SIG_SETMASK, &mask, NULL);
|
||||
#endif
|
||||
|
||||
((struct clientparam *) p)->srv->pf((struct clientparam *)p);
|
||||
}
|
||||
#ifdef _WIN32
|
||||
@@ -260,12 +267,8 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
WSADATA wd;
|
||||
WSAStartup(MAKEWORD( 1, 1 ), &wd);
|
||||
|
||||
|
||||
|
||||
#else
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
srvinit(&srv, &defparam);
|
||||
@@ -288,6 +291,14 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
pthread_mutex_init(&log_mutex, NULL);
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
{
|
||||
sigset_t mask;
|
||||
sigfillset(&mask);
|
||||
pthread_sigmask(SIG_SETMASK, &mask, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (i=1; i<argc; i++) {
|
||||
if(*argv[i]=='-') {
|
||||
switch(argv[i][1]) {
|
||||
@@ -513,7 +524,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
|
||||
#ifndef _WIN32
|
||||
if(inetd) {
|
||||
fcntl(0,F_SETFL,O_NONBLOCK);
|
||||
fcntl(0,F_SETFL,O_NONBLOCK | fcntl(0,F_GETFL));
|
||||
if(!isudp){
|
||||
so._setsockopt(0, SOL_SOCKET, SO_LINGER, (unsigned char *)&lg, sizeof(lg));
|
||||
so._setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (unsigned char *)&opt, sizeof(int));
|
||||
@@ -571,7 +582,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
#ifdef _WIN32
|
||||
ioctlsocket(sock, FIONBIO, &ul);
|
||||
#else
|
||||
fcntl(sock,F_SETFL,O_NONBLOCK);
|
||||
fcntl(sock,F_SETFL,O_NONBLOCK | fcntl(sock,F_GETFL));
|
||||
#endif
|
||||
srv.srvsock = sock;
|
||||
opt = 1;
|
||||
@@ -587,7 +598,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
size = sizeof(srv.intsa);
|
||||
for(sleeptime = SLEEPTIME * 100; so._bind(sock, (struct sockaddr*)&srv.intsa, SASIZE(&srv.intsa))==-1; usleep(sleeptime)) {
|
||||
sprintf((char *)buf, "bind(): %s", strerror(errno));
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, buf);
|
||||
if(!srv.silent)dolog(&defparam, buf);
|
||||
sleeptime = (sleeptime<<1);
|
||||
if(!sleeptime) {
|
||||
so._closesocket(sock);
|
||||
@@ -597,7 +608,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
if(!isudp){
|
||||
if(so._listen (sock, 1 + (srv.maxchild>>4))==-1) {
|
||||
sprintf((char *)buf, "listen(): %s", strerror(errno));
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, buf);
|
||||
if(!srv.silent)dolog(&defparam, buf);
|
||||
return -4;
|
||||
}
|
||||
}
|
||||
@@ -606,13 +617,13 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
|
||||
if(!srv.silent && !iscbc){
|
||||
sprintf((char *)buf, "Accepting connections [%u/%u]", (unsigned)getpid(), (unsigned)pthread_self());
|
||||
(*srv.logfunc)(&defparam, buf);
|
||||
dolog(&defparam, buf);
|
||||
}
|
||||
}
|
||||
if(iscbl){
|
||||
parsehost(srv.family, cbl_string, (struct sockaddr *)&cbsa);
|
||||
if((srv.cbsock=so._socket(SASOCK(&cbsa), SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET) {
|
||||
(*srv.logfunc)(&defparam, (unsigned char *)"Failed to allocate connect back socket");
|
||||
dolog(&defparam, (unsigned char *)"Failed to allocate connect back socket");
|
||||
return -6;
|
||||
}
|
||||
opt = 1;
|
||||
@@ -625,11 +636,11 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
setopts(srv.cbsock, srv.cbssockopts);
|
||||
|
||||
if(so._bind(srv.cbsock, (struct sockaddr*)&cbsa, SASIZE(&cbsa))==-1) {
|
||||
(*srv.logfunc)(&defparam, (unsigned char *)"Failed to bind connect back socket");
|
||||
dolog(&defparam, (unsigned char *)"Failed to bind connect back socket");
|
||||
return -7;
|
||||
}
|
||||
if(so._listen(srv.cbsock, 1 + (srv.maxchild>>4))==-1) {
|
||||
(*srv.logfunc)(&defparam, (unsigned char *)"Failed to listen connect back socket");
|
||||
dolog(&defparam, (unsigned char *)"Failed to listen connect back socket");
|
||||
return -8;
|
||||
}
|
||||
}
|
||||
@@ -649,7 +660,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
nlog++;
|
||||
if(!srv.silent && nlog > 5000) {
|
||||
sprintf((char *)buf, "Warning: too many connected clients (%d/%d)", srv.childcount, srv.maxchild);
|
||||
(*srv.logfunc)(&defparam, buf);
|
||||
dolog(&defparam, buf);
|
||||
nlog = 0;
|
||||
}
|
||||
usleep(SLEEPTIME);
|
||||
@@ -667,7 +678,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
if (error == 0) continue;
|
||||
if (errno != EAGAIN && errno != EINTR) {
|
||||
sprintf((char *)buf, "poll(): %s/%d", strerror(errno), errno);
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, buf);
|
||||
if(!srv.silent)dolog(&defparam, buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -740,7 +751,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
nlog++;
|
||||
if(!srv.silent && (error || nlog > 5000)) {
|
||||
sprintf((char *)buf, "accept(): %s", strerror(errno));
|
||||
(*srv.logfunc)(&defparam, buf);
|
||||
dolog(&defparam, buf);
|
||||
nlog = 0;
|
||||
}
|
||||
continue;
|
||||
@@ -750,13 +761,13 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
size = sizeof(defparam.sincl);
|
||||
if(so._getsockname(new_sock, (struct sockaddr *)&defparam.sincl, &size)){
|
||||
sprintf((char *)buf, "getsockname(): %s", strerror(errno));
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, buf);
|
||||
if(!srv.silent)dolog(&defparam, buf);
|
||||
continue;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
ioctlsocket(new_sock, FIONBIO, &ul);
|
||||
#else
|
||||
fcntl(new_sock,F_SETFL,O_NONBLOCK);
|
||||
fcntl(new_sock,F_SETFL,O_NONBLOCK | fcntl(new_sock,F_GETFL));
|
||||
#endif
|
||||
so._setsockopt(new_sock, SOL_SOCKET, SO_LINGER, (char *)&lg, sizeof(lg));
|
||||
so._setsockopt(new_sock, SOL_SOCKET, SO_OOBINLINE, (char *)&opt, sizeof(int));
|
||||
@@ -767,7 +778,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
if(! (newparam = myalloc (sizeof(defparam)))){
|
||||
if(!isudp) so._closesocket(new_sock);
|
||||
defparam.res = 21;
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, (unsigned char *)"Memory Allocation Failed");
|
||||
if(!srv.silent)dolog(&defparam, (unsigned char *)"Memory Allocation Failed");
|
||||
usleep(SLEEPTIME);
|
||||
continue;
|
||||
};
|
||||
@@ -804,7 +815,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
}
|
||||
else {
|
||||
sprintf((char *)buf, "_beginthreadex(): %s", _strerror(NULL));
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, buf);
|
||||
if(!srv.silent)dolog(&defparam, buf);
|
||||
error = 1;
|
||||
}
|
||||
#else
|
||||
@@ -813,7 +824,7 @@ int MODULEMAINFUNC (int argc, char** argv){
|
||||
srv.childcount++;
|
||||
if(error){
|
||||
sprintf((char *)buf, "pthread_create(): %s", strerror(error));
|
||||
if(!srv.silent)(*srv.logfunc)(&defparam, buf);
|
||||
if(!srv.silent)dolog(&defparam, buf);
|
||||
}
|
||||
else {
|
||||
newparam->threadid = (unsigned)thread;
|
||||
|
||||
+2
-2
@@ -292,9 +292,9 @@ CLEANRET:
|
||||
|
||||
if(param->hostname&¶m->extusername) {
|
||||
sprintf((char *)buf, "%.128s@%.128s%c%hu", param->extusername, param->hostname, *SAPORT(¶m->sinsr)==25?0:':',ntohs(*SAPORT(¶m->sinsr)));
|
||||
(*param->srv->logfunc)(param, buf);
|
||||
dolog(param, buf);
|
||||
}
|
||||
else (*param->srv->logfunc)(param, NULL);
|
||||
else dolog(param, NULL);
|
||||
if(param->clisock != INVALID_SOCKET) {
|
||||
if ((param->res > 0 && param->res < 100) || (param->res > 661 && param->res <700)) socksend(param->clisock, (unsigned char *)"571 \r\n", 6,conf.timeouts[STRING_S]);
|
||||
}
|
||||
|
||||
+624
-539
File diff suppressed because it is too large.
Load diff
+1
-1
@@ -448,7 +448,7 @@ fflush(stderr);
|
||||
else
|
||||
myinet_ntop(*SAFAMILY(¶m->req), SAADDR(¶m->req), (char *)buf + strlen((char *)buf), 64);
|
||||
sprintf((char *)buf+strlen((char *)buf), ":%hu", ntohs(*SAPORT(¶m->req)));
|
||||
(*param->srv->logfunc)(param, buf);
|
||||
dolog(param, buf);
|
||||
myfree(buf);
|
||||
}
|
||||
freeparam(param);
|
||||
|
||||
+1
-1
@@ -740,7 +740,7 @@ struct pluginlink {
|
||||
int (*dobuf2)(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
|
||||
int (*scanaddr)(const unsigned char *s, unsigned long * ip, unsigned long * mask);
|
||||
unsigned long (*getip46)(int family, unsigned char *name, struct sockaddr *sa);
|
||||
int (*sockmap)(struct clientparam * param, int timeo);
|
||||
int (*sockmap)(struct clientparam * param, int timeo, int usesplice);
|
||||
int (*ACLMatches)(struct ace* acentry, struct clientparam * param);
|
||||
int (*alwaysauth)(struct clientparam * param);
|
||||
int (*checkACL)(struct clientparam * param);
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ void * tcppmchild(struct clientparam* param) {
|
||||
RETURN (mapsocket(param, conf.timeouts[CONNECTION_L]));
|
||||
CLEANRET:
|
||||
|
||||
(*param->srv->logfunc)(param, param->hostname);
|
||||
dolog(param, param->hostname);
|
||||
freeparam(param);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
+3
-3
@@ -81,7 +81,7 @@ void * udppmchild(struct clientparam* param) {
|
||||
#ifdef _WIN32
|
||||
ioctlsocket(param->remsock, FIONBIO, &ul);
|
||||
#else
|
||||
fcntl(param->remsock,F_SETFL,O_NONBLOCK);
|
||||
fcntl(param->remsock,F_SETFL,O_NONBLOCK | fcntl(param->remsock,F_GETFL));
|
||||
#endif
|
||||
memcpy(¶m->sinsr, ¶m->req, sizeof(param->req));
|
||||
|
||||
@@ -91,7 +91,7 @@ void * udppmchild(struct clientparam* param) {
|
||||
param->srv->fds.events = POLLIN;
|
||||
}
|
||||
|
||||
param->res = sockmap(param, conf.timeouts[(param->srv->singlepacket)?SINGLEBYTE_L:STRING_L]);
|
||||
param->res = mapsocket(param, conf.timeouts[(param->srv->singlepacket)?SINGLEBYTE_L:STRING_L]);
|
||||
if(!param->srv->singlepacket) {
|
||||
param->srv->fds.events = POLLIN;
|
||||
}
|
||||
@@ -99,7 +99,7 @@ void * udppmchild(struct clientparam* param) {
|
||||
CLEANRET:
|
||||
|
||||
if(buf)myfree(buf);
|
||||
(*param->srv->logfunc)(param, NULL);
|
||||
dolog(param, NULL);
|
||||
#ifndef _WIN32
|
||||
param->clisock = INVALID_SOCKET;
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -576,7 +576,7 @@ CLEANRET:
|
||||
|
||||
printstr(&pp, NULL);
|
||||
if(buf) myfree(buf);
|
||||
(*param->srv->logfunc)(param, (unsigned char *)req);
|
||||
dolog(param, (unsigned char *)req);
|
||||
if(req)myfree(req);
|
||||
freeparam(param);
|
||||
return (NULL);
|
||||
|
||||
Reference in new issue
Block a user