main.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "HttpInitiateConnectionCommand.h"
  23. #include "ConsoleDownloadEngine.h"
  24. #include "TorrentConsoleDownloadEngine.h"
  25. #include "SegmentMan.h"
  26. #include "TorrentMan.h"
  27. #include "SplitSlowestSegmentSplitter.h"
  28. #include "LogFactory.h"
  29. #include "common.h"
  30. #include "DefaultDiskWriter.h"
  31. #include "Util.h"
  32. #include "InitiateConnectionCommandFactory.h"
  33. #include "prefs.h"
  34. #include "PeerListenCommand.h"
  35. #include "TorrentAutoSaveCommand.h"
  36. #include "TrackerWatcherCommand.h"
  37. #include "TrackerUpdateCommand.h"
  38. #include "ByteArrayDiskWriter.h"
  39. #include <deque>
  40. #include <algorithm>
  41. #include <time.h>
  42. #include <signal.h>
  43. #include <unistd.h>
  44. #include <libgen.h>
  45. #include <utility>
  46. extern char* optarg;
  47. extern int optind, opterr, optopt;
  48. #include <getopt.h>
  49. #ifdef HAVE_LIBSSL
  50. // for SSL
  51. # include <openssl/err.h>
  52. # include <openssl/ssl.h>
  53. #endif // HAVE_LIBSSL
  54. #ifdef HAVE_LIBGNUTLS
  55. # include <gnutls/gnutls.h>
  56. #endif // HAVE_LIBGNUTLS
  57. using namespace std;
  58. typedef deque<Request*> Requests;
  59. void printDownloadCompeleteMessage(string filename) {
  60. printf(_("\nThe download was complete. <%s>\n"), filename.c_str());
  61. }
  62. void printDownloadCompeleteMessage() {
  63. printf("\nThe download was complete.\n");
  64. }
  65. void printDownloadAbortMessage() {
  66. printf(_("\nThe download was not complete because of errors. Check the log.\n"));
  67. }
  68. void setSignalHander(int signal, void (*handler)(int)) {
  69. struct sigaction sigact;
  70. sigact.sa_handler = handler;
  71. sigact.sa_flags = 0;
  72. sigemptyset(&sigact.sa_mask);
  73. sigaction(signal, &sigact, NULL);
  74. }
  75. void clearRequest(Request* req) {
  76. delete(req);
  77. }
  78. DownloadEngine* e;
  79. TorrentDownloadEngine* te;
  80. void handler(int signal) {
  81. printf(_("\nstopping application...\n"));
  82. fflush(stdout);
  83. e->segmentMan->save();
  84. e->segmentMan->diskWriter->closeFile();
  85. printf(_("done\n"));
  86. exit(0);
  87. }
  88. void torrentHandler(int signal) {
  89. printf(_("\nstopping application...\n"));
  90. fflush(stdout);
  91. te->torrentMan->diskAdaptor->closeFile();
  92. if(te->torrentMan->downloadComplete() && te->isFilenameFixed()) {
  93. te->torrentMan->remove();
  94. //te->torrentMan->deleteTempFile();
  95. printDownloadCompeleteMessage();
  96. } else {
  97. te->torrentMan->save();
  98. }
  99. printf(_("done\n"));
  100. exit(0);
  101. }
  102. void addCommand(int cuid, const string& url, string referer, Requests& requests) {
  103. Request* req = new Request();
  104. req->setReferer(referer);
  105. if(req->setUrl(url)) {
  106. e->commands.push(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e));
  107. requests.push_back(req);
  108. } else {
  109. fprintf(stderr, _("Unrecognized URL or unsupported protocol: %s\n"), req->getUrl().c_str());
  110. delete(req);
  111. }
  112. }
  113. void showVersion() {
  114. cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl;
  115. cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl;
  116. cout << endl;
  117. cout <<
  118. _("This program is free software; you can redistribute it and/or modify\n"
  119. "it under the terms of the GNU General Public License as published by\n"
  120. "the Free Software Foundation; either version 2 of the License, or\n"
  121. "(at your option) any later version.\n"
  122. "\n"
  123. "This program is distributed in the hope that it will be useful,\n"
  124. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  125. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  126. "GNU General Public License for more details.\n"
  127. "\n"
  128. "You should have received a copy of the GNU General Public License\n"
  129. "along with this program; if not, write to the Free Software\n"
  130. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
  131. cout << endl;
  132. printf(_("Contact Info: %s\n"), "Tasuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>");
  133. cout << endl;
  134. }
  135. void showUsage() {
  136. printf(_("Usage: %s [options] URL ...\n"), PACKAGE_NAME);
  137. #ifdef ENABLE_BITTORRENT
  138. printf(_(" %s [options] -T TORRENT_FILE FILE ...\n"), PACKAGE_NAME);
  139. #endif // ENABLE_BITTORRENT
  140. cout << endl;
  141. cout << _("Options:") << endl;
  142. cout << _(" -d, --dir=DIR The directory to store downloaded file.") << endl;
  143. cout << _(" -o, --out=FILE The file name for downloaded file.") << endl;
  144. cout << _(" -l, --log=LOG The file path to store log. If '-' is specified,\n"
  145. " log is written to stdout.") << endl;
  146. cout << _(" -D, --daemon Run as daemon.") << endl;
  147. cout << _(" -s, --split=N Download a file using N connections. N must be\n"
  148. " between 1 and 5. This option affects all URLs.\n"
  149. " Thus, aria2 connects to each URL with\n"
  150. " N connections.") << endl;
  151. cout << _(" --retry-wait=SEC Set amount of time in second between requests\n"
  152. " for errors. Specify a value between 0 and 60.\n"
  153. " Default: 5") << endl;
  154. cout << _(" -t, --timeout=SEC Set timeout in second. Default: 60") << endl;
  155. cout << _(" -m, --max-tries=N Set number of tries. 0 means unlimited.\n"
  156. " Default: 5") << endl;
  157. cout << _(" --min-segment-size=SIZE[K|M] Set minimum segment size. You can append\n"
  158. " K or M(1K = 1024, 1M = 1024K). This\n"
  159. " value must be greater than or equal to\n"
  160. " 1024.") << endl;
  161. cout << _(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all\n"
  162. " URLs.") << endl;
  163. cout << _(" --http-user=USER Set HTTP user. This affects to all URLs.") << endl;
  164. cout << _(" --http-passwd=PASSWD Set HTTP password. This affects to all URLs.") << endl;
  165. cout << _(" --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs") << endl;
  166. cout << _(" --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs.") << endl;
  167. cout << _(" --http-proxy-method=METHOD Set the method to use in proxy request.\n"
  168. " METHOD is either 'get' or 'tunnel'.\n"
  169. " Default: tunnel") << endl;
  170. cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
  171. " is the only supported scheme.\n"
  172. " Default: basic") << endl;
  173. cout << _(" --referer=REFERER Set Referer. This affects to all URLs.") << endl;
  174. cout << _(" --ftp-user=USER Set FTP user. This affects to all URLs.\n"
  175. " Default: anonymous") << endl;
  176. cout << _(" --ftp-passwd=PASSWD Set FTP password. This affects to all URLs.\n"
  177. " Default: ARIA2USER@") << endl;
  178. cout << _(" --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'\n"
  179. " or 'ascii'.\n"
  180. " Default: binary") << endl;
  181. cout << _(" -p, --ftp-pasv Use passive mode in FTP.") << endl;
  182. cout << _(" --ftp-via-http-proxy=METHOD Use HTTP proxy in FTP. METHOD is either 'get' or\n"
  183. " 'tunnel'.\n"
  184. " Default: tunnel") << endl;
  185. #ifdef ENABLE_BITTORRENT
  186. cout << _(" -T, --torrent-file=TORRENT_FILE The file path to .torrent file.") << endl;
  187. cout << _(" --follow-torrent=true|false Setting this option to false prevents aria2 to\n"
  188. " enter BitTorrent mode even if the filename of\n"
  189. " downloaded file ends with .torrent.\n"
  190. " Default: true") << endl;
  191. cout << _(" -S, --show-files Print file listing of .torrent file and exit.") << endl;
  192. cout << _(" --direct-file-mapping=true|false Directly read from and write to each file\n"
  193. " mentioned in .torrent file.\n"
  194. " Default: true") << endl;
  195. cout << _(" --listen-port Set port number to listen to for peer connection.") << endl;
  196. #endif // ENABLE_BITTORRENT
  197. cout << _(" -v, --version Print the version number and exit.") << endl;
  198. cout << _(" -h, --help Print this message and exit.") << endl;
  199. cout << endl;
  200. cout << "URL:" << endl;
  201. cout << _(" You can specify multiple URLs. All URLs must point to the same file\n"
  202. " or downloading fails.") << endl;
  203. cout << endl;
  204. #ifdef ENABLE_BITTORRENT
  205. cout << "FILE:" << endl;
  206. cout << _(" Specify files in multi-file torrent to download. Use conjunction with\n"
  207. " -T option.") << endl;
  208. cout << endl;
  209. #endif // ENABLE_BITTORRENT
  210. cout << _("Examples:") << endl;
  211. cout << _(" Download a file by 1 connection:") << endl;
  212. cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
  213. cout << _(" Download a file by 2 connections:") << endl;
  214. cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
  215. cout << _(" Download a file by 2 connections, each connects to a different server:") << endl;
  216. cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
  217. cout << _(" You can mix up different protocols:") << endl;
  218. cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
  219. #ifdef ENABLE_BITTORRENT
  220. cout << _(" Download a torrent:") << endl;
  221. cout << " aria2c -t 180 -o test.torrent http://AAA.BBB.CCC/file.torrent" << endl;
  222. cout << _(" Download a torrent using local .torrent file:") << endl;
  223. cout << " aria2c -t 180 -T test.torrent" << endl;
  224. cout << _(" Download only selected files:") << endl;
  225. cout << " aria2c -t 180 -T test.torrent dir/file1.zip dir/file2.zip" << endl;
  226. cout << _(" Print file listing of .torrent file:") << endl;
  227. cout << " aria2c -t 180 -T test.torrent -S" << endl;
  228. cout << endl;
  229. #endif // ENABLE_BITTORRENT
  230. printf(_("Report bugs to %s"), "<tujikawa at users dot sourceforge dot net>");
  231. cout << endl;
  232. }
  233. int main(int argc, char* argv[]) {
  234. #ifdef ENABLE_NLS
  235. setlocale (LC_CTYPE, "");
  236. setlocale (LC_MESSAGES, "");
  237. bindtextdomain (PACKAGE, LOCALEDIR);
  238. textdomain (PACKAGE);
  239. #endif // ENABLE_NLS
  240. bool stdoutLog = false;
  241. string logfile;
  242. string dir = ".";
  243. string ufilename;
  244. int split = 1;
  245. bool daemonMode = false;
  246. string referer;
  247. string torrentFile;
  248. int listenPort = -1;
  249. Strings args;
  250. #ifdef ENABLE_BITTORRENT
  251. bool followTorrent = true;
  252. #else
  253. bool followTorrent = false;
  254. #endif // ENABLE_BITTORRENT
  255. int c;
  256. Option* op = new Option();
  257. op->put(PREF_RETRY_WAIT, "5");
  258. op->put(PREF_TIMEOUT, "60");
  259. op->put(PREF_PEER_CONNECTION_TIMEOUT, "60");
  260. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  261. op->put(PREF_MAX_TRIES, "5");
  262. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  263. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  264. op->put(PREF_FTP_USER, "anonymous");
  265. op->put(PREF_FTP_PASSWD, "ARIA2USER@");
  266. op->put(PREF_FTP_TYPE, V_BINARY);
  267. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  268. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  269. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  270. while(1) {
  271. int optIndex = 0;
  272. int lopt;
  273. static struct option longOpts[] = {
  274. { "daemon", no_argument, NULL, 'D' },
  275. { "dir", required_argument, NULL, 'd' },
  276. { "out", required_argument, NULL, 'o' },
  277. { "log", required_argument, NULL, 'l' },
  278. { "split", required_argument, NULL, 's' },
  279. { "timeout", required_argument, NULL, 't' },
  280. { "max-tries", required_argument, NULL, 'm' },
  281. { "http-proxy", required_argument, &lopt, 1 },
  282. { "http-user", required_argument, &lopt, 2 },
  283. { "http-passwd", required_argument, &lopt, 3 },
  284. { "http-proxy-user", required_argument, &lopt, 4 },
  285. { "http-proxy-passwd", required_argument, &lopt, 5 },
  286. { "http-auth-scheme", required_argument, &lopt, 6 },
  287. { "referer", required_argument, &lopt, 7 },
  288. { "retry-wait", required_argument, &lopt, 8 },
  289. { "ftp-user", required_argument, &lopt, 9 },
  290. { "ftp-passwd", required_argument, &lopt, 10 },
  291. { "ftp-type", required_argument, &lopt, 11 },
  292. { "ftp-pasv", no_argument, NULL, 'p' },
  293. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  294. { "min-segment-size", required_argument, &lopt, 13 },
  295. { "http-proxy-method", required_argument, &lopt, 14 },
  296. #ifdef ENABLE_BITTORRENT
  297. { "torrent-file", required_argument, NULL, 'T' },
  298. { "listen-port", required_argument, &lopt, 15 },
  299. { "follow-torrent", required_argument, &lopt, 16 },
  300. { "show-files", no_argument, NULL, 'S' },
  301. { "no-preallocation", no_argument, &lopt, 18 },
  302. { "direct-file-mapping", required_argument, &lopt, 19 },
  303. #endif // ENABLE_BITTORRENT
  304. { "version", no_argument, NULL, 'v' },
  305. { "help", no_argument, NULL, 'h' },
  306. { 0, 0, 0, 0 }
  307. };
  308. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vhST:", longOpts, &optIndex);
  309. if(c == -1) {
  310. break;
  311. }
  312. switch(c) {
  313. case 0:{
  314. switch(lopt) {
  315. case 1: {
  316. pair<string, string> proxy;
  317. Util::split(proxy, optarg, ':');
  318. int port = (int)strtol(proxy.second.c_str(), NULL, 10);
  319. if(proxy.first.empty() || proxy.second.empty() ||
  320. !(0 < port && port <= 65535)) {
  321. cerr << _("unrecognized proxy format") << endl;
  322. showUsage();
  323. exit(1);
  324. }
  325. op->put(PREF_HTTP_PROXY_HOST, proxy.first);
  326. op->put(PREF_HTTP_PROXY_PORT, Util::itos(port));
  327. op->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  328. break;
  329. }
  330. case 2:
  331. op->put(PREF_HTTP_USER, optarg);
  332. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  333. break;
  334. case 3:
  335. op->put(PREF_HTTP_PASSWD, optarg);
  336. break;
  337. case 4:
  338. op->put(PREF_HTTP_PROXY_USER, optarg);
  339. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  340. break;
  341. case 5:
  342. op->put(PREF_HTTP_PROXY_PASSWD, optarg);
  343. break;
  344. case 6:
  345. if(string(V_BASIC) == optarg) {
  346. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  347. } else {
  348. cerr << _("Currently, supported authentication scheme is basic.") << endl;
  349. }
  350. break;
  351. case 7:
  352. referer = optarg;
  353. break;
  354. case 8: {
  355. int wait = (int)strtol(optarg, NULL, 10);
  356. if(!(0 <= wait && wait <= 60)) {
  357. cerr << _("retry-wait must be between 0 and 60.") << endl;
  358. showUsage();
  359. exit(1);
  360. }
  361. op->put(PREF_RETRY_WAIT, Util::itos(wait));
  362. break;
  363. }
  364. case 9:
  365. op->put(PREF_FTP_USER, optarg);
  366. break;
  367. case 10:
  368. op->put(PREF_FTP_PASSWD, optarg);
  369. break;
  370. case 11:
  371. if(string(optarg) == V_BINARY || string(optarg) == V_ASCII) {
  372. op->put(PREF_FTP_TYPE, optarg);
  373. } else {
  374. cerr << _("ftp-type must be either 'binary' or 'ascii'.") << endl;
  375. showUsage();
  376. exit(1);
  377. }
  378. break;
  379. case 12:
  380. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  381. op->put(PREF_FTP_VIA_HTTP_PROXY, optarg);
  382. } else {
  383. cerr << _("ftp-via-http-proxy must be either 'get' or 'tunnel'.") << endl;
  384. showUsage();
  385. exit(1);
  386. }
  387. break;
  388. case 13: {
  389. string::size_type p = string(optarg).find_first_of("KM");
  390. int mult = 1;
  391. if(p != string::npos) {
  392. if(optarg[p] == 'K') {
  393. mult = 1024;
  394. } else if(optarg[p] == 'M') {
  395. mult = 1024*1024;
  396. }
  397. optarg[p] = '\0';
  398. }
  399. long long int size = strtoll(optarg, NULL, 10)*mult;
  400. if(size < 1024) {
  401. cerr << _("min-segment-size invalid") << endl;
  402. showUsage();
  403. exit(1);
  404. }
  405. op->put(PREF_MIN_SEGMENT_SIZE, Util::llitos(size));
  406. break;
  407. }
  408. case 14:
  409. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  410. op->put(PREF_HTTP_PROXY_METHOD, optarg);
  411. } else {
  412. cerr << _("http-proxy-method must be either 'get' or 'tunnel'.") << endl;
  413. showUsage();
  414. exit(1);
  415. }
  416. break;
  417. case 15:
  418. listenPort = (int)strtol(optarg, NULL, 10);
  419. if(!(1024 <= listenPort && listenPort <= 65535)) {
  420. cerr << _("listen-port must be between 1024 and 65535.") << endl;
  421. showUsage();
  422. exit(1);
  423. }
  424. break;
  425. case 16:
  426. if(string(optarg) == "true") {
  427. followTorrent = true;
  428. } else if(string(optarg) == "false") {
  429. followTorrent = false;
  430. } else {
  431. cerr << _("follow-torrent must be either 'true' or 'false'.") << endl;
  432. showUsage();
  433. exit(1);
  434. }
  435. break;
  436. case 18:
  437. op->put(PREF_NO_PREALLOCATION, V_TRUE);
  438. break;
  439. case 19:
  440. if(string(optarg) == "true") {
  441. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  442. } else if(string(optarg) == "false") {
  443. op->put(PREF_DIRECT_FILE_MAPPING, V_FALSE);
  444. } else {
  445. cerr << "direct-file-mapping must be either 'true' or 'false'." << endl;
  446. showUsage();
  447. exit(1);
  448. }
  449. }
  450. break;
  451. }
  452. case 'D':
  453. daemonMode = true;
  454. break;
  455. case 'd':
  456. dir = optarg;
  457. break;
  458. case 'o':
  459. ufilename = optarg;
  460. break;
  461. case 'l':
  462. if(strcmp("-", optarg) == 0) {
  463. stdoutLog = true;
  464. } else {
  465. logfile = optarg;
  466. }
  467. break;
  468. case 's':
  469. split = (int)strtol(optarg, NULL, 10);
  470. if(!(1 <= split && split <= 5)) {
  471. cerr << _("split must be between 1 and 5.") << endl;
  472. showUsage();
  473. exit(1);
  474. }
  475. break;
  476. case 't': {
  477. int timeout = (int)strtol(optarg, NULL, 10);
  478. if(1 <= timeout && timeout <= 600) {
  479. op->put(PREF_TIMEOUT, Util::itos(timeout));
  480. } else {
  481. cerr << _("timeout must be between 1 and 600") << endl;
  482. showUsage();
  483. exit(1);
  484. }
  485. break;
  486. }
  487. case 'm': {
  488. int retries = (int)strtol(optarg, NULL, 10);
  489. if(retries < 0) {
  490. cerr << _("max-tries invalid") << endl;
  491. showUsage();
  492. exit(1);
  493. }
  494. op->put(PREF_MAX_TRIES, Util::itos(retries));
  495. break;
  496. }
  497. case 'p':
  498. op->put(PREF_FTP_PASV_ENABLED, V_TRUE);
  499. break;
  500. case 'S':
  501. op->put(PREF_SHOW_FILES, V_TRUE);
  502. break;
  503. case 'T':
  504. torrentFile = string(optarg);
  505. break;
  506. case 'v':
  507. showVersion();
  508. exit(0);
  509. case 'h':
  510. showUsage();
  511. exit(0);
  512. default:
  513. showUsage();
  514. exit(1);
  515. }
  516. }
  517. if(torrentFile.empty()) {
  518. if(optind == argc) {
  519. cerr << _("specify at least one URL") << endl;
  520. showUsage();
  521. exit(1);
  522. }
  523. }
  524. if(daemonMode) {
  525. if(daemon(1, 1) < 0) {
  526. perror(_("daemon failed"));
  527. exit(1);
  528. }
  529. }
  530. for(int i = 1; optind+i-1 < argc; i++) {
  531. args.push_back(argv[optind+i-1]);
  532. }
  533. #ifdef HAVE_LIBSSL
  534. // for SSL initialization
  535. SSL_load_error_strings();
  536. SSL_library_init();
  537. #endif // HAVE_LIBSSL
  538. #ifdef HAVE_LIBGNUTLS
  539. gnutls_global_init();
  540. #endif // HAVE_LIBGNUTLS
  541. srandom(time(NULL));
  542. if(stdoutLog) {
  543. LogFactory::setLogFile("/dev/stdout");
  544. } else if(logfile.size()) {
  545. LogFactory::setLogFile(logfile);
  546. }
  547. // make sure logger is configured properly.
  548. try {
  549. LogFactory::getInstance();
  550. } catch(Exception* ex) {
  551. cerr << ex->getMsg() << endl;
  552. delete ex;
  553. exit(1);
  554. }
  555. SegmentSplitter* splitter = new SplitSlowestSegmentSplitter();
  556. splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  557. setSignalHander(SIGPIPE, SIG_IGN);
  558. bool readyToTorrentMode = false;
  559. string downloadedTorrentFile;
  560. if(torrentFile.empty()) {
  561. setSignalHander(SIGINT, handler);
  562. setSignalHander(SIGTERM, handler);
  563. e = new ConsoleDownloadEngine();
  564. e->option = op;
  565. e->segmentMan = new SegmentMan();
  566. e->segmentMan->diskWriter = new DefaultDiskWriter();
  567. e->segmentMan->dir = dir;
  568. e->segmentMan->ufilename = ufilename;
  569. e->segmentMan->option = op;
  570. e->segmentMan->splitter = splitter;
  571. Requests requests;
  572. int cuidCounter = 1;
  573. for(Strings::const_iterator itr = args.begin(); itr != args.end(); itr++) {
  574. for(int s = 1; s <= split; s++) {
  575. addCommand(cuidCounter, *itr, referer, requests);
  576. cuidCounter++;
  577. }
  578. }
  579. e->run();
  580. if(e->segmentMan->finished()) {
  581. printDownloadCompeleteMessage(e->segmentMan->getFilePath());
  582. if(Util::endsWith(e->segmentMan->getFilePath(), ".torrent")) {
  583. downloadedTorrentFile = e->segmentMan->getFilePath();
  584. readyToTorrentMode = true;
  585. }
  586. } else {
  587. printDownloadAbortMessage();
  588. }
  589. for_each(requests.begin(), requests.end(), clearRequest);
  590. requests.clear();
  591. delete(e->segmentMan);
  592. delete(e->segmentMan->diskWriter);
  593. delete(e);
  594. }
  595. if(!torrentFile.empty() || followTorrent && readyToTorrentMode) {
  596. try {
  597. //op->put(PREF_MAX_TRIES, "0");
  598. setSignalHander(SIGINT, torrentHandler);
  599. setSignalHander(SIGTERM, torrentHandler);
  600. Request* req = new Request();
  601. req->isTorrent = true;
  602. req->setTrackerEvent(Request::STARTED);
  603. te = new TorrentConsoleDownloadEngine();
  604. te->option = op;
  605. ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
  606. te->segmentMan = new SegmentMan();
  607. te->segmentMan->diskWriter = byteArrayDiskWriter;
  608. te->segmentMan->option = op;
  609. te->segmentMan->splitter = splitter;
  610. te->torrentMan = new TorrentMan();
  611. te->torrentMan->setStoreDir(dir);
  612. te->torrentMan->option = op;
  613. te->torrentMan->req = req;
  614. string targetTorrentFile = torrentFile.empty() ?
  615. downloadedTorrentFile : torrentFile;
  616. if(op->get(PREF_SHOW_FILES) == V_TRUE) {
  617. FileEntries fileEntries =
  618. te->torrentMan->readFileEntryFromMetaInfoFile(targetTorrentFile);
  619. cout << _("Files:") << endl;
  620. for(FileEntries::const_iterator itr = fileEntries.begin();
  621. itr != fileEntries.end(); itr++) {
  622. printf("%s %s Bytes\n", itr->path.c_str(),
  623. Util::llitos(itr->length, true).c_str());
  624. }
  625. exit(0);
  626. } else {
  627. Strings targetFiles;
  628. if(!torrentFile.empty() && !args.empty()) {
  629. targetFiles = args;
  630. }
  631. te->torrentMan->setup(targetTorrentFile, targetFiles);
  632. }
  633. PeerListenCommand* listenCommand =
  634. new PeerListenCommand(te->torrentMan->getNewCuid(), te);
  635. int port;
  636. if(listenPort == -1) {
  637. port = listenCommand->bindPort(6881, 6999);
  638. } else {
  639. port = listenCommand->bindPort(listenPort, listenPort);
  640. }
  641. if(port == -1) {
  642. printf(_("Errors occurred while binding port.\n"));
  643. exit(1);
  644. }
  645. te->torrentMan->setPort(port);
  646. te->commands.push(listenCommand);
  647. te->commands.push(new TrackerWatcherCommand(te->torrentMan->getNewCuid(),
  648. te));
  649. te->commands.push(new TrackerUpdateCommand(te->torrentMan->getNewCuid(),
  650. te));
  651. te->commands.push(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(),
  652. te,
  653. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  654. te->run();
  655. if(te->torrentMan->downloadComplete()) {
  656. printDownloadCompeleteMessage();
  657. } else {
  658. printDownloadAbortMessage();
  659. }
  660. delete(req);
  661. delete(te->segmentMan);
  662. delete(te->segmentMan->diskWriter);
  663. delete(te->torrentMan);
  664. delete(te);
  665. } catch(Exception* ex) {
  666. cerr << ex->getMsg() << endl;
  667. delete ex;
  668. exit(1);
  669. }
  670. }
  671. delete(op);
  672. delete(splitter);
  673. delete(LogFactory::getInstance());
  674. #ifdef HAVE_LIBGNUTLS
  675. gnutls_global_deinit();
  676. #endif // HAVE_LIBGNUTLS
  677. return 0;
  678. }