use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.
the class AuthenticateHandler method process.
// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
if (req.getMethod().equals("Authenticate")) {
JSONRPC2Error err = _helper.validateParams(requiredArgs, req, JSONRPC2Helper.USE_NO_AUTH);
if (err != null)
return new JSONRPC2Response(err, req.getID());
Map<String, Object> inParams = req.getNamedParams();
String pwd = (String) inParams.get("Password");
// Try get an AuthToken
AuthToken token = _secMan.validatePasswd(pwd);
if (token == null) {
return new JSONRPC2Response(JSONRPC2ExtendedError.INVALID_PASSWORD, req.getID());
}
Object api = inParams.get("API");
err = validateAPIVersion(api);
if (err != null)
return new JSONRPC2Response(err, req.getID());
Map<String, Object> outParams = new HashMap<String, Object>(4);
outParams.put("Token", token.getId());
outParams.put("API", I2PControlVersion.API_VERSION);
return new JSONRPC2Response(outParams, req.getID());
} else {
// Method name not supported
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.
the class EchoHandler method process.
// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
if (req.getMethod().equals("Echo")) {
JSONRPC2Error err = _helper.validateParams(requiredArgs, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
Map<String, Object> inParams = req.getNamedParams();
String echo = (String) inParams.get("Echo");
Map<String, Object> outParams = new HashMap<String, Object>(4);
outParams.put("Result", echo);
return new JSONRPC2Response(outParams, req.getID());
} else {
// Method name not supported
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.
the class I2PControlHandler method process.
private JSONRPC2Response process(JSONRPC2Request req) {
JSONRPC2Error err = _helper.validateParams(null, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
/**
** only if we enable host/port changes
* if (_context == null) {
* return new JSONRPC2Response(
* new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(),
* "RouterContext was not initialized. Query failed"),
* req.getID());
* }
***
*/
Map<String, Object> inParams = req.getNamedParams();
Map<String, Object> outParams = new HashMap<String, Object>(4);
boolean restartNeeded = false;
boolean settingsSaved = false;
String inParam;
if (inParams.containsKey("i2pcontrol.password")) {
if ((inParam = (String) inParams.get("i2pcontrol.password")) != null) {
if (_secMan.setPasswd(inParam)) {
outParams.put("i2pcontrol.password", null);
settingsSaved = true;
}
}
}
/**
**
* if (inParams.containsKey("i2pcontrol.address")) {
* String oldAddress = _conf.getConf("i2pcontrol.listen.address", "127.0.0.1");
* if ((inParam = (String) inParams.get("i2pcontrol.address")) != null) {
* if ((oldAddress == null || !inParam.equals(oldAddress.toString()) &&
* (inParam.equals("0.0.0.0") || inParam.equals("127.0.0.1")))) {
* InetAddress[] newAddress;
*
* try {
* newAddress = InetAddress.getAllByName(inParam);
* } catch (UnknownHostException e) {
* return new JSONRPC2Response(
* new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(),
* "\"i2pcontrol.address\" must be a string representing a hostname or ipaddress. " + inParam + " isn't valid."),
* req.getID());
* }
* try {
* SslSocketConnector ssl = I2PControlController.buildSslListener(inParam, _conf.getConf("i2pcontrol.listen.port", 7650));
* I2PControlController.clearListeners();
* I2PControlController.replaceListener(ssl);
* _conf.setConf("i2pcontrol.listen.address", inParam);
*
* ConfigurationManager.writeConfFile();
* outParams.put("i2pcontrol.address", null);
* settingsSaved = true;
* } catch (Exception e) {
* _conf.setConf("i2pcontrol.listen.address", oldAddress);
* try {
* SslSocketConnector ssl = I2PControlController.buildSslListener(inParam, _conf.getConf("i2pcontrol.listen.port", 7650));
* I2PControlController.clearListeners();
* I2PControlController.replaceListener(ssl);
* } catch (Exception e2) {
* _log.log(Log.CRIT, "Unable to resume server on previous listening ip.");
* }
* _log.error("Client tried to set listen address to, " + newAddress.toString() + " which isn't valid.", e);
* return new JSONRPC2Response(
* new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(),
* "\"i2pcontrol.address\" has been set to an invalid address, reverting. "), req.getID());
* }
* }
* } else {
* outParams.put("i2pcontrol.address", oldAddress);
* }
* outParams.put("RestartNeeded", restartNeeded);
* }
***
*/
outParams.put("SettingsSaved", settingsSaved);
return new JSONRPC2Response(outParams, req.getID());
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.
the class NetworkSettingHandler method process.
private JSONRPC2Response process(JSONRPC2Request req) {
JSONRPC2Error err = _helper.validateParams(null, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
if (_context == null) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "RouterContext was not initialized. Query failed"), req.getID());
}
Map<String, Object> inParams = req.getNamedParams();
Map<String, Object> outParams = new HashMap<String, Object>(4);
boolean restartNeeded = false;
boolean settingsSaved = false;
String inParam;
if (inParams.containsKey("i2p.router.net.ntcp.port")) {
String oldNTCPPort = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_PORT);
if ((inParam = (String) inParams.get("i2p.router.net.ntcp.port")) != null) {
if (oldNTCPPort == null || !oldNTCPPort.equals(inParam.trim())) {
Integer newPort;
try {
newPort = Integer.valueOf(inParam);
if (newPort < 1 || newPort > 65535) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ntcp.port\" must be a string representing a number in the range 1-65535. " + inParam + " isn't valid."), req.getID());
}
Map<String, String> config = new HashMap<String, String>();
config.put(NTCPTransport.PROP_I2NP_NTCP_PORT, String.valueOf(newPort));
config.put(NTCPTransport.PROP_I2NP_NTCP_AUTO_PORT, "false");
_context.router().saveConfig(config, null);
restartNeeded = true;
}
settingsSaved = true;
} else {
String sAutoPort = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_AUTO_PORT, "true");
boolean oldAutoPort = "true".equalsIgnoreCase(sAutoPort);
if (oldAutoPort) {
String oldSSUPort = "" + _context.getProperty(UDPTransport.PROP_INTERNAL_PORT, 8887);
outParams.put("i2p.router.net.ntcp.port", oldSSUPort);
} else {
outParams.put("i2p.router.net.ntcp.port", oldNTCPPort);
}
}
}
if (inParams.containsKey("i2p.router.net.ntcp.hostname")) {
String oldNTCPHostname = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_HOSTNAME);
if ((inParam = (String) inParams.get("i2p.router.net.ntcp.hostname")) != null) {
if (oldNTCPHostname == null || !oldNTCPHostname.equals(inParam.trim())) {
_context.router().saveConfig(NTCPTransport.PROP_I2NP_NTCP_HOSTNAME, inParam);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ntcp.hostname", oldNTCPHostname);
}
}
if (inParams.containsKey("i2p.router.net.ntcp.autoip")) {
String oldNTCPAutoIP = _context.getProperty(NTCPTransport.PROP_I2NP_NTCP_AUTO_IP);
if ((inParam = (String) inParams.get("i2p.router.net.ntcp.autoip")) != null) {
inParam = inParam.trim().toLowerCase();
if (oldNTCPAutoIP == null || !oldNTCPAutoIP.equals(inParam)) {
if ("always".equals(inParam) || "true".equals(inParam) || "false".equals(inParam)) {
_context.router().saveConfig(NTCPTransport.PROP_I2NP_NTCP_AUTO_IP, inParam);
restartNeeded = true;
} else {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ntcp.autoip\" can only be always, true or false. " + inParam + " isn't valid."), req.getID());
}
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ntcp.autoip", oldNTCPAutoIP);
}
}
if (inParams.containsKey("i2p.router.net.ssu.port")) {
String oldSSUPort = "" + _context.getProperty(UDPTransport.PROP_INTERNAL_PORT, 8887);
if ((inParam = (String) inParams.get("i2p.router.net.ssu.port")) != null) {
if (oldSSUPort == null || !oldSSUPort.equals(inParam.trim())) {
Integer newPort;
try {
newPort = Integer.valueOf(inParam);
if (newPort < 1 || newPort > 65535) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ssu.port\" must be a string representing a number in the range 1-65535. " + inParam + " isn't valid."), req.getID());
}
Map<String, String> config = new HashMap<String, String>();
config.put(UDPTransport.PROP_EXTERNAL_PORT, String.valueOf(newPort));
config.put(UDPTransport.PROP_INTERNAL_PORT, String.valueOf(newPort));
_context.router().saveConfig(config, null);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ssu.port", oldSSUPort);
}
}
if (inParams.containsKey("i2p.router.net.ssu.hostname")) {
String oldSSUHostname = _context.getProperty(UDPTransport.PROP_EXTERNAL_HOST);
if ((inParam = (String) inParams.get("i2p.router.net.ssu.hostname")) != null) {
if (oldSSUHostname == null || !oldSSUHostname.equals(inParam.trim())) {
_context.router().saveConfig(UDPTransport.PROP_EXTERNAL_HOST, inParam);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ssu.hostname", oldSSUHostname);
}
}
if (inParams.containsKey("i2p.router.net.ssu.autoip")) {
String oldSSUAutoIP = _context.getProperty(UDPTransport.PROP_SOURCES);
if ((inParam = (String) inParams.get("i2p.router.net.ssu.autoip")) != null) {
inParam = inParam.trim().toLowerCase();
if (oldSSUAutoIP == null || !oldSSUAutoIP.equals(inParam)) {
if (inParam.equals("ssu") || inParam.equals("local,ssu") || inParam.equals("upnp,ssu") || inParam.equals("local,upnp,ssu")) {
_context.router().saveConfig(UDPTransport.PROP_SOURCES, inParam);
restartNeeded = true;
} else {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.ssu.autoip\" can only be ssu/local,upnp,ssu/local/ssu/upnp,ssu. " + inParam + " isn't valid."), req.getID());
}
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.ssu.autoip", oldSSUAutoIP);
}
}
// Non-setable key.
if (inParams.containsKey("i2p.router.net.ssu.detectedip")) {
if ((inParam = (String) inParams.get("i2p.router.net.ssu.autoip")) == null) {
byte[] ipBytes = _context.router().getRouterInfo().getTargetAddress("SSU").getIP();
try {
InetAddress i = InetAddress.getByAddress(ipBytes);
outParams.put("i2p.router.net.ssu.detectedip", i.getHostAddress());
} catch (UnknownHostException e) {
outParams.put("i2p.router.net.ssu.detectedip", "Failed to parse ip address");
}
}
}
if (inParams.containsKey("i2p.router.net.upnp")) {
String oldUPNP = _context.getProperty(TransportManager.PROP_ENABLE_UPNP);
if ((inParam = (String) inParams.get("i2p.router.net.upnp")) != null) {
if (oldUPNP == null || !oldUPNP.equals(inParam.trim())) {
_context.router().saveConfig(TransportManager.PROP_ENABLE_UPNP, inParam);
restartNeeded = true;
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.upnp", oldUPNP);
}
}
if (inParams.containsKey("i2p.router.net.bw.share")) {
String oldShare = _context.router().getConfigSetting(Router.PROP_BANDWIDTH_SHARE_PERCENTAGE);
if ((inParam = (String) inParams.get("i2p.router.net.bw.share")) != null) {
if (oldShare == null || !oldShare.equals(inParam.trim())) {
Integer percent;
try {
percent = Integer.parseInt(inParam);
if (percent < 0 || percent > 100 || inParam.length() == 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.bw.share\" A positive integer must supplied, \"" + inParam + "\" isn't valid"), req.getID());
}
_context.router().saveConfig(Router.PROP_BANDWIDTH_SHARE_PERCENTAGE, inParam);
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.bw.share", oldShare);
}
}
if (inParams.containsKey("i2p.router.net.bw.in")) {
String oldBWIn = _context.getProperty(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH);
if ((inParam = (String) inParams.get("i2p.router.net.bw.in")) != null) {
Integer rate;
try {
rate = Integer.parseInt(inParam);
if (rate < 0 || inParam.length() == 0) {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.bw.in\" A positive integer must supplied, " + inParam + " isn't valid"), req.getID());
}
Integer burstRate = (rate * BW_BURST_PCT) / 100;
Integer burstSize = (burstRate * BW_BURST_TIME);
if (oldBWIn == null || !oldBWIn.equals(rate.toString())) {
Map<String, String> config = new HashMap<String, String>();
config.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH, rate.toString());
config.put(FIFOBandwidthRefiller.PROP_INBOUND_BURST_BANDWIDTH, burstRate.toString());
config.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH_PEAK, burstSize.toString());
_context.router().saveConfig(config, null);
_context.bandwidthLimiter().reinitialize();
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.bw.in", oldBWIn);
}
}
if (inParams.containsKey("i2p.router.net.bw.out")) {
String oldBWOut = _context.getProperty(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH);
if ((inParam = (String) inParams.get("i2p.router.net.bw.out")) != null) {
Integer rate;
try {
rate = Integer.parseInt(inParam);
if (rate < 0 || inParam.length() == 0)
throw new NumberFormatException();
} catch (NumberFormatException e) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "\"i2p.router.net.bw.out\" A positive integer must supplied, " + inParam + " isn't valid"), req.getID());
}
Integer burstRate = (rate * BW_BURST_PCT) / 100;
Integer burstSize = (burstRate * BW_BURST_TIME);
if (oldBWOut == null || !oldBWOut.equals(rate.toString())) {
Map<String, String> config = new HashMap<String, String>();
config.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH, rate.toString());
config.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BURST_BANDWIDTH, burstRate.toString());
config.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH_PEAK, burstSize.toString());
_context.router().saveConfig(config, null);
_context.bandwidthLimiter().reinitialize();
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.bw.out", oldBWOut);
}
}
if (inParams.containsKey("i2p.router.net.laptopmode")) {
String oldLaptopMode = _context.getProperty(UDPTransport.PROP_LAPTOP_MODE);
if ((inParam = (String) inParams.get("i2p.router.net.laptopmode")) != null) {
if (oldLaptopMode == null || !oldLaptopMode.equals(inParam.trim())) {
_context.router().saveConfig(UDPTransport.PROP_LAPTOP_MODE, String.valueOf(inParam));
}
settingsSaved = true;
} else {
outParams.put("i2p.router.net.laptopmode", oldLaptopMode);
}
}
if (settingsSaved)
_context.router().saveConfig();
outParams.put("SettingsSaved", settingsSaved);
outParams.put("RestartNeeded", restartNeeded);
return new JSONRPC2Response(outParams, req.getID());
}
use of com.thetransactioncompany.jsonrpc2.JSONRPC2Request in project i2pplus by I2PPlus.
the class RouterInfoHandler method process.
@SuppressWarnings("unchecked")
private JSONRPC2Response process(JSONRPC2Request req) {
JSONRPC2Error err = _helper.validateParams(null, req);
if (err != null)
return new JSONRPC2Response(err, req.getID());
if (_context == null) {
return new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "RouterContext was not initialized. Query failed"), req.getID());
}
Map<String, Object> inParams = req.getNamedParams();
Map outParams = new HashMap();
if (inParams.containsKey("i2p.router.version")) {
try {
Class rvClass = Class.forName("net.i2p.router.RouterVersion");
java.lang.reflect.Field field = rvClass.getDeclaredField("FULL_VERSION");
String fullVersion = (String) field.get(new RouterVersion());
outParams.put("i2p.router.version", fullVersion);
}// Ignore
catch (Exception e) {
}
}
if (inParams.containsKey("i2p.router.uptime")) {
Router router = _context.router();
if (router == null) {
outParams.put("i2p.router.uptime", 0);
} else {
outParams.put("i2p.router.uptime", router.getUptime());
}
}
if (inParams.containsKey("i2p.router.status")) {
outParams.put("i2p.router.status", _context.throttle().getLocalizedTunnelStatus());
}
if (inParams.containsKey("i2p.router.net.status")) {
outParams.put("i2p.router.net.status", getNetworkStatus().ordinal());
}
if (inParams.containsKey("i2p.router.net.bw.inbound.1s")) {
outParams.put("i2p.router.net.bw.inbound.1s", _context.bandwidthLimiter().getReceiveBps());
}
if (inParams.containsKey("i2p.router.net.bw.outbound.1s")) {
outParams.put("i2p.router.net.bw.outbound.1s", _context.bandwidthLimiter().getSendBps());
}
if (inParams.containsKey("i2p.router.net.bw.inbound.15s")) {
outParams.put("i2p.router.net.bw.inbound.15s", _context.bandwidthLimiter().getReceiveBps15s());
}
if (inParams.containsKey("i2p.router.net.bw.outbound.15s")) {
outParams.put("i2p.router.net.bw.outbound.15s", _context.bandwidthLimiter().getSendBps15s());
}
if (inParams.containsKey("i2p.router.net.tunnels.participating")) {
outParams.put("i2p.router.net.tunnels.participating", _context.tunnelManager().getParticipatingCount());
}
if (inParams.containsKey("i2p.router.netdb.knownpeers")) {
// Why max(-1, 0) is used I don't know, it is the implementation used in the router console.
outParams.put("i2p.router.netdb.knownpeers", Math.max(_context.netDb().getKnownRouters() - 1, 0));
}
if (inParams.containsKey("i2p.router.netdb.activepeers")) {
outParams.put("i2p.router.netdb.activepeers", _context.commSystem().countActivePeers());
}
if (inParams.containsKey("i2p.router.netdb.fastpeers")) {
outParams.put("i2p.router.netdb.fastpeers", _context.profileOrganizer().countFastPeers());
}
if (inParams.containsKey("i2p.router.netdb.highcapacitypeers")) {
outParams.put("i2p.router.netdb.highcapacitypeers", _context.profileOrganizer().countHighCapacityPeers());
}
if (inParams.containsKey("i2p.router.netdb.isreseeding")) {
outParams.put("i2p.router.netdb.isreseeding", Boolean.valueOf(System.getProperty("net.i2p.router.web.ReseedHandler.reseedInProgress")).booleanValue());
}
return new JSONRPC2Response(outParams, req.getID());
}
Aggregations