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

Line 
1--- rtorrent-0.8.2-org/src/command_network.cc   2008-05-07 08:19:11.000000000 -0400
2+++ rtorrent-ip_filter/src/command_network.cc   2008-05-17 19:46:36.964555772 -0400
3@@ -36,6 +36,11 @@
4 
5 #include "config.h"
6 
7+#include <string>
8+#include <sstream>
9+#include <list>
10+#include <unistd.h>
11+
12 #include <functional>
13 #include <rak/address_info.h>
14 #include <rak/path.h>
15@@ -61,6 +66,10 @@
16 #include "control.h"
17 #include "command_helpers.h"
18 
19+#include <boost/algorithm/string/trim.hpp>
20+#include "core/ip_filter.h"
21+
22+
23 torrent::Object
24 apply_encryption(const torrent::Object& rawArgs) {
25   const torrent::Object::list_type& args = rawArgs.as_list();
26@@ -94,6 +103,57 @@
27 }
28 
29 torrent::Object
30+apply_ip_filter(const torrent::Object& rawArgs) {
31+  const torrent::Object::list_type& args = rawArgs.as_list();
32+
33+  std::list<std::string> files;
34+
35+  for (torrent::Object::list_const_iterator itr = args.begin(), last = args.end(); itr != last; itr++) {
36+    std::string file( itr->as_string() );
37+    boost::trim( file );
38+    if( access(file.c_str(),F_OK | R_OK) )
39+      throw torrent::input_error("IpFilter file '" + file + "' does not exist or not readable. Filter could not be loaded");
40+    files.push_back( file );
41+  }
42+
43+  std::stringstream logMsg;
44+  if( files.empty() ) {
45+    logMsg << "IpFilter is empty";
46+    control->core()->push_log( logMsg.str().c_str() );
47+  }
48+  else {
49+    core::IpFilter* f = new core::IpFilter();
50+    logMsg << "IpFilter is initialized with files: ";
51+    int entries = 0;
52+    for( std::list<std::string>::iterator itr = files.begin(); itr != files.end(); itr++) {
53+      std::cout << "Loading IP filters from '" << *itr << "'...";
54+      std::cout.flush();
55+      if( itr != files.begin() )
56+        logMsg << ", ";
57+      logMsg << *itr;
58+      int merges = f->add_from_file( *itr );
59+      if( merges < 0 ) {
60+        std::cout << "error" << std::endl;
61+        std::cout.flush();
62+        throw torrent::input_error("IpFilter could not load file '" + *itr + "'");
63+      }
64+      std::cout << "done. Loaded " << (f->size()-entries) << " ranges. " << merges << " ranges were merged." << std::endl;
65+      std::cout.flush();
66+      entries = f->size();
67+    }
68+    control->core()->push_log( logMsg.str().c_str() );
69+    std::stringstream logMsg2("IpFilter loaded with ");
70+    logMsg2 << f->size() << " ranges total. " << f->get_merges() << " ranges were merged.";
71+    control->core()->push_log( logMsg2.str().c_str() );
72+    std::cout << logMsg2 << std::endl;
73+    std::cout.flush();
74+    control->core()->set_ip_filter( f );
75+  }
76+
77+  return torrent::Object();
78+}
79+
80+torrent::Object
81 apply_tos(const torrent::Object& rawArg) {
82   rpc::Command::value_type value;
83   torrent::ConnectionManager* cm = torrent::connection_manager();
84@@ -366,7 +426,9 @@
85 
86   ADD_VARIABLE_BOOL("peer_exchange", true);
87 
88+  ADD_COMMAND_VOID("reload_ip_filter",           rak::make_mem_fun(control->core(), &core::Manager::reload_ip_filter));
89+  ADD_COMMAND_LIST("ip_filter",          rak::ptr_fn(&apply_ip_filter));
90+
91   // Not really network stuff:
92   ADD_VARIABLE_BOOL("handshake_log", false);
93   ADD_VARIABLE_STRING("tracker_dump", "");
94 }