Search in sources :

Example 21 with OrderedProperties

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

the class DataHelper method writeProperties.

/**
 * Writes the props to the stream, sorted by property name if sort == true or
 * if props is an OrderedProperties.
 * See readProperties() for the format.
 * Property keys and values must not contain '=' or ';', this is not checked and they are not escaped
 * Keys and values must be 255 bytes or less,
 * Formatted length must not exceed 65535 bytes
 *
 * Properties from the defaults table of props (if any) are not written out by this method.
 *
 * jrandom disabled UTF-8 in mid-2004, for performance reasons,
 * i.e. slow foo.getBytes("UTF-8")
 * Re-enable it so we can pass UTF-8 tunnel names through the I2CP SessionConfig.
 *
 * Use utf8 = false for RouterAddress (fast, non UTF-8)
 * Use utf8 = true for SessionConfig (slow, UTF-8)
 * @param props source may be null
 * @param sort should we sort the properties? (set to false if already sorted, e.g. OrderedProperties)
 * @throws DataFormatException if any string is over 255 bytes long, or if the total length
 *                             (not including the two length bytes) is greater than 65535 bytes.
 * @since 0.8.7
 */
public static void writeProperties(OutputStream rawStream, Properties props, boolean utf8, boolean sort) throws DataFormatException, IOException {
    if (props != null && !props.isEmpty()) {
        Properties p;
        if (sort) {
            p = new OrderedProperties();
            p.putAll(props);
        } else {
            p = props;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream(p.size() * 64);
        for (Map.Entry<Object, Object> entry : p.entrySet()) {
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            if (utf8)
                writeStringUTF8(baos, key);
            else
                writeString(baos, key);
            baos.write('=');
            if (utf8)
                writeStringUTF8(baos, val);
            else
                writeString(baos, val);
            baos.write(';');
        }
        if (baos.size() > 65535)
            throw new DataFormatException("Properties too big (65535 max): " + baos.size());
        byte[] propBytes = baos.toByteArray();
        writeLong(rawStream, 2, propBytes.length);
        rawStream.write(propBytes);
    } else {
        writeLong(rawStream, 2, 0);
    }
}
Also used : OrderedProperties(net.i2p.util.OrderedProperties) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 22 with OrderedProperties

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

the class DataHelper method toProperties.

/*
     * Writes the props to the byte array, sorted
     * See readProperties() for the format.
     * Property keys and values must not contain '=' or ';', this is not checked and they are not escaped
     * Keys and values must be 255 bytes or less,
     * Formatted length must not exceed 65535 bytes
     * Strings will be UTF-8 encoded in the byte array.
     * Warning - confusing method name, Properties is the source.
     *
     * Properties from the defaults table of props (if any) are not written out by this method.
     *
     * @deprecated unused
     *
     * @param target returned array as specified in data structure spec
     * @param props source may be null
     * @return new offset
     * @throws DataFormatException if any string is over 255 bytes long, or if the total length
     *                             (not including the two length bytes) is greater than 65535 bytes.
     */
@Deprecated
public static int toProperties(byte[] target, int offset, Properties props) throws DataFormatException, IOException {
    if (props != null) {
        OrderedProperties p = new OrderedProperties();
        p.putAll(props);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(p.size() * 64);
        for (Map.Entry<Object, Object> entry : p.entrySet()) {
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            writeStringUTF8(baos, key);
            baos.write('=');
            writeStringUTF8(baos, val);
            baos.write(';');
        }
        if (baos.size() > 65535)
            throw new DataFormatException("Properties too big (65535 max): " + baos.size());
        byte[] propBytes = baos.toByteArray();
        toLong(target, offset, 2, propBytes.length);
        offset += 2;
        System.arraycopy(propBytes, 0, target, offset, propBytes.length);
        offset += propBytes.length;
        return offset;
    } else {
        toLong(target, offset, 2, 0);
        return offset + 2;
    }
}
Also used : OrderedProperties(net.i2p.util.OrderedProperties) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 23 with OrderedProperties

use of net.i2p.util.OrderedProperties 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 24 with OrderedProperties

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

the class ConfigBean method save.

private void save() {
    try {
        // use loadProps to trim, use storeProps to sort and get line endings right
        Properties props = new OrderedProperties();
        DataHelper.loadProps(props, new ByteArrayInputStream(config.getBytes("UTF-8")));
        synchronized (BaseBean.class) {
            DataHelper.storeProps(props, configFile());
        }
        saved = true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OrderedProperties(net.i2p.util.OrderedProperties) IOException(java.io.IOException) Properties(java.util.Properties) OrderedProperties(net.i2p.util.OrderedProperties)

Example 25 with OrderedProperties

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

the class SnarkManager method saveMagnetStatus.

/**
 *  Just remember we have it.
 *  This used to simply store a line in the config file,
 *  but now we also save it in its own config file,
 *  just like other torrents, so we can remember the directory, tracker, etc.
 *
 *  @param dir may be null
 *  @param trackerURL may be null
 *  @param dn may be null
 *  @since 0.8.4
 */
public void saveMagnetStatus(byte[] ih, String dir, String trackerURL, String dn) {
    // i2psnark.config file
    String infohash = Base64.encode(ih);
    infohash = infohash.replace('=', '$');
    _config.setProperty(PROP_META_MAGNET_PREFIX + infohash, ".");
    // its own config file
    Properties config = new OrderedProperties();
    config.setProperty(PROP_META_MAGNET, "true");
    if (dir != null)
        config.setProperty(PROP_META_MAGNET_DIR, dir);
    if (trackerURL != null)
        config.setProperty(PROP_META_MAGNET_TR, trackerURL);
    if (dn != null)
        config.setProperty(PROP_META_MAGNET_DN, dn);
    String now = Long.toString(System.currentTimeMillis());
    config.setProperty(PROP_META_ADDED, now);
    config.setProperty(PROP_META_STAMP, now);
    // save
    synchronized (_configLock) {
        saveConfig();
        locked_saveTorrentStatus(ih, config);
    }
}
Also used : OrderedProperties(net.i2p.util.OrderedProperties) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties)

Aggregations

OrderedProperties (net.i2p.util.OrderedProperties)34 Properties (java.util.Properties)20 IOException (java.io.IOException)16 File (java.io.File)12 Map (java.util.Map)8 RouterAddress (net.i2p.data.router.RouterAddress)6 UnknownHostException (java.net.UnknownHostException)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 InetAddress (java.net.InetAddress)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 StructureTest (net.i2p.data.StructureTest)3 Test (org.junit.Test)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 Socket (java.net.Socket)2 GeneralSecurityException (java.security.GeneralSecurityException)2 I2PSessionException (net.i2p.client.I2PSessionException)2