Search in sources :

Example 1 with TunnelController

use of net.i2p.i2ptunnel.TunnelController in project i2p.i2p by i2p.

the class GeneralHelper method getClientDestination.

public String getClientDestination(int tunnel) {
    TunnelController tun = getController(tunnel);
    if (tun == null)
        return "";
    String rv;
    if (TunnelController.TYPE_STD_CLIENT.equals(tun.getType()) || TunnelController.TYPE_IRC_CLIENT.equals(tun.getType()) || TunnelController.TYPE_STREAMR_CLIENT.equals(tun.getType()))
        rv = tun.getTargetDestination();
    else
        rv = tun.getProxyList();
    return rv != null ? rv : "";
}
Also used : TunnelController(net.i2p.i2ptunnel.TunnelController)

Example 2 with TunnelController

use of net.i2p.i2ptunnel.TunnelController in project i2p.i2p by i2p.

the class GeneralHelper method updateTunnelConfig.

protected static List<String> updateTunnelConfig(TunnelControllerGroup tcg, int tunnel, TunnelConfig config) {
    // Get current tunnel controller
    TunnelController cur = getController(tcg, tunnel);
    Properties props = config.getConfig();
    List<String> msgs = new ArrayList<String>();
    String type = props.getProperty(TunnelController.PROP_TYPE);
    if (TunnelController.TYPE_STD_CLIENT.equals(type) || TunnelController.TYPE_IRC_CLIENT.equals(type)) {
        // 
        if (Boolean.parseBoolean(props.getProperty(OPT + I2PTunnelClientBase.PROP_USE_SSL))) {
            // add the local interface and all targets to the cert
            String intfc = props.getProperty(TunnelController.PROP_INTFC);
            Set<String> altNames = new HashSet<String>(4);
            if (intfc != null && !intfc.equals("0.0.0.0") && !intfc.equals("::") && !intfc.equals("0:0:0:0:0:0:0:0"))
                altNames.add(intfc);
            String tgts = props.getProperty(TunnelController.PROP_DEST);
            if (tgts != null) {
                altNames.add(intfc);
                String[] hosts = DataHelper.split(tgts, "[ ,]");
                for (String h : hosts) {
                    int colon = h.indexOf(':');
                    if (colon >= 0)
                        h = h.substring(0, colon);
                    altNames.add(h);
                    if (!h.endsWith(".b32.i2p")) {
                        Hash hash = ConvertToHash.getHash(h);
                        if (hash != null)
                            altNames.add(hash.toBase32());
                    }
                }
            }
            try {
                boolean created = SSLClientUtil.verifyKeyStore(props, OPT, altNames);
                if (created) {
                    // config now contains new keystore props
                    String name = props.getProperty(TunnelController.PROP_NAME, "");
                    msgs.add("Created new self-signed certificate for tunnel " + name);
                }
            } catch (IOException ioe) {
                msgs.add("Failed to create new self-signed certificate for tunnel " + getTunnelName(tcg, tunnel) + ", check logs: " + ioe);
            }
        }
    }
    if (cur == null) {
        // creating new
        cur = new TunnelController(props, "", true);
        tcg.addController(cur);
        if (cur.getStartOnLoad())
            cur.startTunnelBackground();
    } else {
        cur.setConfig(props, "");
    }
    // if the current tunnel is shared, and of supported type
    if (Boolean.parseBoolean(cur.getSharedClient()) && TunnelController.isClient(cur.getType())) {
        // all clients use the same I2CP session, and as such, use the same I2CP options
        List<TunnelController> controllers = tcg.getControllers();
        for (int i = 0; i < controllers.size(); i++) {
            TunnelController c = controllers.get(i);
            // Current tunnel modified by user, skip
            if (c == cur)
                continue;
            // if it belongs to a shared destination, and is of supported type
            if (Boolean.parseBoolean(c.getSharedClient()) && TunnelController.isClient(c.getType())) {
                Properties cOpt = c.getConfig("");
                config.updateTunnelQuantities(cOpt);
                cOpt.setProperty("option.inbound.nickname", TunnelConfig.SHARED_CLIENT_NICKNAME);
                cOpt.setProperty("option.outbound.nickname", TunnelConfig.SHARED_CLIENT_NICKNAME);
                c.setConfig(cOpt, "");
            }
        }
    }
    return msgs;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) Hash(net.i2p.data.Hash) ConvertToHash(net.i2p.util.ConvertToHash) TunnelController(net.i2p.i2ptunnel.TunnelController) HashSet(java.util.HashSet)

Example 3 with TunnelController

use of net.i2p.i2ptunnel.TunnelController in project i2p.i2p by i2p.

the class GeneralHelper method deleteTunnel.

/**
 * Stop the tunnel, delete from config,
 * rename the private key file if in the default directory
 *
 * @param privKeyFile The priv key file name from the tunnel edit form. Can
 *                    be null if not known.
 */
public static List<String> deleteTunnel(I2PAppContext context, TunnelControllerGroup tcg, int tunnel, String privKeyFile) {
    List<String> msgs;
    TunnelController cur = getController(tcg, tunnel);
    if (cur == null) {
        msgs = new ArrayList<String>();
        msgs.add("Invalid tunnel number");
        return msgs;
    }
    msgs = tcg.removeController(cur);
    msgs.addAll(saveConfig(context, tcg));
    // Rename private key file if it was a default name in
    // the default directory, so it doesn't get reused when a new
    // tunnel is created.
    // Use configured file name if available, not the one from the form.
    String pk = cur.getPrivKeyFile();
    if (pk == null)
        pk = privKeyFile;
    if (pk != null && pk.startsWith("i2ptunnel") && pk.endsWith("-privKeys.dat") && ((!TunnelController.isClient(cur.getType())) || cur.getPersistentClientKey())) {
        File pkf = new File(context.getConfigDir(), pk);
        if (pkf.exists()) {
            String name = cur.getName();
            if (name == null) {
                name = cur.getDescription();
                if (name == null) {
                    name = cur.getType();
                    if (name == null)
                        name = Long.toString(context.clock().now());
                }
            }
            name = name.replace(' ', '_').replace(':', '_').replace("..", "_").replace('/', '_').replace('\\', '_');
            name = "i2ptunnel-deleted-" + name + '-' + context.clock().now() + "-privkeys.dat";
            File backupDir = new SecureFile(context.getConfigDir(), TunnelController.KEY_BACKUP_DIR);
            File to;
            if (backupDir.isDirectory() || backupDir.mkdir())
                to = new File(backupDir, name);
            else
                to = new File(context.getConfigDir(), name);
            boolean success = FileUtil.rename(pkf, to);
            if (success)
                msgs.add("Private key file " + pkf.getAbsolutePath() + " renamed to " + to.getAbsolutePath());
        }
    }
    return msgs;
}
Also used : SecureFile(net.i2p.util.SecureFile) TunnelController(net.i2p.i2ptunnel.TunnelController) SecureFile(net.i2p.util.SecureFile) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File)

Example 4 with TunnelController

use of net.i2p.i2ptunnel.TunnelController in project i2p.i2p by i2p.

the class EditBean method getSigningPrivateKey.

/**
 **
 *    public String getNameSignature(int tunnel) {
 *        String spoof = getSpoofedHost(tunnel);
 *        if (spoof.length() <= 0)
 *            return "";
 *        TunnelController tun = getController(tunnel);
 *        if (tun == null)
 *            return "";
 *        String keyFile = tun.getPrivKeyFile();
 *        if (keyFile != null && keyFile.trim().length() > 0) {
 *            File f = new File(keyFile);
 *            if (!f.isAbsolute())
 *                f = new File(_context.getConfigDir(), keyFile);
 *            PrivateKeyFile pkf = new PrivateKeyFile(f);
 *            try {
 *                Destination d = pkf.getDestination();
 *                if (d == null)
 *                    return "";
 *                SigningPrivateKey privKey = pkf.getSigningPrivKey();
 *                if (privKey == null)
 *                    return "";
 *                Signature sig = _context.dsa().sign(spoof.getBytes("UTF-8"), privKey);
 *                if (sig == null)
 *                    return "";
 *                return Base64.encode(sig.getData());
 *            } catch (I2PException e) {
 *            } catch (IOException e) {}
 *        }
 *        return "";
 *    }
 ***
 */
/**
 *  @since 0.9.26
 *  @return key or null
 */
public SigningPrivateKey getSigningPrivateKey(int tunnel) {
    TunnelController tun = getController(tunnel);
    if (tun == null)
        return null;
    String keyFile = tun.getPrivKeyFile();
    if (keyFile != null && keyFile.trim().length() > 0) {
        File f = new File(keyFile);
        if (!f.isAbsolute())
            f = new File(_context.getConfigDir(), keyFile);
        PrivateKeyFile pkf = new PrivateKeyFile(f);
        return pkf.getSigningPrivKey();
    }
    return null;
}
Also used : TunnelController(net.i2p.i2ptunnel.TunnelController) PrivateKeyFile(net.i2p.data.PrivateKeyFile) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File)

Example 5 with TunnelController

use of net.i2p.i2ptunnel.TunnelController in project i2p.i2p by i2p.

the class IndexBean method getServerTarget.

/**
 * @return valid host:port only if isServerTargetLinkValid() is true
 */
public String getServerTarget(int tunnel) {
    TunnelController tun = getController(tunnel);
    if (tun != null) {
        String host;
        if ("streamrserver".equals(tun.getType()))
            host = tun.getListenOnInterface();
        else
            host = tun.getTargetHost();
        String port = tun.getTargetPort();
        if (host == null || host.length() == 0)
            host = "<font color=\"red\">" + _t("Host not set") + "</font>";
        else if (Addresses.getIP(host) == null)
            host = "<font color=\"red\">" + _t("Invalid address") + ' ' + host + "</font>";
        else if (host.indexOf(':') >= 0)
            host = '[' + host + ']';
        if (port == null || port.length() == 0)
            port = "<font color=\"red\">" + _t("Port not set") + "</font>";
        else if (Addresses.getPort(port) == 0)
            port = "<font color=\"red\">" + _t("Invalid port") + ' ' + port + "</font>";
        return host + ':' + port;
    } else
        return "";
}
Also used : TunnelController(net.i2p.i2ptunnel.TunnelController)

Aggregations

TunnelController (net.i2p.i2ptunnel.TunnelController)14 File (java.io.File)5 PrivateKeyFile (net.i2p.data.PrivateKeyFile)5 Properties (java.util.Properties)4 IOException (java.io.IOException)3 SecureFile (net.i2p.util.SecureFile)3 I2PException (net.i2p.I2PException)2 Destination (net.i2p.data.Destination)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Certificate (net.i2p.data.Certificate)1 Hash (net.i2p.data.Hash)1 SessionKey (net.i2p.data.SessionKey)1 TunnelControllerGroup (net.i2p.i2ptunnel.TunnelControllerGroup)1 ConvertToHash (net.i2p.util.ConvertToHash)1