Search in sources :

Example 21 with I2PException

use of net.i2p.I2PException 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 22 with I2PException

use of net.i2p.I2PException 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 23 with I2PException

use of net.i2p.I2PException in project i2p.i2p-bote by i2p.

the class SeedlessAnnounce method doSeedlessAnnounce.

private synchronized void doSeedlessAnnounce() {
    List<String> seedlessServers = seedlessScrapeServers.getSeedlessServers();
    if (seedlessServers.isEmpty()) {
        // try again in a minute.
        log.error("SeedlessServers.isEmpty, will retry shortly.");
        lastSeedlessAnnounce = System.currentTimeMillis() - (interval - TimeUnit.MINUTES.toMillis(1));
        return;
    }
    // Announce to 10 servers.
    // We do this over the i2pSocket.
    int successful = Math.min(10, seedlessServers.size());
    log.debug("Try to announce to " + successful + " Seedless Servers");
    Collections.shuffle(seedlessServers, new Random());
    Iterator<String> it = seedlessServers.iterator();
    String line;
    I2PSocket I2P;
    InputStream Iin;
    OutputStream Iout;
    BufferedReader data;
    Boolean didsomething = false;
    BufferedWriter output;
    while (successful > 0 && it.hasNext()) {
        lastSeedlessAnnounce = System.currentTimeMillis();
        String b32 = it.next();
        Destination dest = null;
        I2P = null;
        try {
            lastSeedlessAnnounce = System.currentTimeMillis();
            // deprecated dest = I2PTunnel.destFromName(b32);
            dest = I2PAppContext.getGlobalContext().namingService().lookup(b32);
            lastSeedlessAnnounce = System.currentTimeMillis();
            if (dest == null) {
                log.debug("Could not find the destination: <" + b32 + ">");
                continue;
            }
            line = dest.toBase64();
            dest = new Destination();
            dest.fromBase64(line);
            I2P = socketManager.connect(dest);
            // I2P.setReadTimeout(0); // temp bugfix, this *SHOULD* be the default
            // make readers/writers
            Iin = I2P.getInputStream();
            Iout = I2P.getOutputStream();
            output = new BufferedWriter(new OutputStreamWriter(Iout));
            output.write(announceString);
            output.flush();
            data = new BufferedReader(new InputStreamReader(Iin));
            // Check for success.
            line = data.readLine();
            if (line != null) {
                if (line.contains(" 200 ")) {
                    log.debug("Announced to " + b32);
                    successful--;
                    didsomething = true;
                } else {
                    log.debug("Announce to " + b32 + " Failed with Error " + line);
                    log.debug("We sent " + announceString);
                }
            }
            while ((line = data.readLine()) != null) {
            }
        } catch (DataFormatException ex) {
            log.debug("Not base64!", ex);
        } catch (ConnectException ex) {
            log.debug("ConnectException", ex);
        } catch (NoRouteToHostException ex) {
            log.debug("NoRouteToHostException", ex);
        } catch (InterruptedIOException ex) {
            log.debug("InterruptedIOException", ex);
        } catch (IOException ex) {
            log.debug("IOException", ex);
            ex.printStackTrace();
        } catch (I2PException ex) {
            log.debug("I2PException", ex);
        }
        if (I2P != null) {
            try {
                I2P.close();
            } catch (IOException ex) {
            // don't care.
            }
        }
    }
    if (!didsomething) {
        // try again in 1 minute.
        lastSeedlessAnnounce = System.currentTimeMillis() - (interval - TimeUnit.MINUTES.toMillis(1));
        return;
    }
    lastSeedlessAnnounce = System.currentTimeMillis();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) InterruptedIOException(java.io.InterruptedIOException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) I2PSocket(net.i2p.client.streaming.I2PSocket) OutputStream(java.io.OutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) BufferedWriter(java.io.BufferedWriter) DataFormatException(net.i2p.data.DataFormatException) Random(java.util.Random) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) ConnectException(java.net.ConnectException)

Example 24 with I2PException

use of net.i2p.I2PException in project i2p.i2p by i2p.

the class I2PSocketManagerFactory method createManager.

/**
 * Create a socket manager using a brand new destination connected to the
 * I2CP router on the given machine reachable through the given port.
 *
 * Blocks for a long time while the router builds tunnels.
 * The nonblocking createDisconnectedManager() is preferred.
 *
 * @param i2cpHost I2CP host null to use default, ignored if in router context
 * @param i2cpPort I2CP port &lt;= 0 to use default, ignored if in router context
 * @param opts Streaming and I2CP options, may be null
 * @return the newly created socket manager, or null if there were errors
 */
public static I2PSocketManager createManager(String i2cpHost, int i2cpPort, Properties opts) {
    I2PClient client = I2PClientFactory.createClient();
    ByteArrayOutputStream keyStream = new ByteArrayOutputStream(1024);
    try {
        client.createDestination(keyStream, getSigType(opts));
        ByteArrayInputStream in = new ByteArrayInputStream(keyStream.toByteArray());
        return createManager(in, i2cpHost, i2cpPort, opts);
    } catch (IOException ioe) {
        getLog().error("Error creating the destination for socket manager", ioe);
        return null;
    } catch (I2PException ie) {
        getLog().error("Error creating the destination for socket manager", ie);
        return null;
    }
}
Also used : I2PException(net.i2p.I2PException) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 25 with I2PException

use of net.i2p.I2PException in project i2p.i2p by i2p.

the class I2PSocketManagerFactory method createDisconnectedManager.

/**
 * Create a disconnected socket manager using the destination loaded from the given private key
 * stream, or null for a transient destination.
 *
 * Non-blocking. Does not connect to the router or build tunnels.
 * For servers, caller MUST call getSession().connect() to build tunnels and start listening.
 * For clients, caller may do that to build tunnels in advance;
 * otherwise, the first call to connect() will initiate a connection to the router,
 * with significant delay for tunnel building.
 *
 * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
 *                           or null for a transient destination. Caller must close.
 * @param i2cpHost I2CP host null to use default, ignored if in router context
 * @param i2cpPort I2CP port &lt;= 0 to use default, ignored if in router context
 * @param opts Streaming and I2CP options, may be null
 * @return the newly created socket manager, non-null (throws on error)
 * @since 0.9.8
 */
public static I2PSocketManager createDisconnectedManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort, Properties opts) throws I2PSessionException {
    if (myPrivateKeyStream == null) {
        I2PClient client = I2PClientFactory.createClient();
        ByteArrayOutputStream keyStream = new ByteArrayOutputStream(1024);
        try {
            client.createDestination(keyStream, getSigType(opts));
        } catch (I2PException e) {
            throw new I2PSessionException("Error creating keys", e);
        } catch (IOException e) {
            throw new I2PSessionException("Error creating keys", e);
        }
        myPrivateKeyStream = new ByteArrayInputStream(keyStream.toByteArray());
    }
    return createManager(myPrivateKeyStream, i2cpHost, i2cpPort, opts, false);
}
Also used : I2PException(net.i2p.I2PException) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

I2PException (net.i2p.I2PException)39 IOException (java.io.IOException)31 Destination (net.i2p.data.Destination)26 I2PSocket (net.i2p.client.streaming.I2PSocket)15 Properties (java.util.Properties)8 File (java.io.File)7 InterruptedIOException (java.io.InterruptedIOException)7 ConnectException (java.net.ConnectException)7 I2PAppThread (net.i2p.util.I2PAppThread)7 OutputStream (java.io.OutputStream)6 I2PClient (net.i2p.client.I2PClient)6 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)6 DataFormatException (net.i2p.data.DataFormatException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 I2PSession (net.i2p.client.I2PSession)5 I2PSessionException (net.i2p.client.I2PSessionException)5 I2PServerSocket (net.i2p.client.streaming.I2PServerSocket)5 Hash (net.i2p.data.Hash)5 NoRouteToHostException (java.net.NoRouteToHostException)4