📄 foptioncontainer.cpp
字号:
//Please refer to http://dansguardian.org/?page=copyright2//for the license for this code.//Written by Daniel Barron (daniel@//jadeb/.com).//For support go to http://groups.yahoo.com/group/dansguardian// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#include "autoconf/platform.h"#include <syslog.h>#include "FOptionContainer.hpp"#include "OptionContainer.hpp"#include "RegExp.hpp"#include <string>#include <iostream>#include <fstream>#include <netdb.h> // for gethostby#include <netinet/in.h> // for address structures#include <arpa/inet.h> // for inet_aton()#include <sys/socket.h>//#include <unistd.h> // removeextern bool isDaemonised;extern OptionContainer o;FOptionContainer::~FOptionContainer() { o.lm.deRefList(banned_phrase_list); o.lm.deRefList(exception_site_list); o.lm.deRefList(exception_url_list); o.lm.deRefList(banned_extension_list); o.lm.deRefList(banned_mimetype_list); o.lm.deRefList(banned_site_list); o.lm.deRefList(banned_url_list); o.lm.deRefList(grey_site_list); o.lm.deRefList(grey_url_list); o.lm.deRefList(banned_regexpurl_list); o.lm.deRefList(content_regexp_list);}void FOptionContainer::reset() { o.lm.deRefList(banned_phrase_list); o.lm.deRefList(exception_site_list); o.lm.deRefList(exception_url_list); o.lm.deRefList(banned_extension_list); o.lm.deRefList(banned_mimetype_list); o.lm.deRefList(banned_site_list); o.lm.deRefList(banned_url_list); o.lm.deRefList(grey_site_list); o.lm.deRefList(grey_url_list); o.lm.deRefList(banned_regexpurl_list); o.lm.deRefList(content_regexp_list); banned_phrase_list_index.clear(); conffile.clear(); banned_regexpurl_list_comp.clear(); content_regexp_list_comp.clear(); content_regexp_list_rep.clear(); banned_regexpurl_list_source.clear(); banned_regexpurl_list_ref.clear();}bool FOptionContainer::read(std::string filename) { try { // all sorts of exceptions could occur reading conf files std::string linebuffer; String temp; // for tempory conversion and storage int j; // counter ifstream conffiles(filename.c_str(), ios::in); // dansguardianfN.conf if (!conffiles.good()) { if (!isDaemonised) { std::cerr << "error reading: " << filename << std::endl; } syslog(LOG_ERR, "%s","error reading dansguardian.conf"); return false; } while (!conffiles.eof()) { getline(conffiles, linebuffer); if (!conffiles.eof() && linebuffer.length() != 0) { if (linebuffer[0] != '#') { // i.e. not commented out for(j = 0; j < (signed)linebuffer.length(); j++) { linebuffer[j] = tolower(linebuffer[j]); } temp = (char*)linebuffer.c_str(); if (temp.contains("#")) { temp = temp.before("#"); } temp.removeWhiteSpace(); // get rid of spaces at end of line linebuffer = temp.toCharArray(); conffile.push_back(linebuffer); // stick option in deque } } } conffiles.close(); #ifdef DGDEBUG std::cout << "read conf into memory" << filename << std::endl; #endif // the dansguardian.conf and pics files get amalgamated into one // deque. They are only seperate files for clarity. linebuffer = findoptionS("picsfile"); ifstream picsfiles(linebuffer.c_str(), ios::in); // pics file if (!picsfiles.good()) { if (!isDaemonised) { std::cerr << "error reading: " << linebuffer << std::endl; } syslog(LOG_ERR, "%s","error reading pics file"); return false; } while (!picsfiles.eof()) { getline(picsfiles, linebuffer); if (!picsfiles.eof() && linebuffer.length() != 0) { if (linebuffer[0] != '#') { // i.e. not commented out temp = (char*)linebuffer.c_str(); if (temp.contains("#")) { temp = temp.before("#"); } while (temp.endsWith(" ")) { temp.chop(); // get rid of spaces at end of line } linebuffer = temp.toCharArray(); conffile.push_back(linebuffer); // stick option in deque } } } picsfiles.close(); #ifdef DGDEBUG std::cout << "read pics into memory" << filename << std::endl; #endif if (findoptionS("enablePICS") == "off") { enable_PICS = 0; } else { enable_PICS = 1; } naughtyness_limit = findoptionI("naughtynesslimit"); if (!realitycheck(String(naughtyness_limit), 1, 4, "naughtynesslimit")) { return false; } exception_phrase_list_location = findoptionS("exceptionphraselist"); weighted_phrase_list_location = findoptionS("weightedphraselist"); banned_phrase_list_location = findoptionS("bannedphraselist"); banned_extension_list_location = findoptionS("bannedextensionlist"); banned_mimetype_list_location = findoptionS("bannedmimetypelist"); banned_site_list_location = findoptionS("bannedsitelist"); banned_url_list_location = findoptionS("bannedurllist"); grey_site_list_location = findoptionS("greysitelist"); grey_url_list_location = findoptionS("greyurllist"); banned_regexpurl_list_location = findoptionS("bannedregexpurllist"); content_regexp_list_location = findoptionS("contentregexplist"); exceptions_site_list_location = findoptionS("exceptionsitelist"); exceptions_url_list_location = findoptionS("exceptionurllist"); pics_rsac_nudity = findoptionI("RSACnudity"); pics_rsac_language = findoptionI("RSAClanguage"); pics_rsac_sex = findoptionI("RSACsex"); pics_rsac_violence = findoptionI("RSACviolence"); pics_evaluweb_rating = findoptionI("evaluWEBrating"); pics_cybernot_sex = findoptionI("CyberNOTsex"); pics_cybernot_other = findoptionI("CyberNOTother"); pics_safesurf_agerange = findoptionI("SafeSurfagerange"); pics_safesurf_profanity = findoptionI("SafeSurfprofanity"); pics_safesurf_heterosexualthemes = findoptionI("SafeSurfheterosexualthemes"); pics_safesurf_homosexualthemes = findoptionI("SafeSurfhomosexualthemes"); pics_safesurf_nudity = findoptionI("SafeSurfnudity"); pics_safesurf_violence = findoptionI("SafeSurfviolence"); pics_safesurf_sexviolenceandprofanity = findoptionI("SafeSurfsexviolenceandprofanity"); pics_safesurf_intolerance = findoptionI("SafeSurfintolerance"); pics_safesurf_druguse = findoptionI("SafeSurfdruguse"); pics_safesurf_otheradultthemes = findoptionI("SafeSurfotheradultthemes"); pics_safesurf_gambling = findoptionI("SafeSurfgambling"); pics_icra_chat = findoptionI("ICRAchat"); pics_icra_moderatedchat = findoptionI("ICRAmoderatedchat"); pics_icra_languagesexual = findoptionI("ICRAlanguagesexual"); pics_icra_languageprofanity = findoptionI("ICRAlanguageprofanity"); pics_icra_languagemildexpletives = findoptionI("ICRAlanguagemildexpletives"); pics_icra_nuditygraphic = findoptionI("ICRAnuditygraphic"); pics_icra_nuditymalegraphic = findoptionI("ICRAnuditymalegraphic"); pics_icra_nudityfemalegraphic = findoptionI("ICRAnudityfemalegraphic"); pics_icra_nuditytopless = findoptionI("ICRAnuditytopless"); pics_icra_nuditybottoms = findoptionI("ICRAnuditybottoms"); pics_icra_nuditysexualacts = findoptionI("ICRAnuditysexualacts"); pics_icra_nudityobscuredsexualacts = findoptionI("ICRAnudityobscuredsexualacts"); pics_icra_nuditysexualtouching = findoptionI("ICRAnuditysexualtouching"); pics_icra_nuditykissing = findoptionI("ICRAnuditykissing"); pics_icra_nudityartistic = findoptionI("ICRAnudityartistic"); pics_icra_nudityeducational = findoptionI("ICRAnudityeducational"); pics_icra_nuditymedical = findoptionI("ICRAnuditymedical"); pics_icra_drugstobacco = findoptionI("ICRAdrugstobacco"); pics_icra_drugsalcohol = findoptionI("ICRAdrugsalcohol"); pics_icra_drugsuse = findoptionI("ICRAdrugsuse"); pics_icra_gambling = findoptionI("ICRAgambling"); pics_icra_weaponuse = findoptionI("ICRAweaponuse"); pics_icra_intolerance = findoptionI("ICRAintolerance"); pics_icra_badexample = findoptionI("ICRAbadexample"); pics_icra_pgmaterial = findoptionI("ICRApgmaterial"); pics_icra_violenceobjects = findoptionI("ICRAviolenceobjects"); pics_icra_violencerape = findoptionI("ICRAviolencerape"); pics_icra_violencetohumans = findoptionI("ICRAviolencetohumans"); pics_icra_violencetoanimals = findoptionI("ICRAviolencetoanimals"); pics_icra_violencetofantasy = findoptionI("ICRAviolencetofantasy"); pics_icra_violencekillinghumans = findoptionI("ICRAviolencekillinghumans"); pics_icra_violencekillinganimals = findoptionI("ICRAviolencekillinganimals"); pics_icra_violencekillingfantasy = findoptionI("ICRAviolencekillingfantasy"); pics_icra_violenceinjuryhumans = findoptionI("ICRAviolenceinjuryhumans"); pics_icra_violenceinjuryanimals = findoptionI("ICRAviolenceinjuryanimals"); pics_icra_violenceinjuryfantasy = findoptionI("ICRAviolenceinjuryfantasy"); pics_icra_violenceartisitic = findoptionI("ICRAviolenceartisitic"); pics_icra_violenceeducational = findoptionI("ICRAviolenceeducational"); pics_icra_violencemedical = findoptionI("ICRAviolencemedical"); pics_icra_violencesports = findoptionI("ICRAviolencesports"); pics_weburbia_rating = findoptionI("Weburbiarating"); pics_vancouver_multiculturalism = findoptionI("Vancouvermulticulturalism"); pics_vancouver_educationalcontent = findoptionI("Vancouvereducationalcontent"); pics_vancouver_environmentalawareness = findoptionI("Vancouverenvironmentalawareness"); pics_vancouver_tolerance = findoptionI("Vancouvertolerance"); pics_vancouver_violence = findoptionI("Vancouverviolence"); pics_vancouver_sex = findoptionI("Vancouversex"); pics_vancouver_profanity = findoptionI("Vancouverprofanity"); pics_vancouver_safety = findoptionI("Vancouversafety"); pics_vancouver_canadiancontent = findoptionI("Vancouvercanadiancontent"); pics_vancouver_commercialcontent = findoptionI("Vancouvercommercialcontent"); pics_vancouver_gambling = findoptionI("Vancouvergambling"); bypass_mode = findoptionI("bypass"); if (!realitycheck(String(bypass_mode), 1, 4, "bypass")) { return false; } if (bypass_mode != 0) { magic = findoptionS("bypasskey"); if (magic.length() < 9) { std::string s(16u, ' '); for (int i = 0; i < 16; i++) { s[i] = (rand() % 26) + 'A'; } magic = s; } #ifdef DGDEBUG std::cout << "Setting magic key to '" << magic << "'" << std::endl; #endif // Create the Bypass Cookie magic key cookie_magic = std::string(16u, ' '); for (int i = 0; i < 16; i++) { cookie_magic[i] = (rand() % 26) + 'A'; } } // Most of the readfoofiles could be amalgamated into one fuction // and will be one day. So it's a bit messy at the moment. #ifdef DGDEBUG std::cout << "settings into memory" << filename << std::endl; #endif if (!readbplfile(banned_phrase_list_location.c_str(), exception_phrase_list_location.c_str(), weighted_phrase_list_location.c_str())) { return false; } // read banned, exception, weighted phrase list #ifdef DGDEBUG std::cout << "read phrase lists into memory" << filename << std::endl; #endif if (!readeslfile(exceptions_site_list_location.c_str())) { return false; } // site exceptions if (!readeurllfile(exceptions_url_list_location.c_str())) { return false; } // url exceptions if (!readbelfile(banned_extension_list_location.c_str())) { return false; } // file extensions if (!readbmlfile(banned_mimetype_list_location.c_str())) { return false; } // mime types if (!readbslfile(banned_site_list_location.c_str())) { return false; } // banned domains if (!readbulfile(banned_url_list_location.c_str())) { return false; } // banned urls if (!readgslfile(grey_site_list_location.c_str())) { return false; } // grey domains if (!readgulfile(grey_url_list_location.c_str())) { return false; } // grey urls if (!readbreulfile(banned_regexpurl_list_location.c_str())) { return false; } // banned reg exp urls if (!readcrelfile(content_regexp_list_location.c_str())) { return false; } // content replacement regular expressions #ifdef DGDEBUG std::cout << "lists into memory" << filename << std::endl; #endif if (!precompileregexps()) { return false; } // precompiled reg exps for speed if ((*o.lm.l[banned_site_list]).inList("**")) { blanketblock = 1; } else { blanketblock = 0; } if ((*o.lm.l[banned_site_list]).inList("*ip")) { blanket_ip_block = 1; } else { blanket_ip_block = 0; } } catch (exception& e) { if (!isDaemonised) { std::cerr << e.what() << std::endl; // when called the daemon has not // detached so we can do this } return false; } return true;}bool FOptionContainer::readbplfile(const char* banned, const char* exception, const char* weighted) { int res = o.lm.newPhraseList(exception, banned, weighted); if (res < 0) { if (!isDaemonised) { std::cerr << "Error opening phraselists" << std::endl; } syslog(LOG_ERR, "%s","Error opening phraselists"); return false; } banned_phrase_list = res; if (!(*o.lm.l[banned_phrase_list]).used) { #ifdef DGDEBUG std::cout << "Reading new phrase lists" << std::endl; #endif bool result = (*o.lm.l[banned_phrase_list]).readPhraseList(exception, true); if (!result) { if (!isDaemonised) { std::cerr << "Error opening exceptionphraselist" << std::endl; } syslog(LOG_ERR, "%s","Error opening exceptionphraselist"); return false; } result = (*o.lm.l[banned_phrase_list]).readPhraseList(banned, false); if (!result) { if (!isDaemonised) { std::cerr << "Error opening bannedphraselist" << std::endl; } syslog(LOG_ERR, "%s","Error opening bannedphraselist"); return false; } if (weighted_phrase_mode > 0) { // if zero wpl is deactivated #ifdef DGDEBUG std::cout << "Reading weighted phrase list" << std::endl; #endif result = (*o.lm.l[banned_phrase_list]).readPhraseList(weighted, false); if (!result) { if (!isDaemonised) { std::cerr << "Error opening weightedphraselist" << std::endl; } syslog(LOG_ERR, "%s","Error opening weightedphraselist");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -