-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.cpp
174 lines (142 loc) · 3.9 KB
/
test.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
#include "utorrent_webui.hpp"
#include "libtorrent_webui.hpp"
#include "file_downloader.hpp"
#include "auto_load.hpp"
#include "save_settings.hpp"
#include "save_resume.hpp"
#include "torrent_history.hpp"
#include "auth.hpp"
#include "pam_auth.hpp"
//#include "text_ui.hpp"
#include "libtorrent/session.hpp"
#include "alert_handler.hpp"
#include "stats_logging.hpp"
#include <signal.h>
#include <iostream>
#include <thread>
#include <chrono>
bool quit = false;
bool force_quit = false;
void sighandler(int s)
{
quit = true;
}
void sighandler_forcequit(int s)
{
force_quit = true;
}
using namespace libtorrent;
namespace lt = libtorrent;
struct external_ip_observer : alert_observer
{
external_ip_observer(lt::session& s, alert_handler* h)
: m_alerts(h)
, m_ses(s)
{
m_alerts->subscribe(this, 0, external_ip_alert::alert_type, 0);
}
~external_ip_observer()
{
m_alerts->unsubscribe(this);
}
void handle_alert(alert const* a)
{
external_ip_alert const* ip = alert_cast<external_ip_alert>(a);
if (ip == NULL) return;
error_code ec;
printf("EXTERNAL IP: %s\n", ip->external_address.to_string().c_str());
if (m_last_known_addr != address()
&& m_last_known_addr != ip->external_address)
{
// our external IP changed. stop the session.
printf("pausing session\n");
m_ses.pause();
return;
}
if (m_ses.is_paused() && m_last_known_addr == ip->external_address)
{
printf("resuming session\n");
m_ses.resume();
return;
}
m_last_known_addr = ip->external_address;
}
alert_handler* m_alerts;
lt::session& m_ses;
address m_last_known_addr;
};
int main(int argc, char *const argv[])
{
session_params s;
error_code ec;
s.settings.set_str(settings_pack::listen_interfaces, "0.0.0.0:6881");
s.settings.set_int(settings_pack::alert_mask, 0xffffffff);
load_settings(s, "settings.dat", ec);
if (ec) std::cout << "Failed to load settings: " << ec.message() << '\n';
lt::session ses(s);
alert_handler alerts(ses);
save_settings sett(ses, s.settings, "settings.dat");
torrent_history hist(&alerts);
auth authorizer;
ec.clear();
authorizer.load_accounts("users.conf", ec);
if (ec)
authorizer.add_account("admin", "test", 0);
ec.clear();
// pam_auth authorizer("bittorrent");
save_resume resume(ses, "resume.dat", &alerts);
add_torrent_params p;
p.save_path = sett.get_str("save_path", ".");
resume.load(ec, p);
// external_ip_observer eip(ses, &alerts);
auto_load al(ses, &sett);
utorrent_webui ut_handler(ses, &sett, &al, &hist, &authorizer);
file_downloader file_handler(ses, &authorizer);
libtorrent_webui lt_handler(ses, &hist, &authorizer, &alerts);
stats_logging log(ses, &alerts);
webui_base webport;
webport.add_handler(<_handler);
webport.add_handler(&ut_handler);
webport.add_handler(&file_handler);
webport.start(8090, "server.pem");
if (!webport.is_running())
{
fprintf(stderr, "failed to start web server\n");
return 1;
}
signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler);
bool shutting_down = false;
while (!quit || !resume.ok_to_quit())
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
alerts.dispatch_alerts();
if (!shutting_down) ses.post_torrent_updates();
if (quit && !shutting_down)
{
resume.save_all();
shutting_down = true;
fprintf(stderr, "saving resume data\n");
signal(SIGTERM, &sighandler_forcequit);
signal(SIGINT, &sighandler_forcequit);
}
if (force_quit)
{
fprintf(stderr, "force quitting\n");
break;
}
}
fprintf(stderr, "abort alerts\n");
// it's important to disable any more alert subscriptions
// and cancel the ones in flught now, otherwise the webport
// may dead-lock. Some of its threads may be blocked waiting
// for alerts. Those alerts aren't likely to ever arrive at
// this point.
alerts.abort();
fprintf(stderr, "closing web server\n");
webport.stop();
fprintf(stderr, "saving settings\n");
sett.save(ec);
fprintf(stderr, "destructing session\n");
return 0;
}