main.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "common.h"
  36. #include "LogFactory.h"
  37. #include "Util.h"
  38. #include "prefs.h"
  39. #include "FeatureConfig.h"
  40. #include "MultiUrlRequestInfo.h"
  41. #include "TorrentRequestInfo.h"
  42. #include "BitfieldManFactory.h"
  43. #include "SimpleRandomizer.h"
  44. #include "ConsoleFileAllocationMonitor.h"
  45. #include "Netrc.h"
  46. #include "RequestFactory.h"
  47. #include "OptionParser.h"
  48. #include "OptionHandlerFactory.h"
  49. #include "FatalException.h"
  50. #include "File.h"
  51. #include "CUIDCounter.h"
  52. #include "FileUriListParser.h"
  53. #include "StreamUriListParser.h"
  54. #include "CookieBoxFactory.h"
  55. #include "a2algo.h"
  56. #include "message.h"
  57. #include <deque>
  58. #include <algorithm>
  59. #include <time.h>
  60. #include <signal.h>
  61. #include <unistd.h>
  62. #include <libgen.h>
  63. #include <utility>
  64. #include <fstream>
  65. #include <sstream>
  66. extern char* optarg;
  67. extern int optind, opterr, optopt;
  68. #include <getopt.h>
  69. #ifdef ENABLE_METALINK
  70. #include "MetalinkRequestInfo.h"
  71. #include "Xml2MetalinkProcessor.h"
  72. #endif
  73. #ifdef HAVE_LIBSSL
  74. // for SSL
  75. # include <openssl/err.h>
  76. # include <openssl/ssl.h>
  77. #endif // HAVE_LIBSSL
  78. #ifdef HAVE_LIBGNUTLS
  79. # include <gnutls/gnutls.h>
  80. #endif // HAVE_LIBGNUTLS
  81. using namespace std;
  82. void showVersion() {
  83. cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl;
  84. cout << "**Configuration**" << endl;
  85. cout << FeatureConfig::getInstance()->getConfigurationSummary();
  86. cout << endl;
  87. cout << "Copyright (C) 2006, 2007 Tatsuhiro Tsujikawa" << endl;
  88. cout << endl;
  89. cout <<
  90. _("This program is free software; you can redistribute it and/or modify\n"
  91. "it under the terms of the GNU General Public License as published by\n"
  92. "the Free Software Foundation; either version 2 of the License, or\n"
  93. "(at your option) any later version.\n"
  94. "\n"
  95. "This program is distributed in the hope that it will be useful,\n"
  96. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  97. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  98. "GNU General Public License for more details.\n"
  99. "\n"
  100. "You should have received a copy of the GNU General Public License\n"
  101. "along with this program; if not, write to the Free Software\n"
  102. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n");
  103. cout << endl;
  104. printf(_("Contact Info: %s\n"), "Tatsuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>");
  105. cout << endl;
  106. }
  107. void showUsage() {
  108. printf(_("Usage: %s [options] URL ...\n"), PACKAGE_NAME);
  109. #ifdef ENABLE_BITTORRENT
  110. printf(_(" %s [options] -T TORRENT_FILE FILE ...\n"), PACKAGE_NAME);
  111. #endif // ENABLE_BITTORRENT
  112. #ifdef ENABLE_METALINK
  113. printf(_(" %s [options] -M METALINK_FILE\n"), PACKAGE_NAME);
  114. #endif // ENABLE_METALINK
  115. cout << endl;
  116. cout << _("Options:") << endl;
  117. cout << _(" -d, --dir=DIR The directory to store downloaded file.") << endl;
  118. cout << _(" -o, --out=FILE The file name for downloaded file.") << endl;
  119. cout << _(" -l, --log=LOG The file path to store log. If '-' is specified,\n"
  120. " log is written to stdout.") << endl;
  121. cout << _(" -D, --daemon Run as daemon.") << endl;
  122. cout << _(" -s, --split=N Download a file using N connections. N must be\n"
  123. " between 1 and 5. This option affects all URLs.\n"
  124. " Thus, aria2 connects to each URL with\n"
  125. " N connections.\n"
  126. " Default: 1") << endl;
  127. cout << _(" --retry-wait=SEC Set amount of time in second between requests\n"
  128. " for errors. Specify a value between 0 and 60.\n"
  129. " Default: 5") << endl;
  130. cout << _(" -t, --timeout=SEC Set timeout in second. Default: 60") << endl;
  131. cout << _(" -m, --max-tries=N Set number of tries. 0 means unlimited.\n"
  132. " Default: 5") << endl;
  133. /*
  134. cout << _(" --min-segment-size=SIZE[K|M] Set minimum segment size. You can append\n"
  135. " K or M(1K = 1024, 1M = 1024K). This\n"
  136. " value must be greater than or equal to\n"
  137. " 1024. Default: 1M") << endl;
  138. */
  139. cout << _(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all\n"
  140. " URLs.") << endl;
  141. cout << _(" --http-user=USER Set HTTP user. This affects to all URLs.") << endl;
  142. cout << _(" --http-passwd=PASSWD Set HTTP password. This affects to all URLs.") << endl;
  143. cout << _(" --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs") << endl;
  144. cout << _(" --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs.") << endl;
  145. cout << _(" --http-proxy-method=METHOD Set the method to use in proxy request.\n"
  146. " METHOD is either 'get' or 'tunnel'.\n"
  147. " Default: tunnel") << endl;
  148. cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
  149. " is the only supported scheme.\n"
  150. " Default: basic") << endl;
  151. cout << _(" --referer=REFERER Set Referer. This affects to all URLs.") << endl;
  152. cout << _(" --ftp-user=USER Set FTP user. This affects to all URLs.\n"
  153. " Default: anonymous") << endl;
  154. cout << _(" --ftp-passwd=PASSWD Set FTP password. This affects to all URLs.\n"
  155. " Default: ARIA2USER@") << endl;
  156. cout << _(" --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'\n"
  157. " or 'ascii'.\n"
  158. " Default: binary") << endl;
  159. cout << _(" -p, --ftp-pasv Use passive mode in FTP.") << endl;
  160. cout << _(" --ftp-via-http-proxy=METHOD Use HTTP proxy in FTP. METHOD is either 'get' or\n"
  161. " 'tunnel'.\n"
  162. " Default: tunnel") << endl;
  163. cout << _(" --lowest-speed-limit=SPEED Close connection if download speed is lower than\n"
  164. " or equal to this value(bytes per sec).\n"
  165. " 0 means aria2 does not care lowest speed limit.\n"
  166. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  167. " This option does not affect BitTorrent download.\n"
  168. " Default: 0") << endl;
  169. cout << _(" --max-download-limit=SPEED Set max download speed in bytes per sec.\n"
  170. " 0 means unrestricted.\n"
  171. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  172. " Default: 0") << endl;
  173. cout << _(" --file-allocation=METHOD Specify file allocation method. METHOD is either\n"
  174. " 'none' or 'prealloc'.\n"
  175. " 'none' doesn't pre-allocate file space. 'prealloc'\n"
  176. " pre-allocates file space before download begins.\n"
  177. " This may take some time depending on the size of\n"
  178. " file.\n"
  179. " Default: none") << endl;
  180. cout << _(" --allow-overwrite=true|false If this option set to false, aria2 doesn't\n"
  181. " download a file which already exists in the file\n"
  182. " system but its corresponding .aria2 file doesn't\n"
  183. " exist.\n"
  184. " Default: false") << endl;
  185. #ifdef ENABLE_MESSAGE_DIGEST
  186. cout << _(" --check-integrity=true|false Check file integrity by validating piece hash.\n"
  187. " This option makes effect in BitTorrent download\n"
  188. " and Metalink with chunk checksums.\n"
  189. " Use this option to redownload a damaged portion of\n"
  190. " file.\n"
  191. " You may need to specify --allow-overwrite=true\n"
  192. " option if .aria2 file doesn't exist.\n"
  193. " Default: false") << endl;
  194. cout << _(" --realtime-chunk-checksum=true|false Validate chunk checksum while downloading\n"
  195. " a file in Metalink mode. This option makes effect\n"
  196. " in Metalink with chunk checksums.\n"
  197. " Default: true") << endl;
  198. #endif // ENABLE_MESSAGE_DIGEST
  199. cout << _(" -c, --continue Continue downloading a partially downloaded\n"
  200. " file. Use this option to resume a download started\n"
  201. " by web browsers or another programs\n"
  202. " which download files sequentially from the\n"
  203. " beginning. Currently this option is applicable to\n"
  204. " http(s)/ftp downloads.") << endl;
  205. cout << _(" -U, --user-agent=USER_AGENT Set user agent for http(s) downloads.") << endl;
  206. cout << _(" -n, --no-netrc Disables netrc support.") << endl;
  207. cout << _(" -i, --input-file=FILE Downloads URIs found in FILE. You can specify\n"
  208. " multiple URIs for a single entity: deliminate\n"
  209. " URIs by Tab in a single line.\n"
  210. " Reads input from stdin when '-' is specified.") << endl;
  211. cout << _(" -j, --max-concurrent-downloads=N Set maximum number of concurrent downloads.\n"
  212. " It should be used with -i option.\n"
  213. " Default: 5") << endl;
  214. cout << _(" --load-cookies=FILE Load cookies from FILE. The format of FILE is\n"
  215. " one used by Netscape and Mozilla.") << endl;
  216. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  217. cout << _(" -S, --show-files Print file listing of .torrent or .metalink file\n"
  218. " and exit.") << endl;
  219. cout << _(" --select-file=INDEX... Set file to download by specifing its index.\n"
  220. " You can know file index through --show-files\n"
  221. " option. Multiple indexes can be specified by using\n"
  222. " ',' like \"3,6\".\n"
  223. " You can also use '-' to specify rangelike \"1-5\".\n"
  224. " ',' and '-' can be used together.\n"
  225. " When used with -M option, index may vary depending\n"
  226. " on the query(see --metalink-* options).") << endl;
  227. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  228. #ifdef ENABLE_BITTORRENT
  229. cout << _(" -T, --torrent-file=TORRENT_FILE The file path to .torrent file.") << endl;
  230. cout << _(" --follow-torrent=true|false Setting this option to false prevents aria2 to\n"
  231. " enter BitTorrent mode even if the filename of\n"
  232. " downloaded file ends with .torrent.\n"
  233. " Default: true") << endl;
  234. cout << _(" --direct-file-mapping=true|false Directly read from and write to each file\n"
  235. " mentioned in .torrent file.\n"
  236. " Default: true") << endl;
  237. cout << _(" --listen-port=PORT Set port number to listen to for peer connection.\n"
  238. " Default: 6881-6999") << endl;
  239. cout << _(" --max-upload-limit=SPEED Set max upload speed in bytes per sec.\n"
  240. " 0 means unrestricted.\n"
  241. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  242. " Default: 0") << endl;
  243. cout << _(" --seed-time=MINUTES Specify seeding time in minutes. See also\n"
  244. " --seed-ratio option.") << endl;
  245. cout << _(" --seed-ratio=RATIO Specify share ratio. Seed completed torrents until\n"
  246. " share ratio reaches RATIO. 1.0 is encouraged.\n"
  247. " If --seed-time option is specified along with\n"
  248. " this option, seeding ends when at least one of\n"
  249. " the conditions is satisfied.") << endl;
  250. #endif // ENABLE_BITTORRENT
  251. #ifdef ENABLE_METALINK
  252. cout << _(" -M, --metalink-file=METALINK_FILE The file path to .metalink file.") << endl;
  253. cout << _(" -C, --metalink-servers=NUM_SERVERS The number of servers to connect to\n"
  254. " simultaneously.\n"
  255. " Default: 5") << endl;
  256. cout << _(" --metalink-version=VERSION The version of file to download.") << endl;
  257. cout << _(" --metalink-language=LANGUAGE The language of file to download.") << endl;
  258. cout << _(" --metalink-os=OS The operating system the file is targeted.") << endl;
  259. cout << _(" --metalink-location=LOCATION The location of the prefered server.") << endl;
  260. cout << _(" --follow-metalink=true|false Setting this option to false prevents aria2 to\n"
  261. " enter Metalink mode even if the filename of\n"
  262. " downloaded file ends with .metalink.\n"
  263. " Default: true") << endl;
  264. #endif // ENABLE_METALINK
  265. cout << _(" -v, --version Print the version number and exit.") << endl;
  266. cout << _(" -h, --help Print this message and exit.") << endl;
  267. cout << endl;
  268. cout << "URL:" << endl;
  269. cout << _(" You can specify multiple URLs. All URLs must point to the same file\n"
  270. " or downloading fails.") << endl;
  271. cout << endl;
  272. #ifdef ENABLE_BITTORRENT
  273. cout << "FILE:" << endl;
  274. cout << _(" Specify files in multi-file torrent to download. Use conjunction with\n"
  275. " -T option. This arguments are ignored if you specify --select-file option.") << endl;
  276. cout << endl;
  277. #endif // ENABLE_BITTORRENT
  278. cout << _("Examples:") << endl;
  279. cout << _(" Download a file by 1 connection:") << endl;
  280. cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
  281. cout << _(" Download a file by 2 connections:") << endl;
  282. cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
  283. cout << _(" Download a file by 2 connections, each connects to a different server:") << endl;
  284. cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
  285. cout << _(" You can mix up different protocols:") << endl;
  286. cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
  287. #ifdef ENABLE_BITTORRENT
  288. cout << endl;
  289. cout << _(" Download a torrent:") << endl;
  290. cout << " aria2c -o test.torrent http://AAA.BBB.CCC/file.torrent" << endl;
  291. cout << _(" Download a torrent using local .torrent file:") << endl;
  292. cout << " aria2c -T test.torrent" << endl;
  293. cout << _(" Download only selected files:") << endl;
  294. cout << " aria2c -T test.torrent dir/file1.zip dir/file2.zip" << endl;
  295. cout << _(" Print file listing of .torrent file:") << endl;
  296. cout << " aria2c -T test.torrent -S" << endl;
  297. #endif // ENABLE_BITTORRENT
  298. #ifdef ENABLE_METALINK
  299. cout << endl;
  300. cout << _(" Metalink downloading:") << endl;
  301. cout << " aria2c http://AAA.BBB.CCC/file.metalink" << endl;
  302. cout << _(" Download a file using local .metalink file:") << endl;
  303. cout << " aria2c -M test.metalink" << endl;
  304. cout << _(" Metalink downloading with preferences:") << endl;
  305. cout << " aria2c -M test.metalink --metalink-version=1.1.1 --metalink-language=en-US" << endl;
  306. cout << _(" Download only selected files:") << endl;
  307. cout << " aria2c -M test.metalink --metalink-language=en-US dir/file1.zip dir/file2.zip" << endl;
  308. cout << _(" Download only selected files using index:") << endl;
  309. cout << " aria2c -M test.metalink --metalink-language=en-US --select-file 1,3-5" << endl;
  310. cout << _(" Print file listing of .metalink file:") << endl;
  311. cout << " aria2c -M test.metalink -S --metalink-language=en-US" << endl;
  312. #endif // ENABLE_METALINK
  313. cout << endl;
  314. printf(_("Report bugs to %s"), "<tujikawa at users dot sourceforge dot net>");
  315. cout << endl;
  316. }
  317. int main(int argc, char* argv[]) {
  318. #ifdef ENABLE_NLS
  319. setlocale (LC_CTYPE, "");
  320. setlocale (LC_MESSAGES, "");
  321. bindtextdomain (PACKAGE, LOCALEDIR);
  322. textdomain (PACKAGE);
  323. #endif // ENABLE_NLS
  324. stringstream cmdstream;
  325. int c;
  326. Option* op = new Option();
  327. op->put(PREF_STDOUT_LOG, V_FALSE);
  328. op->put(PREF_DIR, ".");
  329. op->put(PREF_SPLIT, "1");
  330. op->put(PREF_DAEMON, V_FALSE);
  331. op->put(PREF_SEGMENT_SIZE, Util::itos(1024*1024));
  332. op->put(PREF_HTTP_KEEP_ALIVE, V_FALSE);
  333. op->put(PREF_LISTEN_PORT, "-1");
  334. op->put(PREF_METALINK_SERVERS, "5");
  335. op->put(PREF_FOLLOW_TORRENT,
  336. #ifdef ENABLE_BITTORRENT
  337. V_TRUE
  338. #else
  339. V_FALSE
  340. #endif // ENABLE_BITTORRENT
  341. );
  342. op->put(PREF_FOLLOW_METALINK,
  343. #ifdef ENABLE_METALINK
  344. V_TRUE
  345. #else
  346. V_FALSE
  347. #endif // ENABLE_METALINK
  348. );
  349. op->put(PREF_RETRY_WAIT, "5");
  350. op->put(PREF_TIMEOUT, "60");
  351. op->put(PREF_DNS_TIMEOUT, "10");
  352. op->put(PREF_PEER_CONNECTION_TIMEOUT, "60");
  353. op->put(PREF_BT_TIMEOUT, "180");
  354. op->put(PREF_BT_REQUEST_TIMEOUT, "60");
  355. op->put(PREF_BT_KEEP_ALIVE_INTERVAL, "120");
  356. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  357. op->put(PREF_MAX_TRIES, "5");
  358. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  359. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  360. op->put(PREF_FTP_TYPE, V_BINARY);
  361. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  362. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  363. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  364. op->put(PREF_LOWEST_SPEED_LIMIT, "0");
  365. op->put(PREF_MAX_DOWNLOAD_LIMIT, "0");
  366. op->put(PREF_MAX_UPLOAD_LIMIT, "0");
  367. op->put(PREF_STARTUP_IDLE_TIME, "10");
  368. op->put(PREF_TRACKER_MAX_TRIES, "10");
  369. op->put(PREF_FILE_ALLOCATION, V_NONE);
  370. op->put(PREF_ALLOW_OVERWRITE, V_FALSE);
  371. op->put(PREF_REALTIME_CHUNK_CHECKSUM, V_TRUE);
  372. op->put(PREF_CHECK_INTEGRITY, V_FALSE);
  373. op->put(PREF_NETRC_PATH, Util::getHomeDir()+"/.netrc");
  374. op->put(PREF_CONTINUE, V_FALSE);
  375. op->put(PREF_USER_AGENT, "aria2");
  376. op->put(PREF_NO_NETRC, V_FALSE);
  377. op->put(PREF_MAX_CONCURRENT_DOWNLOADS, "5");
  378. op->put(PREF_DIRECT_DOWNLOAD_TIMEOUT, "15");
  379. while(1) {
  380. int optIndex = 0;
  381. int lopt;
  382. static struct option longOpts[] = {
  383. { "daemon", no_argument, NULL, 'D' },
  384. { "dir", required_argument, NULL, 'd' },
  385. { "out", required_argument, NULL, 'o' },
  386. { "log", required_argument, NULL, 'l' },
  387. { "split", required_argument, NULL, 's' },
  388. { "timeout", required_argument, NULL, 't' },
  389. { "max-tries", required_argument, NULL, 'm' },
  390. { "http-proxy", required_argument, &lopt, 1 },
  391. { "http-user", required_argument, &lopt, 2 },
  392. { "http-passwd", required_argument, &lopt, 3 },
  393. { "http-proxy-user", required_argument, &lopt, 4 },
  394. { "http-proxy-passwd", required_argument, &lopt, 5 },
  395. { "http-auth-scheme", required_argument, &lopt, 6 },
  396. { "referer", required_argument, &lopt, 7 },
  397. { "retry-wait", required_argument, &lopt, 8 },
  398. { "ftp-user", required_argument, &lopt, 9 },
  399. { "ftp-passwd", required_argument, &lopt, 10 },
  400. { "ftp-type", required_argument, &lopt, 11 },
  401. { "ftp-pasv", no_argument, NULL, 'p' },
  402. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  403. //{ "min-segment-size", required_argument, &lopt, 13 },
  404. { "http-proxy-method", required_argument, &lopt, 14 },
  405. { "lowest-speed-limit", required_argument, &lopt, 200 },
  406. { "max-download-limit", required_argument, &lopt, 201 },
  407. { "file-allocation", required_argument, 0, 'a' },
  408. { "allow-overwrite", required_argument, &lopt, 202 },
  409. #ifdef ENABLE_MESSAGE_DIGEST
  410. { "check-integrity", required_argument, &lopt, 203 },
  411. { "realtime-chunk-checksum", required_argument, &lopt, 204 },
  412. #endif // ENABLE_MESSAGE_DIGEST
  413. { "continue", no_argument, 0, 'c' },
  414. { "user-agent", required_argument, 0, 'U' },
  415. { "no-netrc", no_argument, 0, 'n' },
  416. { "input-file", required_argument, 0, 'i' },
  417. { "max-concurrent-downloads", required_argument, 0, 'j' },
  418. { "load-cookies", required_argument, &lopt, 205 },
  419. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  420. { "show-files", no_argument, NULL, 'S' },
  421. { "select-file", required_argument, &lopt, 21 },
  422. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  423. #ifdef ENABLE_BITTORRENT
  424. { "torrent-file", required_argument, NULL, 'T' },
  425. { "listen-port", required_argument, &lopt, 15 },
  426. { "follow-torrent", required_argument, &lopt, 16 },
  427. { "no-preallocation", no_argument, &lopt, 18 },
  428. { "direct-file-mapping", required_argument, &lopt, 19 },
  429. // TODO remove upload-limit.
  430. //{ "upload-limit", required_argument, &lopt, 20 },
  431. { "seed-time", required_argument, &lopt, 22 },
  432. { "seed-ratio", required_argument, &lopt, 23 },
  433. { "max-upload-limit", required_argument, &lopt, 24 },
  434. #endif // ENABLE_BITTORRENT
  435. #ifdef ENABLE_METALINK
  436. { "metalink-file", required_argument, NULL, 'M' },
  437. { "metalink-servers", required_argument, NULL, 'C' },
  438. { "metalink-version", required_argument, &lopt, 100 },
  439. { "metalink-language", required_argument, &lopt, 101 },
  440. { "metalink-os", required_argument, &lopt, 102 },
  441. { "follow-metalink", required_argument, &lopt, 103 },
  442. { "metalink-location", required_argument, &lopt, 104 },
  443. #endif // ENABLE_METALINK
  444. { "version", no_argument, NULL, 'v' },
  445. { "help", no_argument, NULL, 'h' },
  446. { 0, 0, 0, 0 }
  447. };
  448. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vhST:M:C:a:cU:ni:j:", longOpts, &optIndex);
  449. if(c == -1) {
  450. break;
  451. }
  452. switch(c) {
  453. case 0:{
  454. switch(lopt) {
  455. case 1:
  456. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  457. break;
  458. case 2:
  459. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  460. break;
  461. case 3:
  462. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  463. break;
  464. case 4:
  465. cmdstream << PREF_HTTP_PROXY_USER << "=" << optarg << "\n";
  466. break;
  467. case 5:
  468. cmdstream << PREF_HTTP_PROXY_PASSWD << "=" << optarg << "\n";
  469. break;
  470. case 6:
  471. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  472. break;
  473. case 7:
  474. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  475. break;
  476. case 8:
  477. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  478. break;
  479. case 9:
  480. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  481. break;
  482. case 10:
  483. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  484. break;
  485. case 11:
  486. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  487. break;
  488. case 12:
  489. cmdstream << PREF_FTP_VIA_HTTP_PROXY << "=" << optarg << "\n";
  490. break;
  491. case 13:
  492. cmdstream << PREF_MIN_SEGMENT_SIZE << "=" << optarg << "\n";
  493. break;
  494. case 14:
  495. cmdstream << PREF_HTTP_PROXY_METHOD << "=" << optarg << "\n";
  496. break;
  497. case 15:
  498. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  499. break;
  500. case 16:
  501. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  502. break;
  503. case 18:
  504. cmdstream << PREF_NO_PREALLOCATION << "=" << V_TRUE << "\n";
  505. break;
  506. case 19:
  507. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  508. break;
  509. case 21:
  510. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  511. break;
  512. case 22:
  513. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  514. break;
  515. case 23:
  516. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  517. break;
  518. case 24:
  519. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  520. break;
  521. case 100:
  522. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  523. break;
  524. case 101:
  525. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  526. break;
  527. case 102:
  528. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  529. break;
  530. case 103:
  531. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  532. break;
  533. case 104:
  534. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  535. break;
  536. case 200:
  537. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  538. break;
  539. case 201:
  540. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  541. break;
  542. case 202:
  543. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  544. break;
  545. case 203:
  546. cmdstream << PREF_CHECK_INTEGRITY << "=" << optarg << "\n";
  547. break;
  548. case 204:
  549. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  550. break;
  551. case 205:
  552. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  553. break;
  554. }
  555. break;
  556. }
  557. case 'D':
  558. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  559. break;
  560. case 'd':
  561. cmdstream << PREF_DIR << "=" << optarg << "\n";
  562. break;
  563. case 'o':
  564. cmdstream << PREF_OUT << "=" << optarg << "\n";
  565. break;
  566. case 'l':
  567. cmdstream << PREF_LOG << "=" << optarg << "\n";
  568. break;
  569. case 's':
  570. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  571. break;
  572. case 't':
  573. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  574. break;
  575. case 'm':
  576. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  577. break;
  578. case 'p':
  579. cmdstream << PREF_FTP_PASV << "=" << V_TRUE << "\n";
  580. break;
  581. case 'S':
  582. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  583. break;
  584. case 'T':
  585. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  586. break;
  587. case 'M':
  588. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  589. break;
  590. case 'C':
  591. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  592. break;
  593. case 'a':
  594. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  595. break;
  596. case 'c':
  597. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  598. break;
  599. case 'U':
  600. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  601. break;
  602. case 'n':
  603. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  604. break;
  605. case 'i':
  606. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  607. break;
  608. case 'j':
  609. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  610. break;
  611. case 'v':
  612. showVersion();
  613. exit(EXIT_SUCCESS);
  614. case 'h':
  615. showUsage();
  616. exit(EXIT_SUCCESS);
  617. default:
  618. exit(EXIT_FAILURE);
  619. }
  620. }
  621. {
  622. OptionParser oparser;
  623. oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
  624. string cfname = Util::getHomeDir()+"/.aria2/aria2.conf";
  625. ifstream cfstream(cfname.c_str());
  626. try {
  627. oparser.parse(op, cfstream);
  628. } catch(Exception* e) {
  629. cerr << "Parse error in " << cfname << endl;
  630. cerr << e->getMsg() << endl;
  631. delete e;
  632. exit(EXIT_FAILURE);
  633. }
  634. try {
  635. oparser.parse(op, cmdstream);
  636. } catch(Exception* e) {
  637. cerr << e->getMsg() << endl;
  638. delete e;
  639. exit(EXIT_FAILURE);
  640. }
  641. }
  642. if(op->defined(PREF_HTTP_USER)) {
  643. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  644. }
  645. if(op->defined(PREF_HTTP_PROXY_USER)) {
  646. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  647. }
  648. if(
  649. #ifdef ENABLE_BITTORRENT
  650. !op->defined(PREF_TORRENT_FILE) &&
  651. #endif // ENABLE_BITTORRENT
  652. #ifdef ENABLE_METALINK
  653. !op->defined(PREF_METALINK_FILE) &&
  654. #endif // ENABLE_METALINK
  655. !op->defined(PREF_INPUT_FILE)) {
  656. if(optind == argc) {
  657. cerr << _("specify at least one URL") << endl;
  658. exit(EXIT_FAILURE);
  659. }
  660. }
  661. if(op->getAsBool(PREF_DAEMON)) {
  662. if(daemon(1, 1) < 0) {
  663. perror(_("daemon failed"));
  664. exit(EXIT_FAILURE);
  665. }
  666. }
  667. Strings args(argv+optind, argv+argc);
  668. #ifdef HAVE_LIBSSL
  669. // for SSL initialization
  670. SSL_load_error_strings();
  671. SSL_library_init();
  672. #endif // HAVE_LIBSSL
  673. #ifdef HAVE_LIBGNUTLS
  674. gnutls_global_init();
  675. #endif // HAVE_LIBGNUTLS
  676. #ifdef ENABLE_METALINK
  677. xmlInitParser();
  678. #endif // ENABLE_METALINK
  679. SimpleRandomizer::init();
  680. BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance());
  681. FileAllocationMonitorFactory::setFactory(new ConsoleFileAllocationMonitorFactory());
  682. if(op->getAsBool(PREF_STDOUT_LOG)) {
  683. LogFactory::setLogFile("/dev/stdout");
  684. } else if(op->get(PREF_LOG).size()) {
  685. LogFactory::setLogFile(op->get(PREF_LOG));
  686. } else {
  687. LogFactory::setLogFile("/dev/null");
  688. }
  689. int exitStatus = EXIT_SUCCESS;
  690. try {
  691. Logger* logger = LogFactory::getInstance();
  692. logger->info("%s %s", PACKAGE, PACKAGE_VERSION);
  693. logger->info("Logging started.");
  694. RequestFactoryHandle requestFactory = new RequestFactory();
  695. requestFactory->setOption(op);
  696. File netrccf(op->get(PREF_NETRC_PATH));
  697. if(!op->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  698. mode_t mode = netrccf.mode();
  699. if(mode&(S_IRWXG|S_IRWXO)) {
  700. logger->notice(".netrc file %s does not have correct permissions. It should be 600. netrc support disabled.",
  701. op->get(PREF_NETRC_PATH).c_str());
  702. } else {
  703. NetrcHandle netrc = new Netrc();
  704. netrc->parse(op->get(PREF_NETRC_PATH));
  705. requestFactory->setNetrc(netrc);
  706. }
  707. }
  708. CookieBoxFactoryHandle cookieBoxFactory = new CookieBoxFactory();
  709. CookieBoxFactorySingletonHolder::instance(cookieBoxFactory);
  710. if(op->defined(PREF_LOAD_COOKIES)) {
  711. File cookieFile(op->get(PREF_LOAD_COOKIES));
  712. if(cookieFile.isFile()) {
  713. ifstream in(op->get(PREF_LOAD_COOKIES).c_str());
  714. CookieBoxFactorySingletonHolder::instance()->loadDefaultCookie(in);
  715. } else {
  716. logger->error("Failed to load cookies from %s", op->get(PREF_LOAD_COOKIES).c_str());
  717. exit(EXIT_FAILURE);
  718. }
  719. }
  720. RequestFactorySingletonHolder::instance(requestFactory);
  721. CUIDCounterHandle cuidCounter = new CUIDCounter();
  722. CUIDCounterSingletonHolder::instance(cuidCounter);
  723. Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
  724. RequestInfo* firstReqInfo;
  725. #ifdef ENABLE_BITTORRENT
  726. if(op->defined(PREF_TORRENT_FILE)) {
  727. firstReqInfo = new TorrentRequestInfo(op->get(PREF_TORRENT_FILE),
  728. op);
  729. Strings targetFiles;
  730. if(op->defined(PREF_TORRENT_FILE) && !args.empty()) {
  731. targetFiles = args;
  732. }
  733. ((TorrentRequestInfo*)firstReqInfo)->setTargetFiles(targetFiles);
  734. }
  735. else
  736. #endif // ENABLE_BITTORRENT
  737. #ifdef ENABLE_METALINK
  738. if(op->defined(PREF_METALINK_FILE)) {
  739. firstReqInfo = new MetalinkRequestInfo(op->get(PREF_METALINK_FILE),
  740. op);
  741. Strings targetFiles;
  742. if(op->defined(PREF_METALINK_FILE) && !args.empty()) {
  743. targetFiles = args;
  744. }
  745. ((MetalinkRequestInfo*)firstReqInfo)->setTargetFiles(targetFiles);
  746. }
  747. else
  748. #endif // ENABLE_METALINK
  749. if(op->defined(PREF_INPUT_FILE)) {
  750. SharedHandle<UriListParser> flparser(0);
  751. if(op->get(PREF_INPUT_FILE) == "-") {
  752. flparser = new StreamUriListParser(cin);
  753. } else {
  754. if(!File(op->get(PREF_INPUT_FILE)).isFile()) {
  755. throw new FatalException(EX_FILE_OPEN, op->get(PREF_INPUT_FILE).c_str(), "No such file");
  756. }
  757. flparser = new FileUriListParser(op->get(PREF_INPUT_FILE));
  758. }
  759. RequestGroups groups;
  760. while(flparser->hasNext()) {
  761. Strings uris = flparser->next();
  762. if(!uris.empty()) {
  763. Strings xuris;
  764. ncopy(uris.begin(), uris.end(), op->getAsInt(PREF_SPLIT),
  765. back_inserter(xuris));
  766. RequestGroupHandle rg = new RequestGroup(xuris, op);
  767. groups.push_back(rg);
  768. }
  769. }
  770. firstReqInfo = new MultiUrlRequestInfo(groups, op);
  771. }
  772. else
  773. {
  774. Strings xargs;
  775. ncopy(args.begin(), args.end(), op->getAsInt(PREF_SPLIT),
  776. back_inserter(xargs));
  777. firstReqInfo = new MultiUrlRequestInfo(xargs, op);
  778. }
  779. RequestInfos reqInfos;
  780. if(firstReqInfo) {
  781. reqInfos.push_front(firstReqInfo);
  782. }
  783. while(reqInfos.size()) {
  784. RequestInfoHandle reqInfo = reqInfos.front();
  785. reqInfos.pop_front();
  786. RequestInfos nextReqInfos = reqInfo->execute();
  787. copy(nextReqInfos.begin(), nextReqInfos.end(), front_inserter(reqInfos));
  788. /*
  789. if(reqInfo->isFail()) {
  790. exitStatus = EXIT_FAILURE;
  791. } else if(reqInfo->getFileInfo().checkReady()) {
  792. cout << _("Now verifying checksum.\n"
  793. "This may take some time depending on your PC environment"
  794. " and the size of file.") << endl;
  795. if(reqInfo->getFileInfo().check()) {
  796. cout << _("checksum OK.") << endl;
  797. } else {
  798. // TODO
  799. cout << _("checksum ERROR.") << endl;
  800. exitStatus = EXIT_FAILURE;
  801. }
  802. }
  803. */
  804. }
  805. } catch(Exception* ex) {
  806. cerr << "Exception caught:\n" << ex->getMsg() << endl;
  807. delete ex;
  808. exit(EXIT_FAILURE);
  809. }
  810. delete op;
  811. LogFactory::release();
  812. #ifdef HAVE_LIBGNUTLS
  813. gnutls_global_deinit();
  814. #endif // HAVE_LIBGNUTLS
  815. #ifdef ENABLE_METALINK
  816. xmlCleanupParser();
  817. #endif // ENABLE_METALINK
  818. FeatureConfig::release();
  819. return exitStatus;
  820. }