Search in sources :

Example 6 with JSONRPC2Error

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

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

Example 7 with JSONRPC2Error

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

the class RouterManagerHandler 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();
    final Map<String, Object> outParams = new HashMap<String, Object>(4);
    if (inParams.containsKey("Shutdown")) {
        outParams.put("Shutdown", null);
        (new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(SHUTDOWN_WAIT);
                } catch (InterruptedException e) {
                }
                _context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD));
                _context.router().shutdown(Router.EXIT_HARD);
            }
        }).start();
        return new JSONRPC2Response(outParams, req.getID());
    }
    if (inParams.containsKey("Restart")) {
        outParams.put("Restart", null);
        (new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(SHUTDOWN_WAIT);
                } catch (InterruptedException e) {
                }
                _context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_HARD_RESTART));
                _context.router().shutdown(Router.EXIT_HARD_RESTART);
            }
        }).start();
        return new JSONRPC2Response(outParams, req.getID());
    }
    if (inParams.containsKey("ShutdownGraceful")) {
        outParams.put("ShutdownGraceful", null);
        (new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(SHUTDOWN_WAIT);
                } catch (InterruptedException e) {
                }
                _context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL));
                _context.router().shutdownGracefully();
            }
        }).start();
        return new JSONRPC2Response(outParams, req.getID());
    }
    if (inParams.containsKey("RestartGraceful")) {
        outParams.put("RestartGraceful", null);
        (new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(SHUTDOWN_WAIT);
                } catch (InterruptedException e) {
                }
                _context.addShutdownTask(new UpdateWrapperManagerTask(Router.EXIT_GRACEFUL_RESTART));
                _context.router().shutdownGracefully(Router.EXIT_GRACEFUL_RESTART);
            }
        }).start();
        return new JSONRPC2Response(outParams, req.getID());
    }
    if (inParams.containsKey("Reseed")) {
        outParams.put("Reseed", null);
        (new Thread() {

            @Override
            public void run() {
                ReseedChecker reseeder = new ReseedChecker(_context);
                reseeder.requestReseed();
            }
        }).start();
        return new JSONRPC2Response(outParams, req.getID());
    }
    if (inParams.containsKey("FindUpdates")) {
        Thread t = new Thread() {

            @Override
            public void run() {
                ClientAppManager clmgr = I2PAppContext.getCurrentContext().clientAppManager();
                if (clmgr == null) {
                    outParams.put("FindUpdates", "ClientAppManager is null");
                    return;
                }
                UpdateManager upmgr = (UpdateManager) clmgr.getRegisteredApp(UpdateManager.APP_NAME);
                if (upmgr == null) {
                    outParams.put("FindUpdates", "UpdateManager is null");
                    return;
                }
                boolean updateIsAvailable = upmgr.checkAvailable(UpdateType.ROUTER_SIGNED) != null;
                outParams.put("FindUpdates", updateIsAvailable);
            }
        };
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
        }
        return new JSONRPC2Response(outParams, req.getID());
    }
    if (inParams.containsKey("Update")) {
        Thread t = new Thread() {

            @Override
            public void run() {
                ClientAppManager clmgr = I2PAppContext.getCurrentContext().clientAppManager();
                if (clmgr == null) {
                    outParams.put("Update", "ClientAppManager is null");
                    return;
                }
                UpdateManager upmgr = (UpdateManager) clmgr.getRegisteredApp(UpdateManager.APP_NAME);
                if (upmgr == null) {
                    outParams.put("Update", "UpdateManager is null");
                    return;
                }
                boolean updateStarted = upmgr.update(UpdateType.ROUTER_SIGNED);
                if (!updateStarted) {
                    outParams.put("Update", "Update not started");
                    return;
                }
                boolean isUpdating = upmgr.isUpdateInProgress(UpdateType.ROUTER_SIGNED);
                while (isUpdating) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                    }
                    isUpdating = upmgr.isUpdateInProgress(UpdateType.ROUTER_SIGNED);
                }
                outParams.put("Update", upmgr.getStatus());
            }
        };
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
        }
        return new JSONRPC2Response(outParams, req.getID());
    }
    return new JSONRPC2Response(outParams, req.getID());
}
Also used : ReseedChecker(net.i2p.router.networkdb.reseed.ReseedChecker) HashMap(java.util.HashMap) JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) ClientAppManager(net.i2p.app.ClientAppManager) JSONRPC2Error(com.thetransactioncompany.jsonrpc2.JSONRPC2Error) UpdateManager(net.i2p.update.UpdateManager)

Example 8 with JSONRPC2Error

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.

the class AdvancedSettingsHandler method process.

// Processes the requests
@SuppressWarnings("unchecked")
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
    if (req.getMethod().equals("AdvancedSettings")) {
        JSONRPC2Error err = _helper.validateParams(requiredArgs, 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());
        }
        @SuppressWarnings("rawtypes") Map<String, Object> inParams = req.getNamedParams();
        Map outParams = new HashMap();
        if (inParams.containsKey("setAll")) {
            Object obj = inParams.get("setAll");
            if (!(obj instanceof Map)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"setAll\" is not a Map");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            @SuppressWarnings("rawtypes") Map objMap = (Map) inParams.get("setAll");
            if (objMap.size() > 0) {
                if (!(objMap.keySet().toArray()[0] instanceof String) && !(objMap.values().toArray()[0] instanceof String)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain String keys and values");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                if (!checkTypes(objMap)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Some of the supplied values are not strings");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                Map<String, String> allSettings = (Map<String, String>) objMap;
                boolean success = setAdvancedSettings(allSettings, true);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            } else {
                // Empty list of settings submitted
                boolean success = setAdvancedSettings(null, true);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            }
        }
        if (inParams.containsKey("getAll")) {
            outParams.put("getAll", getAdvancedSettings());
        }
        if (inParams.containsKey("set")) {
            Object obj = inParams.get("set");
            if (!(obj instanceof Map)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"set\" is not a Map");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            Map objMap = (Map) inParams.get("set");
            if (objMap.size() > 0) {
                if (!(objMap.keySet().toArray()[0] instanceof String) && !(objMap.values().toArray()[0] instanceof String)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain String keys and values");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                if (!checkTypes(objMap)) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Some of the supplied values are not strings");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
                Map<String, String> allSettings = (Map<String, String>) objMap;
                boolean success = setAdvancedSettings(allSettings, false);
                if (!success) {
                    JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INTERNAL_ERROR.getCode(), "Failed to save new config");
                    return new JSONRPC2Response(rpcErr, req.getID());
                }
            } else {
                // Empty list of settings submitted
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Map of settings does not contain any entries");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
        }
        if (inParams.containsKey("get")) {
            Object obj = inParams.get("get");
            if (!(obj instanceof String)) {
                JSONRPC2Error rpcErr = new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(), "Value of \"get\" is not a string");
                return new JSONRPC2Response(rpcErr, req.getID());
            }
            String getStr = (String) obj;
            String getVal = getAdvancedSetting(getStr);
            Map<String, String> outMap = new HashMap<String, String>();
            outMap.put(getStr, getVal);
            outParams.put("get", outMap);
        }
        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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with JSONRPC2Error

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.

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 10 with JSONRPC2Error

use of com.thetransactioncompany.jsonrpc2.JSONRPC2Error in project i2p.i2p by i2p.

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

Aggregations

JSONRPC2Error (com.thetransactioncompany.jsonrpc2.JSONRPC2Error)24 JSONRPC2Response (com.thetransactioncompany.jsonrpc2.JSONRPC2Response)24 HashMap (java.util.HashMap)24 Map (java.util.Map)6 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