Search in sources :

Example 36 with RouterAddress

use of net.i2p.data.router.RouterAddress in project i2p.i2p by i2p.

the class ReseedBundler method createZip.

/**
 *  Create a zip file with
 *  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.
 *
 *  The file will be in the temp directory. Caller must move or delete.
 */
public File createZip(int count) throws IOException {
    Hash me = _context.routerHash();
    int routerCount = 0;
    int copied = 0;
    long tooOld = System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000L;
    List<RouterInfo> infos = new ArrayList<RouterInfo>(_context.netDb().getRouters());
    // IP to router hash
    Map<String, Hash> ipMap = new HashMap<String, Hash>(count);
    List<RouterInfo> toWrite = new ArrayList<RouterInfo>(count);
    Collections.shuffle(infos);
    for (RouterInfo ri : infos) {
        if (copied >= count)
            break;
        Hash key = ri.getIdentity().calculateHash();
        if (key.equals(me)) {
            continue;
        }
        if (ri.getPublished() < tooOld)
            continue;
        if (ri.getCapabilities().contains("U"))
            continue;
        if (ri.getCapabilities().contains("K"))
            continue;
        Collection<RouterAddress> addrs = ri.getAddresses();
        if (addrs.isEmpty())
            continue;
        String name = getRouterInfoName(key);
        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;
                Hash old = ipMap.put(host, key);
                if (old != null && !old.equals(key)) {
                    dupIP = true;
                    break;
                }
            }
        }
        if (dupIP)
            continue;
        if (hasIntro)
            continue;
        if (!hasIPv4)
            continue;
        if (_context.commSystem().isInBadCountry(ri))
            continue;
        toWrite.add(ri);
        copied++;
    }
    if (toWrite.isEmpty())
        throw new IOException("No router infos to include. Reseed yourself first.");
    if (toWrite.size() < Math.min(count, MINIMUM))
        throw new IOException("Not enough router infos to include, wanted " + count + " but only found " + toWrite.size() + ". Please try again later.");
    File rv = new File(_context.getTempDir(), "genreseed-" + _context.random().nextInt() + ".zip");
    ZipOutputStream zip = null;
    try {
        zip = new ZipOutputStream(new FileOutputStream(rv));
        for (RouterInfo ri : toWrite) {
            String name = getRouterInfoName(ri.getIdentity().calculateHash());
            ZipEntry entry = new ZipEntry(name);
            entry.setTime(ri.getPublished());
            zip.putNextEntry(entry);
            ri.writeBytes(zip);
            zip.closeEntry();
        }
    } catch (DataFormatException dfe) {
        rv.delete();
        IOException ioe = new IOException(dfe.getMessage());
        ioe.initCause(dfe);
        throw ioe;
    } catch (IOException ioe) {
        rv.delete();
        throw ioe;
    } finally {
        if (zip != null) {
            try {
                zip.finish();
                zip.close();
            } catch (IOException ioe) {
                rv.delete();
                throw ioe;
            }
        }
    }
    return rv;
}
Also used : HashMap(java.util.HashMap) RouterInfo(net.i2p.data.router.RouterInfo) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) RouterAddress(net.i2p.data.router.RouterAddress) IOException(java.io.IOException) Hash(net.i2p.data.Hash) DataFormatException(net.i2p.data.DataFormatException) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 37 with RouterAddress

use of net.i2p.data.router.RouterAddress in project i2p.i2p by i2p.

the class NetDbRenderer method renderRouterInfoHTML.

/**
 *  One String must be non-null
 *
 *  @param routerPrefix may be null. "." for our router only
 *  @param version may be null
 *  @param country may be null
 *  @param family may be null
 */
public void renderRouterInfoHTML(Writer out, String routerPrefix, String version, String country, String family, String caps, String ip, String sybil, int port, SigType type, String mtu, String ipv6, String ssucaps, int cost) throws IOException {
    StringBuilder buf = new StringBuilder(4 * 1024);
    List<Hash> sybils = sybil != null ? new ArrayList<Hash>(128) : null;
    if (".".equals(routerPrefix)) {
        renderRouterInfo(buf, _context.router().getRouterInfo(), true, true);
    } else {
        boolean notFound = true;
        Set<RouterInfo> routers = _context.netDb().getRouters();
        int ipMode = 0;
        if (ip != null) {
            if (ip.endsWith("/24")) {
                ipMode = 1;
            } else if (ip.endsWith("/16")) {
                ipMode = 2;
            } else if (ip.endsWith("/8")) {
                ipMode = 3;
            }
            for (int i = 0; i < ipMode; i++) {
                int last = ip.substring(0, ip.length() - 1).lastIndexOf('.');
                if (last > 0)
                    ip = ip.substring(0, last + 1);
            }
        }
        for (RouterInfo ri : routers) {
            Hash key = ri.getIdentity().getHash();
            if ((routerPrefix != null && key.toBase64().startsWith(routerPrefix)) || (version != null && version.equals(ri.getVersion())) || (country != null && country.equals(_context.commSystem().getCountry(key))) || (family != null && family.equals(ri.getOption("family"))) || (caps != null && ri.getCapabilities().contains(caps)) || (type != null && type == ri.getIdentity().getSigType())) {
                renderRouterInfo(buf, ri, false, true);
                if (sybil != null)
                    sybils.add(key);
                notFound = false;
            } else if (ip != null) {
                for (RouterAddress ra : ri.getAddresses()) {
                    if (ipMode == 0) {
                        if (ip.equals(ra.getHost())) {
                            renderRouterInfo(buf, ri, false, true);
                            if (sybil != null)
                                sybils.add(key);
                            notFound = false;
                            break;
                        }
                    } else {
                        String host = ra.getHost();
                        if (host != null && host.startsWith(ip)) {
                            renderRouterInfo(buf, ri, false, true);
                            if (sybil != null)
                                sybils.add(key);
                            notFound = false;
                            break;
                        }
                    }
                }
            } else if (port != 0) {
                for (RouterAddress ra : ri.getAddresses()) {
                    if (port == ra.getPort()) {
                        renderRouterInfo(buf, ri, false, true);
                        if (sybil != null)
                            sybils.add(key);
                        notFound = false;
                        break;
                    }
                }
            } else if (mtu != null) {
                for (RouterAddress ra : ri.getAddresses()) {
                    if (mtu.equals(ra.getOption("mtu"))) {
                        renderRouterInfo(buf, ri, false, true);
                        if (sybil != null)
                            sybils.add(key);
                        notFound = false;
                        break;
                    }
                }
            } else if (ipv6 != null) {
                for (RouterAddress ra : ri.getAddresses()) {
                    String host = ra.getHost();
                    if (host != null && host.startsWith(ipv6)) {
                        renderRouterInfo(buf, ri, false, true);
                        if (sybil != null)
                            sybils.add(key);
                        notFound = false;
                        break;
                    }
                }
            } else if (ssucaps != null) {
                for (RouterAddress ra : ri.getAddresses()) {
                    if (!"SSU".equals(ra.getTransportStyle()))
                        continue;
                    if (ssucaps.equals(ra.getOption("caps"))) {
                        renderRouterInfo(buf, ri, false, true);
                        if (sybil != null)
                            sybils.add(key);
                        notFound = false;
                        break;
                    }
                }
            } else if (cost != 0) {
                for (RouterAddress ra : ri.getAddresses()) {
                    if (cost == ra.getCost()) {
                        renderRouterInfo(buf, ri, false, true);
                        if (sybil != null)
                            sybils.add(key);
                        notFound = false;
                        break;
                    }
                }
            }
        }
        if (notFound) {
            buf.append("<div class=\"netdbnotfound\">");
            buf.append(_t("Router")).append(' ');
            if (routerPrefix != null)
                buf.append(routerPrefix);
            else if (version != null)
                buf.append(version);
            else if (country != null)
                buf.append(country);
            else if (family != null)
                buf.append(_t("Family")).append(' ').append(family);
            buf.append(' ').append(_t("not found in network database"));
            buf.append("</div>");
        }
    }
    out.write(buf.toString());
    out.flush();
    if (sybil != null)
        SybilRenderer.renderSybilHTML(out, _context, sybils, sybil);
}
Also used : RouterInfo(net.i2p.data.router.RouterInfo) RouterAddress(net.i2p.data.router.RouterAddress) Hash(net.i2p.data.Hash)

Example 38 with RouterAddress

use of net.i2p.data.router.RouterAddress in project i2p.i2p by i2p.

the class PeerHelper method renderStatusHTML.

/**
 *  Warning - blocking, very slow, queries the active UPnP router,
 *  will take many seconds if it has vanished.
 *
 *  @since 0.9.31 moved from TransportManager
 */
private void renderStatusHTML(Writer out, String urlBase, int sortFlags) throws IOException {
    if (isAdvanced()) {
        out.write("<p id=\"upnpstatus\"><b>");
        out.write(_t("Status"));
        out.write(": ");
        out.write(_t(_context.commSystem().getStatus().toStatusString()));
        out.write("</b></p>");
    }
    SortedMap<String, Transport> transports = _context.commSystem().getTransports();
    for (Map.Entry<String, Transport> e : transports.entrySet()) {
        String style = e.getKey();
        Transport t = e.getValue();
        if (style.equals("NTCP")) {
            NTCPTransport nt = (NTCPTransport) t;
            render(nt, out, urlBase, sortFlags);
        } else if (style.equals("SSU")) {
            UDPTransport ut = (UDPTransport) t;
            render(ut, out, urlBase, sortFlags);
        } else {
            // pluggable (none yet)
            t.renderStatusHTML(out, urlBase, sortFlags);
        }
    }
    if (!transports.isEmpty()) {
        out.write(getTransportsLegend());
    }
    StringBuilder buf = new StringBuilder(4 * 1024);
    buf.append("<h3 id=\"transports\">").append(_t("Router Transport Addresses")).append("</h3><pre id=\"transports\">\n");
    for (Transport t : transports.values()) {
        if (t.hasCurrentAddress()) {
            for (RouterAddress ra : t.getCurrentAddresses()) {
                buf.append(ra.toString());
                buf.append("\n\n");
            }
        } else {
            buf.append(_t("{0} is used for outbound connections only", t.getStyle()));
            buf.append("\n\n");
        }
    }
    buf.append("</pre>\n");
    out.write(buf.toString());
    // UPnP Status
    _context.commSystem().renderStatusHTML(_out, _urlBase, _sortFlags);
    out.write("</p>\n");
    out.flush();
}
Also used : RouterAddress(net.i2p.data.router.RouterAddress) UDPTransport(net.i2p.router.transport.udp.UDPTransport) NTCPTransport(net.i2p.router.transport.ntcp.NTCPTransport) Transport(net.i2p.router.transport.Transport) UDPTransport(net.i2p.router.transport.udp.UDPTransport) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) NTCPTransport(net.i2p.router.transport.ntcp.NTCPTransport)

Example 39 with RouterAddress

use of net.i2p.data.router.RouterAddress in project i2p.i2p by i2p.

the class SummaryHelper method reachability.

private NetworkStateMessage reachability() {
    if (_context.commSystem().isDummy())
        return new NetworkStateMessage(NetworkState.VMCOMM, "VM Comm System");
    if (_context.router().getUptime() > 60 * 1000 && (!_context.router().gracefulShutdownInProgress()) && !_context.clientManager().isAlive())
        // not a router problem but the user should know
        return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Client Manager I2CP Error - check logs"));
    // Warn based on actual skew from peers, not update status, so if we successfully offset
    // the clock, we don't complain.
    // if (!_context.clock().getUpdatedSuccessfully())
    long skew = _context.commSystem().getFramedAveragePeerClockSkew(33);
    // Display the actual skew, not the offset
    if (Math.abs(skew) > 30 * 1000)
        return new NetworkStateMessage(NetworkState.CLOCKSKEW, _t("ERR-Clock Skew of {0}", DataHelper.formatDuration2(Math.abs(skew))));
    if (_context.router().isHidden())
        return new NetworkStateMessage(NetworkState.HIDDEN, _t("Hidden"));
    RouterInfo routerInfo = _context.router().getRouterInfo();
    if (routerInfo == null)
        return new NetworkStateMessage(NetworkState.TESTING, _t("Testing"));
    Status status = _context.commSystem().getStatus();
    NetworkState state = NetworkState.RUNNING;
    switch(status) {
        case OK:
        case IPV4_OK_IPV6_UNKNOWN:
        case IPV4_OK_IPV6_FIREWALLED:
        case IPV4_UNKNOWN_IPV6_OK:
        case IPV4_DISABLED_IPV6_OK:
        case IPV4_SNAT_IPV6_OK:
            RouterAddress ra = routerInfo.getTargetAddress("NTCP");
            if (ra == null)
                return new NetworkStateMessage(NetworkState.RUNNING, _t(status.toStatusString()));
            byte[] ip = ra.getIP();
            if (ip == null)
                return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Unresolved TCP Address"));
            // TODO set IPv6 arg based on configuration?
            if (TransportUtil.isPubliclyRoutable(ip, true))
                return new NetworkStateMessage(NetworkState.RUNNING, _t(status.toStatusString()));
            return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Private TCP Address"));
        case IPV4_SNAT_IPV6_UNKNOWN:
        case DIFFERENT:
            return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-SymmetricNAT"));
        case REJECT_UNSOLICITED:
            state = NetworkState.FIREWALLED;
        case IPV4_DISABLED_IPV6_FIREWALLED:
            if (routerInfo.getTargetAddress("NTCP") != null)
                return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled with Inbound TCP Enabled"));
        // fall through...
        case IPV4_FIREWALLED_IPV6_OK:
        case IPV4_FIREWALLED_IPV6_UNKNOWN:
            if (((FloodfillNetworkDatabaseFacade) _context.netDb()).floodfillEnabled())
                return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled and Floodfill"));
            // return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled and Fast"));
            return new NetworkStateMessage(state, _t(status.toStatusString()));
        case DISCONNECTED:
            return new NetworkStateMessage(NetworkState.TESTING, _t("Disconnected - check network connection"));
        case HOSED:
            return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-UDP Port In Use - Set i2np.udp.internalPort=xxxx in advanced config and restart"));
        case UNKNOWN:
            state = NetworkState.TESTING;
        case IPV4_UNKNOWN_IPV6_FIREWALLED:
        case IPV4_DISABLED_IPV6_UNKNOWN:
        default:
            ra = routerInfo.getTargetAddress("SSU");
            if (ra == null && _context.router().getUptime() > 5 * 60 * 1000) {
                if (getActivePeers() <= 0)
                    return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-No Active Peers, Check Network Connection and Firewall"));
                else if (_context.getProperty(ConfigNetHelper.PROP_I2NP_NTCP_HOSTNAME) == null || _context.getProperty(ConfigNetHelper.PROP_I2NP_NTCP_PORT) == null)
                    return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-UDP Disabled and Inbound TCP host/port not set"));
                else
                    return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled with UDP Disabled"));
            }
            return new NetworkStateMessage(state, _t(status.toStatusString()));
    }
}
Also used : Status(net.i2p.router.CommSystemFacade.Status) FloodfillNetworkDatabaseFacade(net.i2p.router.networkdb.kademlia.FloodfillNetworkDatabaseFacade) RouterInfo(net.i2p.data.router.RouterInfo) RouterAddress(net.i2p.data.router.RouterAddress)

Example 40 with RouterAddress

use of net.i2p.data.router.RouterAddress in project i2p.i2p by i2p.

the class RouterGenerator method createTCPAddress.

private static RouterAddress createTCPAddress(int num) {
    OrderedProperties props = new OrderedProperties();
    String name = "blah.random.host.org";
    String port = "" + (1024 + num);
    props.setProperty("host", name);
    props.setProperty("port", port);
    RouterAddress addr = new RouterAddress("TCP", props, 10);
    return addr;
}
Also used : OrderedProperties(net.i2p.util.OrderedProperties) RouterAddress(net.i2p.data.router.RouterAddress)

Aggregations

RouterAddress (net.i2p.data.router.RouterAddress)42 RouterInfo (net.i2p.data.router.RouterInfo)17 Hash (net.i2p.data.Hash)11 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 OrderedProperties (net.i2p.util.OrderedProperties)6 InetAddress (java.net.InetAddress)5 Map (java.util.Map)5 RouterIdentity (net.i2p.data.router.RouterIdentity)5 UnknownHostException (java.net.UnknownHostException)4 HashMap (java.util.HashMap)4 DataFormatException (net.i2p.data.DataFormatException)4 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 Date (java.util.Date)3 Status (net.i2p.router.CommSystemFacade.Status)3 OutNetMessage (net.i2p.router.OutNetMessage)3 File (java.io.File)2 InetSocketAddress (java.net.InetSocketAddress)2 Properties (java.util.Properties)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2