Search in sources :

Example 21 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by vituperative.

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());
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Example 22 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response in project i2pplus by vituperative.

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());
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) Router(net.i2p.router.Router) RouterVersion(net.i2p.router.RouterVersion) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response 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());
    }
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) AuthToken(net.i2p.i2pcontrol.security.AuthToken)

Example 24 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response 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());
    }
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Example 25 with JSONRPC2Response

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Response 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());
}
Also used : HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error)

Aggregations

JSONRPC2Response (com.thetransactioncompany.jsonrpc2.JSONRPC2Response)31 JSONRPC2Error (com.thetransactioncompany.jsonrpc2.JSONRPC2Error)24 HashMap (java.util.HashMap)24 Map (java.util.Map)6 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONRPC2Request (com.thetransactioncompany.jsonrpc2.JSONRPC2Request)4 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ClientAppManager (net.i2p.app.ClientAppManager)3 AuthToken (net.i2p.i2pcontrol.security.AuthToken)3 Router (net.i2p.router.Router)3 RouterVersion (net.i2p.router.RouterVersion)3 ReseedChecker (net.i2p.router.networkdb.reseed.ReseedChecker)3 Rate (net.i2p.stat.Rate)3 RateStat (net.i2p.stat.RateStat)3 UpdateManager (net.i2p.update.UpdateManager)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1