-
Notifications
You must be signed in to change notification settings - Fork 0
/
jan-nik.cpp
253 lines (223 loc) · 6.25 KB
/
jan-nik.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "jan-nik.hpp"
#include <fstream>
#include <pcap.h>
#include <stdio.h>
#include <iostream>
#include <boost/regex.hpp>
#include <wx/wx.h>
#include <wx/string.h>
#include <wx/clipbrd.h>
#include <wx/wxprec.h>
#include <exception>
#ifdef linux
#include <gtkmm.h>
#endif
#ifdef linux
const std::string basepath = "/usr/share/fastw3join/"; // regex.txt
const std::string basepath2 = "/etc/fastw3join/"; // device.txt
#else
const std::string basepath = "";
const std::string basepath2 = "";
#endif
using namespace boost;
std::vector<regex> expressions_;
const std::vector<wxString>& Network::GetDevices()
{
return wxDevices_;
}
const size_t Network::GetActiveDevice()
{
return activeDevice_;
}
MyThread::MyThread(pcap_t* handle, bpf_u_int32 net) : handle_(handle)
{
struct bpf_program fp; /* The compiled filter */
char filter_exp[] = "src host europe.battle.net || asia.battle.net || uswest.battle.net || useast.battle.net"; /* The filter expression */
/* Compile and apply the filter */
std::cout << "Parsing filter..." << std::endl;
if (pcap_compile(handle_, &fp, filter_exp, 0, net) == -1)
{
throw std::runtime_error(std::string("Couldn't parse filter ") + filter_exp + ": " + pcap_geterr(handle));
}
if (pcap_setfilter(handle_, &fp) == -1)
{
throw std::runtime_error(std::string("Couldn't install filter ") + filter_exp + ": " + pcap_geterr(handle));
}
}
Network::Network() : activeDevice_(0), myThread_(0)
{
std::ifstream fin((basepath + "regex.txt").c_str());
if (!fin)
{
throw std::runtime_error(std::string("Could not find " + basepath + "regex.txt!"));
}
do
{
std::string temp;
std::getline(fin, temp);
if (!temp.empty())
{
expressions_.push_back(regex(temp));
}
}
while (fin);
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *alldevs;
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
throw std::runtime_error(errbuf);
}
if (!alldevs)
{
throw std::runtime_error("No devices found! (Try to run as root)");
}
devices_.clear();
for (pcap_if_t* d = alldevs; d; d = d->next)
{
std::string temp(d->name);
devices_.push_back(temp);
if (d->description)
{
#ifdef linux
temp += " (";
temp += d->description;
temp += ")";
#else
temp = d->description;
#endif
}
wxDevices_.push_back(wxString(wxConvUTF8.cMB2WC(temp.c_str()), *wxConvCurrent));
}
// Set Active Device
const char* const dev = pcap_lookupdev(errbuf);
std::ifstream deviceTxt((basepath2 + "device.txt").c_str());
std::string savedDevice;
if(deviceTxt)
{
std::getline(deviceTxt, savedDevice);
}
#ifdef linux
else
{
system("mkdir /etc/fastw3join");
}
#endif
bool alreadySetForAny = false; // any hat Vorang vorm Standard-Device
for (size_t i = 0; i < devices_.size(); ++i)
{
if(!alreadySetForAny &&
dev &&
devices_[i] == dev)
{
activeDevice_ = i;
}
if(devices_[i] == "any")
{
activeDevice_ = i;
}
if(deviceTxt && devices_[i] == savedDevice) // Falls ein Device vom letzten Mal gespeichert wurde...
{
activeDevice_ = i;
break; // ... hat es vor allen anderen Vorrang
}
}
}
void Network::ActivateDevice()
{
if(activeDevice_ == -1)
return;
pcap_t *handle; /* Session handle */
std::vector<char> dev(devices_[activeDevice_].begin(), devices_[activeDevice_].end());
dev.push_back(0); // Nicht vergessen!!!
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
bpf_u_int32 net; /* Our IP */
bpf_u_int32 mask; /* Our netmask */
std::cout << "Opening device " << &dev[0] << "..." << std::endl;
/* Find the properties for the device */
if (pcap_lookupnet(&dev[0], &net, &mask, errbuf) == -1)
{
throw std::runtime_error(std::string("Couldn't get netmask for device ") + &dev[0] + ": " + errbuf);
}
/* Open the session in promiscuous mode */
handle = pcap_open_live(&dev[0], BUFSIZ, 1, 1000, errbuf);
if (handle == NULL)
{
throw std::runtime_error(std::string("Couldn't open device ") + &dev[0] + ": " + errbuf);
}
std::cout << "Starting thread..." << std::endl;
myThread_ = new MyThread(handle, net);
myThread_->Create();
myThread_->Run();
}
Network::~Network()
{
std::ofstream fout((basepath2 + "device.txt").c_str());
fout << devices_[activeDevice_];
}
bool invalid(char c)
{
return c == '\0' || c == '\n';
}
#ifdef linux
class ClipboardEntry
{
public:
ClipboardEntry(std::string data) : data_(data)
{
}
void on_get(Gtk::SelectionData& selection_data, guint)
{
selection_data.set("STRING", data_);
}
void on_clear()
{
delete this;
}
private:
std::string data_;
};
#endif
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
std::string x((const char*)packet, (size_t)header->len);
x = std::string(x.begin(), std::remove_if(x.begin(), x.end(), invalid));
cmatch what;
for (std::vector<regex>::const_iterator it = expressions_.begin(); it != expressions_.end(); ++it)
{
if (regex_match(x.c_str(), what, *it))
{
const std::string temp = what[2];
std::cout << temp;
HGLOBAL hText = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, temp.size() + 1);
char* pText = (char*)GlobalLock(hText);
strcpy(pText, temp.c_str());
GlobalUnlock(hText);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hText);
CloseClipboard();
break;
}
}
std::cout << "." << std::flush;
}
void* MyThread::Entry()
{
std::cout << "Waiting for packets..." << std::endl;
while (!TestDestroy())
{
pcap_dispatch(handle_, 1000, got_packet, 0);
}
std::cout << "Exiting Thread..." << std::endl;
pcap_close(handle_);
return 0;
}
void Network::ChangeDevice(const int index)
{
std::cout << "Sending close message to thread..." << std::endl;
if(activeDevice_ != -1)
myThread_->Delete();
activeDevice_ = index;
if(index != -1)
ActivateDevice();
}