-
Notifications
You must be signed in to change notification settings - Fork 10
/
ipfilter.cpp
86 lines (66 loc) · 1.91 KB
/
ipfilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "hrktorrent.h"
CIPFilter* IPFilter = 0;
CIPFilter::CIPFilter()
{
if(!Settings->GetI("ipfilter")) {
_active = false;
return;
}
else
_active = true;
std::string path = *Settings->getDir();
path.append("ipfilter.dat");
std::ifstream config(path.c_str(), std::ifstream::in);
if(!config.is_open()) {
Core->VerbosePrint("Settings", "Unable to load ip filter from file. Aborting.");
return;
}
std::cout << "Loading IPFilter. This could probably take a while!" << std::endl;
while(config.good()) {
std::string line;
getline(config, line);
ParseFilterLine(line);
}
libtorrent::ip_filter::filter_tuple_t filters = _ipfilter.export_filter();
unsigned int entrycount = filters.get<0>().size() + filters.get<1>().size();
std::cout << entrycount << " entries imported." << std::endl;
config.close();
}
CIPFilter::~CIPFilter()
{
Core->VerbosePrint("IPFilter", "Destroying IPFilter class.");
}
bool
CIPFilter::AddRule(std::string start, std::string end)
{
if(start.empty() || end.empty() || !_active) return false;
libtorrent::address astart, aend;
try {
astart = libtorrent::address::from_string(start);
aend = libtorrent::address::from_string(end);
} catch(libtorrent::error_code& e) {
return false; // invalid rule
}
try {
_ipfilter.add_rule(astart, aend, libtorrent::ip_filter::blocked);
} catch(std::exception& e) {
return false;
}
return true;
}
void
CIPFilter::ParseFilterLine(std::string line)
{
if(line.size() == 0 || line.size() < 5 ) return;
std::string start;
std::string end;
std::string::size_type loc = line.find_first_of("-");
std::string::size_type loc2 = line.find_first_of(",");
start.assign(line);
start.resize(loc-1);
end.assign(line);
end.erase(0,loc+2);
end.resize(loc-1);
if(!AddRule(start,end))
Core->VerbosePrint("Settings", "Error adding line to ip filter.");
}