use of gnu.getopt.Getopt in project i2p.i2p by i2p.
the class I2Ping method runCommand.
public void runCommand(String cmd) throws InterruptedException, IOException {
long timeout = PING_TIMEOUT;
int count = PING_COUNT;
boolean countPing = false;
boolean reportTimes = true;
String hostListFile = null;
int localPort = 0;
int remotePort = 0;
boolean error = false;
String[] argv = DataHelper.split(cmd, " ");
Getopt g = new Getopt("ping", argv, "t:m:n:chl:f:p:");
int c;
while ((c = g.getopt()) != -1) {
switch(c) {
case // timeout
't':
timeout = Long.parseLong(g.getOptarg());
// convenience, convert msec to sec
if (timeout < 100)
timeout *= 1000;
break;
case // max simultaneous pings
'm':
MAX_SIMUL_PINGS = Integer.parseInt(g.getOptarg());
break;
case // number of pings
'n':
count = Integer.parseInt(g.getOptarg());
break;
case // "count" ping
'c':
countPing = true;
count = CPING_COUNT;
break;
case // ping all hosts
'h':
if (hostListFile != null)
error = true;
else
hostListFile = "hosts.txt";
break;
case // ping a list of hosts
'l':
if (hostListFile != null)
error = true;
else
hostListFile = g.getOptarg();
break;
case // local port
'f':
localPort = Integer.parseInt(g.getOptarg());
break;
case // remote port
'p':
remotePort = Integer.parseInt(g.getOptarg());
break;
case '?':
case ':':
default:
error = true;
}
}
int remaining = argv.length - g.getOptind();
if (error || remaining > 1 || (remaining <= 0 && hostListFile == null) || (remaining > 0 && hostListFile != null)) {
System.out.println(usage());
return;
}
if (hostListFile != null) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(hostListFile), "UTF-8"));
String line;
List<PingHandler> pingHandlers = new ArrayList<PingHandler>();
int i = 0;
while ((line = br.readLine()) != null) {
// comments
if (line.startsWith("#"))
continue;
if (line.startsWith(";"))
continue;
if (line.startsWith("!"))
continue;
if (line.indexOf('=') != -1) {
// maybe file is hosts.txt?
line = line.substring(0, line.indexOf('='));
}
PingHandler ph = new PingHandler(line, count, localPort, remotePort, timeout, countPing, reportTimes);
ph.start();
pingHandlers.add(ph);
if (++i > 1)
reportTimes = false;
}
br.close();
for (Thread t : pingHandlers) t.join();
return;
} finally {
if (br != null)
try {
br.close();
} catch (IOException ioe) {
}
}
}
String host = argv[g.getOptind()];
Thread t = new PingHandler(host, count, localPort, remotePort, timeout, countPing, reportTimes);
t.start();
t.join();
}
use of gnu.getopt.Getopt in project i2p.i2p by i2p.
the class EepHead method main.
/**
* EepHead [-p 127.0.0.1:4444] [-n #retries] url
*
* This doesn't really do much since it doesn't register a listener.
* EepGet doesn't have a method to store and return all the headers, so just print
* out the ones we have methods for.
* Turn on logging to use it for a decent test.
*/
public static void main(String[] args) {
String proxyHost = "127.0.0.1";
int proxyPort = 4444;
int numRetries = 0;
int inactivityTimeout = 60 * 1000;
String username = null;
String password = null;
boolean error = false;
Getopt g = new Getopt("eephead", args, "p:cn:t:u:x:");
try {
int c;
while ((c = g.getopt()) != -1) {
switch(c) {
case 'p':
String s = g.getOptarg();
int colon = s.indexOf(':');
if (colon >= 0) {
// Todo IPv6 [a:b:c]:4444
proxyHost = s.substring(0, colon);
String port = s.substring(colon + 1);
proxyPort = Integer.parseInt(port);
} else {
proxyHost = s;
// proxyPort remains default
}
break;
case 'c':
// no proxy, same as -p :0
proxyHost = "";
proxyPort = 0;
break;
case 'n':
numRetries = Integer.parseInt(g.getOptarg());
break;
case 't':
inactivityTimeout = 1000 * Integer.parseInt(g.getOptarg());
break;
case 'u':
username = g.getOptarg();
break;
case 'x':
password = g.getOptarg();
break;
case '?':
case ':':
default:
error = true;
break;
}
// switch
}
// while
} catch (RuntimeException e) {
e.printStackTrace();
error = true;
}
if (error || args.length - g.getOptind() != 1) {
usage();
System.exit(1);
}
String url = args[g.getOptind()];
EepHead get = new EepHead(I2PAppContext.getGlobalContext(), proxyHost, proxyPort, numRetries, url);
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);
}
if (get.fetch(45 * 1000, -1, inactivityTimeout)) {
System.err.println("Content-Type: " + get.getContentType());
System.err.println("Content-Length: " + get.getContentLength());
System.err.println("Last-Modified: " + get.getLastModified());
System.err.println("Etag: " + get.getETag());
} else {
System.err.println("Failed " + url);
System.exit(1);
}
}
use of gnu.getopt.Getopt in project i2p.i2p by i2p.
the class SAMBridge method getOptions.
/**
* Usage:
* <pre>SAMBridge [ keyfile [listenHost ] listenPort [ name=val ]* ]</pre>
* or:
* <pre>SAMBridge [ name=val ]* </pre>
*
* name=val options are passed to the I2CP code to build a session,
* allowing the bridge to specify an alternate I2CP host and port, tunnel
* depth, etc.
* @param args [ keyfile [ listenHost ] listenPort [ name=val ]* ]
* @return non-null Options or throws Exception
* @throws HelpRequestedException on command line problems
* @throws IllegalArgumentException if specified config file does not exist
* @throws IOException if specified config file cannot be read, or on SSL keystore problems
* @since 0.9.6
*/
private static Options getOptions(String[] args) throws Exception {
String keyfile = null;
int port = -1;
String host = null;
boolean isSSL = false;
String cfile = null;
Getopt g = new Getopt("SAM", args, "hsc:");
int c;
while ((c = g.getopt()) != -1) {
switch(c) {
case 's':
isSSL = true;
break;
case 'c':
cfile = g.getOptarg();
break;
case 'h':
case '?':
case ':':
default:
throw new HelpRequestedException();
}
// switch
}
// while
int startArgs = g.getOptind();
// possible args before ones containing '=';
// (none)
// key port
// key host port
int startOpts;
for (startOpts = startArgs; startOpts < args.length; startOpts++) {
if (args[startOpts].contains("="))
break;
}
int numArgs = startOpts - startArgs;
switch(numArgs) {
case 0:
break;
case 2:
keyfile = args[startArgs];
try {
port = Integer.parseInt(args[startArgs + 1]);
} catch (NumberFormatException nfe) {
throw new HelpRequestedException();
}
break;
case 3:
keyfile = args[startArgs];
host = args[startArgs + 1];
try {
port = Integer.parseInt(args[startArgs + 2]);
} catch (NumberFormatException nfe) {
throw new HelpRequestedException();
}
break;
default:
throw new HelpRequestedException();
}
String scfile = cfile != null ? cfile : DEFAULT_SAM_CONFIGFILE;
File file = new File(scfile);
if (!file.isAbsolute())
file = new File(I2PAppContext.getGlobalContext().getConfigDir(), scfile);
Properties opts = new Properties();
if (file.exists()) {
DataHelper.loadProps(opts, file);
} else if (cfile != null) {
// only throw if specified on command line
throw new IllegalArgumentException("Config file not found: " + file);
}
// command line trumps config file trumps defaults
if (host == null)
host = opts.getProperty(PROP_TCP_HOST, DEFAULT_TCP_HOST);
if (port < 0) {
try {
port = Integer.parseInt(opts.getProperty(PROP_TCP_PORT, DEFAULT_TCP_PORT));
} catch (NumberFormatException nfe) {
throw new HelpRequestedException();
}
}
if (keyfile == null)
keyfile = opts.getProperty(PROP_SAM_KEYFILE, DEFAULT_SAM_KEYFILE);
if (!isSSL)
isSSL = Boolean.parseBoolean(opts.getProperty(PROP_SAM_SSL));
if (isSSL) {
// must do this before we add command line opts since we may be writing them back out
boolean shouldSave = SSLUtil.verifyKeyStore(opts);
if (shouldSave)
DataHelper.storeProps(opts, file);
}
int remaining = args.length - startOpts;
if (remaining > 0) {
parseOptions(args, startOpts, opts);
}
return new Options(host, port, isSSL, opts, keyfile, file);
}
use of gnu.getopt.Getopt in project i2p.i2p by i2p.
the class SSLEepGet method main.
/**
* SSLEepGet https://foo/bar
* or to save cert chain:
* SSLEepGet -s https://foo/bar
*/
public static void main(String[] args) {
int saveCerts = 0;
boolean noVerify = false;
String proxyHost = "127.0.0.1";
int proxyPort = 0;
ProxyType ptype = ProxyType.NONE;
boolean error = false;
Getopt g = new Getopt("ssleepget", args, "p:y:sz");
try {
int c;
while ((c = g.getopt()) != -1) {
switch(c) {
case 'p':
String s = g.getOptarg();
int colon = s.indexOf(':');
if (colon >= 0) {
// Todo IPv6 [a:b:c]:4444
proxyHost = s.substring(0, colon);
String port = s.substring(colon + 1);
proxyPort = Integer.parseInt(port);
} else {
proxyHost = s;
// proxyPort remains default
}
break;
case 'y':
String y = g.getOptarg().toUpperCase(Locale.US);
if (y.equals("HTTP") || y.equals("HTTPS")) {
ptype = ProxyType.HTTP;
} else if (y.equals("SOCKS4")) {
ptype = ProxyType.SOCKS4;
} else if (y.equals("SOCKS5")) {
ptype = ProxyType.SOCKS5;
} else if (y.equals("I2P")) {
ptype = ProxyType.INTERNAL;
proxyHost = "localhost";
proxyPort = 4444;
} else {
error = true;
}
break;
case 's':
saveCerts++;
break;
case 'z':
noVerify = true;
break;
case '?':
case ':':
default:
error = true;
break;
}
// switch
}
// while
} catch (RuntimeException e) {
e.printStackTrace();
error = true;
}
if (error || args.length - g.getOptind() != 1) {
usage();
System.exit(1);
}
String url = args[g.getOptind()];
String saveAs = suggestName(url);
SSLEepGet get;
if (proxyHost != null) {
if (proxyPort == 0) {
if (ptype == ProxyType.HTTP)
proxyPort = 8080;
else
proxyPort = 1080;
}
get = new SSLEepGet(I2PAppContext.getGlobalContext(), ptype, proxyHost, proxyPort, saveAs, url);
} else {
get = new SSLEepGet(I2PAppContext.getGlobalContext(), saveAs, url);
}
if (saveCerts > 0)
get._saveCerts = saveCerts;
if (noVerify)
get._bypassVerification = true;
get._commandLine = true;
get.addStatusListener(get.new CLIStatusListener(1024, 40));
if (!get.fetch(45 * 1000, -1, 60 * 1000))
System.exit(1);
}
use of gnu.getopt.Getopt in project i2p.i2p by i2p.
the class Router method main.
/**
* Usage: Router [rebuild]
* No other options allowed, for now
* Instantiates Router(), and either installs updates and exits,
* or calls runRouter().
*
* Not recommended for embedded use.
* Applications bundling I2P should instantiate a Router and call runRouter().
*
* @param args null ok
* @throws IllegalArgumentException
*/
public static void main(String[] args) {
boolean rebuild = false;
if (args != null) {
boolean error = false;
Getopt g = new Getopt("router", args, "");
int c;
while ((c = g.getopt()) != -1) {
switch(c) {
default:
error = true;
}
}
int remaining = args.length - g.getOptind();
if (remaining > 1) {
error = true;
} else if (remaining == 1) {
rebuild = args[g.getOptind()].equals("rebuild");
;
if (!rebuild)
error = true;
}
if (error)
throw new IllegalArgumentException();
}
System.out.println("Starting I2P " + RouterVersion.FULL_VERSION);
// verifyWrapperConfig();
Router r;
try {
r = new Router();
} catch (IllegalStateException ise) {
System.exit(-1);
return;
}
if (rebuild) {
r.rebuildNewIdentity();
} else {
// This is here so that we can get the directory location from the context
// for the zip file and the base location to unzip to.
// If it does an update, it never returns.
// I guess it's better to have the other-router check above this, we don't want to
// overwrite an existing running router's jar files. Other than ours.
InstallUpdate.installUpdates(r);
// ********* Start no threads before here ********* //
r.runRouter();
}
}
Aggregations