Ticket #239: src__command_network.cc.diff

File src__command_network.cc.diff, 3.1 KB (added by comer, 4 years ago)

changes to src/command_network.cc

  • src/command_network.cc

    old new  
    3636 
    3737#include "config.h" 
    3838 
     39#include <string> 
     40#include <sstream> 
     41#include <list> 
     42#include <unistd.h> 
     43 
    3944#include <functional> 
    4045#include <rak/address_info.h> 
    4146#include <rak/path.h> 
     
    6166#include "control.h" 
    6267#include "command_helpers.h" 
    6368 
     69#include <boost/algorithm/string/trim.hpp> 
     70#include "core/ip_filter.h" 
     71 
     72 
    6473torrent::Object 
    6574apply_encryption(const torrent::Object& rawArgs) { 
    6675  const torrent::Object::list_type& args = rawArgs.as_list(); 
     
    94103} 
    95104 
    96105torrent::Object 
     106apply_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 
     156torrent::Object 
    97157apply_tos(const torrent::Object& rawArg) { 
    98158  rpc::Command::value_type value; 
    99159  torrent::ConnectionManager* cm = torrent::connection_manager(); 
     
    366426 
    367427  ADD_VARIABLE_BOOL("peer_exchange", true); 
    368428 
     429  ADD_COMMAND_VOID("reload_ip_filter",           rak::make_mem_fun(control->core(), &core::Manager::reload_ip_filter)); 
     430  ADD_COMMAND_LIST("ip_filter",          rak::ptr_fn(&apply_ip_filter)); 
     431 
    369432  // Not really network stuff: 
    370   ADD_VARIABLE_BOOL("handshake_log", false); 
    371433  ADD_VARIABLE_STRING("tracker_dump", ""); 
    372434}