Search in sources :

Example 6 with HostTxtEntry

use of net.i2p.client.naming.HostTxtEntry in project i2p.i2p by i2p.

the class HostTxtParser method parse.

/**
 * Return a Map using the contents of the File file. See parse(BufferedReader)
 * for details of the input format.
 *
 * Returned map will not contain null ("remove") entries.
 *
 * @param file
 *            A File to parse.
 * @return A Map containing the key, value pairs from file.
 * @throws IOException
 *             if file cannot be read.
 */
public static Map<String, HostTxtEntry> parse(File file) throws IOException {
    FileInputStream fileStream = null;
    try {
        fileStream = new FileInputStream(file);
        BufferedReader input = new BufferedReader(new InputStreamReader(fileStream, "UTF-8"));
        Map<String, HostTxtEntry> rv = parse(input);
        return rv;
    } finally {
        if (fileStream != null) {
            try {
                fileStream.close();
            } catch (IOException ioe) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) HostTxtEntry(net.i2p.client.naming.HostTxtEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 7 with HostTxtEntry

use of net.i2p.client.naming.HostTxtEntry in project i2p.i2p by i2p.

the class HostTxtParser method main.

/**
 * Usage: HostTxtParser [-q] validate example.i2p=b64dest[#!key1=val1#key2=val2]
 */
public static void main(String[] args) throws Exception {
    boolean quiet = false;
    if (args.length > 0 && args[0].equals("-q")) {
        quiet = true;
        args = java.util.Arrays.copyOfRange(args, 1, args.length);
    }
    if (args.length != 2 || !args[0].equals("validate")) {
        System.err.println("Usage: HostTxtParser validate example.i2p=b64dest[#!key1=val1#key2=val2]");
        System.exit(1);
    }
    HostTxtEntry e = parse(args[1].trim(), false);
    if (e == null) {
        if (!quiet)
            System.err.println("Bad format");
        System.exit(2);
    }
    if (!e.hasValidSig()) {
        if (!quiet)
            System.err.println("Bad signature");
        System.exit(3);
    }
    Properties p = e.getProps();
    if (p != null) {
        if (p.containsKey(HostTxtEntry.PROP_ACTION) || p.containsKey(HostTxtEntry.PROP_OLDDEST) || p.containsKey(HostTxtEntry.PROP_OLDNAME) || p.containsKey(HostTxtEntry.PROP_OLDSIG)) {
            if (!e.hasValidSig()) {
                if (!quiet)
                    System.err.println("Bad inner signature");
                System.exit(4);
            }
        }
    }
    if (!quiet)
        System.err.println("Good signature for " + e.getName());
    System.exit(0);
}
Also used : HostTxtEntry(net.i2p.client.naming.HostTxtEntry) Properties(java.util.Properties)

Example 8 with HostTxtEntry

use of net.i2p.client.naming.HostTxtEntry in project i2p.i2p by i2p.

the class AddressBook method merge2.

private void merge2(AddressBook other, Iterator<Map.Entry<String, HostTxtEntry>> iter, boolean overwrite, Log log) {
    while (iter.hasNext()) {
        Map.Entry<String, HostTxtEntry> entry = iter.next();
        String otherKey = entry.getKey();
        HostTxtEntry otherValue = entry.getValue();
        if (isValidKey(otherKey) && isValidDest(otherValue.getDest())) {
            if (this.addresses.containsKey(otherKey) && !overwrite) {
                if (DEBUG && log != null && !this.addresses.get(otherKey).equals(otherValue.getDest())) {
                    log.append("Conflict for " + otherKey + " from " + other.location + ". Destination in remote address book is " + otherValue);
                }
            } else if (!this.addresses.containsKey(otherKey) || !this.addresses.get(otherKey).equals(otherValue)) {
                this.addresses.put(otherKey, otherValue);
                this.modified = true;
                if (log != null) {
                    log.append("New address " + otherKey + " added to address book. From: " + other.location);
                }
            }
        }
    }
}
Also used : HostTxtEntry(net.i2p.client.naming.HostTxtEntry) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with HostTxtEntry

use of net.i2p.client.naming.HostTxtEntry in project i2p.i2p by i2p.

the class PrivateKeyFile method main.

/**
 *  Create a new PrivateKeyFile, or modify an existing one, with various
 *  types of Certificates.
 *
 *  Changing a Certificate does not change the public or private keys.
 *  But it does change the Destination Hash, which effectively makes it
 *  a new Destination. In other words, don't change the Certificate on
 *  a Destination you've already registered in a hosts.txt key add form.
 *
 *  Copied and expanded from that in Destination.java
 */
public static void main(String[] args) {
    int hashEffort = HASH_EFFORT;
    String stype = null;
    String hostname = null;
    int mode = 0;
    boolean error = false;
    Getopt g = new Getopt("pkf", args, "t:nuxhse:c:a:");
    int c;
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 'c':
                stype = g.getOptarg();
                break;
            case 't':
                stype = g.getOptarg();
            case 'n':
            case 'u':
            case 'x':
            case 'h':
            case 's':
                if (mode == 0)
                    mode = c;
                else
                    error = true;
                break;
            case 'a':
                hostname = g.getOptarg();
                if (mode == 0)
                    mode = c;
                else
                    error = true;
                break;
            case 'e':
                hashEffort = Integer.parseInt(g.getOptarg());
                break;
            case '?':
            case ':':
            default:
                error = true;
                break;
        }
    // switch
    }
    // while
    int remaining = args.length - g.getOptind();
    int reqd = mode == 's' ? 2 : 1;
    if (error || remaining != reqd) {
        usage();
        System.exit(1);
    }
    String filearg = args[g.getOptind()];
    I2PClient client = I2PClientFactory.createClient();
    try {
        File f = new File(filearg);
        boolean exists = f.exists();
        PrivateKeyFile pkf = new PrivateKeyFile(f, client);
        Destination d;
        if (stype != null) {
            SigType type = SigType.parseSigType(stype);
            if (type == null)
                throw new IllegalArgumentException("Signature type " + stype + " is not supported");
            d = pkf.createIfAbsent(type);
        } else {
            d = pkf.createIfAbsent();
        }
        if (exists)
            System.out.println("Original Destination:");
        else
            System.out.println("Created Destination:");
        System.out.println(pkf);
        verifySignature(d);
        switch(mode) {
            case 0:
                // we are done
                break;
            case 'n':
                // Cert constructor generates a null cert
                pkf.setCertType(Certificate.CERTIFICATE_TYPE_NULL);
                System.out.println("New destination with null cert is:");
                break;
            case 'u':
                pkf.setCertType(99);
                System.out.println("New destination with unknown cert is:");
                break;
            case 'x':
                pkf.setCertType(Certificate.CERTIFICATE_TYPE_HIDDEN);
                System.out.println("New destination with hidden cert is:");
                break;
            case 'h':
                System.out.println("Estimating hashcash generation time, stand by...");
                System.out.println(estimateHashCashTime(hashEffort));
                pkf.setHashCashCert(hashEffort);
                System.out.println("New destination with hashcash cert is:");
                break;
            case 's':
                // Sign dest1 with dest2's Signing Private Key
                PrivateKeyFile pkf2 = new PrivateKeyFile(args[g.getOptind() + 1]);
                pkf.setSignedCert(pkf2);
                System.out.println("New destination with signed cert is:");
                break;
            case 't':
                // KeyCert
                SigType type = SigType.parseSigType(stype);
                if (type == null)
                    throw new IllegalArgumentException("Signature type " + stype + " is not supported");
                pkf.setKeyCert(type);
                System.out.println("New destination with key cert is:");
                break;
            case 'a':
                // addressbook auth
                OrderedProperties props = new OrderedProperties();
                HostTxtEntry he = new HostTxtEntry(hostname, d.toBase64(), props);
                he.sign(pkf.getSigningPrivKey());
                System.out.println("Addressbook Authentication String:");
                OutputStreamWriter out = new OutputStreamWriter(System.out);
                he.write(out);
                out.flush();
                System.out.println("");
                return;
            default:
                // shouldn't happen
                usage();
                return;
        }
        if (mode != 0) {
            System.out.println(pkf);
            pkf.write();
            verifySignature(pkf.getDestination());
        }
    } catch (I2PException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : I2PException(net.i2p.I2PException) HostTxtEntry(net.i2p.client.naming.HostTxtEntry) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType) Getopt(gnu.getopt.Getopt) I2PClient(net.i2p.client.I2PClient) OrderedProperties(net.i2p.util.OrderedProperties) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 10 with HostTxtEntry

use of net.i2p.client.naming.HostTxtEntry in project i2p.i2p by i2p.

the class HostTxtParser method parse.

/**
 * Return a HostTxtEntry from the contents of the inputLine.
 *
 * @param inputLine key=value[#!k1=v1#k2=v2...]
 * @param allowCommandOnly if true, a line starting with #! will return
 *                         a HostTxtEntry with a null name and dest and non-null props.
 *                         If false, these lines will return null.
 * @return null if no entry found or on error
 */
public static HostTxtEntry parse(String inputLine, boolean allowCommandOnly) {
    if (inputLine.startsWith(";"))
        return null;
    int comment = inputLine.indexOf('#');
    String kv;
    String sprops;
    if (comment >= 0) {
        int shebang = inputLine.indexOf(HostTxtEntry.PROPS_SEPARATOR);
        if (shebang == comment && shebang + 2 < inputLine.length()) {
            if (comment == 0 && !allowCommandOnly)
                return null;
            sprops = inputLine.substring(shebang + 2);
        } else {
            if (comment == 0)
                return null;
            sprops = null;
        }
        kv = inputLine.substring(0, comment);
    } else {
        sprops = null;
        kv = inputLine;
    }
    String name, dest;
    if (comment != 0) {
        // we have a name=dest
        String[] splitLine = DataHelper.split(kv, "=", 2);
        if (splitLine.length < 2)
            return null;
        name = splitLine[0].trim().toLowerCase(Locale.US);
        dest = splitLine[1].trim();
        if (name.length() == 0 || dest.length() == 0)
            return null;
    } else {
        // line starts with #!, rv will contain props only
        name = null;
        dest = null;
    }
    HostTxtEntry he;
    if (sprops != null) {
        try {
            he = new HostTxtEntry(name, dest, sprops);
        } catch (IllegalArgumentException iae) {
            return null;
        }
    } else {
        he = new HostTxtEntry(name, dest);
    }
    return he;
}
Also used : HostTxtEntry(net.i2p.client.naming.HostTxtEntry)

Aggregations

HostTxtEntry (net.i2p.client.naming.HostTxtEntry)10 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Properties (java.util.Properties)3 OrderedProperties (net.i2p.util.OrderedProperties)3 Getopt (gnu.getopt.Getopt)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 I2PException (net.i2p.I2PException)1 I2PClient (net.i2p.client.I2PClient)1 NamingService (net.i2p.client.naming.NamingService)1 SingleFileNamingService (net.i2p.client.naming.SingleFileNamingService)1 SigType (net.i2p.crypto.SigType)1