Search in sources :

Example 6 with EepGet

use of net.i2p.util.EepGet in project i2p.i2p by i2p.

the class EepGetNamingService method fetchAddr.

private String fetchAddr(String url, String hostname) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(MAX_RESPONSE);
    try {
        // Do a proxied eepget into our ByteArrayOutputStream with 0 retries
        EepGet get = new EepGet(_context, true, "localhost", 4444, 0, DEST_SIZE, MAX_RESPONSE, null, baos, url + hostname, false, null, null);
        // 10s header timeout, 15s total timeout, unlimited inactivity timeout
        if (get.fetch(10 * 1000l, 15 * 1000l, -1l)) {
            if (baos.size() < DEST_SIZE) {
                _log.error("Short response: " + url + hostname);
                return null;
            }
            String key = baos.toString();
            if (// strip hostname=
            key.startsWith(hostname + "="))
                key = key.substring(hostname.length() + 1);
            // catch IndexOutOfBounds exception below
            key = key.substring(0, DEST_SIZE);
            if (!key.endsWith("AA")) {
                _log.error("Invalid key: " + url + hostname);
                return null;
            }
            if (key.replaceAll("[a-zA-Z0-9~-]", "").length() != 0) {
                _log.error("Invalid chars: " + url + hostname);
                return null;
            }
            return key;
        }
        _log.error("Fetch failed from: " + url + hostname);
        return null;
    } catch (Throwable t) {
        _log.error("Error fetching the addr", t);
    }
    _log.error("Caught from: " + url + hostname);
    return null;
}
Also used : EepGet(net.i2p.util.EepGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with EepGet

use of net.i2p.util.EepGet in project i2p.i2p by i2p.

the class I2PSnarkUtil method get.

/**
 * @param retries if &lt; 0, set timeout to a few seconds
 */
public File get(String url, boolean rewrite, int retries) {
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Fetching [" + url + "] proxy=" + _proxyHost + ":" + _proxyPort + ": " + _shouldProxy);
    File out = null;
    try {
        // we could use the system tmp dir but deleteOnExit() doesn't seem to work on all platforms...
        out = SecureFile.createTempFile("i2psnark", null, _tmpDir);
    } catch (IOException ioe) {
        _log.error("temp file error", ioe);
        if (out != null)
            out.delete();
        return null;
    }
    out.deleteOnExit();
    String fetchURL = url;
    if (rewrite)
        fetchURL = rewriteAnnounce(url);
    // _log.debug("Rewritten url [" + fetchURL + "]");
    // EepGet get = new EepGet(_context, _shouldProxy, _proxyHost, _proxyPort, retries, out.getAbsolutePath(), fetchURL);
    // Use our tunnel for announces and .torrent fetches too! Make sure we're connected first...
    int timeout;
    if (retries < 0) {
        if (!connected())
            return null;
        timeout = EEPGET_CONNECT_TIMEOUT_SHORT;
        retries = 0;
    } else {
        timeout = EEPGET_CONNECT_TIMEOUT;
        if (!connected()) {
            if (!connect())
                return null;
        }
    }
    EepGet get = new I2PSocketEepGet(_context, _manager, retries, out.getAbsolutePath(), fetchURL);
    get.addHeader("User-Agent", EEPGET_USER_AGENT);
    if (get.fetch(timeout)) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Fetch successful [" + url + "]: size=" + out.length());
        return out;
    } else {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Fetch failed [" + url + "]");
        out.delete();
        return null;
    }
}
Also used : I2PSocketEepGet(net.i2p.client.streaming.I2PSocketEepGet) EepGet(net.i2p.util.EepGet) I2PSocketEepGet(net.i2p.client.streaming.I2PSocketEepGet) IOException(java.io.IOException) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Aggregations

EepGet (net.i2p.util.EepGet)7 File (java.io.File)3 URI (java.net.URI)2 SSLEepGet (net.i2p.util.SSLEepGet)2 SecureFile (net.i2p.util.SecureFile)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 I2PSocketEepGet (net.i2p.client.streaming.I2PSocketEepGet)1 SU3File (net.i2p.crypto.SU3File)1 PartialEepGet (net.i2p.util.PartialEepGet)1