⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcc.c

📁 一个不错的服务代理器
💻 C
📖 第 1 页 / 共 2 页
字号:
				 * buffer before we hit the end of the header.				 *				 * Since we have to wait for more from the				 * server before we can parse the headers				 * we just continue here.				 */				if(p) continue;				/* we have now received the entire header.				 * filter it and send the result to the client				 */				hdr = sed(					server_patterns,					add_server_headers,					csp);				n   = strlen(hdr);				/* write the server's (modified) header to				 * the client (along with anything else that				 * may be in the buffer)				 */				if((write_socket(csp->cfd, hdr, n) != n)				|| (flush_socket(csp->cfd, csp   ) <  0)) {					if(DEBUG(CON)) {						fprintf(logfp,							"%s: write header to client failed: ",								prog);						fperror(logfp, "");					}					/* the write failed, so don't bother					 * mentioning it to the client...					 * it probably can't hear us anyway.					 */					freez(hdr);					return;				}				/* we're finished with the server's header */				freez(hdr);				server_body = 1;			}			continue;		}		return; /* huh? we should never get here */	}}voidserve(struct client_state *csp){	chat(csp);	close_socket(csp->cfd);	if(csp->sfd >= 0) {		close_socket(csp->sfd);	}	csp->active = 0;}#ifdef __BEOS__int32server_thread(void *data){	serve((struct client_state *) data);	return 0;}#endifintmain(int argc, char *argv[]){	char buf[BUFSIZ];	int cfd, bfd;	char *p, *q;	extern char *optarg;	extern int optind;	struct client_state *csp;	char *default_configfile = NULL;	char *configfile = NULL;	FILE *configfp = NULL;	int err = 0;	prog = argv[0];	logfp = stdout;	init_proxy_args(argc, argv);	cfd  = -1;#ifdef _WIN32	default_configfile = "junkbstr.ini";#endif	configfile = default_configfile;	if(argc > 1) {		configfile = argv[1];	}	if(configfile) {		if((configfp = fopen(configfile, "r")) == NULL) {			if(configfile != default_configfile) {				fprintf(logfp,					"%s: can't open configuration file '%s': ",						prog, configfile);				fperror(logfp, "");				exit(1);			}		}	}	if(configfp) {		int line_num = 0;		while(fgets(buf, sizeof(buf), configfp)) {			char cmd[BUFSIZ];			char arg[BUFSIZ];			char tmp[BUFSIZ];			line_num++;			strcpy(tmp, buf);			if((p = strpbrk(tmp, "#\r\n"))) *p = '\0';			p = tmp;			/* leading skip whitespace */			while(*p && ((*p == ' ') || (*p == '\t'))) p++;			q = cmd;			while(*p && (*p != ' ') && (*p != '\t')) *q++ = *p++;			*q = '\0';			while(*p && ((*p == ' ') || (*p == '\t'))) p++;			strcpy(arg, p);			p = arg + strlen(arg) - 1;			/* ignore trailing whitespace */			while(*p && ((*p == ' ') || (*p == '\t'))) *p-- = '\0';			if(*cmd == '\0') continue;			/* insure the command field is lower case */			for(p=cmd; *p; p++) if(isupper(*p)) *p = tolower(*p);			savearg(cmd, arg);			if(strcmp(cmd, "trustfile") == 0) {				trustfile = strdup(arg);				continue;			}			if(strcmp(cmd, "trust_info_url") == 0) {				enlist(trust_info, arg);				continue;			}			if(strcmp(cmd, "debug") == 0) {				debug |= atoi(arg);				continue;			}			if(strcmp(cmd, "add-forwarded-header") == 0) {				add_forwarded = 1;				continue;			}			if(strcmp(cmd, "single-threaded") == 0) {				multi_threaded = 0;				continue;			}			if(strcmp(cmd, "suppress-vanilla-wafer") == 0) {				suppress_vanilla_wafer = 1;				continue;			}			if(strcmp(cmd, "wafer") == 0) {				enlist(wafer_list, arg);				continue;			}			if(strcmp(cmd, "add-header") == 0) {				enlist(xtra_list,  arg);				continue;			}			if(strcmp(cmd, "cookiefile") == 0) {				cookiefile = strdup(arg);				continue;			}			if(strcmp(cmd, "logfile") == 0) {				logfile = strdup(arg);				continue;			}			if(strcmp(cmd, "blockfile") == 0) {				blockfile = strdup(arg);				continue;			}			if(strcmp(cmd, "jarfile") == 0) {				jarfile = strdup(arg);				continue;			}			if(strcmp(cmd, "listen-address") == 0) {				haddr = strdup(arg);				continue;			}			if(strcmp(cmd, "forwardfile") == 0) {				forwardfile = strdup(arg);				continue;			}			if(strcmp(cmd, "aclfile") == 0) {				aclfile = strdup(arg);				continue;			}			if(strcmp(cmd, "user-agent") == 0) {				uagent = strdup(arg);				continue;			}			if((strcmp(cmd, "referrer") == 0)			|| (strcmp(cmd, "referer" ) == 0)) {				referrer = strdup(arg);				continue;			}			if(strcmp(cmd, "from") == 0) {				from = strdup(arg);				continue;			}			if(strcmp(cmd, "hide-console") == 0) {				hideConsole = 1;				continue;			}			fprintf(logfp,				"%s: unrecognized directive "				"in configuration file "				"at line number %d:\n%s",				prog, line_num, buf);			err = 1;		}		fclose(configfp);	}	if(err) exit(1);#ifdef _WIN32	InitWin32();#endif	if(logfile) {		FILE *tlog = fopen(logfile, "a");		if(tlog == NULL) {			fprintf(logfp, "%s: can't open logfile '%s': ",				prog, logfile);			fperror(logfp, "");			err = 1;		}		logfp = tlog;	}	setbuf(logfp, NULL);	if(cookiefile)   add_loader(load_cookiefile);	if(blockfile)    add_loader(load_blockfile);	if(trustfile)    add_loader(load_trustfile);	if(forwardfile)  add_loader(load_forwardfile);	if(aclfile)      add_loader(load_aclfile);	if(jarfile) {		jar = fopen(jarfile, "a");		if(jar == NULL) {			fprintf(logfp, "%s: can't open jarfile '%s': ",				prog, jarfile);			fperror(logfp, "");			err = 1;		}		setbuf(jar, NULL);	}	if(haddr) {		if((p = strchr(haddr, ':'))) {			*p++ = '\0';			if(*p) hport = atoi(p);		}		if(hport <= 0) {			*--p = ':' ;			fprintf(logfp, "%s: invalid bind port spec %s",				prog, haddr);			err = 1;		}		if(*haddr == '\0') haddr = NULL;	}	if(run_loader(NULL)) err = 1;	if(err) exit(1);	/* if we're logging cookies in a cookie jar,	 * and the user has not supplied any wafers,	 * and the user has not told us to suppress the vanilla wafer,	 * then send the vanilla wafer.	 */	if((jarfile != NULL)	&& (wafer_list->next == NULL)	&& (suppress_vanilla_wafer == 0)) {		enlist(wafer_list, VANILLA_WAFER);	}	if(DEBUG(CON)) {		fprintf(logfp, "%s: bind (%s, %d)\n",			prog, haddr ? haddr : "INADDR_ANY", hport);	}	bfd = bind_port(haddr, hport);	if(bfd < 0) {		fprintf(logfp, "%s: can't bind %s:%d: ",			prog, haddr ? haddr : "INADDR_ANY", hport);		fperror(logfp, "");		fprintf(logfp,			"There may be another junkbuster or some other "			"proxy running on port %d\n", hport);		err = 1;	}	if(err) exit(1);	end_proxy_args();#ifndef _WIN32	signal(SIGPIPE, SIG_IGN);	signal(SIGCHLD, SIG_IGN);#endif#ifdef _WIN32{	/* print a verbose messages about FAQ's and such */	extern char *win32_blurb;	if(logfp == stdout) fprintf(logfp, win32_blurb);}#endif	for(;;) {#if !defined(_WIN32) && !defined(__BEOS__)		while(waitpid(-1, NULL, WNOHANG) > 0) {			/* zombie children */		}#endif		sweep();		if(DEBUG(CON)) {			fprintf(logfp, "%s: accept connection ... ", prog);		}		cfd = accept_connection(bfd);		if(cfd < 0) {			if(DEBUG(CON)) {				fprintf(logfp, "%s: accept failed: ", prog);				fperror(logfp, "");			}			continue;		} else {			if(DEBUG(CON)) {				fprintf(logfp, "OK\n");			}		}		csp = (struct client_state *) malloc(sizeof(*csp));		if(csp == NULL) {			fprintf(logfp, "%s: malloc(%d) for csp failed: ", prog, sizeof(*csp));			fperror(logfp, "");			close_socket(cfd);			continue;		}		memset(csp, '\0', sizeof(*csp));		csp->active = 1;		csp->cfd    = cfd;		csp->sfd    =  -1;		csp->ip_addr_str  = remote_ip_str;		csp->ip_addr_long = remote_ip_long;		/* add it to the list of clients */		csp->next = clients->next;		clients->next = csp;		if(run_loader(csp)) {			fprintf(logfp, "%s: a loader failed - must exit\n", prog);			exit(1);		}		if(multi_threaded) {			int child_id;/* this is a switch() statment in the C preprocessor - ugh */#undef SELECTED_ONE_OPTION#if defined(_WIN32) && !defined(SELECTED_ONE_OPTION)#define SELECTED_ONE_OPTION			child_id = _beginthread(					(void*)serve,					64 * 1024,					csp);#endif#if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)#define SELECTED_ONE_OPTION			{				thread_id tid = spawn_thread					(server_thread, "server", B_NORMAL_PRIORITY, csp);				if ((tid >= 0) && (resume_thread(tid) == B_OK)) {					child_id = (int) tid;				} else {					child_id = -1;				}			}#endif#if !defined(SELECTED_ONE_OPTION)			child_id = fork();#endif#undef SELECTED_ONE_OPTION/* end of cpp switch() */			if(child_id < 0) {	/* failed */				fprintf(logfp, "%s: can't fork: ", prog);				fperror(logfp, "");				sprintf(buf , "%s: can't fork: errno = %d",					prog, errno);				write_socket(csp->cfd, buf, strlen(buf));				close_socket(csp->cfd);				csp->active = 0;				sleep(5);				continue;			}#if !defined(_WIN32) && !defined(__BEOS__)			/* This block is only needed when using fork().			 * When using threads, the server thread was			 * created and run by the call to _beginthread().			 */			if(child_id == 0) {	/* child */				serve(csp);				_exit(0);			} else {		/* parent */				/* in a fork()'d environment, the parent's				 * copy of the client socket and the CSP				 * are not used.				 */				close_socket(csp->cfd);				csp->active = 0;			}#endif		} else {			serve(csp);		}	}	/* NOTREACHED */}char *safe_strerror(int err){	char buf[BUFSIZ];	char *s = NULL;#ifndef   NOSTRERROR	s = strerror(err);#endif /* NOSTRERROR */	if(s == NULL) {		sprintf(buf, "(errno = %d)", err);		s = buf;	}	return(strdup(s));}voidfperror(FILE *fp, char *str){	char *eno = safe_strerror(errno);	if(str && *str) {		fprintf(fp, "%s: %s\n", str, eno);	} else {		fprintf(fp, "%s\n", eno);	}	freez(eno);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -