forked from I2P_Developers/i2p.i2p
Use getopt for SAM args processing
Args processing cleanups Change default host from 0.0.0.0 to 127.0.0.1 Add -s option for SSL (unimplemented) Put help text in a single string
This commit is contained in:
@@ -27,6 +27,8 @@ import java.util.Map;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import gnu.getopt.Getopt;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.app.*;
|
import net.i2p.app.*;
|
||||||
import static net.i2p.app.ClientAppState.*;
|
import static net.i2p.app.ClientAppState.*;
|
||||||
@@ -72,12 +74,12 @@ public class SAMBridge implements Runnable, ClientApp {
|
|||||||
public static final String DEFAULT_SAM_KEYFILE = "sam.keys";
|
public static final String DEFAULT_SAM_KEYFILE = "sam.keys";
|
||||||
public static final String PROP_TCP_HOST = "sam.tcp.host";
|
public static final String PROP_TCP_HOST = "sam.tcp.host";
|
||||||
public static final String PROP_TCP_PORT = "sam.tcp.port";
|
public static final String PROP_TCP_PORT = "sam.tcp.port";
|
||||||
protected static final String DEFAULT_TCP_HOST = "0.0.0.0";
|
protected static final String DEFAULT_TCP_HOST = "127.0.0.1";
|
||||||
protected static final String DEFAULT_TCP_PORT = "7656";
|
protected static final String DEFAULT_TCP_PORT = "7656";
|
||||||
|
|
||||||
public static final String PROP_DATAGRAM_HOST = "sam.udp.host";
|
public static final String PROP_DATAGRAM_HOST = "sam.udp.host";
|
||||||
public static final String PROP_DATAGRAM_PORT = "sam.udp.port";
|
public static final String PROP_DATAGRAM_PORT = "sam.udp.port";
|
||||||
protected static final String DEFAULT_DATAGRAM_HOST = "0.0.0.0";
|
protected static final String DEFAULT_DATAGRAM_HOST = "127.0.0.1";
|
||||||
protected static final String DEFAULT_DATAGRAM_PORT = "7655";
|
protected static final String DEFAULT_DATAGRAM_PORT = "7655";
|
||||||
|
|
||||||
|
|
||||||
@@ -456,9 +458,11 @@ public class SAMBridge implements Runnable, ClientApp {
|
|||||||
private final String host, keyFile;
|
private final String host, keyFile;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Properties opts;
|
private final Properties opts;
|
||||||
|
private final boolean isSSL;
|
||||||
|
|
||||||
public Options(String host, int port, Properties opts, String keyFile) {
|
public Options(String host, int port, boolean isSSL, Properties opts, String keyFile) {
|
||||||
this.host = host; this.port = port; this.opts = opts; this.keyFile = keyFile;
|
this.host = host; this.port = port; this.opts = opts; this.keyFile = keyFile;
|
||||||
|
this.isSSL = isSSL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,67 +483,115 @@ public class SAMBridge implements Runnable, ClientApp {
|
|||||||
String keyfile = DEFAULT_SAM_KEYFILE;
|
String keyfile = DEFAULT_SAM_KEYFILE;
|
||||||
int port = SAM_LISTENPORT;
|
int port = SAM_LISTENPORT;
|
||||||
String host = DEFAULT_TCP_HOST;
|
String host = DEFAULT_TCP_HOST;
|
||||||
|
boolean isSSL = false;
|
||||||
Properties opts = null;
|
Properties opts = null;
|
||||||
if (args.length > 0) {
|
Getopt g = new Getopt("SAM", args, "hs");
|
||||||
opts = parseOptions(args, 0);
|
int c;
|
||||||
keyfile = args[0];
|
while ((c = g.getopt()) != -1) {
|
||||||
int portIndex = 1;
|
switch (c) {
|
||||||
|
case 's':
|
||||||
|
isSSL = true;
|
||||||
|
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 {
|
try {
|
||||||
if (args.length>portIndex) port = Integer.parseInt(args[portIndex]);
|
port = Integer.parseInt(args[startArgs + 1]);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
host = args[portIndex];
|
throw new HelpRequestedException();
|
||||||
portIndex++;
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
keyfile = args[startArgs];
|
||||||
|
host = args[startArgs + 1];
|
||||||
try {
|
try {
|
||||||
if (args.length>portIndex) port = Integer.parseInt(args[portIndex]);
|
port = Integer.parseInt(args[startArgs + 2]);
|
||||||
} catch (NumberFormatException nfe1) {
|
} catch (NumberFormatException nfe) {
|
||||||
port = Integer.parseInt(opts.getProperty(SAMBridge.PROP_TCP_PORT, SAMBridge.DEFAULT_TCP_PORT));
|
throw new HelpRequestedException();
|
||||||
host = opts.getProperty(SAMBridge.PROP_TCP_HOST, SAMBridge.DEFAULT_TCP_HOST);
|
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
}
|
|
||||||
return new Options(host, port, opts, keyfile);
|
default:
|
||||||
|
throw new HelpRequestedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int remaining = args.length - startOpts;
|
||||||
|
if (remaining > 0) {
|
||||||
|
opts = parseOptions(args, startOpts);
|
||||||
|
}
|
||||||
|
return new Options(host, port, isSSL, opts, keyfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse key=value options starting at startArgs.
|
||||||
|
* @throws HelpRequestedException on any item not of the form key=value.
|
||||||
|
*/
|
||||||
private static Properties parseOptions(String args[], int startArgs) throws HelpRequestedException {
|
private static Properties parseOptions(String args[], int startArgs) throws HelpRequestedException {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
// skip over first few options
|
|
||||||
for (int i = startArgs; i < args.length; i++) {
|
for (int i = startArgs; i < args.length; i++) {
|
||||||
if (args[i].equals("-h")) throw new HelpRequestedException();
|
|
||||||
int eq = args[i].indexOf('=');
|
int eq = args[i].indexOf('=');
|
||||||
if (eq <= 0) continue;
|
if (eq <= 0)
|
||||||
if (eq >= args[i].length()-1) continue;
|
throw new HelpRequestedException();
|
||||||
|
if (eq >= args[i].length()-1)
|
||||||
|
throw new HelpRequestedException();
|
||||||
String key = args[i].substring(0, eq);
|
String key = args[i].substring(0, eq);
|
||||||
String val = args[i].substring(eq+1);
|
String val = args[i].substring(eq+1);
|
||||||
key = key.trim();
|
key = key.trim();
|
||||||
val = val.trim();
|
val = val.trim();
|
||||||
if ( (key.length() > 0) && (val.length() > 0) )
|
if ( (key.length() > 0) && (val.length() > 0) )
|
||||||
props.setProperty(key, val);
|
props.setProperty(key, val);
|
||||||
|
else
|
||||||
|
throw new HelpRequestedException();
|
||||||
}
|
}
|
||||||
return props;
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void usage() {
|
private static void usage() {
|
||||||
System.err.println("Usage: SAMBridge [keyfile [listenHost] listenPortNum[ name=val]*]");
|
System.err.println("Usage: SAMBridge [keyfile [listenHost] listenPortNum[ name=val]*]\n" +
|
||||||
System.err.println("or:");
|
"or:\n" +
|
||||||
System.err.println(" SAMBridge [ name=val ]*");
|
" SAMBridge [ name=val ]*\n" +
|
||||||
System.err.println(" keyfile: location to persist private keys (default sam.keys)");
|
" keyfile: location to persist private keys (default sam.keys)\n" +
|
||||||
System.err.println(" listenHost: interface to listen on (0.0.0.0 for all interfaces)");
|
" listenHost: interface to listen on (0.0.0.0 for all interfaces)\n" +
|
||||||
System.err.println(" listenPort: port to listen for SAM connections on (default 7656)");
|
" listenPort: port to listen for SAM connections on (default 7656)\n" +
|
||||||
System.err.println(" name=val: options to pass when connecting via I2CP, such as ");
|
" name=val: options to pass when connecting via I2CP, such as \n" +
|
||||||
System.err.println(" i2cp.host=localhost and i2cp.port=7654");
|
" i2cp.host=localhost and i2cp.port=7654\n" +
|
||||||
System.err.println("");
|
"\n" +
|
||||||
System.err.println("Host and ports of the SAM bridge can be specified with the alternate");
|
"Host and ports of the SAM bridge can be specified with the alternate\n" +
|
||||||
System.err.println("form by specifying options "+SAMBridge.PROP_TCP_HOST+" and/or "+
|
"form by specifying options "+SAMBridge.PROP_TCP_HOST+" and/or "+
|
||||||
SAMBridge.PROP_TCP_PORT);
|
SAMBridge.PROP_TCP_PORT +
|
||||||
System.err.println("");
|
"\n" +
|
||||||
System.err.println("Options "+SAMBridge.PROP_DATAGRAM_HOST+" and "+SAMBridge.PROP_DATAGRAM_PORT+
|
"Options "+SAMBridge.PROP_DATAGRAM_HOST+" and "+SAMBridge.PROP_DATAGRAM_PORT+
|
||||||
" specify the listening ip");
|
" specify the listening ip\n" +
|
||||||
System.err.println("range and the port of SAM datagram server. This server is");
|
"range and the port of SAM datagram server. This server is\n" +
|
||||||
System.err.println("only launched after a client creates the first SAM datagram");
|
"only launched after a client creates the first SAM datagram\n" +
|
||||||
System.err.println("or raw session, after a handshake with SAM version >= 3.0.");
|
"or raw session, after a handshake with SAM version >= 3.0.\n" +
|
||||||
System.err.println("");
|
"\n" +
|
||||||
System.err.println("The option loglevel=[DEBUG|WARN|ERROR|CRIT] can be used");
|
"The option loglevel=[DEBUG|WARN|ERROR|CRIT] can be used\n" +
|
||||||
System.err.println("for tuning the log verbosity.\n");
|
"for tuning the log verbosity.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
Reference in New Issue
Block a user