3proxy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.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.paused++;
  50. conf.timetoexit = 1;
  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(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. memcpy(&tmold, tm, sizeof(tmold));
  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. struct trafcount *tl;
  162. if(counterd >= 0 && tlin) {
  163. conf.time = time(0);
  164. if(cheader.updated && conf.countertype && timechanged(cheader.updated, conf.time, conf.countertype)){
  165. FILE * cfp;
  166. cfp = fopen((char *)dologname(tmpbuf, (unsigned char *)conf.counterfile, NULL, conf.countertype, cheader.updated), "w");
  167. if(cfp){
  168. for(tl = tlin; cfp && tl; tl = tl->next){
  169. if(tl->type >= conf.countertype)
  170. fprintf(cfp, "%05d %020"PRINTF_INT64_MODIFIER"u%s%s\n", tl->number, tl->traf64, tl->comment?" #" : "", tl->comment? tl->comment : "");
  171. }
  172. fclose(cfp);
  173. }
  174. }
  175. cheader.updated = conf.time;
  176. lseek(counterd, 0, SEEK_SET);
  177. write(counterd, &cheader, sizeof(struct counter_header));
  178. for(tl=tlin; tl; tl = tl->next){
  179. if(tl->number){
  180. lseek(counterd,
  181. sizeof(struct counter_header) + (tl->number - 1) * sizeof(struct counter_record),
  182. SEEK_SET);
  183. crecord.traf64 = tl->traf64;
  184. crecord.cleared = tl->cleared;
  185. crecord.updated = tl->updated;
  186. write(counterd, &crecord, sizeof(struct counter_record));
  187. }
  188. if(tl->type!=NEVER && timechanged(tl->cleared, conf.time, tl->type)){
  189. tl->cleared = conf.time;
  190. tl->traf64 = 0;
  191. }
  192. }
  193. }
  194. }
  195. void cyclestep(void){
  196. struct tm *tm;
  197. time_t minutecounter;
  198. minutecounter = time(0);
  199. for(;;){
  200. usleep(SLEEPTIME*999);
  201. conf.time = time(0);
  202. if(conf.needreload) {
  203. doschedule();
  204. reload();
  205. conf.needreload = 0;
  206. }
  207. doschedule();
  208. if(conf.stdlog)fflush(conf.stdlog);
  209. if(timechanged(minutecounter, conf.time, MINUTELY)) {
  210. struct filemon *fm;
  211. struct stat sb;
  212. for(fm=conf.fmon; fm; fm=fm->next){
  213. if(!stat(fm->path, &sb)){
  214. if(fm->sb.st_mtime != sb.st_mtime || fm->sb.st_size != sb.st_size){
  215. stat(fm->path, &fm->sb);
  216. conf.needreload = 1;
  217. }
  218. }
  219. }
  220. }
  221. if(timechanged(basetime, conf.time, DAILY)) {
  222. tm = localtime(&conf.time);
  223. wday = (1 << tm->tm_wday);
  224. tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
  225. basetime = mktime(tm);
  226. }
  227. if(conf.logname) {
  228. if(timechanged(conf.logtime, conf.time, conf.logtype)) {
  229. FILE *fp;
  230. fp = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.time), "a");
  231. if (fp) {
  232. pthread_mutex_lock(&log_mutex);
  233. fclose(conf.stdlog);
  234. conf.stdlog = fp;
  235. pthread_mutex_unlock(&log_mutex);
  236. }
  237. fseek(stdout, 0L, SEEK_END);
  238. usleep(SLEEPTIME);
  239. conf.logtime = conf.time;
  240. if(conf.logtype != NONE && conf.rotate) {
  241. int t;
  242. t = 1;
  243. switch(conf.logtype){
  244. case ANNUALLY:
  245. t = t * 12;
  246. case MONTHLY:
  247. t = t * 4;
  248. case WEEKLY:
  249. t = t * 7;
  250. case DAILY:
  251. t = t * 24;
  252. case HOURLY:
  253. t = t * 60;
  254. case MINUTELY:
  255. t = t * 60;
  256. default:
  257. break;
  258. }
  259. dologname (tmpbuf, conf.logname, (conf.archiver)?conf.archiver[1]:NULL, conf.logtype, (conf.logtime - t * conf.rotate));
  260. remove ((char *) tmpbuf);
  261. if(conf.archiver) {
  262. int i;
  263. *tmpbuf = 0;
  264. for(i = 2; i < conf.archiverc && strlen((char *)tmpbuf) < 512; i++){
  265. strcat((char *)tmpbuf, " ");
  266. if(!strcmp((char *)conf.archiver[i], "%A")){
  267. strcat((char *)tmpbuf, "\"");
  268. dologname (tmpbuf + strlen((char *)tmpbuf), conf.logname, conf.archiver[1], conf.logtype, (conf.logtime - t));
  269. strcat((char *)tmpbuf, "\"");
  270. }
  271. else if(!strcmp((char *)conf.archiver[i], "%F")){
  272. strcat((char *)tmpbuf, "\"");
  273. dologname (tmpbuf+strlen((char *)tmpbuf), conf.logname, NULL, conf.logtype, (conf.logtime-t));
  274. strcat((char *)tmpbuf, "\"");
  275. }
  276. else
  277. strcat((char *)tmpbuf, (char *)conf.archiver[i]);
  278. }
  279. system((char *)tmpbuf+1);
  280. }
  281. }
  282. }
  283. }
  284. if(conf.counterd >= 0 && conf.trafcounter) {
  285. if(timechanged(cheader.updated, conf.time, MINUTELY)){
  286. dumpcounters(conf.trafcounter, conf.counterd);
  287. }
  288. }
  289. if(conf.timetoexit){
  290. conf.paused++;
  291. doschedule();
  292. usleep(SLEEPTIME*999);
  293. usleep(SLEEPTIME*999);
  294. usleep(SLEEPTIME*999);
  295. return;
  296. }
  297. }
  298. }
  299. #define RETURN(x) {res = x; goto CLEARRETURN;}
  300. #ifndef _WINCE
  301. int main(int argc, char * argv[]) {
  302. #else
  303. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow){
  304. int argc;
  305. char ** argv;
  306. WNDCLASS wc;
  307. HWND hwnd = 0;
  308. #endif
  309. int res = 0;
  310. FILE * fp = NULL;
  311. #ifdef _WIN32
  312. unsigned char * arg;
  313. WSADATA wd;
  314. WSAStartup(MAKEWORD( 1, 1 ), &wd);
  315. osv.dwOSVersionInfoSize = sizeof(osv);
  316. GetVersionEx(&osv);
  317. #endif
  318. #ifdef _WINCE
  319. argc = ceparseargs((char *)lpCmdLine);
  320. argv = ceargv;
  321. if(FindWindow(L"3proxy", L"3proxy")) return 0;
  322. ZeroMemory(&wc,sizeof(wc));
  323. wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  324. wc.hInstance=hInstance;
  325. wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  326. wc.lpfnWndProc=DefWindowProc;
  327. wc.style=CS_HREDRAW|CS_VREDRAW;
  328. wc.lpszClassName=L"3proxy";
  329. RegisterClass(&wc);
  330. hwnd = CreateWindowEx(0,L"3proxy",L"3proxy",WS_VISIBLE|WS_POPUP,0,0,0,0,0,0,hInstance,0);
  331. #endif
  332. conf.stringtable = strings;
  333. #ifdef _WIN32
  334. #ifndef _WINCE
  335. if((argc == 2 || argc == 3)&& !strcmp((char *)argv[1], "--install")) {
  336. sprintf((char *)tmpbuf, "%s will be installed and started.\n"
  337. "By clicking Yes you confirm you read and accepted License Agreement.\n"
  338. "You can use Administration/Services to control %s service.",
  339. conf.stringtable[1], conf.stringtable[2]);
  340. if(MessageBox(NULL, (char *)tmpbuf, conf.stringtable[2], MB_YESNO|MB_ICONASTERISK) != IDYES) return 1;
  341. *tmpbuf = '\"';
  342. if (!(res = SearchPath(NULL, argv[0], ".exe", 256, (char *)tmpbuf+1, (LPTSTR*)&arg))) {
  343. perror("Failed to find executable filename");
  344. RETURN(102);
  345. }
  346. strcat((char *)tmpbuf, "\" \"");
  347. if(!(res = GetFullPathName ((argc == 3)?argv[2]:(char*)DEFAULTCONFIG, 256, (char *)tmpbuf+res+4, (char **)&arg))){
  348. perror("Failed to find config filename");
  349. RETURN(103);
  350. }
  351. strcat((char *)tmpbuf, "\" --service");
  352. if(osv.dwPlatformId == VER_PLATFORM_WIN32_NT){
  353. SC_HANDLE sch;
  354. if(!(sch = OpenSCManager(NULL, NULL, GENERIC_WRITE|SERVICE_START ))){
  355. perror("Failed to open Service Manager");
  356. RETURN(101);
  357. }
  358. if (!(sch = CreateService(sch, conf.stringtable[1], conf.stringtable[2], GENERIC_EXECUTE, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, (char *)tmpbuf, NULL, NULL, NULL, NULL, NULL))){
  359. perror("Failed to create service");
  360. RETURN(103);
  361. }
  362. if (!StartService(sch, 0, NULL)) {
  363. perror("Failed to start service");
  364. RETURN(103);
  365. }
  366. }
  367. else {
  368. HKEY runsrv;
  369. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  370. "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  371. 0,
  372. KEY_ALL_ACCESS,
  373. &runsrv) != ERROR_SUCCESS){
  374. perror("Failed to open registry");
  375. RETURN(104);
  376. }
  377. if(RegSetValueEx( runsrv,
  378. conf.stringtable[1],
  379. 0,
  380. REG_EXPAND_SZ,
  381. (char *)tmpbuf,
  382. (int)strlen((char *)tmpbuf)+1)!=ERROR_SUCCESS){
  383. perror("Failed to set registry value");
  384. RETURN(105);
  385. }
  386. }
  387. return 0;
  388. }
  389. if((argc == 2 || argc == 3)&& !strcmp((char *)argv[1], "--remove")) {
  390. if(osv.dwPlatformId == VER_PLATFORM_WIN32_NT){
  391. SC_HANDLE sch;
  392. if(!(sch = OpenSCManager(NULL, NULL, GENERIC_WRITE))){
  393. perror("Failed to open Service Manager\n");
  394. RETURN(106);
  395. }
  396. if (!(sch = OpenService(sch, conf.stringtable[1], DELETE))){
  397. perror("Failed to open service");
  398. RETURN(107);
  399. }
  400. if (!DeleteService(sch)){
  401. perror("Failed to delete service");
  402. RETURN(108);
  403. }
  404. }
  405. else {
  406. HKEY runsrv;
  407. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  408. "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  409. 0,
  410. KEY_ALL_ACCESS,
  411. &runsrv) != ERROR_SUCCESS){
  412. perror("Failed to open registry");
  413. RETURN(109);
  414. }
  415. if(RegDeleteValue(runsrv, conf.stringtable[1]) != ERROR_SUCCESS){
  416. perror("Failed to clear registry");
  417. RETURN(110);
  418. }
  419. }
  420. RETURN(0);
  421. }
  422. if(argc==3 && !strcmp(argv[2], "--service")){
  423. service = 1;
  424. argc = 2;
  425. }
  426. #endif
  427. #endif
  428. conf.conffile = mystrdup((argc==2)?argv[1]:(char*)DEFAULTCONFIG);
  429. if(conf.conffile && *conf.conffile != '-') {
  430. fp = confopen();
  431. #ifndef _WIN32
  432. if(!fp) fp = stdin;
  433. #endif
  434. }
  435. if(argc > 2 || !(fp)) {
  436. fprintf(stderr, "Usage: %s [conffile]\n", argv[0]);
  437. #ifdef _WIN32
  438. fprintf(stderr, "\n\t%s --install [conffile]\n\tto install as service\n"
  439. "\n\t%s --remove\n\tto remove service\n", argv[0], argv[0]);
  440. #else
  441. fprintf(stderr, "\n if conffile is missing, configuration is expected from stdin\n");
  442. #endif
  443. fprintf(stderr, "\n%s %s\n%s\n", conf.stringtable[2], conf.stringtable[3], copyright);
  444. return 1;
  445. }
  446. pthread_mutex_init(&log_mutex, NULL);
  447. logmutexinit = 1;
  448. pthread_mutex_init(&config_mutex, NULL);
  449. pthread_mutex_init(&bandlim_mutex, NULL);
  450. pthread_mutex_init(&hash_mutex, NULL);
  451. pthread_mutex_init(&tc_mutex, NULL);
  452. pthread_mutex_init(&pwl_mutex, NULL);
  453. freeconf(&conf);
  454. res = readconfig(fp);
  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. { 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. }