* EepGet:

- Convert to getopt (ticket #1173)
   - New option -c for clearnet (no proxy), same as -p:0
   - Proxy option -p with host name arg only (no ':') now allowed
   - Proxy password option is now -x, not the second arg to -u
   - Prompt for proxy password if not supplied in options
   - Line length option is now -l, not the second arg to -m
   - Error on nonproxied .onion hosts
   - Update man page, sort options (ticket #1173)
This commit is contained in:
zzz
2014-02-05 16:10:56 +00:00
parent 839bd51bc8
commit 4998f86efe
2 changed files with 161 additions and 51 deletions

View File

@ -1,16 +1,19 @@
package net.i2p.util; package net.i2p.util;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException;
import java.net.URL; import java.net.URL;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,6 +22,8 @@ import java.util.Formatter;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import gnu.getopt.Getopt;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.data.Base64; import net.i2p.data.Base64;
import net.i2p.data.ByteArray; import net.i2p.data.ByteArray;
@ -167,52 +172,96 @@ public class EepGet {
List<String> extra = null; List<String> extra = null;
String username = null; String username = null;
String password = null; String password = null;
boolean error = false;
//
// note: if you add options, please update installer/resources/man/eepget.1
//
Getopt g = new Getopt("eepget", args, "p:cn:t:e:o:m:l:h:u:x:");
try { try {
for (int i = 0; i < args.length; i++) { int c;
if (args[i].equals("-p")) { while ((c = g.getopt()) != -1) {
proxyHost = args[++i].substring(0, args[i].indexOf(':')); switch (c) {
String port = args[i].substring(args[i].indexOf(':')+1); case 'p':
proxyPort = Integer.parseInt(port); String s = g.getOptarg();
} else if (args[i].equals("-n")) { int colon = s.indexOf(':');
numRetries = Integer.parseInt(args[i+1]); if (colon >= 0) {
i++; // Todo IPv6 [a:b:c]:4444
} else if (args[i].equals("-t")) { proxyHost = s.substring(0, colon);
inactivityTimeout = 1000 * Integer.parseInt(args[i+1]); String port = s.substring(colon + 1);
i++; proxyPort = Integer.parseInt(port);
} else if (args[i].equals("-e")) { } else {
etag = "\"" + args[i+1] + "\""; proxyHost = s;
i++; // proxyPort remains default
} else if (args[i].equals("-o")) { }
saveAs = args[i+1]; break;
i++;
} else if (args[i].equals("-m")) { case 'c':
markSize = Integer.parseInt(args[++i]); // no proxy, same as -p :0
lineLen = Integer.parseInt(args[++i]); proxyHost = "";
} else if (args[i].equals("-h")) { proxyPort = 0;
break;
case 'n':
numRetries = Integer.parseInt(g.getOptarg());
break;
case 't':
inactivityTimeout = 1000 * Integer.parseInt(g.getOptarg());
break;
case 'e':
etag = "\"" + g.getOptarg() + "\"";
break;
case 'o':
saveAs = g.getOptarg();
break;
case 'm':
markSize = Integer.parseInt(g.getOptarg());
break;
case 'l':
lineLen = Integer.parseInt(g.getOptarg());
break;
case 'h':
if (extra == null) if (extra == null)
extra = new ArrayList<String>(2); extra = new ArrayList<String>(2);
extra.add(args[++i]); String a = g.getOptarg();
extra.add(args[++i]); String key = a.substring(0, a.indexOf('='));
} else if (args[i].equals("-u")) { String val = a.substring(a.indexOf('=')+1);
username = args[++i]; extra.add(key);
password = args[++i]; extra.add(val);
} else if (args[i].startsWith("-")) { break;
usage();
return; case 'u':
} else { username = g.getOptarg();
url = args[i]; break;
}
} case 'x':
password = g.getOptarg();
break;
case '?':
case ':':
default:
error = true;
break;
} // switch
} // while
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
usage(); error = true;
return;
} }
if (url == null) { int remaining = args.length - g.getOptind();
if (error || remaining != 1) {
usage(); usage();
return; System.exit(1);
} }
url = args[g.getOptind()];
if (saveAs == null) if (saveAs == null)
saveAs = suggestName(url); saveAs = suggestName(url);
@ -222,8 +271,23 @@ public class EepGet {
get.addHeader(extra.get(i), extra.get(i + 1)); get.addHeader(extra.get(i), extra.get(i + 1));
} }
} }
if (username != null && password != null) if (username != null) {
if (password == null) {
try {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
do {
System.err.print("Proxy password: ");
password = r.readLine();
if (password == null)
throw new IOException();
password = password.trim();
} while (password.length() <= 0);
} catch (IOException ioe) {
System.exit(1);
}
}
get.addAuthorization(username, password); get.addAuthorization(username, password);
}
get.addStatusListener(get.new CLIStatusListener(markSize, lineLen)); get.addStatusListener(get.new CLIStatusListener(markSize, lineLen));
if (!get.fetch(CONNECT_TIMEOUT, -1, inactivityTimeout)) if (!get.fetch(CONNECT_TIMEOUT, -1, inactivityTimeout))
System.exit(1); System.exit(1);
@ -278,10 +342,15 @@ public class EepGet {
} }
private static void usage() { private static void usage() {
System.err.println("EepGet [-p 127.0.0.1:4444] [-n #retries] [-o outputFile]\n" + System.err.println("eepget [-p 127.0.0.1:4444] [-c] [-o outputFile]\n" +
" [-m markSize lineLen] [-t timeout] [-h headerKey headerValue]\n" + " [-n #retries] (default 5)\n" +
" [-u username password] url]\n" + " [-m markSize] (default 1024)\n" +
" (use -p :0 for no proxy)"); " [-l lineLen] (default 40)\n" +
" [-t timeout] (default 60 sec)\n" +
" [-e etag]\n" +
" [-h headerName=headerValue]\n" +
" [-u username] [-x password] url\n" +
" (use -c or -p :0 for no proxy)");
} }
public static interface StatusListener { public static interface StatusListener {
@ -532,6 +601,7 @@ public class EepGet {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("ERR: doFetch failed ", ioe); _log.warn("ERR: doFetch failed ", ioe);
if (ioe instanceof MalformedURLException || if (ioe instanceof MalformedURLException ||
ioe instanceof UnknownHostException ||
ioe instanceof ConnectException) // proxy or nonproxied host Connection Refused ioe instanceof ConnectException) // proxy or nonproxied host Connection Refused
_keepFetching = false; _keepFetching = false;
} finally { } finally {
@ -1061,8 +1131,11 @@ public class EepGet {
URL url = new URL(_actualURL); URL url = new URL(_actualURL);
if ("http".equals(url.getProtocol())) { if ("http".equals(url.getProtocol())) {
String host = url.getHost(); String host = url.getHost();
if (host.toLowerCase(Locale.US).endsWith(".i2p")) String hostlc = host.toLowerCase(Locale.US);
throw new MalformedURLException("I2P addresses must be proxied"); if (hostlc.endsWith(".i2p"))
throw new UnknownHostException("I2P addresses must be proxied");
if (hostlc.endsWith(".onion"))
throw new UnknownHostException("Tor addresses must be proxied");
int port = url.getPort(); int port = url.getPort();
if (port == -1) if (port == -1)
port = 80; port = 80;

View File

@ -1,4 +1,4 @@
.TH EEEPGET 1 "April 19, 2012" "" "Eepget - I2P Downloader" .TH EEEPGET 1 "February 5, 2014" "" "Eepget - I2P Downloader"
.SH NAME .SH NAME
Eepget \- I2P downloader Eepget \- I2P downloader
@ -21,15 +21,39 @@ from the point of interruption.
.SH OPTIONS .SH OPTIONS
.B .B
\fB\-p\fR proxy_host:port \fB\-c\fR
.TP .TP
Specify an I2P proxy server (eeproxy) to use. If this option is not specified, eepget will use 127.0.0.1:4444. Specify \fB\-p\fR :0 to disable the eeproxy. Clearnet. Do not use a proxy. Same as \fB\-p\fR :0 .
.TP
.B
\fB\-e\fR etag
.TP
Sets the etag value in the request headers.
.TP
.B
\fB\-h\fR name=value
.TP
Adds an arbitrary request header with the given name and value.
.TP
.B
\fB\-l\fR lineLen
.TP
Controls the progress display. \fB\ lineLen \fP is the length of one progress line in characters. The default is 40.
.TP
.B
\fB\-m\fR markSize
.TP
Controls the progress display. \fB\ markSize \fP is the number of bytes one '#' character represents. The default is 1024.
.TP .TP
.B .B
\fB\-n\fR retries \fB\-n\fR retries
.TP .TP
Specify the number of times to retry downloading if the download isn't successful. If this option is not specified, eepget will retry downloading the file 3 times. Specify the number of times to retry downloading if the download isn't successful. If this option is not specified, eepget will retry downloading the file 5 times.
.TP .TP
.B .B
@ -39,15 +63,28 @@ Sets the output file to write to. If this option is not given, the output filena
.TP .TP
.B .B
\fB\-m\fR markSize lineLen \fB\-p\fR proxy_host[:port]
.TP .TP
Controls the progress display. \fB\ markSize \fP is the number of bytes one '#' character represents. \fB\ lineLen \fP is the length of one progress line in characters. The defaults are 1024 and 40. Specify an I2P proxy server (eeproxy) to use. If the port is not specified, eepget will use 4444. If this option is not specified, eepget will use 127.0.0.1:4444. Specify \fB\-c\fR or \fB\-p\fR :0 to disable the eeproxy.
.TP .TP
.B .B
\fB\-t\fR seconds \fB\-t\fR seconds
.TP .TP
Sets the inactivity timeout. The default is 60 seconds. Sets the inactivity timeout. The default is 60 seconds.
.TP
.B
\fB\-u\fR username
.TP
Sets the username for proxy authorization, if required.
.TP
.B
\fB\-x\fR password
.TP
Sets the password for proxy authorization, if required. If a username is specified but not a password, EepGet will prompt for the password.
.TP
.SH EXIT STATUS .SH EXIT STATUS
@ -56,5 +93,5 @@ exits with status zero upon successful transfer and non-zero if there were probl
.SH "SEE ALSO" .SH "SEE ALSO"
wget(1) curl(1) wget(1)