Search in sources :

Example 86 with Destination

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

the class PacketLocal method formatAsString.

@Override
public StringBuilder formatAsString() {
    StringBuilder buf = super.formatAsString();
    // if ( (_tagsSent != null) && (!_tagsSent.isEmpty()) )
    // buf.append(" with tags");
    final int nackCount = _nackCount.get();
    if (nackCount > 0)
        buf.append(" nacked ").append(nackCount).append(" times");
    synchronized (this) {
        if (_ackOn > 0)
            buf.append(" ack after ").append(getAckTime());
    }
    int numSends = _numSends.get();
    if (numSends > 1)
        buf.append(" sent ").append(numSends).append(" times");
    if (isFlagSet(FLAG_SYNCHRONIZE | FLAG_CLOSE | FLAG_RESET)) {
        Connection con = _connection;
        if (con != null) {
            buf.append(" from ");
            Destination local = _session.getMyDestination();
            if (local != null)
                buf.append(local.calculateHash().toBase64().substring(0, 4));
            else
                buf.append("unknown");
            buf.append(" to ");
            Destination remote = con.getRemotePeer();
            if (remote != null)
                buf.append(remote.calculateHash().toBase64().substring(0, 4));
            else
                buf.append("unknown");
        }
    }
    return buf;
}
Also used : Destination(net.i2p.data.Destination)

Example 87 with Destination

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

the class AddressbookBean method getMessages.

/**
 * Perform actions, returning messages about this.
 */
public String getMessages() {
    // Loading config and addressbook moved into getLoadBookMessages()
    String message = "";
    if (action != null) {
        if (_context.getBooleanProperty(PROP_PW_ENABLE) || (serial != null && serial.equals(lastSerial))) {
            boolean changed = false;
            if (action.equals(_t("Add")) || action.equals(_t("Replace"))) {
                if (addressbook != null && hostname != null && destination != null) {
                    try {
                        // throws IAE with translated message
                        String host = AddressBean.toASCII(hostname);
                        String displayHost = host.equals(hostname) ? hostname : hostname + " (" + host + ')';
                        String oldDest = (String) addressbook.get(host);
                        if (destination.equals(oldDest)) {
                            message = _t("Host name {0} is already in address book, unchanged.", displayHost);
                        } else if (oldDest != null && !action.equals(_t("Replace"))) {
                            message = _t("Host name {0} is already in address book with a different destination. Click \"Replace\" to overwrite.", displayHost);
                        } else {
                            boolean valid = true;
                            try {
                                // just to check validity
                                new Destination(destination);
                            } catch (DataFormatException dfe) {
                                valid = false;
                            }
                            if (valid) {
                                addressbook.put(host, destination);
                                changed = true;
                                if (oldDest == null)
                                    message = _t("Destination added for {0}.", displayHost);
                                else
                                    message = _t("Destination changed for {0}.", displayHost);
                                if (!host.endsWith(".i2p"))
                                    message += "<br>" + _t("Warning - host name does not end with \".i2p\"");
                                // clear form
                                hostname = null;
                                destination = null;
                            } else {
                                message = _t("Invalid Base 64 destination.");
                            }
                        }
                    } catch (IllegalArgumentException iae) {
                        message = iae.getMessage();
                        if (message == null)
                            message = _t("Invalid host name \"{0}\".", hostname);
                    }
                } else {
                    message = _t("Please enter a host name and destination");
                }
                // clear search when adding
                search = null;
            } else if (action.equals(_t("Delete Selected")) || action.equals(_t("Delete Entry"))) {
                String name = null;
                int deleted = 0;
                for (String n : deletionMarks) {
                    addressbook.remove(n);
                    String uni = AddressBean.toUnicode(n);
                    String displayHost = uni.equals(n) ? n : uni + " (" + n + ')';
                    if (deleted++ == 0) {
                        changed = true;
                        name = displayHost;
                    }
                }
                if (changed) {
                    if (deleted == 1)
                        message = _t("Destination {0} deleted.", name);
                    else
                        // parameter will always be >= 2
                        message = ngettext("1 destination deleted.", "{0} destinations deleted.", deleted);
                } else {
                    message = _t("No entries selected to delete.");
                }
                if (action.equals(_t("Delete Entry")))
                    search = null;
            } else if (action.equals(_t("Add Alternate"))) {
                // button won't be in UI
                message = "Unsupported";
            }
            if (changed) {
                try {
                    save();
                    message += "<br>" + _t("Address book saved.");
                } catch (IOException e) {
                    warn(e);
                    message += "<br>" + _t("ERROR: Could not write addressbook file.");
                }
            }
        } else {
            message = _t("Invalid form submission, probably because you used the \"back\" or \"reload\" button on your browser. Please resubmit.") + ' ' + _t("If the problem persists, verify that you have cookies enabled in your browser.");
        }
    }
    action = null;
    if (message.length() > 0)
        message = "<p class=\"messages\">" + message + "</p>";
    return message;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException)

Example 88 with Destination

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

the class NamingServiceBean method getMessages.

/**
 * Perform actions, returning messages about this.
 */
@Override
public String getMessages() {
    if (isDirect())
        return super.getMessages();
    // Loading config and addressbook moved into getLoadBookMessages()
    String message = "";
    if (action != null) {
        Properties nsOptions = new Properties();
        // only blockfile needs this
        nsOptions.setProperty("list", getFileName());
        if (_context.getBooleanProperty(PROP_PW_ENABLE) || (serial != null && serial.equals(lastSerial))) {
            boolean changed = false;
            if (action.equals(_t("Add")) || action.equals(_t("Replace")) || action.equals(_t("Add Alternate"))) {
                if (hostname != null && destination != null) {
                    try {
                        // throws IAE with translated message
                        String host = AddressBean.toASCII(hostname);
                        String displayHost = host.equals(hostname) ? hostname : hostname + " (" + host + ')';
                        Properties outProperties = new Properties();
                        Destination oldDest = getNamingService().lookup(host, nsOptions, outProperties);
                        if (oldDest != null && destination.equals(oldDest.toBase64())) {
                            message = _t("Host name {0} is already in address book, unchanged.", displayHost);
                        } else if (oldDest == null && action.equals(_t("Add Alternate"))) {
                            message = _t("Host name {0} is not in  the address book.", displayHost);
                        } else if (oldDest != null && action.equals(_t("Add"))) {
                            message = _t("Host name {0} is already in address book with a different destination. Click \"Replace\" to overwrite.", displayHost);
                        } else {
                            try {
                                Destination dest = new Destination(destination);
                                if (oldDest != null) {
                                    nsOptions.putAll(outProperties);
                                    String now = Long.toString(_context.clock().now());
                                    if (action.equals(_t("Add Alternate")))
                                        nsOptions.setProperty("a", now);
                                    else
                                        nsOptions.setProperty("m", now);
                                }
                                nsOptions.setProperty("s", _t("Manually added via SusiDNS"));
                                boolean success;
                                if (action.equals(_t("Add Alternate"))) {
                                    // check all for dups
                                    List<Destination> all = getNamingService().lookupAll(host);
                                    if (all == null || !all.contains(dest)) {
                                        success = getNamingService().addDestination(host, dest, nsOptions);
                                    } else {
                                        // will get generic message below
                                        success = false;
                                    }
                                } else {
                                    success = getNamingService().put(host, dest, nsOptions);
                                }
                                if (success) {
                                    changed = true;
                                    if (oldDest == null || action.equals(_t("Add Alternate")))
                                        message = _t("Destination added for {0}.", displayHost);
                                    else
                                        message = _t("Destination changed for {0}.", displayHost);
                                    if (!host.endsWith(".i2p"))
                                        message += "<br>" + _t("Warning - host name does not end with \".i2p\"");
                                    // clear form
                                    hostname = null;
                                    destination = null;
                                } else {
                                    message = _t("Failed to add Destination for {0} to naming service {1}", displayHost, getNamingService().getName()) + "<br>";
                                }
                            } catch (DataFormatException dfe) {
                                message = _t("Invalid Base 64 destination.");
                            }
                        }
                    } catch (IllegalArgumentException iae) {
                        message = iae.getMessage();
                        if (message == null)
                            message = _t("Invalid host name \"{0}\".", hostname);
                    }
                } else {
                    message = _t("Please enter a host name and destination");
                }
                // clear search when adding
                search = null;
            } else if (action.equals(_t("Delete Selected")) || action.equals(_t("Delete Entry"))) {
                String name = null;
                int deleted = 0;
                Destination matchDest = null;
                if (action.equals(_t("Delete Entry"))) {
                    // remove specified dest only in case there is more than one
                    if (destination != null) {
                        try {
                            matchDest = new Destination(destination);
                        } catch (DataFormatException dfe) {
                        }
                    }
                }
                for (String n : deletionMarks) {
                    boolean success;
                    if (matchDest != null)
                        success = getNamingService().remove(n, matchDest, nsOptions);
                    else
                        success = getNamingService().remove(n, nsOptions);
                    String uni = AddressBean.toUnicode(n);
                    String displayHost = uni.equals(n) ? n : uni + " (" + n + ')';
                    if (!success) {
                        message += _t("Failed to delete Destination for {0} from naming service {1}", displayHost, getNamingService().getName()) + "<br>";
                    } else if (deleted++ == 0) {
                        changed = true;
                        name = displayHost;
                    }
                }
                if (changed) {
                    if (deleted == 1)
                        // parameter is a host name
                        message += _t("Destination {0} deleted.", name);
                    else
                        // parameter will always be >= 2
                        message = ngettext("1 destination deleted.", "{0} destinations deleted.", deleted);
                } else {
                    message = _t("No entries selected to delete.");
                }
                // clear search when deleting
                if (action.equals(_t("Delete Entry")))
                    search = null;
            }
            if (changed) {
                message += "<br>" + _t("Address book saved.");
            }
        } else {
            message = _t("Invalid form submission, probably because you used the \"back\" or \"reload\" button on your browser. Please resubmit.") + ' ' + _t("If the problem persists, verify that you have cookies enabled in your browser.");
        }
    }
    action = null;
    if (message.length() > 0)
        message = "<p class=\"messages\">" + message + "</p>";
    return message;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException) Properties(java.util.Properties)

Example 89 with Destination

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

the class I2PTunnel method makeKey.

/**
 * Create a new destination, storing the destination and its private keys where
 * instructed.
 * Does NOT support non-default sig types.
 * Deprecated - only used by CLI
 *
 * @param writeTo location to store the destination and private keys
 * @param pubDest location to store the destination
 * @param l logger to send messages to
 */
private static void makeKey(OutputStream writeTo, OutputStream pubDest, Logging l) {
    try {
        l.log("Generating new keys...");
        I2PClient client = I2PClientFactory.createClient();
        Destination d = client.createDestination(writeTo);
        l.log("New destination: " + d.toBase32());
        writeTo.flush();
        writeTo.close();
        writePubKey(d, pubDest, l);
    } catch (I2PException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PClient(net.i2p.client.I2PClient) IOException(java.io.IOException)

Example 90 with Destination

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

the class I2PTunnel method showKey.

/**
 * Read in the given destination, display it, and write it to the given location
 * Deprecated - only used by CLI
 *
 * @param readFrom stream to read the destination from
 * @param pubDest stream to write the destination to
 * @param l logger to send messages to
 */
private static void showKey(InputStream readFrom, OutputStream pubDest, Logging l) {
    try {
        Destination d = new Destination();
        d.readBytes(readFrom);
        l.log("Destination: " + d.toBase32());
        readFrom.close();
        writePubKey(d, pubDest, l);
    } catch (I2PException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) IOException(java.io.IOException)

Aggregations

Destination (net.i2p.data.Destination)149 IOException (java.io.IOException)46 DataFormatException (net.i2p.data.DataFormatException)33 Properties (java.util.Properties)29 I2PException (net.i2p.I2PException)26 Hash (net.i2p.data.Hash)18 ArrayList (java.util.ArrayList)13 File (java.io.File)12 I2PSessionException (net.i2p.client.I2PSessionException)12 SigType (net.i2p.crypto.SigType)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 I2PSession (net.i2p.client.I2PSession)10 I2PSocket (net.i2p.client.streaming.I2PSocket)10 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 I2PClient (net.i2p.client.I2PClient)7 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)7 Test (org.junit.Test)6