Search in sources :

Example 71 with DataFormatException

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

the class RouterInfo method writeBytes.

/**
 *  This does NOT validate the signature
 */
public void writeBytes(OutputStream out) throws DataFormatException, IOException {
    if (_signature == null)
        throw new DataFormatException("Signature is null");
    writeDataBytes(out);
    _signature.writeBytes(out);
}
Also used : DataFormatException(net.i2p.data.DataFormatException)

Example 72 with DataFormatException

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

the class RouterInfo method writeDataBytes.

/**
 * Write out the raw payload of the routerInfo, excluding the signature.  This
 * caches the data in memory if possible.
 *
 * @throws DataFormatException if the data is somehow b0rked (missing props, etc)
 * @throws IOException
 * @since 0.9.24
 */
private void writeDataBytes(OutputStream out) throws DataFormatException, IOException {
    if (_identity == null)
        throw new DataFormatException("Missing identity");
    if (_published < 0)
        throw new DataFormatException("Invalid published date: " + _published);
    _identity.writeBytes(out);
    // avoid thrashing objects
    // DataHelper.writeDate(out, new Date(_published));
    DataHelper.writeLong(out, 8, _published);
    int sz = _addresses.size();
    if (sz <= 0 || isHidden()) {
        // Do not send IP address to peers in hidden mode
        out.write((byte) 0);
    } else {
        out.write((byte) sz);
        for (RouterAddress addr : _addresses) {
            addr.writeBytes(out);
        }
    }
    // XXX: what about peers?
    // answer: they're always empty... they're a placeholder for one particular
    // method of trusted links, which isn't implemented in the router
    // at the moment, and may not be later.
    int psz = _peers == null ? 0 : _peers.size();
    out.write((byte) psz);
    if (psz > 0) {
        Collection<Hash> peers = _peers;
        if (psz > 1)
            // WARNING this sort algorithm cannot be changed, as it must be consistent
            // network-wide. The signature is not checked at readin time, but only
            // later, and the hashes are stored in a Set, not a List.
            peers = SortHelper.sortStructures(peers);
        for (Hash peerHash : peers) {
            peerHash.writeBytes(out);
        }
    }
    DataHelper.writeProperties(out, _options);
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Hash(net.i2p.data.Hash) SHA1Hash(net.i2p.crypto.SHA1Hash)

Example 73 with DataFormatException

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

the class RouterInfo method main.

/**
 *  Print out routerinfos from files specified on the command line.
 *  Exits 1 if any RI is invalid, fails signature, etc.
 *  @since 0.8
 */
public static void main(String[] args) {
    if (args.length <= 0) {
        System.err.println("Usage: RouterInfo file ...");
        System.exit(1);
    }
    boolean fail = false;
    for (int i = 0; i < args.length; i++) {
        RouterInfo ri = new RouterInfo();
        InputStream is = null;
        try {
            is = new java.io.FileInputStream(args[i]);
            ri.readBytes(is);
            if (ri.isValid()) {
                System.out.println(ri.toString());
            } else {
                System.err.println("Router info " + args[i] + " is invalid");
                fail = true;
            }
        } catch (IOException e) {
            System.err.println("Error reading " + args[i] + ": " + e);
            fail = true;
        } catch (DataFormatException e) {
            System.err.println("Error reading " + args[i] + ": " + e);
            fail = true;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) {
                }
            }
        }
    }
    if (fail)
        System.exit(1);
}
Also used : DataFormatException(net.i2p.data.DataFormatException) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 74 with DataFormatException

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

the class RouterInfo method readBytes.

/**
 *  If verifySig is true,
 *  this validates the signature while reading in,
 *  and throws a DataFormatException if the sig is invalid.
 *  This is faster than reserializing to validate later.
 *
 *  @throws IllegalStateException if RouterInfo was already read in
 *  @since 0.9
 */
public void readBytes(InputStream in, boolean verifySig) throws DataFormatException, IOException {
    if (_signature != null)
        throw new IllegalStateException();
    _identity = new RouterIdentity();
    _identity.readBytes(in);
    // can't set the digest until we know the sig type
    InputStream din;
    MessageDigest digest;
    if (verifySig) {
        SigType type = _identity.getSigningPublicKey().getType();
        if (type != SigType.EdDSA_SHA512_Ed25519) {
            // This won't work for EdDSA
            digest = _identity.getSigningPublicKey().getType().getDigestInstance();
            // TODO any better way?
            digest.update(_identity.toByteArray());
            din = new DigestInputStream(in, digest);
        } else {
            digest = null;
            din = in;
        }
    } else {
        digest = null;
        din = in;
    }
    // avoid thrashing objects
    // Date when = DataHelper.readDate(in);
    // if (when == null)
    // _published = 0;
    // else
    // _published = when.getTime();
    _published = DataHelper.readLong(din, 8);
    int numAddresses = (int) DataHelper.readLong(din, 1);
    for (int i = 0; i < numAddresses; i++) {
        RouterAddress address = new RouterAddress();
        address.readBytes(din);
        _addresses.add(address);
    }
    int numPeers = (int) DataHelper.readLong(din, 1);
    if (numPeers == 0) {
        _peers = null;
    } else {
        _peers = new HashSet<Hash>(numPeers);
        for (int i = 0; i < numPeers; i++) {
            Hash peerIdentityHash = new Hash();
            peerIdentityHash.readBytes(din);
            _peers.add(peerIdentityHash);
        }
    }
    DataHelper.readProperties(din, _options);
    _signature = new Signature(_identity.getSigningPublicKey().getType());
    _signature.readBytes(in);
    if (verifySig) {
        SigType type = _identity.getSigningPublicKey().getType();
        if (type != SigType.EdDSA_SHA512_Ed25519) {
            // This won't work for EdDSA
            SimpleDataStructure hash = _identity.getSigningPublicKey().getType().getHashInstance();
            hash.setData(digest.digest());
            _isValid = DSAEngine.getInstance().verifySignature(_signature, hash, _identity.getSigningPublicKey());
            _validated = true;
        } else {
            doValidate();
        }
        if (!_isValid) {
            throw new DataFormatException("Bad sig");
        }
    }
// _log.debug("Read routerInfo: " + toString());
}
Also used : DigestInputStream(java.security.DigestInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) Hash(net.i2p.data.Hash) SHA1Hash(net.i2p.crypto.SHA1Hash) SigType(net.i2p.crypto.SigType) DataFormatException(net.i2p.data.DataFormatException) Signature(net.i2p.data.Signature) MessageDigest(java.security.MessageDigest) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 75 with DataFormatException

use of net.i2p.data.DataFormatException 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)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8