-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmegaproxy.cpp
110 lines (91 loc) · 3.34 KB
/
megaproxy.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
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#define HAVE_LIBUV 1
#include <megaapi.h>
using namespace mega;
using namespace std;
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
// Parse options
string username;
string password;
bool localOnly;
int port;
bool fileServer;
bool folderServer;
bool subtitlesSupport;
int maxBufferSize;
int maxOutputSize;
po::options_description generic("Generic options");
generic.add_options()
("help,h", "produce help message")
("config,c", po::value<string>(), "configuration file")
;
po::options_description config("Configuration");
config.add_options()
("user,u", po::value<string>(&username)->required(), "mega.nz username (required)")
("pass,p", po::value<string>(&password)->required(), "mega.nz password (required)")
("local-only", po::value<bool>(&localOnly)->default_value(true), "listen on 127.0.0.1 only")
("port", po::value<int>(&port)->default_value(4443), "listening port")
("files", po::value<bool>(&fileServer)->default_value(true), "allow to serve files")
("folders", po::value<bool>(&folderServer)->default_value(true), "allow to serve folders")
("subtitles", po::value<bool>(&subtitlesSupport)->default_value(false), "enable subtitles support")
("buffer", po::value<int>(&maxBufferSize)->default_value(0), "maximum buffer size (in bytes)")
("output", po::value<int>(&maxOutputSize)->default_value(0), "maximum output size (in bytes)")
;
po::options_description desc;
desc.add(generic).add(config);
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
cout << "Usage: " << argv[0] << " [options]" << endl << desc << endl;
return 0;
}
if (vm.count("config")) {
const string &path = vm["config"].as<string>();
ifstream file(path.c_str());
if (!file) {
cerr << "Cannot open config file: " << path << endl;
} else {
po::store(po::parse_config_file(file, config), vm);
}
}
po::notify(vm);
} catch (exception& e) {
cerr << "Invalid options: " << e.what() << endl << desc << endl;
return 1;
}
// Initiliaze the SDK
MegaApi megaApi = MegaApi("CBMQALQS", (const char *)NULL, "HTTP proxy for mega.nz");
megaApi.setLogLevel(MegaApi::LOG_LEVEL_INFO);
// Login
SynchronousRequestListener listener;
megaApi.login(username.c_str(), password.c_str(), &listener);
listener.wait();
if (listener.getError()->getErrorCode() != MegaError::API_OK) {
MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Login error");
return 2;
}
MegaApi::log(MegaApi::LOG_LEVEL_INFO, "Login OK. Fetching nodes");
// Fetch nodes
megaApi.fetchNodes(&listener);
listener.wait();
if (listener.getError()->getErrorCode() != MegaError::API_OK) {
MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Error fetching nodes");
return 3;
}
// Setup the HTTP server
megaApi.httpServerEnableFileServer(fileServer);
megaApi.httpServerEnableFolderServer(folderServer);
megaApi.httpServerSetRestrictedMode(MegaApi::HTTP_SERVER_ALLOW_ALL);
megaApi.httpServerEnableSubtitlesSupport(subtitlesSupport);
megaApi.httpServerSetMaxBufferSize(maxBufferSize);
megaApi.httpServerSetMaxOutputSize(maxOutputSize);
megaApi.httpServerStart(localOnly, port);
MegaApi::log(MegaApi::LOG_LEVEL_INFO, "MEGA initialization complete!");
megaApi.setLogLevel(MegaApi::LOG_LEVEL_WARNING);
pause();
}