3proxy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #include "proxy.h"
  7. #ifndef _WIN32
  8. #include <sys/resource.h>
  9. #ifndef NOPLUGINS
  10. #include <dlfcn.h>
  11. #endif
  12. #endif
  13. #ifndef DEFAULTCONFIG
  14. #define DEFAULTCONFIG conf.stringtable[25]
  15. #endif
  16. FILE * confopen();
  17. extern unsigned char *strings[];
  18. extern FILE *writable;
  19. extern struct counter_header cheader;
  20. extern struct counter_record crecord;
  21. time_t basetime = 0;
  22. void doschedule(void);
  23. #ifdef _WIN32
  24. OSVERSIONINFO osv;
  25. int service = 0;
  26. void cyclestep(void);
  27. #ifndef _WINCE
  28. SERVICE_STATUS_HANDLE hSrv;
  29. DWORD dwCurrState;
  30. int SetStatus( DWORD dwState, DWORD dwExitCode, DWORD dwProgress )
  31. {
  32. SERVICE_STATUS srvStatus;
  33. srvStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  34. srvStatus.dwCurrentState = dwCurrState = dwState;
  35. srvStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
  36. srvStatus.dwWin32ExitCode = dwExitCode;
  37. srvStatus.dwServiceSpecificExitCode = 0;
  38. srvStatus.dwCheckPoint = dwProgress;
  39. srvStatus.dwWaitHint = 3000;
  40. return SetServiceStatus( hSrv, &srvStatus );
  41. }
  42. void __stdcall CommandHandler( DWORD dwCommand )
  43. {
  44. switch( dwCommand )
  45. {
  46. case SERVICE_CONTROL_STOP:
  47. case SERVICE_CONTROL_SHUTDOWN:
  48. SetStatus( SERVICE_STOP_PENDING, 0, 1 );
  49. conf.timetoexit = 1;
  50. conf.paused++;
  51. Sleep(2000);
  52. SetStatus( SERVICE_STOPPED, 0, 0 );
  53. #ifndef NOODBC
  54. pthread_mutex_lock(&log_mutex);
  55. close_sql();
  56. pthread_mutex_unlock(&log_mutex);
  57. #endif
  58. break;
  59. case SERVICE_CONTROL_PAUSE:
  60. SetStatus( SERVICE_PAUSE_PENDING, 0, 1 );
  61. conf.paused++;
  62. SetStatus( SERVICE_PAUSED, 0, 0 );
  63. break;
  64. case SERVICE_CONTROL_CONTINUE:
  65. SetStatus( SERVICE_CONTINUE_PENDING, 0, 1 );
  66. conf.needreload = 1;
  67. SetStatus( SERVICE_RUNNING, 0, 0 );
  68. break;
  69. default: ;
  70. }
  71. }
  72. void __stdcall ServiceMain(int argc, unsigned char* argv[] )
  73. {
  74. hSrv = RegisterServiceCtrlHandler((LPCSTR)conf.stringtable[1], (LPHANDLER_FUNCTION)CommandHandler);
  75. if( hSrv == 0 ) return;
  76. SetStatus( SERVICE_START_PENDING, 0, 1 );
  77. SetStatus( SERVICE_RUNNING, 0, 0 );
  78. cyclestep();
  79. }
  80. #endif
  81. #else
  82. void mysigusr1 (int sig){
  83. conf.needreload = 1;
  84. }
  85. int even = 0;
  86. void mysigpause (int sig){
  87. conf.paused++;
  88. even = !even;
  89. if(!even){
  90. conf.needreload = 1;
  91. }
  92. }
  93. void mysigterm (int sig){
  94. conf.paused++;
  95. usleep(999*SLEEPTIME);
  96. usleep(999*SLEEPTIME);
  97. #ifndef NOODBC
  98. pthread_mutex_lock(&log_mutex);
  99. close_sql();
  100. pthread_mutex_unlock(&log_mutex);
  101. #endif
  102. conf.timetoexit = 1;
  103. }
  104. #endif
  105. void dumpmem(void);
  106. struct schedule *schedule;
  107. int wday = 0;
  108. int timechanged (time_t oldtime, time_t newtime, ROTATION lt){
  109. struct tm tmold;
  110. struct tm *tm;
  111. tm = localtime(&oldtime);
  112. tmold = *tm;
  113. tm = localtime(&newtime);
  114. switch(lt){
  115. case MINUTELY:
  116. if(tm->tm_min != tmold.tm_min)return 1;
  117. break;
  118. case HOURLY:
  119. if(tm->tm_hour != tmold.tm_hour)return 1;
  120. break;
  121. case DAILY:
  122. if(tm->tm_yday != tmold.tm_yday)return 1;
  123. break;
  124. case MONTHLY:
  125. if(tm->tm_mon != tmold.tm_mon)return 1;
  126. break;
  127. case ANNUALLY:
  128. if(tm->tm_year != tmold.tm_year)return 1;
  129. break;
  130. case WEEKLY:
  131. if(((newtime - oldtime) > (60*60*24*7))
  132. || tm->tm_wday < tmold.tm_wday
  133. || (tm->tm_wday == tmold.tm_wday && (newtime - oldtime) > (60*60*24*6))
  134. )return 1;
  135. break;
  136. default:
  137. break;
  138. }
  139. return 0;
  140. }
  141. void doschedule(void){
  142. struct schedule *sched, *prevsched = NULL, *nextsched;
  143. int res;
  144. conf.time = time(0);
  145. for(sched=schedule; sched; sched=sched->next){
  146. if(conf.needreload || conf.timetoexit || (conf.time > sched->start_time && timechanged(sched->start_time, conf.time, sched->type))){
  147. sched->start_time = conf.time;
  148. nextsched = sched->next;
  149. res = (*sched->function)(sched->data);
  150. switch(res){
  151. case 1:
  152. if(prevsched) prevsched->next = nextsched;
  153. else schedule = nextsched;
  154. break;
  155. }
  156. }
  157. prevsched = sched;
  158. }
  159. }
  160. void dumpcounters(struct trafcount *tlin, int counterd){
  161. unsigned char tmpbuf[8192];
  162. struct trafcount *tl;
  163. if(counterd >= 0 && tlin) {
  164. conf.time = time(0);
  165. if(cheader.updated && conf.countertype && timechanged(cheader.updated, conf.time, conf.countertype)){
  166. FILE * cfp;
  167. cfp = fopen((char *)dologname(tmpbuf, (unsigned char *)conf.counterfile, NULL, conf.countertype, cheader.updated), "w");
  168. if(cfp){
  169. for(tl = tlin; cfp && tl; tl = tl->next){
  170. if(tl->type >= conf.countertype)
  171. fprintf(cfp, "%05d %020"PRINTF_INT64_MODIFIER"u%s%s\n", tl->number, tl->traf64, tl->comment?" #" : "", tl->comment? tl->comment : "");
  172. }
  173. fclose(cfp);
  174. }
  175. }
  176. cheader.updated = conf.time;
  177. lseek(counterd, 0, SEEK_SET);
  178. write(counterd, &cheader, sizeof(struct counter_header));
  179. for(tl=tlin; tl; tl = tl->next){
  180. if(tl->number){
  181. lseek(counterd,
  182. sizeof(struct counter_header) + (tl->number - 1) * sizeof(struct counter_record),
  183. SEEK_SET);
  184. crecord.traf64 = tl->traf64;
  185. crecord.cleared = tl->cleared;
  186. crecord.updated = tl->updated;
  187. write(counterd, &crecord, sizeof(struct counter_record));
  188. }
  189. if(tl->type!=NEVER && timechanged(tl->cleared, conf.time, tl->type)){
  190. tl->cleared = conf.time;
  191. tl->traf64 = 0;
  192. }
  193. }
  194. }
  195. }
  196. void cyclestep(void){
  197. struct tm *tm;
  198. time_t minutecounter;
  199. unsigned char tmpbuf[8192];
  200. minutecounter = time(0);
  201. for(;;){
  202. usleep(SLEEPTIME*999);
  203. conf.time = time(0);
  204. if(conf.needreload) {
  205. doschedule();
  206. reload();
  207. conf.needreload = 0;
  208. }
  209. doschedule();
  210. if(conf.stdlog)fflush(conf.stdlog);
  211. if(timechanged(minutecounter, conf.time, MINUTELY)) {
  212. struct filemon *fm;
  213. struct stat sb;
  214. for(fm=conf.fmon; fm; fm=fm->next){
  215. if(!stat(fm->path, &sb)){
  216. if(fm->sb.st_mtime != sb.st_mtime || fm->sb.st_size != sb.st_size){
  217. stat(fm->path, &fm->sb);
  218. conf.needreload = 1;
  219. }
  220. }
  221. }
  222. }
  223. if(timechanged(basetime, conf.time, DAILY)) {
  224. tm = localtime(&conf.time);
  225. wday = (1 << tm->tm_wday);
  226. tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
  227. basetime = mktime(tm);
  228. }
  229. if(conf.logname) {
  230. if(timechanged(conf.logtime, conf.time, conf.logtype)) {
  231. if(conf.stdlog) conf.stdlog = freopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.time), "a", conf.stdlog);
  232. else conf.stdlog = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.time), "a");
  233. conf.logtime = conf.time;
  234. if(conf.logtype != NONE && conf.rotate) {
  235. int t;
  236. t = 1;
  237. switch(conf.logtype){
  238. case ANNUALLY:
  239. t = t * 12;
  240. case MONTHLY:
  241. t = t * 4;
  242. case WEEKLY:
  243. t = t * 7;
  244. case DAILY:
  245. t = t * 24;
  246. case HOURLY:
  247. t = t * 60;
  248. case MINUTELY:
  249. t = t * 60;
  250. default:
  251. break;
  252. }
  253. dologname (tmpbuf, conf.logname, (conf.archiver)?conf.archiver[1]:NULL, conf.logtype, (conf.logtime - t * conf.rotate));
  254. remove ((char *) tmpbuf);
  255. if(conf.archiver) {
  256. int i;
  257. *tmpbuf = 0;
  258. for(i = 2; i < conf.archiverc && strlen((char *)tmpbuf) < 512; i++){
  259. strcat((char *)tmpbuf, " ");
  260. if(!strcmp((char *)conf.archiver[i], "%A")){
  261. strcat((char *)tmpbuf, "\"");
  262. dologname (tmpbuf + strlen((char *)tmpbuf), conf.logname, conf.archiver[1], conf.logtype, (conf.logtime - t));
  263. strcat((char *)tmpbuf, "\"");
  264. }
  265. else if(!strcmp((char *)conf.archiver[i], "%F")){
  266. strcat((char *)tmpbuf, "\"");
  267. dologname (tmpbuf+strlen((char *)tmpbuf), conf.logname, NULL, conf.logtype, (conf.logtime-t));
  268. strcat((char *)tmpbuf, "\"");
  269. }
  270. else
  271. strcat((char *)tmpbuf, (char *)conf.archiver[i]);
  272. }
  273. system((char *)tmpbuf+1);
  274. }
  275. }
  276. }
  277. }
  278. if(conf.counterd >= 0 && conf.trafcounter) {
  279. if(timechanged(cheader.updated, conf.time, MINUTELY)){
  280. dumpcounters(conf.trafcounter, conf.counterd);
  281. }
  282. }
  283. if(conf.timetoexit){
  284. conf.paused++;
  285. doschedule();
  286. usleep(SLEEPTIME*999);
  287. usleep(SLEEPTIME*999);
  288. usleep(SLEEPTIME*999);
  289. return;
  290. }
  291. }
  292. }
  293. #define RETURN(x) {res = x; goto CLEARRETURN;}
  294. #ifndef _WINCE
  295. int main(int argc, char * argv[]) {
  296. #else
  297. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow){
  298. int argc;
  299. char ** argv;
  300. WNDCLASS wc;
  301. HWND hwnd = 0;
  302. #endif
  303. int res = 0;
  304. FILE * fp = NULL;
  305. #ifdef _WIN32
  306. unsigned char * arg;
  307. WSADATA wd;
  308. unsigned char tmpbuf[8192];
  309. WSAStartup(MAKEWORD( 1, 1 ), &wd);
  310. osv.dwOSVersionInfoSize = sizeof(osv);
  311. GetVersionEx(&osv);
  312. #endif
  313. #ifdef _WINCE
  314. argc = ceparseargs((char *)lpCmdLine);
  315. argv = ceargv;
  316. if(FindWindow(L"3proxy", L"3proxy")) return 0;
  317. ZeroMemory(&wc,sizeof(wc));
  318. wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  319. wc.hInstance=hInstance;
  320. wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  321. wc.lpfnWndProc=DefWindowProc;
  322. wc.style=CS_HREDRAW|CS_VREDRAW;
  323. wc.lpszClassName=L"3proxy";
  324. RegisterClass(&wc);
  325. hwnd = CreateWindowEx(0,L"3proxy",L"3proxy",WS_VISIBLE|WS_POPUP,0,0,0,0,0,0,hInstance,0);
  326. #endif
  327. conf.stringtable = strings;
  328. #ifdef _WIN32
  329. #ifndef _WINCE
  330. if((argc == 2 || argc == 3)&& !strcmp((char *)argv[1], "--install")) {
  331. sprintf((char *)tmpbuf, "%s will be installed and started.\n"
  332. "By clicking Yes you confirm you read and accepted License Agreement.\n"
  333. "You can use Administration/Services to control %s service.",
  334. conf.stringtable[1], conf.stringtable[2]);
  335. if(MessageBox(NULL, (LPCSTR)tmpbuf, (LPCSTR)conf.stringtable[2], MB_YESNO|MB_ICONASTERISK) != IDYES) return 1;
  336. *tmpbuf = '\"';
  337. if (!(res = SearchPath(NULL, argv[0], ".exe", 256, (char *)tmpbuf+1, (LPTSTR*)&arg))) {
  338. perror("Failed to find executable filename");
  339. RETURN(102);
  340. }
  341. strcat((char *)tmpbuf, "\" \"");
  342. if(!(res = GetFullPathName ((argc == 3)?argv[2]:(char*)DEFAULTCONFIG, 256, (char *)tmpbuf+res+4, (char **)&arg))){
  343. perror("Failed to find config filename");
  344. RETURN(103);
  345. }
  346. strcat((char *)tmpbuf, "\" --service");
  347. if(osv.dwPlatformId == VER_PLATFORM_WIN32_NT){
  348. SC_HANDLE sch;
  349. if(!(sch = OpenSCManager(NULL, NULL, GENERIC_WRITE|SERVICE_START ))){
  350. perror("Failed to open Service Manager");
  351. RETURN(101);
  352. }
  353. if (!(sch = CreateService(sch, (LPCSTR)conf.stringtable[1], (LPCSTR)conf.stringtable[2], GENERIC_EXECUTE, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, (char *)tmpbuf, NULL, NULL, NULL, NULL, NULL))){
  354. perror("Failed to create service");
  355. RETURN(103);
  356. }
  357. if (!StartService(sch, 0, NULL)) {
  358. perror("Failed to start service");
  359. RETURN(103);
  360. }
  361. }
  362. else {
  363. HKEY runsrv;
  364. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  365. "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  366. 0,
  367. KEY_ALL_ACCESS,
  368. &runsrv) != ERROR_SUCCESS){
  369. perror("Failed to open registry");
  370. RETURN(104);
  371. }
  372. if(RegSetValueEx( runsrv,
  373. (LPCSTR)conf.stringtable[1],
  374. 0,
  375. REG_EXPAND_SZ,
  376. (BYTE *)tmpbuf,
  377. (int)strlen((char *)tmpbuf)+1)!=ERROR_SUCCESS){
  378. perror("Failed to set registry value");
  379. RETURN(105);
  380. }
  381. }
  382. return 0;
  383. }
  384. if((argc == 2 || argc == 3)&& !strcmp((char *)argv[1], "--remove")) {
  385. if(osv.dwPlatformId == VER_PLATFORM_WIN32_NT){
  386. SC_HANDLE sch;
  387. if(!(sch = OpenSCManager(NULL, NULL, GENERIC_WRITE))){
  388. perror("Failed to open Service Manager\n");
  389. RETURN(106);
  390. }
  391. if (!(sch = OpenService(sch, (LPCSTR)conf.stringtable[1], DELETE))){
  392. perror("Failed to open service");
  393. RETURN(107);
  394. }
  395. if (!DeleteService(sch)){
  396. perror("Failed to delete service");
  397. RETURN(108);
  398. }
  399. }
  400. else {
  401. HKEY runsrv;
  402. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  403. "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  404. 0,
  405. KEY_ALL_ACCESS,
  406. &runsrv) != ERROR_SUCCESS){
  407. perror("Failed to open registry");
  408. RETURN(109);
  409. }
  410. if(RegDeleteValue(runsrv, (LPCSTR)conf.stringtable[1]) != ERROR_SUCCESS){
  411. perror("Failed to clear registry");
  412. RETURN(110);
  413. }
  414. }
  415. RETURN(0);
  416. }
  417. if(argc==3 && !strcmp(argv[2], "--service")){
  418. service = 1;
  419. argc = 2;
  420. }
  421. #endif
  422. #endif
  423. conf.conffile = mystrdup((argc==2)?argv[1]:(char*)DEFAULTCONFIG);
  424. if(conf.conffile && *conf.conffile != '-') {
  425. fp = confopen();
  426. #ifndef _WIN32
  427. if(!fp) fp = stdin;
  428. #endif
  429. }
  430. if(argc > 2 || !(fp)) {
  431. fprintf(stderr, "Usage: %s [conffile]\n", argv[0]);
  432. #ifdef _WIN32
  433. fprintf(stderr, "\n\t%s --install [conffile]\n\tto install as service\n"
  434. "\n\t%s --remove\n\tto remove service\n", argv[0], argv[0]);
  435. #else
  436. fprintf(stderr, "\n if conffile is missing, configuration is expected from stdin\n");
  437. #endif
  438. fprintf(stderr, "available socket options:\n\t%s\n", printopts("\n\t"));
  439. fprintf(stderr, "\n%s %s\n%s\n", conf.stringtable[2], conf.stringtable[3], copyright);
  440. return 1;
  441. }
  442. pthread_mutex_init(&config_mutex, NULL);
  443. pthread_mutex_init(&bandlim_mutex, NULL);
  444. pthread_mutex_init(&connlim_mutex, NULL);
  445. pthread_mutex_init(&hash_mutex, NULL);
  446. pthread_mutex_init(&tc_mutex, NULL);
  447. pthread_mutex_init(&pwl_mutex, NULL);
  448. pthread_mutex_init(&log_mutex, NULL);
  449. #ifndef NORADIUS
  450. pthread_mutex_init(&rad_mutex, NULL);
  451. #endif
  452. freeconf(&conf);
  453. res = readconfig(fp);
  454. conf.version++;
  455. if(res) RETURN(res);
  456. if(!writable)fclose(fp);
  457. #ifdef _WIN32
  458. #ifndef _WINCE
  459. if(service){
  460. SERVICE_TABLE_ENTRY ste[] =
  461. {
  462. { (LPSTR)conf.stringtable[1], (LPSERVICE_MAIN_FUNCTION)ServiceMain},
  463. { NULL, NULL }
  464. };
  465. if(!StartServiceCtrlDispatcher( ste ))cyclestep();
  466. }
  467. else
  468. #endif
  469. {
  470. cyclestep();
  471. }
  472. #else
  473. signal(SIGCONT, mysigpause);
  474. signal(SIGTERM, mysigterm);
  475. signal(SIGUSR1, mysigusr1);
  476. signal(SIGPIPE, SIG_IGN);
  477. cyclestep();
  478. #endif
  479. CLEARRETURN:
  480. return 0;
  481. }