Search in sources :

Example 36 with I2PException

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

the class TCPtoI2P method run.

/**
 * TCP stream to I2P stream thread starter
 */
public void run() {
    String line, input;
    InputStream Iin = null;
    OutputStream Iout = null;
    InputStream in = null;
    OutputStream out = null;
    Thread t = null;
    Thread q = null;
    try {
        try {
            in = sock.getInputStream();
            out = sock.getOutputStream();
            line = lnRead(in);
            input = line.toLowerCase(Locale.US);
            Destination dest = null;
            if (input.endsWith(".i2p")) {
                // dest = I2PTunnel.destFromName(input);
                dest = I2PAppContext.getGlobalContext().namingService().lookup(input);
                if (dest != null) {
                    line = dest.toBase64();
                } else {
                    Emsg("Can't find destination: " + input, out);
                    return;
                }
            }
            dest = new Destination();
            dest.fromBase64(line);
            try {
                // get a client socket
                I2P = socketManager.connect(dest);
                // temp bugfix, this *SHOULD* be the default
                I2P.setReadTimeout(0);
                // make readers/writers
                Iin = I2P.getInputStream();
                Iout = I2P.getOutputStream();
                // setup to cross the streams
                // app -> I2P
                TCPio conn_c = new TCPio(in, Iout, lives);
                // I2P -> app
                TCPio conn_a = new TCPio(Iin, out, lives);
                t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " TCPioA");
                q = new I2PAppThread(conn_a, Thread.currentThread().getName() + " TCPioB");
                // Fire!
                t.start();
                q.start();
                while (t.isAlive() && q.isAlive() && lives.get()) {
                    // AND is used here to kill off the other thread
                    // sleep for 10 ms
                    Thread.sleep(10);
                }
            } catch (I2PException e) {
                Emsg(e.toString(), out);
            } catch (ConnectException e) {
                Emsg(e.toString(), out);
            } catch (NoRouteToHostException e) {
                Emsg(e.toString(), out);
            }
        } catch (InterruptedIOException e) {
        // We're breaking away.
        } catch (InterruptedException e) {
        // ditto
        } catch (IOException e) {
            try {
                Emsg(e.toString(), out);
            } catch (IOException ex) {
            // ditto
            }
        } catch (DataFormatException e) {
            try {
                Emsg(e.toString(), out);
            } catch (IOException ex) {
            // ditto
            }
        }
    } finally {
        try {
            t.interrupt();
        } catch (Exception e) {
        }
        try {
            q.interrupt();
        } catch (Exception e) {
        }
        try {
            in.close();
        } catch (Exception e) {
        }
        try {
            out.close();
        } catch (Exception e) {
        }
        try {
            Iin.close();
        } catch (Exception e) {
        }
        try {
            Iout.close();
        } catch (Exception e) {
        }
        try {
            // System.out.println("TCPtoI2P: Close I2P");
            I2P.close();
        } catch (Exception e) {
        }
        try {
            // System.out.println("TCPtoI2P: Close sock");
            sock.close();
        } catch (Exception e) {
        }
    }
// System.out.println("TCPtoI2P: Done.");
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) InterruptedIOException(java.io.InterruptedIOException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) I2PAppThread(net.i2p.util.I2PAppThread) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) I2PException(net.i2p.I2PException) ConnectException(java.net.ConnectException) NoRouteToHostException(java.net.NoRouteToHostException) I2PAppThread(net.i2p.util.I2PAppThread) DataFormatException(net.i2p.data.DataFormatException) ConnectException(java.net.ConnectException)

Example 37 with I2PException

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

the class I2PSnarkUtil method connect.

/**
 * connect to the given destination
 */
I2PSocket connect(PeerID peer) throws IOException {
    I2PSocketManager mgr = _manager;
    if (mgr == null)
        throw new IOException("No socket manager");
    Destination addr = peer.getAddress();
    if (addr == null)
        throw new IOException("Null address");
    if (addr.equals(getMyDestination()))
        throw new IOException("Attempt to connect to myself");
    Hash dest = addr.calculateHash();
    if (_banlist.contains(dest))
        throw new IOException("Not trying to contact " + dest.toBase64() + ", as they are banlisted");
    try {
        // TODO opts.setPort(xxx); connect(addr, opts)
        // DHT moved above 6881 in 0.9.9
        I2PSocket rv = _manager.connect(addr);
        if (rv != null)
            _banlist.remove(dest);
        return rv;
    } catch (I2PException ie) {
        _banlist.add(dest);
        _context.simpleTimer2().addEvent(new Unbanlist(dest), 10 * 60 * 1000);
        IOException ioe = new IOException("Unable to reach the peer " + peer);
        ioe.initCause(ie);
        throw ioe;
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PSocketManager(net.i2p.client.streaming.I2PSocketManager) I2PSocket(net.i2p.client.streaming.I2PSocket) IOException(java.io.IOException) Hash(net.i2p.data.Hash)

Example 38 with I2PException

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

the class I2Plistener method run.

/**
 * Simply listen on I2P port, and thread connections
 */
public void run() {
    boolean g = false;
    I2PSocket sessSocket = null;
    int conn = 0;
    try {
        try {
            serverSocket.setSoTimeout(50);
            while (lives.get()) {
                try {
                    sessSocket = serverSocket.accept();
                    g = true;
                } catch (ConnectException ce) {
                    g = false;
                } catch (SocketTimeoutException ste) {
                    g = false;
                }
                if (g) {
                    g = false;
                    conn++;
                    // toss the connection to a new thread.
                    I2PtoTCP conn_c = new I2PtoTCP(sessSocket, info, database, lives);
                    Thread t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " I2PtoTCP " + conn);
                    t.start();
                }
            }
        } catch (I2PException e) {
            // bad stuff
            System.out.println("Exception " + e);
        }
    } finally {
        try {
            serverSocket.close();
        } catch (I2PException ex) {
        }
    // System.out.println("I2Plistener: Close");
    }
}
Also used : I2PException(net.i2p.I2PException) SocketTimeoutException(java.net.SocketTimeoutException) I2PSocket(net.i2p.client.streaming.I2PSocket) I2PAppThread(net.i2p.util.I2PAppThread) ConnectException(java.net.ConnectException) I2PAppThread(net.i2p.util.I2PAppThread)

Example 39 with I2PException

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

the class I2PBote method initializeSession.

/**
 * Sets up a {@link I2PSession}, using the I2P destination stored on disk or creating a new I2P
 * destination if no key file exists.
 */
private void initializeSession() throws I2PSessionException {
    Properties sessionProperties = new Properties();
    // set tunnel names
    sessionProperties.setProperty("inbound.nickname", "I2P-Bote");
    sessionProperties.setProperty("outbound.nickname", "I2P-Bote");
    sessionProperties.putAll(configuration.getI2CPOptions());
    // According to sponge, muxed depends on gzip, so leave gzip enabled
    // read the local destination key from the key file if it exists
    File destinationKeyFile = configuration.getDestinationKeyFile();
    FileReader fileReader = null;
    try {
        fileReader = new FileReader(destinationKeyFile);
        char[] destKeyBuffer = new char[(int) destinationKeyFile.length()];
        fileReader.read(destKeyBuffer);
        byte[] localDestinationKey = Base64.decode(new String(destKeyBuffer));
        ByteArrayInputStream inputStream = new ByteArrayInputStream(localDestinationKey);
        socketManager = I2PSocketManagerFactory.createDisconnectedManager(inputStream, null, 0, sessionProperties);
    } catch (IOException e) {
        log.debug("Destination key file doesn't exist or isn't readable." + e);
    } catch (I2PSessionException e) {
    // Won't happen, inputStream != null
    } finally {
        if (fileReader != null)
            try {
                fileReader.close();
            } catch (IOException e) {
                log.debug("Error closing file: <" + destinationKeyFile.getAbsolutePath() + ">" + e);
            }
    }
    // if the local destination key can't be read or is invalid, create a new one
    if (socketManager == null) {
        log.debug("Creating new local destination key");
        try {
            ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
            i2pClient.createDestination(arrayStream);
            byte[] localDestinationKey = arrayStream.toByteArray();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(localDestinationKey);
            socketManager = I2PSocketManagerFactory.createDisconnectedManager(inputStream, null, 0, sessionProperties);
            saveLocalDestinationKeys(destinationKeyFile, localDestinationKey);
        } catch (I2PException e) {
            log.error("Error creating local destination key.", e);
        } catch (IOException e) {
            log.error("Error writing local destination key to file.", e);
        }
    }
    i2pSession = socketManager.getSession();
    // Throws I2PSessionException if the connection fails
    i2pSession.connect();
    Destination localDestination = i2pSession.getMyDestination();
    log.info("Local destination key (base64): " + localDestination.toBase64());
    log.info("Local destination hash (base64): " + localDestination.calculateHash().toBase64());
    log.info("Local destination hash (base32): " + Util.toBase32(localDestination));
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSessionException(net.i2p.client.I2PSessionException) FileReader(java.io.FileReader) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

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