| | 106 | apply_ip_filter(const torrent::Object& rawArgs) { |
| | 107 | const torrent::Object::list_type& args = rawArgs.as_list(); |
| | 108 | |
| | 109 | std::list<std::string> files; |
| | 110 | |
| | 111 | for (torrent::Object::list_const_iterator itr = args.begin(), last = args.end(); itr != last; itr++) { |
| | 112 | std::string file( itr->as_string() ); |
| | 113 | boost::trim( file ); |
| | 114 | if( access(file.c_str(),F_OK | R_OK) ) |
| | 115 | throw torrent::input_error("IpFilter file '" + file + "' does not exist or not readable. Filter could not be loaded"); |
| | 116 | files.push_back( file ); |
| | 117 | } |
| | 118 | |
| | 119 | std::stringstream logMsg; |
| | 120 | if( files.empty() ) { |
| | 121 | logMsg << "IpFilter is empty"; |
| | 122 | control->core()->push_log( logMsg.str().c_str() ); |
| | 123 | } |
| | 124 | else { |
| | 125 | core::IpFilter* f = new core::IpFilter(); |
| | 126 | logMsg << "IpFilter is initialized with files: "; |
| | 127 | int entries = 0; |
| | 128 | for( std::list<std::string>::iterator itr = files.begin(); itr != files.end(); itr++) { |
| | 129 | std::cout << "Loading IP filters from '" << *itr << "'..."; |
| | 130 | std::cout.flush(); |
| | 131 | if( itr != files.begin() ) |
| | 132 | logMsg << ", "; |
| | 133 | logMsg << *itr; |
| | 134 | int merges = f->add_from_file( *itr ); |
| | 135 | if( merges < 0 ) { |
| | 136 | std::cout << "error" << std::endl; |
| | 137 | std::cout.flush(); |
| | 138 | throw torrent::input_error("IpFilter could not load file '" + *itr + "'"); |
| | 139 | } |
| | 140 | std::cout << "done. Loaded " << (f->size()-entries) << " ranges. " << merges << " ranges were merged." << std::endl; |
| | 141 | std::cout.flush(); |
| | 142 | entries = f->size(); |
| | 143 | } |
| | 144 | control->core()->push_log( logMsg.str().c_str() ); |
| | 145 | std::stringstream logMsg2("IpFilter loaded with "); |
| | 146 | logMsg2 << f->size() << " ranges total. " << f->get_merges() << " ranges were merged."; |
| | 147 | control->core()->push_log( logMsg2.str().c_str() ); |
| | 148 | std::cout << logMsg2 << std::endl; |
| | 149 | std::cout.flush(); |
| | 150 | control->core()->set_ip_filter( f ); |
| | 151 | } |
| | 152 | |
| | 153 | return torrent::Object(); |
| | 154 | } |
| | 155 | |
| | 156 | torrent::Object |