Search in sources :

Example 21 with Hash

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

the class ConfigPeerHandler method processForm.

@Override
protected void processForm() {
    if ("Save Configuration".equals(_action)) {
        _context.router().saveConfig();
        addFormNotice("Settings saved - not really!!!!!");
    } else if (_action.equals(_t("Ban peer until restart"))) {
        Hash h = getHash();
        if (h != null) {
            _context.banlist().banlistRouterForever(h, _t("Manually banned via {0}"), "<a href=\"configpeer\">configpeer</a>");
            addFormNotice(_t("Peer") + " " + _peer + " " + _t("banned until restart"));
            return;
        }
        addFormError(_t("Invalid peer"));
    } else if (_action.equals(_t("Unban peer"))) {
        Hash h = getHash();
        if (h != null) {
            if (_context.banlist().isBanlisted(h)) {
                _context.banlist().unbanlistRouter(h);
                addFormNotice(_t("Peer") + " " + _peer + " " + _t("unbanned"));
            } else
                addFormNotice(_t("Peer") + " " + _peer + " " + _t("is not currently banned"));
            return;
        }
        addFormError(_t("Invalid peer"));
    } else if (_action.equals(_t("Adjust peer bonuses"))) {
        Hash h = getHash();
        if (h != null) {
            PeerProfile prof = _context.profileOrganizer().getProfile(h);
            if (prof != null) {
                try {
                    prof.setSpeedBonus(Integer.parseInt(_speed));
                } catch (NumberFormatException nfe) {
                    addFormError(_t("Bad speed value"));
                }
                try {
                    prof.setCapacityBonus(Integer.parseInt(_capacity));
                } catch (NumberFormatException nfe) {
                    addFormError(_t("Bad capacity value"));
                }
                addFormNotice("Bonuses adjusted for " + _peer);
            } else
                addFormError("No profile exists for " + _peer);
            return;
        }
        addFormError(_t("Invalid peer"));
    } else if (_action.startsWith("Check")) {
        addFormError(_t("Unsupported"));
    } else {
    // addFormError(_t("Unsupported") + ' ' + _action + '.');
    }
}
Also used : PeerProfile(net.i2p.router.peermanager.PeerProfile) Hash(net.i2p.data.Hash)

Example 22 with Hash

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

the class I2PSocketEepGet method sendRequest.

/**
 *  Look up the address, get a socket from the I2PSocketManager supplied in the constructor,
 *  and send the request.
 *
 *  @param timeout ignored
 */
@Override
protected void sendRequest(SocketTimeout timeout) throws IOException {
    if (_outputStream == null) {
        File outFile = new File(_outputFile);
        if (outFile.exists())
            _alreadyTransferred = outFile.length();
    }
    if (_proxyIn != null)
        try {
            _proxyIn.close();
        } catch (IOException ioe) {
        }
    if (_proxyOut != null)
        try {
            _proxyOut.close();
        } catch (IOException ioe) {
        }
    if (_socket != null)
        try {
            _socket.close();
        } catch (IOException ioe) {
        }
    try {
        URI url = new URI(_actualURL);
        if ("http".equals(url.getScheme())) {
            String host = url.getHost();
            if (host == null)
                throw new MalformedURLException("no hostname: " + _actualURL);
            int port = url.getPort();
            if (port <= 0 || port > 65535)
                port = 80;
            // as the naming service accepts B64KEY (but not B64KEY.i2p atm)
            if ("i2p".equals(host)) {
                String file = url.getRawPath();
                try {
                    int slash = 1 + file.substring(1).indexOf('/');
                    host = file.substring(1, slash);
                    _actualURL = "http://" + host + file.substring(slash);
                    String query = url.getRawQuery();
                    if (query != null)
                        _actualURL = _actualURL + '?' + query;
                } catch (IndexOutOfBoundsException ioobe) {
                    throw new MalformedURLException("Bad /i2p/ format: " + _actualURL);
                }
            }
            // Use existing I2PSession for lookups.
            // This is much more efficient than using the naming service
            Destination dest;
            I2PSession sess = _socketManager.getSession();
            if (sess != null && !sess.isClosed()) {
                try {
                    if (host.length() == 60 && host.endsWith(".b32.i2p")) {
                        byte[] b = Base32.decode(host.substring(0, 52));
                        if (b != null) {
                            Hash h = Hash.create(b);
                            dest = sess.lookupDest(h, 20 * 1000);
                        } else {
                            dest = null;
                        }
                    } else {
                        dest = sess.lookupDest(host, 20 * 1000);
                    }
                } catch (I2PSessionException ise) {
                    dest = null;
                }
            } else {
                dest = _context.namingService().lookup(host);
            }
            if (dest == null)
                throw new UnknownHostException("Unknown or non-i2p host: " + host);
            // Set the timeouts, using the other existing options in the socket manager
            // This currently duplicates what SocketTimeout is doing in EepGet,
            // but when that's ripped out of EepGet to use setsotimeout, we'll need this.
            Properties props = new Properties();
            props.setProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT, "" + CONNECT_TIMEOUT);
            props.setProperty(I2PSocketOptions.PROP_READ_TIMEOUT, "" + INACTIVITY_TIMEOUT);
            // This is important - even if the underlying socket doesn't have a connect delay,
            // we want to set it for this connection, so the request headers will go out
            // in the SYN packet, saving one RTT.
            props.setProperty(PROP_CONNECT_DELAY, CONNECT_DELAY);
            I2PSocketOptions opts = _socketManager.buildOptions(props);
            opts.setPort(port);
            _socket = _socketManager.connect(dest, opts);
        } else {
            throw new MalformedURLException("Unsupported protocol: " + _actualURL);
        }
    } catch (URISyntaxException use) {
        IOException ioe = new MalformedURLException("Bad URL");
        ioe.initCause(use);
        throw ioe;
    } catch (I2PException ie) {
        throw new IOException("I2P error", ie);
    }
    _proxyIn = _socket.getInputStream();
    _proxyOut = _socket.getOutputStream();
    // SocketTimeout doesn't take an I2PSocket, but no matter, because we
    // always close our socket in fetch() above.
    // timeout.setSocket(_socket);
    String req = getRequest();
    _proxyOut.write(DataHelper.getUTF8(req));
    _proxyOut.flush();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Hash(net.i2p.data.Hash) Properties(java.util.Properties) URI(java.net.URI) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) File(java.io.File)

Example 23 with Hash

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

the class I2PSessionImpl method destReceived.

/**
 *  Called by the message handler
 *  on reception of DestReplyMessage
 *  @param d non-null
 */
void destReceived(Destination d) {
    Hash h = d.calculateHash();
    synchronized (_lookupCache) {
        _lookupCache.put(h, d);
    }
    for (LookupWaiter w : _pendingLookups) {
        if (h.equals(w.hash)) {
            synchronized (w) {
                w.destination = d;
                w.notifyAll();
            }
        }
    }
}
Also used : Hash(net.i2p.data.Hash)

Example 24 with Hash

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

the class I2PSessionImpl method destReceived.

/**
 *  Called by the message handler
 *  on reception of HostReplyMessage
 *  @param d non-null
 *  @since 0.9.11
 */
void destReceived(long nonce, Destination d) {
    // notify by nonce and hash
    Hash h = d.calculateHash();
    for (LookupWaiter w : _pendingLookups) {
        if (nonce == w.nonce || h.equals(w.hash)) {
            synchronized (_lookupCache) {
                if (w.name != null)
                    _lookupCache.put(w.name, d);
                _lookupCache.put(h, d);
            }
            synchronized (w) {
                w.destination = d;
                w.notifyAll();
            }
        }
    }
}
Also used : Hash(net.i2p.data.Hash)

Example 25 with Hash

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

the class ConnectionAcceptor method run2.

private void run2() {
    while (!stop) {
        I2PServerSocket serverSocket = _util.getServerSocket();
        while ((serverSocket == null) && (!stop)) {
            if (!(_util.isConnecting() || _util.connected())) {
                stop = true;
                break;
            }
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException ie) {
            }
            serverSocket = _util.getServerSocket();
        }
        if (stop)
            break;
        try {
            I2PSocket socket = serverSocket.accept();
            if (socket == null) {
                continue;
            } else {
                if (socket.getPeerDestination().equals(_util.getMyDestination())) {
                    _log.error("Incoming connection from myself");
                    try {
                        socket.close();
                    } catch (IOException ioe) {
                    }
                    continue;
                }
                Hash h = socket.getPeerDestination().calculateHash();
                if (socket.getLocalPort() == 80) {
                    _badCounter.increment(h);
                    if (_log.shouldLog(Log.WARN))
                        _log.error("Dropping incoming HTTP from " + h);
                    try {
                        socket.close();
                    } catch (IOException ioe) {
                    }
                    continue;
                }
                int bad = _badCounter.count(h);
                if (bad >= MAX_BAD) {
                    if (_log.shouldLog(Log.WARN))
                        _log.warn("Rejecting connection from " + h + " after " + bad + " failures, max is " + MAX_BAD);
                    try {
                        socket.close();
                    } catch (IOException ioe) {
                    }
                    continue;
                }
                Thread t = new I2PAppThread(new Handler(socket), "I2PSnark incoming connection");
                t.start();
            }
        } catch (I2PException ioe) {
            int level = stop ? Log.WARN : Log.ERROR;
            if (_log.shouldLog(level))
                _log.log(level, "Error while accepting", ioe);
            synchronized (this) {
                if (!stop) {
                    locked_halt();
                    thread = null;
                    stop = true;
                }
            }
        } catch (ConnectException ioe) {
            // which does not currently call our halt(), although it should
            if (_log.shouldWarn())
                _log.warn("Error while accepting", ioe);
            synchronized (this) {
                if (!stop) {
                    locked_halt();
                    thread = null;
                    stop = true;
                }
            }
        } catch (IOException ioe) {
            int level = stop ? Log.WARN : Log.ERROR;
            if (_log.shouldLog(level))
                _log.log(level, "Error while accepting", ioe);
            synchronized (this) {
                if (!stop) {
                    locked_halt();
                    thread = null;
                    stop = true;
                }
            }
        }
    // catch oom?
    }
    if (_log.shouldLog(Log.WARN))
        _log.warn("ConnectionAcceptor closed");
}
Also used : I2PException(net.i2p.I2PException) I2PSocket(net.i2p.client.streaming.I2PSocket) IOException(java.io.IOException) I2PServerSocket(net.i2p.client.streaming.I2PServerSocket) Hash(net.i2p.data.Hash) I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread) ConnectException(java.net.ConnectException)

Aggregations

Hash (net.i2p.data.Hash)235 RouterInfo (net.i2p.data.router.RouterInfo)45 ArrayList (java.util.ArrayList)29 TunnelId (net.i2p.data.TunnelId)20 Destination (net.i2p.data.Destination)18 HashSet (java.util.HashSet)17 ConvertToHash (net.i2p.util.ConvertToHash)17 IOException (java.io.IOException)16 TunnelInfo (net.i2p.router.TunnelInfo)15 DataFormatException (net.i2p.data.DataFormatException)14 Properties (java.util.Properties)13 Date (java.util.Date)12 DatabaseEntry (net.i2p.data.DatabaseEntry)11 SessionKey (net.i2p.data.SessionKey)11 RouterAddress (net.i2p.data.router.RouterAddress)11 DatabaseStoreMessage (net.i2p.data.i2np.DatabaseStoreMessage)9 I2NPMessage (net.i2p.data.i2np.I2NPMessage)9 Job (net.i2p.router.Job)9 OutNetMessage (net.i2p.router.OutNetMessage)9 TunnelPoolSettings (net.i2p.router.TunnelPoolSettings)8