3proxy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 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. break;
  54. case SERVICE_CONTROL_PAUSE:
  55. SetStatus( SERVICE_PAUSE_PENDING, 0, 1 );
  56. conf.paused++;
  57. SetStatus( SERVICE_PAUSED, 0, 0 );
  58. break;
  59. case SERVICE_CONTROL_CONTINUE:
  60. SetStatus( SERVICE_CONTINUE_PENDING, 0, 1 );
  61. conf.needreload = 1;
  62. SetStatus( SERVICE_RUNNING, 0, 0 );
  63. break;
  64. default: ;
  65. }
  66. }
  67. void __stdcall ServiceMain(int argc, char* argv[] )
  68. {
  69. hSrv = RegisterServiceCtrlHandler((LPCSTR)conf.stringtable[1], (LPHANDLER_FUNCTION)CommandHandler);
  70. if( hSrv == 0 ) return;
  71. SetStatus( SERVICE_START_PENDING, 0, 1 );
  72. SetStatus( SERVICE_RUNNING, 0, 0 );
  73. cyclestep();
  74. }
  75. #endif
  76. #else
  77. void mysigusr1 (int sig){
  78. conf.needreload = 1;
  79. }
  80. int even = 0;
  81. void mysigpause (int sig){
  82. conf.paused++;
  83. even = !even;
  84. if(!even){
  85. conf.needreload = 1;
  86. }
  87. }
  88. void mysigterm (int sig){
  89. conf.paused++;
  90. usleep(2000*SLEEPTIME);
  91. conf.timetoexit = 1;
  92. }
  93. #endif
  94. void dumpmem(void);
  95. struct schedule *schedule;
  96. int wday = 0;
  97. int timechanged (time_t oldtime, time_t newtime, ROTATION lt){
  98. struct tm tmold;
  99. struct tm *tm;
  100. tm = localtime(&oldtime);
  101. tmold = *tm;
  102. tm = localtime(&newtime);
  103. switch(lt){
  104. case MINUTELY:
  105. if(tm->tm_min != tmold.tm_min)return 1;
  106. break;
  107. case HOURLY:
  108. if(tm->tm_hour != tmold.tm_hour)return 1;
  109. break;
  110. case DAILY:
  111. if(tm->tm_yday != tmold.tm_yday)return 1;
  112. break;
  113. case MONTHLY:
  114. if(tm->tm_mon != tmold.tm_mon)return 1;
  115. break;
  116. case ANNUALLY:
  117. if(tm->tm_year != tmold.tm_year)return 1;
  118. break;
  119. case WEEKLY:
  120. if(((newtime - oldtime) > (60*60*24*7))
  121. || tm->tm_wday < tmold.tm_wday
  122. || (tm->tm_wday == tmold.tm_wday && (newtime - oldtime) > (60*60*24*6))
  123. )return 1;
  124. break;
  125. default:
  126. break;
  127. }
  128. return 0;
  129. }
  130. void doschedule(void){
  131. struct schedule *sched, *prevsched = NULL, *nextsched;
  132. int res;
  133. conf.time = time(0);
  134. for(sched=schedule; sched; sched=sched->next){
  135. if(conf.needreload || conf.timetoexit || (conf.time > sched->start_time && timechanged(sched->start_time, conf.time, sched->type))){
  136. sched->start_time = conf.time;
  137. nextsched = sched->next;
  138. res = (*sched->function)(sched->data);
  139. switch(res){
  140. case 1:
  141. if(prevsched) prevsched->next = nextsched;
  142. else schedule = nextsched;
  143. break;
  144. }
  145. }
  146. prevsched = sched;
  147. }
  148. }
  149. void dumpcounters(struct trafcount *tlin, int counterd){
  150. char tmpbuf[8192];
  151. struct trafcount *tl;
  152. if(counterd >= 0 && tlin) {
  153. conf.time = time(0);
  154. if(cheader.updated && conf.countertype && timechanged(cheader.updated, conf.time, conf.countertype)){
  155. FILE * cfp;
  156. cfp = fopen((char *)dologname(tmpbuf, sizeof(tmpbuf), (char *)conf.counterfile, NULL, conf.countertype, cheader.updated), "w");
  157. if(cfp){
  158. for(tl = tlin; cfp && tl; tl = tl->next){
  159. if(tl->type >= conf.countertype)
  160. fprintf(cfp, "%05d %020" PRIu64 "%s%s\n", tl->number, tl->traf64, tl->comment?" #" : "", tl->comment? tl->comment : "");
  161. }
  162. fclose(cfp);
  163. }
  164. }
  165. cheader.updated = conf.time;
  166. lseek(counterd, 0, SEEK_SET);
  167. (void)write(counterd, &cheader, sizeof(struct counter_header));
  168. for(tl=tlin; tl; tl = tl->next){
  169. if(tl->number){
  170. lseek(counterd,
  171. sizeof(struct counter_header) + (tl->number - 1) * sizeof(struct counter_record),
  172. SEEK_SET);
  173. crecord.traf64 = tl->traf64;
  174. crecord.cleared = tl->cleared;
  175. crecord.updated = tl->updated;
  176. (void)write(counterd, &crecord, sizeof(struct counter_record));
  177. }
  178. if(tl->type!=NEVER && timechanged(tl->cleared, conf.time, tl->type)){
  179. tl->cleared = conf.time;
  180. tl->traf64 = 0;
  181. }
  182. }
  183. }
  184. }
  185. void cyclestep(void){
  186. struct tm *tm;
  187. time_t minutecounter;
  188. minutecounter = time(0);
  189. for(;;){
  190. usleep(SLEEPTIME*999);
  191. flushlogs();
  192. conf.time = time(0);
  193. if(conf.needreload) {
  194. doschedule();
  195. reload();
  196. conf.needreload = 0;
  197. }
  198. doschedule();
  199. if(timechanged(minutecounter, conf.time, MINUTELY)) {
  200. struct filemon *fm;
  201. struct stat sb;
  202. for(fm=conf.fmon; fm; fm=fm->next){
  203. if(!stat(fm->path, &sb)){
  204. if(fm->sb.st_mtime != sb.st_mtime || fm->sb.st_size != sb.st_size){
  205. stat(fm->path, &fm->sb);
  206. conf.needreload = 1;
  207. }
  208. }
  209. }
  210. }
  211. if(timechanged(basetime, conf.time, DAILY)) {
  212. tm = localtime(&conf.time);
  213. wday = (1 << tm->tm_wday);
  214. tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
  215. basetime = mktime(tm);
  216. }
  217. if(conf.counterd >= 0 && conf.trafcounter) {
  218. if(timechanged(cheader.updated, conf.time, MINUTELY)){
  219. dumpcounters(conf.trafcounter, conf.counterd);
  220. }
  221. }
  222. if(conf.timetoexit){
  223. conf.paused++;
  224. doschedule();
  225. usleep(SLEEPTIME*999);
  226. usleep(SLEEPTIME*999);
  227. usleep(SLEEPTIME*999);
  228. return;
  229. }
  230. }
  231. }
  232. #define RETURN(x) {res = x; goto CLEARRETURN;}
  233. #ifndef _WINCE
  234. int main(int argc, char * argv[]) {
  235. #else
  236. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow){
  237. int argc;
  238. char ** argv;
  239. WNDCLASS wc;
  240. HWND hwnd = 0;
  241. #endif
  242. int res = 0;
  243. FILE * fp = NULL;
  244. #ifdef _WIN32
  245. char * arg;
  246. WSADATA wd;
  247. char tmpbuf[8192];
  248. WSAStartup(MAKEWORD( 1, 1 ), &wd);
  249. osv.dwOSVersionInfoSize = sizeof(osv);
  250. GetVersionEx(&osv);
  251. #endif
  252. #ifdef _WINCE
  253. argc = ceparseargs((char *)lpCmdLine);
  254. argv = ceargv;
  255. if(FindWindow(L"3proxy", L"3proxy")) return 0;
  256. ZeroMemory(&wc,sizeof(wc));
  257. wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  258. wc.hInstance=hInstance;
  259. wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  260. wc.lpfnWndProc=DefWindowProc;
  261. wc.style=CS_HREDRAW|CS_VREDRAW;
  262. wc.lpszClassName=L"3proxy";
  263. RegisterClass(&wc);
  264. hwnd = CreateWindowEx(0,L"3proxy",L"3proxy",WS_VISIBLE|WS_POPUP,0,0,0,0,0,0,hInstance,0);
  265. #endif
  266. conf.stringtable = strings;
  267. #ifdef _WIN32
  268. #ifndef _WINCE
  269. if((argc == 2 || argc == 3)&& !strcmp((char *)argv[1], "--install")) {
  270. sprintf((char *)tmpbuf, "%s will be installed and started.\n"
  271. "By clicking Yes you confirm you read and accepted License Agreement.\n"
  272. "You can use Administration/Services to control %s service.",
  273. conf.stringtable[1], conf.stringtable[2]);
  274. if(MessageBox(NULL, (LPCSTR)tmpbuf, (LPCSTR)conf.stringtable[2], MB_YESNO|MB_ICONASTERISK) != IDYES) return 1;
  275. *tmpbuf = '\"';
  276. if (!(res = SearchPath(NULL, argv[0], ".exe", 256, (char *)tmpbuf+1, (LPTSTR*)&arg))) {
  277. perror("Failed to find executable filename");
  278. RETURN(102);
  279. }
  280. strcat((char *)tmpbuf, "\" \"");
  281. if(!(res = GetFullPathName ((argc == 3)?argv[2]:(char*)DEFAULTCONFIG, 256, (char *)tmpbuf+res+4, (char **)&arg))){
  282. perror("Failed to find config filename");
  283. RETURN(103);
  284. }
  285. strcat((char *)tmpbuf, "\" --service");
  286. if(osv.dwPlatformId == VER_PLATFORM_WIN32_NT){
  287. SC_HANDLE sch;
  288. if(!(sch = OpenSCManager(NULL, NULL, GENERIC_WRITE|SERVICE_START ))){
  289. perror("Failed to open Service Manager");
  290. RETURN(101);
  291. }
  292. 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))){
  293. perror("Failed to create service");
  294. RETURN(103);
  295. }
  296. if (!StartService(sch, 0, NULL)) {
  297. perror("Failed to start service");
  298. RETURN(103);
  299. }
  300. }
  301. else {
  302. HKEY runsrv;
  303. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  304. "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  305. 0,
  306. KEY_ALL_ACCESS,
  307. &runsrv) != ERROR_SUCCESS){
  308. perror("Failed to open registry");
  309. RETURN(104);
  310. }
  311. if(RegSetValueEx( runsrv,
  312. (LPCSTR)conf.stringtable[1],
  313. 0,
  314. REG_EXPAND_SZ,
  315. (BYTE *)tmpbuf,
  316. (int)strlen((char *)tmpbuf)+1)!=ERROR_SUCCESS){
  317. perror("Failed to set registry value");
  318. RETURN(105);
  319. }
  320. }
  321. return 0;
  322. }
  323. if((argc == 2 || argc == 3)&& !strcmp((char *)argv[1], "--remove")) {
  324. if(osv.dwPlatformId == VER_PLATFORM_WIN32_NT){
  325. SC_HANDLE sch;
  326. if(!(sch = OpenSCManager(NULL, NULL, GENERIC_WRITE))){
  327. perror("Failed to open Service Manager\n");
  328. RETURN(106);
  329. }
  330. if (!(sch = OpenService(sch, (LPCSTR)conf.stringtable[1], DELETE))){
  331. perror("Failed to open service");
  332. RETURN(107);
  333. }
  334. if (!DeleteService(sch)){
  335. perror("Failed to delete service");
  336. RETURN(108);
  337. }
  338. }
  339. else {
  340. HKEY runsrv;
  341. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  342. "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  343. 0,
  344. KEY_ALL_ACCESS,
  345. &runsrv) != ERROR_SUCCESS){
  346. perror("Failed to open registry");
  347. RETURN(109);
  348. }
  349. if(RegDeleteValue(runsrv, (LPCSTR)conf.stringtable[1]) != ERROR_SUCCESS){
  350. perror("Failed to clear registry");
  351. RETURN(110);
  352. }
  353. }
  354. RETURN(0);
  355. }
  356. if(argc==3 && !strcmp(argv[2], "--service")){
  357. service = 1;
  358. argc = 2;
  359. }
  360. #endif
  361. #endif
  362. conf.conffile = mystrdup((argc==2)?argv[1]:(char*)DEFAULTCONFIG);
  363. if(conf.conffile && *conf.conffile != '-') {
  364. fp = confopen();
  365. #ifndef _WIN32
  366. if(!fp) fp = stdin;
  367. #endif
  368. }
  369. if(argc > 2 || !(fp)) {
  370. fprintf(stderr, "Usage: %s [conffile]\n", argv[0]);
  371. #ifdef _WIN32
  372. fprintf(stderr, "\n\t%s --install [conffile]\n\tto install as service\n"
  373. "\n\t%s --remove\n\tto remove service\n", argv[0], argv[0]);
  374. #else
  375. fprintf(stderr, "\n if conffile is missing, configuration is expected from stdin\n");
  376. #endif
  377. fprintf(stderr, "available socket options:\n\t%s\n", printopts("\n\t"));
  378. fprintf(stderr, "\n%s %s\n%s\n", conf.stringtable[2], conf.stringtable[3], copyright);
  379. return 1;
  380. }
  381. pthread_mutex_init(&config_mutex, NULL);
  382. pthread_mutex_init(&bandlim_mutex, NULL);
  383. pthread_mutex_init(&connlim_mutex, NULL);
  384. pthread_mutex_init(&hash_mutex, NULL);
  385. pthread_mutex_init(&tc_mutex, NULL);
  386. pthread_mutex_init(&pwl_mutex, NULL);
  387. #ifndef NORADIUS
  388. pthread_mutex_init(&rad_mutex, NULL);
  389. #endif
  390. initlog();
  391. freeconf(&conf);
  392. res = readconfig(fp);
  393. conf.version++;
  394. if(res) RETURN(res);
  395. if(!writable)fclose(fp);
  396. #ifdef _WIN32
  397. #ifndef _WINCE
  398. if(service){
  399. SERVICE_TABLE_ENTRY ste[] =
  400. {
  401. { (LPSTR)conf.stringtable[1], (LPSERVICE_MAIN_FUNCTION)ServiceMain},
  402. { NULL, NULL }
  403. };
  404. if(!StartServiceCtrlDispatcher( ste ))cyclestep();
  405. }
  406. else
  407. #endif
  408. {
  409. cyclestep();
  410. }
  411. #else
  412. signal(SIGCONT, mysigpause);
  413. signal(SIGTERM, mysigterm);
  414. signal(SIGUSR1, mysigusr1);
  415. signal(SIGPIPE, SIG_IGN);
  416. cyclestep();
  417. #endif
  418. CLEARRETURN:
  419. return 0;
  420. }