📄 connectionhandler.cpp
字号:
// if we don't do this the browsers complainstd::string ConnectionHandler::miniURLEncode(std::string s) { std::string encoded; char* buf = new char[16]; // way longer than needed unsigned char c; for(int i=0; i < (signed)s.length(); i++) { c = s[i]; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { // allowed characters in a url that have non special meaning encoded += c; continue; } sprintf(buf, "%x", c); encoded += "%"; encoded += buf; } delete[] buf; return encoded;}void ConnectionHandler::doTheLogMan(std::string who, std::string from, std::string where, std::string what, std::string how, int size, struct timeval *thestart, bool cachehit, int code, std::string mimetype) { std::string logline,year,month,day,hour,min,sec,when,ssize,duration, utime, hitmiss, hier; struct timeval theend; struct timezone notused; gettimeofday(&theend, ¬used); long durationsecs, durationusecs; durationsecs = theend.tv_sec - (*thestart).tv_sec; durationusecs = theend.tv_usec - (*thestart).tv_usec; durationusecs = (durationusecs / 1000) + durationsecs * 1000; String temp = String((int)durationusecs); while (temp.length() < 6) { temp = " " + temp; } duration = temp.toCharArray(); temp = String((int)(theend.tv_usec / 1000)); while (temp.length() < 3) { temp = "0" + temp; } if (temp.length() > 3) { temp = "999"; } utime = temp.toCharArray(); utime = "." + utime; utime = String((int)theend.tv_sec).toCharArray() + utime; if (what.length() > 3000) { what = what.substr(0, 2999); } if (code == 403) { hitmiss = "TCP_DENIED/403"; } else { if (cachehit) { hitmiss = "TCP_HIT/"; hitmiss += String((int)code).toCharArray(); } else { hitmiss = "TCP_MISS/"; hitmiss += String((int)code).toCharArray(); } } hier = "DEFAULT_PARENT/"; hier += o.proxy_ip; time_t tnow; // to hold the result from time() struct tm *tmnow; // to hold the result from localtime() time(&tnow); // get the time after the lock so all entries in order tmnow = localtime(&tnow); // convert to local time (BST, etc) year = String(tmnow->tm_year + 1900).toCharArray(); month = String(tmnow->tm_mon + 1).toCharArray(); day = String(tmnow->tm_mday).toCharArray(); hour = String(tmnow->tm_hour).toCharArray(); temp = String(tmnow->tm_min); if (temp.length() == 1) { temp = "0" + temp;} min = temp.toCharArray(); temp = String(tmnow->tm_sec); if (temp.length() == 1) { temp = "0" + temp;} sec = temp.toCharArray(); ssize = String(size).toCharArray(); when = year + "." + month + "." + day + " " + hour + ":" + min + ":" + sec; switch(o.log_file_format){ case 4: logline = when +"\t"+ who + "\t" + from + "\t" + where + "\t" + what + "\t" + how + "\t" + ssize + "\n"; break; case 3: logline = utime + " " + duration + " " + from + " " + hitmiss + " " + ssize + " " + how + " " + where + " " + who + " " + hier + " " + mimetype + "\n"; break; case 2: logline = "\"" + when +"\",\""+ who + "\",\"" + from + "\",\"" + where + "\",\"" + what + "\",\"" + how + "\",\"" + ssize + "\"\n"; break; default: logline = when +" "+ who + " " + from + " " + where + " " + what + " " + how + " " + ssize + "\n"; } UDSocket ipcsock; if (ipcsock.getFD() < 0) { syslog(LOG_ERR, "%s","Error creating ipc socket to log"); return; } if (ipcsock.connect((char*) o.ipc_filename.c_str()) < 0) { // connect to dedicated logging proc syslog(LOG_ERR, "%s","Error connecting via ipc to log"); ipcsock.close(); return; } ipcsock.writeString(logline.c_str()); ipcsock.close();}void ConnectionHandler::decideHowToLog(std::string who, std::string from, std::string where, unsigned int port, std::string what, std::string how, int size, int loglevel, bool isnaughty, bool isexception, int logexceptions, bool istext, struct timeval *thestart, bool cachehit, int code, std::string mimetype) { if (loglevel == 0) { return; } if (port != 0 && port != 80) { String newwhere = where.c_str(); if (newwhere.after("://").contains("/")) { String proto, host, path; proto = newwhere.before("://"); host = newwhere.after("://"); path = host.after("/"); host = host.before("/"); newwhere = proto; newwhere += "://"; newwhere += host; newwhere += ":"; newwhere += String((int)port); newwhere += "/"; newwhere += path; where = newwhere.toCharArray(); } else { where += ":"; where += String((int)port).toCharArray(); } } if (isnaughty) { what = "*DENIED* " + what; // make it stand out in the logs and also // more easily findable with a search } if (isexception) { if (logexceptions == 1) { what = "*EXCEPTION* " + what; } else { what = ""; } } if ((isexception && logexceptions == 1) || isnaughty || loglevel == 3 || (loglevel == 2 && istext)) { doTheLogMan(who, from, where, what, how, size, thestart, cachehit, code, mimetype); }}bool ConnectionHandler::wasClean(String url) { url = url.after("://"); UDSocket ipcsock; if (ipcsock.getFD() < 0) { syslog(LOG_ERR, "%s","Error creating ipc socket to url cache"); return false; } if (ipcsock.connect((char*) o.urlipc_filename.c_str()) < 0) { // conn to dedicated url cach proc syslog(LOG_ERR, "%s","Error connecting via ipc to url cache"); ipcsock.close(); return false; } url += "\n"; char* reply = new char[8]; #ifdef DGDEBUG std::cout << "sending clean request:" << url.toCharArray() << std::endl; #endif try { ipcsock.writeString(url.toCharArray()); // throws on err } catch (exception& e) { #ifdef DGDEBUG std::cerr << "Exception writing to url cache" << std::endl; std::cerr << e.what() << std::endl; #endif syslog(LOG_ERR, "%s","Exception writing to url cache"); syslog(LOG_ERR, "%s", e.what()); } try { ipcsock.getline(reply, 7, 6); // throws on err } catch (exception& e) { #ifdef DGDEBUG std::cerr << "Exception reading from url cache" << std::endl; std::cerr << e.what() << std::endl; #endif syslog(LOG_ERR, "%s","Exception reading from url cache"); syslog(LOG_ERR, "%s", e.what()); } ipcsock.close(); if (reply[0] == 'Y') { delete[] reply; return true; } delete[] reply; return false;}void ConnectionHandler::addToClean(String url) { url = url.after("://"); UDSocket ipcsock; if (ipcsock.getFD() < 0) { syslog(LOG_ERR, "%s","Error creating ipc socket to url cache"); return; } if (ipcsock.connect((char*) o.urlipc_filename.c_str()) < 0) { // conn to dedicated url cach proc syslog(LOG_ERR, "%s","Error connecting via ipc to url cache"); #ifdef DGDEBUG std::cout << "Error connecting via ipc to url cache" << std::endl; #endif return; } url += "\n"; url = "A " + url; try { ipcsock.writeString(url.toCharArray()); // throws on err } catch (exception& e) { #ifdef DGDEBUG std::cerr << "Exception adding to url cache" << std::endl; std::cerr << e.what() << std::endl; #endif syslog(LOG_ERR, "%s","Exception adding to url cache"); syslog(LOG_ERR, "%s", e.what()); } ipcsock.close();}void ConnectionHandler::requestChecks(HTTPHeader *header, NaughtyFilter *checkme, String *urld, std::string *clientip, std::string *clientuser, int filtergroup, bool *ispostblock) { char *i; int j; String temp; temp = (*urld); bool igsl = (*o.fg[filtergroup]).inGreySiteList(temp); bool igul = (*o.fg[filtergroup]).inGreyURLList(temp); if ((*o.fg[filtergroup]).blanketblock == 1 && !igsl && !igul) { (*checkme).isItNaughty = true; (*checkme).whatIsNaughty = o.language_list.getTranslation(502); // Blanket Block is active and that site is not on the white list. (*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; } else if (o.inBannedIPList(clientip)) { (*checkme).isItNaughty = true; (*checkme).whatIsNaughtyLog = o.language_list.getTranslation(100); // Your IP address is not allowed to web browse: (*checkme).whatIsNaughtyLog += (*clientip); (*checkme).whatIsNaughty = o.language_list.getTranslation(101); // Your IP address is not allowed to web browse. } else if (o.inBannedUserList(clientuser)) { (*checkme).isItNaughty = true; (*checkme).whatIsNaughtyLog = o.language_list.getTranslation(102); // Your username is not allowed to web browse: (*checkme).whatIsNaughtyLog += (*clientuser); (*checkme).whatIsNaughty = (*checkme).whatIsNaughtyLog; } if (!(*checkme).isItNaughty && (*o.fg[filtergroup]).blanketblock == 0) { if ((*o.fg[filtergroup]).blanket_ip_block == 1 && isIPHostnameStrip(temp)) { (*checkme).isItNaughty = true; (*checkme).whatIsNaughty = o.language_list.getTranslation(502); //Blanket IP Block is active and that address is an IP only address. (*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; } else { if (!igsl && !igul && ((i = (*o.fg[filtergroup]).inBannedSiteList(temp)) != NULL)) { (*checkme).whatIsNaughty = o.language_list.getTranslation(500); // banned site (*checkme).whatIsNaughty += i; (*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; (*checkme).isItNaughty = true; } } } if (!(*checkme).isItNaughty) { if (!igsl && !igul && ((i = (*o.fg[filtergroup]).inBannedURLList(temp)) != NULL)) { (*checkme).whatIsNaughty = o.language_list.getTranslation(501); // Banned URL: (*checkme).whatIsNaughty += i; (*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; (*checkme).isItNaughty = true; } else if (!igsl && !igul && ((j = (*o.fg[filtergroup]).inBannedRegExpURLList(temp)) >= 0)) { (*checkme).isItNaughty = true; (*checkme).whatIsNaughtyLog = o.language_list.getTranslation(503); // Banned Regular Expression URL: (*checkme).whatIsNaughtyLog += (*o.fg[filtergroup]).banned_regexpurl_list_source[j].toCharArray(); (*checkme).whatIsNaughty = o.language_list.getTranslation(504); // Banned Regular Expression URL found.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -