Search in sources :

Example 36 with I2PAppContext

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

the class ConnectIT method test.

@Test
public void test() throws Exception {
    I2PAppContext context = I2PAppContext.getGlobalContext();
    _log = context.logManager().getLog(ConnectIT.class);
    _log.debug("creating server session");
    _server = createSession();
    _log.debug("running server");
    Thread server = runServer(context, _server);
    Thread[] clients = new Thread[5];
    for (int i = 0; i < 5; i++) {
        _log.debug("running client");
        clients[i] = runClient(context, createSession());
    }
    for (Thread c : clients) c.join();
}
Also used : I2PAppContext(net.i2p.I2PAppContext) Test(org.junit.Test)

Example 37 with I2PAppContext

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

the class ConnectInactivityIT method test.

@Test
public void test() throws Exception {
    I2PAppContext context = I2PAppContext.getGlobalContext();
    _log = context.logManager().getLog(ConnectIT.class);
    _log.debug("creating server session");
    _server = createSession();
    _log.debug("running server");
    runServer(context, _server);
    _log.debug("creating client session");
    _client = createSession();
    _log.debug("running client");
    Thread client = runClient(context, _client);
    client.join(LONG_TIME + 1000);
}
Also used : I2PAppContext(net.i2p.I2PAppContext) Test(org.junit.Test)

Example 38 with I2PAppContext

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

the class ConnectTimeoutIT method testNonexistant.

@Test
public void testNonexistant() throws Exception {
    I2PAppContext context = I2PAppContext.getGlobalContext();
    _log = context.logManager().getLog(ConnectIT.class);
    _log.debug("creating server dest");
    _serverDest = I2PClientFactory.createClient().createDestination(new ByteArrayOutputStream());
    _log.debug("creating client session");
    _client = createSession();
    _log.debug("running client");
    runClient(context, _client).join();
}
Also used : I2PAppContext(net.i2p.I2PAppContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 39 with I2PAppContext

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

the class BundleRouterInfos method main.

/**
 *  Usage: PersistentDataStore -i configDir -o toDir -c count
 *
 *  Copy a random selection of 'count' router infos from configDir/netDb
 *  to 'toDir'. Skip your own router info, and old, hidden, unreachable, and
 *  introduced routers, and those from bad countries.
 *
 *  @since 0.9.15
 */
public static void main(String[] args) {
    Getopt g = new Getopt("PersistentDataStore", args, "i:o:c:");
    String in = System.getProperty("user.home") + "/.i2p";
    String out = "netDb";
    int count = 200;
    boolean error = false;
    int c;
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 'i':
                in = g.getOptarg();
                break;
            case 'o':
                out = g.getOptarg();
                break;
            case 'c':
                String scount = g.getOptarg();
                try {
                    count = Integer.parseInt(scount);
                } catch (NumberFormatException nfe) {
                    error = true;
                }
                break;
            case '?':
            case ':':
            default:
                error = true;
        }
    }
    if (error) {
        usage();
        System.exit(1);
    }
    Properties props = new Properties();
    props.setProperty(GeoIP.PROP_GEOIP_DIR, System.getProperty("user.dir") + "/installer/resources");
    GeoIP geoIP = new GeoIP(new I2PAppContext(props));
    File confDir = new File(in);
    File dbDir = new File(confDir, "netDb");
    if (!dbDir.exists()) {
        System.out.println("NetDB directory " + dbDir + " does not exist");
        System.exit(1);
    }
    File myFile = new File(confDir, "router.info");
    File toDir = new File(out);
    toDir.mkdirs();
    InputStream fis = null;
    Hash me = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(myFile));
        RouterInfo ri = new RouterInfo();
        // true = verify sig on read
        ri.readBytes(fis, true);
        me = ri.getIdentity().getHash();
    } catch (IOException e) {
    // System.out.println("Can't determine our identity");
    } catch (DataFormatException e) {
    // System.out.println("Can't determine our identity");
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException ioe) {
            }
    }
    int routerCount = 0;
    List<File> toRead = new ArrayList<File>(2048);
    for (int j = 0; j < Base64.ALPHABET_I2P.length(); j++) {
        File subdir = new File(dbDir, PersistentDataStore.DIR_PREFIX + Base64.ALPHABET_I2P.charAt(j));
        File[] files = subdir.listFiles(PersistentDataStore.RI_FILTER);
        if (files == null)
            continue;
        routerCount += files.length;
        for (int i = 0; i < files.length; i++) {
            toRead.add(files[i]);
        }
    }
    if (toRead.isEmpty()) {
        System.out.println("No files to copy in " + dbDir);
        System.exit(1);
    }
    Collections.shuffle(toRead);
    int copied = 0;
    long tooOld = System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000L;
    Map<String, String> ipMap = new HashMap<String, String>(count);
    for (File file : toRead) {
        if (copied >= count)
            break;
        Hash key = PersistentDataStore.getRouterInfoHash(file.getName());
        if (key == null) {
            System.out.println("Skipping bad " + file);
            continue;
        }
        if (key.equals(me)) {
            System.out.println("Skipping my RI");
            continue;
        }
        fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file));
            RouterInfo ri = new RouterInfo();
            // true = verify sig on read
            ri.readBytes(fis, true);
            try {
                fis.close();
            } catch (IOException ioe) {
            }
            fis = null;
            if (ri.getPublished() < tooOld) {
                System.out.println("Skipping too old " + key);
                continue;
            }
            if (ri.getCapabilities().contains("U")) {
                System.out.println("Skipping unreachable " + key);
                continue;
            }
            if (ri.getCapabilities().contains("K")) {
                System.out.println("Skipping slow " + key);
                continue;
            }
            Collection<RouterAddress> addrs = ri.getAddresses();
            if (addrs.isEmpty()) {
                System.out.println("Skipping hidden " + key);
                continue;
            }
            boolean hasIntro = false;
            boolean hasIPv4 = false;
            boolean dupIP = false;
            for (RouterAddress addr : addrs) {
                if ("SSU".equals(addr.getTransportStyle()) && addr.getOption("ihost0") != null) {
                    hasIntro = true;
                    break;
                }
                String host = addr.getHost();
                if (host != null && host.contains(".")) {
                    hasIPv4 = true;
                    geoIP.add(host);
                    String old = ipMap.put(host, file.getName());
                    if (old != null && !old.equals(file.getName())) {
                        dupIP = true;
                        break;
                    }
                }
            }
            if (dupIP) {
                System.out.println("Skipping dup IP " + key);
                continue;
            }
            if (hasIntro) {
                System.out.println("Skipping introduced " + key);
                continue;
            }
            if (!hasIPv4) {
                System.out.println("Skipping IPv6-only " + key);
                continue;
            }
            File toFile = new File(toDir, file.getName());
            // We could call ri.write() to avoid simultaneous change by the router
            boolean ok = FileUtil.copy(file, toFile, true, true);
            if (ok)
                copied++;
            else
                System.out.println("Failed copy of " + file + " to " + toDir);
        } catch (IOException e) {
            System.out.println("Skipping bad " + file);
        } catch (DataFormatException e) {
            System.out.println("Skipping bad " + file);
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException ioe) {
                }
        }
    }
    if (copied > 0) {
        // now do all the geoip lookups, and delete any bad countries
        geoIP.blockingLookup();
        for (Map.Entry<String, String> e : ipMap.entrySet()) {
            String co = geoIP.get(e.getKey());
            if (co != null) {
                if (BadCountries.contains(co)) {
                    String name = e.getValue();
                    File toFile = new File(toDir, name);
                    if (toFile.delete()) {
                        String full = geoIP.fullName(co);
                        if (full == null)
                            full = co;
                        System.out.println("Skipping " + full + ": " + name);
                        copied--;
                    }
                }
            }
        }
    }
    if (copied > 0) {
        System.out.println("Copied " + copied + " router info files to " + toDir);
    } else {
        System.out.println("Failed to copy any files to " + toDir);
        System.exit(1);
    }
}
Also used : GeoIP(net.i2p.router.transport.GeoIP) I2PAppContext(net.i2p.I2PAppContext) HashMap(java.util.HashMap) RouterInfo(net.i2p.data.router.RouterInfo) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Hash(net.i2p.data.Hash) Getopt(gnu.getopt.Getopt) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RouterAddress(net.i2p.data.router.RouterAddress) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DataFormatException(net.i2p.data.DataFormatException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 40 with I2PAppContext

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

the class MultiRouter method main.

public static void main(String[] args) {
    if ((args == null) || (args.length < 1)) {
        usage();
        return;
    }
    Scanner scan = null;
    try {
        scan = new Scanner(args[0]);
        if (!scan.hasNextInt()) {
            usage();
            return;
        }
        nbrRouters = scan.nextInt();
        if (nbrRouters < 0) {
            usage();
            return;
        }
    } finally {
        if (scan != null)
            scan.close();
    }
    _out = System.out;
    buildClientProps(0);
    _defaultContext = new I2PAppContext(buildRouterProps(0));
    _defaultContext.clock().setOffset(0);
    _out.println("RouterConsole for Router 0 is listening on: 127.0.0.1:" + (BASE_PORT - 1));
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            _out.println("Shutting down in a few moments..");
            for (Router r : _routers) {
                r.shutdown(0);
            }
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ie) {
            }
            Runtime.getRuntime().halt(0);
        }
    });
    for (int i = 0; i < nbrRouters; i++) {
        Router router = new Router(buildRouterProps(i));
        router.setKillVMOnEnd(false);
        _routers.add(router);
        _out.println("Router " + i + " was created");
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
        }
    }
    for (int i = 0; i < nbrRouters; i++) {
        final Router r = _routers.get(i);
        long offset = r.getContext().random().nextLong(Router.CLOCK_FUDGE_FACTOR / 2);
        if (r.getContext().random().nextBoolean())
            offset = 0 - offset;
        r.getContext().clock().setOffset(offset, true);
        /* Start the routers in separate threads since it takes some time. */
        (new Thread() {

            public void run() {
                r.runRouter();
            }
        }).start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
        }
        _out.println("Router " + i + " was started with time offset " + offset);
    }
    _out.println("All routers have been started");
    /* Wait for routers to start services and generate keys
         * before doing the internal reseed. */
    int waitForRouters = (nbrRouters / 10) * 1000;
    _out.println("Waiting " + waitForRouters / 1000 + " seconds for routers to start" + "before doing the internal reseed");
    try {
        Thread.sleep(waitForRouters);
    } catch (InterruptedException ie) {
    }
    internalReseed();
    waitForCompletion();
}
Also used : Scanner(java.util.Scanner) I2PAppContext(net.i2p.I2PAppContext) Router(net.i2p.router.Router)

Aggregations

I2PAppContext (net.i2p.I2PAppContext)55 SessionKey (net.i2p.data.SessionKey)13 File (java.io.File)11 IOException (java.io.IOException)11 Properties (java.util.Properties)9 Test (org.junit.Test)7 Getopt (gnu.getopt.Getopt)5 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 DataFormatException (net.i2p.data.DataFormatException)3 Log (net.i2p.util.Log)3 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)3 Encoding (i2p.susi.webmail.encoding.Encoding)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 HashSet (java.util.HashSet)2 I2PSession (net.i2p.client.I2PSession)2 NamingService (net.i2p.client.naming.NamingService)2