proxy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2021 by Vladimir Dubrovin <3proxy@3proxy.org>
  4. please read License Agreement
  5. */
  6. #define COPYRIGHT "(c)3APA3A, Vladimir Dubrovin & 3proxy.org\n"\
  7. "Documentation and sources: https://3proxy.org/\n"\
  8. "Please read license agreement in \'copying\' file.\n"\
  9. "You may not use this program without accepting license agreement"
  10. #ifndef _3PROXY_H_
  11. #define _3PROXY_H_
  12. #include "version.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <time.h>
  21. #define MAXUSERNAME 128
  22. #define _PASSWORD_LEN 256
  23. #define MAXNSERVERS 5
  24. #define UDPBUFSIZE 16384
  25. #define TCPBUFSIZE 8192
  26. #define SRVBUFSIZE (param->srv->bufsize?param->srv->bufsize:((param->service == S_UDPPM)?UDPBUFSIZE:TCPBUFSIZE))
  27. #ifdef _WIN32
  28. #include <winsock2.h>
  29. #include <sys/timeb.h>
  30. #ifndef _WINCE
  31. #include <io.h>
  32. #else
  33. #include <sys/unistd.h>
  34. #endif
  35. #include <process.h>
  36. #define SASIZETYPE int
  37. #define SHUT_RDWR SD_BOTH
  38. #else
  39. #ifndef FD_SETSIZE
  40. #define FD_SETSIZE 4096
  41. #endif
  42. #include <signal.h>
  43. #include <sys/uio.h>
  44. #include <sys/time.h>
  45. #include <unistd.h>
  46. #include <pthread.h>
  47. #include <syslog.h>
  48. #include <errno.h>
  49. #endif
  50. #ifdef __CYGWIN__
  51. #include <windows.h>
  52. #define daemonize() FreeConsole()
  53. #define SLEEPTIME 1000
  54. #undef _WIN32
  55. #elif _WIN32
  56. #ifdef errno
  57. #undef errno
  58. #endif
  59. #define errno WSAGetLastError()
  60. #ifdef EAGAIN
  61. #undef EAGAIN
  62. #endif
  63. #define EAGAIN WSAEWOULDBLOCK
  64. #ifdef EINTR
  65. #undef EINTR
  66. #endif
  67. #ifndef EINPROGRESS
  68. #define EINPROGRESS WSAEWOULDBLOCK
  69. #endif
  70. #define EINTR WSAEWOULDBLOCK
  71. #define SLEEPTIME 1
  72. #define usleep Sleep
  73. #define pthread_self GetCurrentThreadId
  74. #define getpid GetCurrentProcessId
  75. #define pthread_t unsigned
  76. #ifndef _WINCE
  77. #define daemonize() FreeConsole()
  78. #else
  79. #define daemonize()
  80. #endif
  81. #define socket(x, y, z) WSASocket(x, y, z, NULL, 0, 0)
  82. #define accept(x, y, z) WSAAccept(x, y, z, NULL, 0)
  83. #define ftruncate chsize
  84. #else
  85. #include <pthread.h>
  86. #ifndef PTHREAD_STACK_MIN
  87. #define PTHREAD_STACK_MIN 32768
  88. #define sockerror strerror
  89. #endif
  90. void daemonize(void);
  91. #define SLEEPTIME 1000
  92. #ifndef O_BINARY
  93. #define O_BINARY 0
  94. #endif
  95. #endif
  96. #ifndef NOODBC
  97. #ifndef _WIN32
  98. #include <sqltypes.h>
  99. #endif
  100. #include <sql.h>
  101. #include <sqlext.h>
  102. #endif
  103. #ifdef _WIN32
  104. #define strcasecmp stricmp
  105. #define strncasecmp strnicmp
  106. #define seterrno3(x) _set_errno(x)
  107. #else
  108. #define seterrno3(x) (errno = x)
  109. #endif
  110. #ifndef SOCKET_ERROR
  111. #define SOCKET_ERROR -1
  112. #endif
  113. #ifndef isnumber
  114. #define isnumber(n) (n >= '0' && n <= '9')
  115. #endif
  116. #ifndef ishex
  117. #define ishex(n) ((n >= '0' && n <= '9') || (n >= 'a' && n<='f') || (n >= 'A' && n <= 'F'))
  118. #endif
  119. #define isallowed(n) ((n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '*' && n <= '/') || n == '_')
  120. #include "structures.h"
  121. #define MAXRADIUS 5
  122. #define DEFLOGFORMAT "G%y%m%d%H%M%S.%. %p %E %U %C:%c %R:%r %O %I %h %T"
  123. #define myalloc malloc
  124. #define myfree free
  125. #define myrealloc realloc
  126. #define mystrdup strdup
  127. extern RESOLVFUNC resolvfunc;
  128. extern int wday;
  129. extern time_t basetime;
  130. extern int timetoexit;
  131. extern struct extparam conf;
  132. int sockmap(struct clientparam * param, int timeo, int usesplice);
  133. int socksend(SOCKET sock, unsigned char * buf, int bufsize, int to);
  134. int socksendto(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  135. int sockrecvfrom(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  136. int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec);
  137. int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec);
  138. int sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec);
  139. int sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec);
  140. int sockgetlinebuf(struct clientparam * param, DIRECTION which, unsigned char * buf, int bufsize, int delim, int to);
  141. void dolog(struct clientparam * param, const unsigned char *s);
  142. int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
  143. int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
  144. extern FILE * stdlog;
  145. void logstdout(struct clientparam * param, const unsigned char *s);
  146. void logsyslog(struct clientparam * param, const unsigned char *s);
  147. void lognone(struct clientparam * param, const unsigned char *s);
  148. void logradius(struct clientparam * param, const unsigned char *s);
  149. #ifndef NOSQL
  150. void logsql(struct clientparam * param, const unsigned char *s);
  151. int init_sql(char * s);
  152. void close_sql();
  153. #endif
  154. int doconnect(struct clientparam * param);
  155. int alwaysauth(struct clientparam * param);
  156. int ipauth(struct clientparam * param);
  157. int doauth(struct clientparam * param);
  158. int strongauth(struct clientparam * param);
  159. void trafcountfunc(struct clientparam *param);
  160. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  161. int scanaddr(const unsigned char *s, unsigned long * ip, unsigned long * mask);
  162. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  163. extern struct nserver nservers[MAXNSERVERS];
  164. extern struct nserver authnserver;
  165. unsigned long getip(unsigned char *name);
  166. unsigned long getip46(int family, unsigned char *name, struct sockaddr *sa);
  167. int afdetect(unsigned char *name);
  168. unsigned long myresolver(int, unsigned char *, unsigned char *);
  169. unsigned long fakeresolver (int, unsigned char *, unsigned char*);
  170. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  171. void freeparam(struct clientparam * param);
  172. void clearstat(struct clientparam * param);
  173. void dumpcounters(struct trafcount *tl, int counterd);
  174. int startconnlims (struct clientparam *param);
  175. void stopconnlims (struct clientparam *param);
  176. extern struct auth authfuncs[];
  177. int reload (void);
  178. extern int paused;
  179. extern int demon;
  180. unsigned char * mycrypt(const unsigned char *key, const unsigned char *salt, unsigned char *buf);
  181. unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int tohex);
  182. int de64 (const unsigned char *in, unsigned char *out, int maxlen);
  183. unsigned char* en64 (const unsigned char *in, unsigned char *out, int inlen);
  184. void tohex(unsigned char *in, unsigned char *out, int len);
  185. void fromhex(unsigned char *in, unsigned char *out, int len);
  186. int ftplogin(struct clientparam *param, char *buf, int *inbuf);
  187. int ftpcd(struct clientparam *param, unsigned char* path, char *buf, int *inbuf);
  188. int ftpsyst(struct clientparam *param, unsigned char *buf, unsigned len);
  189. int ftppwd(struct clientparam *param, unsigned char *buf, unsigned len);
  190. int ftptype(struct clientparam *param, unsigned char* f_type);
  191. int ftpres(struct clientparam *param, unsigned char * buf, int len);
  192. SOCKET ftpcommand(struct clientparam *param, unsigned char * command, unsigned char *arg);
  193. int text2unicode(const char * text, char * buf, int buflen);
  194. void unicode2text(const char *unicode, char * buf, int len);
  195. void genchallenge(struct clientparam *param, char * challenge, char *buf);
  196. void mschap(const unsigned char *win_password,
  197. const unsigned char *challenge, unsigned char *response);
  198. struct hashtable;
  199. void hashadd(struct hashtable *ht, const unsigned char* name, unsigned char* value, time_t expires);
  200. int parsehost(int family, unsigned char *host, struct sockaddr *sa);
  201. int parsehostname(char *hostname, struct clientparam *param, unsigned short port);
  202. int parseusername(char *username, struct clientparam *param, int extpasswd);
  203. int parseconnusername(char *username, struct clientparam *param, int extpasswd, unsigned short port);
  204. int ACLmatches(struct ace* acentry, struct clientparam * param);
  205. int checkACL(struct clientparam * param);
  206. extern int havelog;
  207. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  208. struct ace * copyacl (struct ace *ac);
  209. struct auth * copyauth (struct auth *);
  210. void * itfree(void *data, void * retval);
  211. void freeacl(struct ace *ac);
  212. void freeauth(struct auth *);
  213. void freefilter(struct filter *filter);
  214. void freeconf(struct extparam *confp);
  215. struct passwords * copypwl (struct passwords *pwl);
  216. void freepwl(struct passwords *pw);
  217. void copyfilter(struct filter *, struct srvparam *srv);
  218. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param);
  219. FILTER_ACTION handlereqfilters(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  220. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  221. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  222. FILTER_ACTION handlepredatflt(struct clientparam *param);
  223. FILTER_ACTION handledatfltcli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  224. FILTER_ACTION handledatfltsrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  225. void srvinit(struct srvparam * srv, struct clientparam *param);
  226. void srvinit2(struct srvparam * srv, struct clientparam *param);
  227. void srvfree(struct srvparam * srv);
  228. unsigned char * dologname (unsigned char *buf, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t);
  229. int readconfig(FILE * fp);
  230. int connectwithpoll(SOCKET sock, struct sockaddr *sa, SASIZETYPE size, int to);
  231. int myrand(void * entropy, int len);
  232. extern char *copyright;
  233. #define SERVICES 5
  234. void * dnsprchild(struct clientparam * param);
  235. void * pop3pchild(struct clientparam * param);
  236. void * smtppchild(struct clientparam * param);
  237. void * proxychild(struct clientparam * param);
  238. void * sockschild(struct clientparam * param);
  239. void * tcppmchild(struct clientparam * param);
  240. void * udppmchild(struct clientparam * param);
  241. void * adminchild(struct clientparam * param);
  242. void * ftpprchild(struct clientparam * param);
  243. struct datatype;
  244. struct dictionary;
  245. struct node;
  246. struct property;
  247. extern pthread_mutex_t config_mutex;
  248. extern pthread_mutex_t bandlim_mutex;
  249. extern pthread_mutex_t connlim_mutex;
  250. extern pthread_mutex_t hash_mutex;
  251. extern pthread_mutex_t tc_mutex;
  252. extern pthread_mutex_t pwl_mutex;
  253. extern pthread_mutex_t log_mutex;
  254. extern pthread_mutex_t rad_mutex;
  255. extern struct datatype datatypes[64];
  256. extern struct commands commandhandlers[];
  257. #ifdef WITHSPLICE
  258. #define mapsocket(a,b) ((a->srv->usesplice && !a->ndatfilterssrv && !a->ndatfilterscli)?sockmap(a,b,1):sockmap(a,b,0))
  259. #else
  260. #define mapsocket(a,b) sockmap(a,b, 0)
  261. #endif
  262. extern struct radserver {
  263. #ifdef NOIPV6
  264. struct sockaddr_in authaddr, logaddr, localaddr;
  265. #else
  266. struct sockaddr_in6 authaddr, logaddr, localaddr;
  267. #endif
  268. /*
  269. SOCKET logsock;
  270. */
  271. } radiuslist[MAXRADIUS];
  272. extern char radiussecret[64];
  273. extern int nradservers;
  274. extern struct socketoptions {
  275. int opt;
  276. char * optname;
  277. } sockopts[];
  278. void setopts(SOCKET s, int opts);
  279. char * printopts(char *sep);
  280. #ifdef _WINCE
  281. char * CEToUnicode (const char *str);
  282. int cesystem(const char *str);
  283. int ceparseargs(const char *str);
  284. extern char * ceargv[32];
  285. #define system(S) cesystem(S)
  286. #endif
  287. #define WEBBANNERS 35
  288. #endif