Search in sources :

Example 46 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class TCBShare method updateOptsFromShare.

/**
 * retrieve from cache
 */
public void updateOptsFromShare(Connection con) {
    Destination dest = con.getRemotePeer();
    if (dest == null)
        return;
    ConnectionOptions opts = con.getOptions();
    if (opts == null)
        return;
    Entry e = _cache.get(dest);
    if (e == null || e.isExpired())
        return;
    final int rtt, rttDev, wdw;
    synchronized (e) {
        rtt = e.getRTT();
        rttDev = e.getRTTDev();
        wdw = e.getWindowSize();
    }
    if (_log.shouldLog(Log.DEBUG)) {
        _log.debug("From cache: " + con.getSession().getMyDestination().calculateHash().toBase64().substring(0, 4) + '-' + dest.calculateHash().toBase64().substring(0, 4) + " RTT: " + rtt + " RTTDev: " + rttDev + " wdw: " + wdw);
    }
    opts.loadFromCache(rtt, rttDev, wdw);
}
Also used : Destination(net.i2p.data.Destination)

Example 47 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class GeneralHelper method getSigType.

/**
 *  @param newTunnelType used if tunnel < 0
 *  @return the current type if we have a destination already,
 *          else the default for that type of tunnel
 */
public int getSigType(int tunnel, String newTunnelType) {
    SigType type;
    String ttype;
    boolean isShared;
    if (tunnel >= 0) {
        Destination d = getDestination(tunnel);
        if (d != null) {
            type = d.getSigType();
            if (type != null)
                return type.getCode();
        }
        String stype = getProperty(tunnel, I2PClient.PROP_SIGTYPE, null);
        type = stype != null ? SigType.parseSigType(stype) : null;
        ttype = getTunnelType(tunnel);
        isShared = isSharedClient(tunnel);
    } else {
        type = null;
        ttype = newTunnelType;
        isShared = false;
    }
    if (type == null) {
        // same default logic as in TunnelController.setConfig()
        if (!TunnelController.isClient(ttype) || TunnelController.TYPE_IRC_CLIENT.equals(ttype) || TunnelController.TYPE_SOCKS_IRC.equals(ttype) || TunnelController.TYPE_SOCKS.equals(ttype) || TunnelController.TYPE_STREAMR_CLIENT.equals(ttype) || TunnelController.TYPE_STD_CLIENT.equals(ttype) || (TunnelController.TYPE_HTTP_CLIENT.equals(ttype) && isShared))
            type = TunnelController.PREFERRED_SIGTYPE;
        else
            type = SigType.DSA_SHA1;
    }
    return type.getCode();
}
Also used : Destination(net.i2p.data.Destination) SigType(net.i2p.crypto.SigType)

Example 48 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class IndexBean method modifyDestination.

/**
 * Modify or create a destination
 */
private String modifyDestination() {
    String privKeyFile = _config.getPrivKeyFile();
    if (privKeyFile == null)
        return "Private Key File not specified";
    TunnelController tun = getController(_tunnel);
    Properties config = getConfig();
    if (tun == null) {
        // creating new
        tun = new TunnelController(config, "", true);
        _group.addController(tun);
        saveChanges();
    } else if (tun.getIsRunning() || tun.getIsStarting()) {
        return "Tunnel must be stopped before modifying destination";
    }
    File keyFile = new File(privKeyFile);
    if (!keyFile.isAbsolute())
        keyFile = new File(_context.getConfigDir(), privKeyFile);
    PrivateKeyFile pkf = new PrivateKeyFile(keyFile);
    try {
        pkf.createIfAbsent();
    } catch (I2PException e) {
        return "Create private key file failed: " + e;
    } catch (IOException e) {
        return "Create private key file failed: " + e;
    }
    switch(_certType) {
        case Certificate.CERTIFICATE_TYPE_NULL:
        case Certificate.CERTIFICATE_TYPE_HIDDEN:
            pkf.setCertType(_certType);
            break;
        case Certificate.CERTIFICATE_TYPE_HASHCASH:
            pkf.setHashCashCert(_hashCashValue);
            break;
        case Certificate.CERTIFICATE_TYPE_SIGNED:
            if (_certSigner == null || _certSigner.trim().length() <= 0)
                return "No signing destination specified";
            // find the signer's key file...
            String signerPKF = null;
            for (int i = 0; i < getTunnelCount(); i++) {
                TunnelController c = getController(i);
                if (_certSigner.equals(c.getConfig("").getProperty(TunnelController.PROP_NAME)) || _certSigner.equals(c.getConfig("").getProperty(TunnelController.PROP_SPOOFED_HOST))) {
                    signerPKF = c.getConfig("").getProperty(TunnelController.PROP_FILE);
                    break;
                }
            }
            if (signerPKF == null || signerPKF.length() <= 0)
                return "Signing destination " + _certSigner + " not found";
            if (privKeyFile.equals(signerPKF))
                return "Self-signed destinations not allowed";
            Certificate c = pkf.setSignedCert(new PrivateKeyFile(signerPKF));
            if (c == null)
                return "Signing failed - does signer destination exist?";
            break;
        default:
            return "Unknown certificate type";
    }
    Destination newdest;
    try {
        pkf.write();
        newdest = pkf.getDestination();
    } catch (I2PException e) {
        return "Modification failed: " + e;
    } catch (IOException e) {
        return "Modification failed: " + e;
    }
    return "Destination modified - " + "New Base32 is " + newdest.toBase32() + "New Destination is " + newdest.toBase64();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) TunnelController(net.i2p.i2ptunnel.TunnelController) PrivateKeyFile(net.i2p.data.PrivateKeyFile) IOException(java.io.IOException) Properties(java.util.Properties) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File) Certificate(net.i2p.data.Certificate)

Example 49 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SOCKS4aServer method getDestinationI2PSocket.

/**
 * Get an I2PSocket that can be used to send/receive 8-bit clean data
 * to/from the destination of the SOCKS connection.
 *
 * @return an I2PSocket connected with the destination
 */
public I2PSocket getDestinationI2PSocket(I2PSOCKSTunnel t) throws SOCKSException {
    setupServer();
    if (connHostName == null) {
        _log.error("BUG: destination host name has not been initialized!");
        throw new SOCKSException("BUG! See the logs!");
    }
    if (connPort == 0) {
        _log.error("BUG: destination port has not been initialized!");
        throw new SOCKSException("BUG! See the logs!");
    }
    // for errors
    DataOutputStream out;
    try {
        out = new DataOutputStream(clientSock.getOutputStream());
    } catch (IOException e) {
        throw new SOCKSException("Connection error", e);
    }
    // FIXME: here we should read our config file, select an
    // outproxy, and instantiate the proper socket class that
    // handles the outproxy itself (SOCKS4a, SOCKS4a, HTTP CONNECT...).
    I2PSocket destSock;
    try {
        if (connHostName.toLowerCase(Locale.US).endsWith(".i2p")) {
            Destination dest = _context.namingService().lookup(connHostName);
            if (dest == null) {
                try {
                    sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                } catch (IOException ioe) {
                }
                throw new SOCKSException("Host not found");
            }
            if (_log.shouldDebug())
                _log.debug("connecting to " + connHostName + "...");
            Properties overrides = new Properties();
            I2PSocketOptions sktOpts = t.buildOptions(overrides);
            sktOpts.setPort(connPort);
            destSock = t.createI2PSocket(dest, sktOpts);
        } else if ("localhost".equals(connHostName) || "127.0.0.1".equals(connHostName)) {
            String err = "No localhost accesses allowed through the Socks Proxy";
            _log.error(err);
            try {
                sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
            } catch (IOException ioe) {
            }
            throw new SOCKSException(err);
        /**
         **
         *            } else if (connPort == 80) {
         *                // rewrite GET line to include hostname??? or add Host: line???
         *                // or forward to local eepProxy (but that's a Socket not an I2PSocket)
         *                // use eepProxy configured outproxies?
         *                String err = "No handler for HTTP outproxy implemented - to: " + connHostName;
         *                _log.error(err);
         *                try {
         *                    sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
         *                } catch (IOException ioe) {}
         *                throw new SOCKSException(err);
         ***
         */
        } else {
            Outproxy outproxy = getOutproxyPlugin();
            if (outproxy != null) {
                try {
                    destSock = new SocketWrapper(outproxy.connect(connHostName, connPort));
                } catch (IOException ioe) {
                    try {
                        sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                    } catch (IOException ioe2) {
                    }
                    throw new SOCKSException("connect failed via outproxy plugin", ioe);
                }
            } else {
                List<String> proxies = t.getProxies(connPort);
                if (proxies == null || proxies.isEmpty()) {
                    String err = "No outproxy configured for port " + connPort + " and no default configured either - host: " + connHostName;
                    _log.error(err);
                    try {
                        sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                    } catch (IOException ioe) {
                    }
                    throw new SOCKSException(err);
                }
                int p = _context.random().nextInt(proxies.size());
                String proxy = proxies.get(p);
                Destination dest = _context.namingService().lookup(proxy);
                if (dest == null) {
                    try {
                        sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                    } catch (IOException ioe) {
                    }
                    throw new SOCKSException("Outproxy not found");
                }
                if (_log.shouldDebug())
                    _log.debug("connecting to port " + connPort + " proxy " + proxy + " for " + connHostName + "...");
                // this isn't going to work, these need to be socks outproxies so we need
                // to do a socks session to them?
                destSock = t.createI2PSocket(dest);
            }
        }
        confirmConnection();
        _log.debug("connection confirmed - exchanging data...");
    } catch (DataFormatException e) {
        try {
            sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error in destination format", e);
    } catch (IOException e) {
        try {
            sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error connecting", e);
    } catch (I2PException e) {
        try {
            sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error connecting", e);
    }
    return destSock;
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) I2PSocket(net.i2p.client.streaming.I2PSocket) Outproxy(net.i2p.app.Outproxy) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) IOException(java.io.IOException) Properties(java.util.Properties) DataFormatException(net.i2p.data.DataFormatException) List(java.util.List)

Example 50 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SOCKS5Server method outproxyConnect.

/**
 *  Act as a SOCKS 5 client to connect to an outproxy
 *  @return open socket or throws error
 *  @since 0.8.2
 */
private I2PSocket outproxyConnect(I2PSOCKSTunnel tun, String proxy) throws IOException, I2PException {
    Properties overrides = new Properties();
    overrides.setProperty("option.i2p.streaming.connectDelay", "1000");
    I2PSocketOptions proxyOpts = tun.buildOptions(overrides);
    Destination dest = _context.namingService().lookup(proxy);
    if (dest == null)
        throw new SOCKSException("Outproxy not found");
    I2PSocket destSock = tun.createI2PSocket(dest, proxyOpts);
    OutputStream out = null;
    InputStream in = null;
    try {
        out = destSock.getOutputStream();
        in = destSock.getInputStream();
        boolean authAvail = Boolean.parseBoolean(props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH));
        String configUser = null;
        String configPW = null;
        if (authAvail) {
            configUser = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER_PREFIX + proxy);
            configPW = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_PW_PREFIX + proxy);
            if (configUser == null || configPW == null) {
                configUser = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER);
                configPW = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_PW);
            }
        }
        SOCKS5Client.connect(in, out, connHostName, connPort, configUser, configPW);
    } catch (IOException e) {
        try {
            destSock.close();
        } catch (IOException ioe) {
        }
        if (in != null)
            try {
                in.close();
            } catch (IOException ioe) {
            }
        if (out != null)
            try {
                out.close();
            } catch (IOException ioe) {
            }
        throw e;
    }
    // that's it, caller will send confirmation to our client
    return destSock;
}
Also used : Destination(net.i2p.data.Destination) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) SOCKSException(net.i2p.socks.SOCKSException) I2PSocket(net.i2p.client.streaming.I2PSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) IOException(java.io.IOException) Properties(java.util.Properties)

Aggregations

Destination (net.i2p.data.Destination)149 IOException (java.io.IOException)46 DataFormatException (net.i2p.data.DataFormatException)33 Properties (java.util.Properties)29 I2PException (net.i2p.I2PException)26 Hash (net.i2p.data.Hash)18 ArrayList (java.util.ArrayList)13 File (java.io.File)12 I2PSessionException (net.i2p.client.I2PSessionException)12 SigType (net.i2p.crypto.SigType)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 I2PSession (net.i2p.client.I2PSession)10 I2PSocket (net.i2p.client.streaming.I2PSocket)10 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 I2PClient (net.i2p.client.I2PClient)7 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)7 Test (org.junit.Test)6