Search in sources :

Example 26 with I2PSession

use of net.i2p.client.I2PSession in project i2p.i2p by i2p.

the class I2PTunnelServer method addSubsession.

/**
 *  Add a non-DSA_SHA1 subsession to the DSA_SHA1 server if necessary.
 *
 *  @return subsession, or null if none was added
 *  @since 0.9.30
 */
private I2PSession addSubsession(I2PSocketManager sMgr, String alt) {
    File altFile = TunnelController.filenameToFile(alt);
    if (altFile == null)
        return null;
    I2PSession sess = sMgr.getSession();
    if (sess.getMyDestination().getSigType() != SigType.DSA_SHA1)
        return null;
    Properties props = new Properties();
    props.putAll(getTunnel().getClientOptions());
    // fixme get actual sig type
    String name = props.getProperty("inbound.nickname");
    if (name != null)
        props.setProperty("inbound.nickname", name + " (EdDSA)");
    name = props.getProperty("outbound.nickname");
    if (name != null)
        props.setProperty("outbound.nickname", name + " (EdDSA)");
    props.setProperty(I2PClient.PROP_SIGTYPE, "EdDSA_SHA512_Ed25519");
    FileInputStream privData = null;
    try {
        privData = new FileInputStream(altFile);
        return sMgr.addSubsession(privData, props);
    } catch (IOException ioe) {
        _log.error("Failed to add subssession", ioe);
        return null;
    } catch (I2PSessionException ise) {
        _log.error("Failed to add subssession", ise);
        return null;
    } finally {
        if (privData != null)
            try {
                privData.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 27 with I2PSession

use of net.i2p.client.I2PSession in project i2p.i2p by i2p.

the class TunnelController method acquire.

/**
 * Note the fact that we are using some sessions, so that they dont get
 * closed by some other tunnels
 */
private void acquire() {
    List<I2PSession> sessions = _tunnel.getSessions();
    if (!sessions.isEmpty()) {
        for (int i = 0; i < sessions.size(); i++) {
            I2PSession session = sessions.get(i);
            if (_log.shouldLog(Log.INFO))
                _log.info("Acquiring session " + session);
            TunnelControllerGroup group = TunnelControllerGroup.getInstance();
            if (group != null)
                group.acquire(this, session);
        }
        _sessions = sessions;
    } else {
        if (_log.shouldLog(Log.WARN))
            _log.warn("No sessions to acquire? for " + getName());
    }
}
Also used : I2PSession(net.i2p.client.I2PSession)

Example 28 with I2PSession

use of net.i2p.client.I2PSession in project i2p.i2p by i2p.

the class TunnelController method setConfig.

/**
 *  As of 0.9.1, updates the options on an existing session
 */
public void setConfig(Properties config, String prefix) {
    Properties props = new Properties();
    for (Map.Entry<Object, Object> e : config.entrySet()) {
        String key = (String) e.getKey();
        if (key.startsWith(prefix)) {
            key = key.substring(prefix.length());
            String val = (String) e.getValue();
            props.setProperty(key, val);
        }
    }
    Properties oldConfig = _config;
    _config = props;
    // Set up some per-type defaults
    // This really isn't the best spot to do this but for servers in particular,
    // it's hard to override settings in the subclass since the session connect
    // is done in the I2PTunnelServer constructor.
    String type = getType();
    if (type != null) {
        if (type.equals(TYPE_HTTP_SERVER)) {
            if (!_config.containsKey(OPT_LIMIT_ACTION))
                _config.setProperty(OPT_LIMIT_ACTION, "http");
        }
        if (type.equals(TYPE_HTTP_SERVER) || type.equals(TYPE_STREAMR_SERVER)) {
            if (!_config.containsKey(OPT_BUNDLE_REPLY))
                _config.setProperty(OPT_BUNDLE_REPLY, "false");
        } else if (!isClient(type)) {
            // override UI that sets it to false
            _config.setProperty(OPT_BUNDLE_REPLY, "true");
        }
        if (type.contains("irc") || type.equals(TYPE_STREAMR_CLIENT)) {
            // maybe a bad idea for ircclient if DCC is enabled
            if (!_config.containsKey(OPT_TAGS_SEND))
                _config.setProperty(OPT_TAGS_SEND, "20");
            if (!_config.containsKey(OPT_LOW_TAGS))
                _config.setProperty(OPT_LOW_TAGS, "14");
        }
        // same default logic as in EditBean.getSigType()
        if (!isClient(type) || type.equals(TYPE_IRC_CLIENT) || type.equals(TYPE_STD_CLIENT) || type.equals(TYPE_SOCKS) || type.equals(TYPE_SOCKS_IRC) || type.equals(TYPE_STREAMR_CLIENT) || (type.equals(TYPE_HTTP_CLIENT) && Boolean.valueOf(getSharedClient()))) {
            if (!_config.containsKey(OPT_SIG_TYPE))
                _config.setProperty(OPT_SIG_TYPE, PREFERRED_SIGTYPE.name());
        }
        if (!isClient(type)) {
            String p1 = _config.getProperty(OPT_MAX_CONNS_MIN, "0");
            String p2 = _config.getProperty(OPT_MAX_CONNS_HOUR, "0");
            String p3 = _config.getProperty(OPT_MAX_CONNS_DAY, "0");
            String p4 = _config.getProperty(OPT_MAX_TOTAL_CONNS_MIN, "0");
            String p5 = _config.getProperty(OPT_MAX_TOTAL_CONNS_HOUR, "0");
            String p6 = _config.getProperty(OPT_MAX_TOTAL_CONNS_DAY, "0");
            String p7 = _config.getProperty(OPT_MAX_STREAMS, "0");
            String p8 = _config.getProperty(OPT_LIMITS_SET, "false");
            if (p1.equals("0") && p2.equals("0") && p3.equals("0") && p4.equals("0") && p5.equals("0") && p6.equals("0") && p7.equals("0") && !p8.equals("true")) {
                // No limits set, let's set some defaults
                _config.setProperty(OPT_MAX_CONNS_MIN, Integer.toString(DEFAULT_MAX_CONNS_MIN));
                _config.setProperty(OPT_MAX_CONNS_HOUR, Integer.toString(DEFAULT_MAX_CONNS_HOUR));
                _config.setProperty(OPT_MAX_CONNS_DAY, Integer.toString(DEFAULT_MAX_CONNS_DAY));
                _config.setProperty(OPT_MAX_TOTAL_CONNS_MIN, Integer.toString(DEFAULT_MAX_TOTAL_CONNS_MIN));
                _config.setProperty(OPT_MAX_STREAMS, Integer.toString(DEFAULT_MAX_STREAMS));
            }
            if (type.equals(TYPE_HTTP_SERVER) && !p8.equals("true")) {
                String p9 = _config.getProperty(OPT_POST_MAX, "0");
                String p10 = _config.getProperty(OPT_POST_TOTAL_MAX, "0");
                if (p9.equals("0") && p10.equals("0")) {
                    _config.setProperty(OPT_POST_MAX, Integer.toString(I2PTunnelHTTPServer.DEFAULT_POST_MAX));
                    _config.setProperty(OPT_POST_TOTAL_MAX, Integer.toString(I2PTunnelHTTPServer.DEFAULT_POST_TOTAL_MAX));
                }
            }
        }
    }
    // tell i2ptunnel, who will tell the TunnelTask, who will tell the SocketManager
    setSessionOptions();
    synchronized (this) {
        if (_state != TunnelState.RUNNING) {
            if (_log.shouldLog(Log.DEBUG)) {
                _log.debug("Not running, not updating sessions");
            }
            return;
        }
    }
    if (oldConfig != null) {
        if (configChanged(_config, oldConfig, PROP_FILE) || configChanged(_config, oldConfig, OPT_ALT_PKF) || configChanged(_config, oldConfig, OPT_SIG_TYPE)) {
            log("Tunnel must be stopped and restarted for private key file changes to take effect");
        }
    }
    // Running, so check sessions
    Collection<I2PSession> sessions = getAllSessions();
    if (sessions.isEmpty()) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Running but no sessions to update");
    }
    for (I2PSession s : sessions) {
        // tell the router via the session
        if (!s.isClosed()) {
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Session is open, updating: " + s);
            s.updateOptions(_tunnel.getClientOptions());
        } else {
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Session is closed, not updating: " + s);
        }
    }
}
Also used : I2PSession(net.i2p.client.I2PSession) Properties(java.util.Properties) Map(java.util.Map)

Example 29 with I2PSession

use of net.i2p.client.I2PSession in project i2p.i2p by i2p.

the class MasterSession method add.

/**
 *  Add a session
 *  @return null for success, or error message
 */
public synchronized String add(String nick, String style, Properties props) {
    if (props.containsKey("DESTINATION"))
        return "SESSION ADD may not contain DESTINATION";
    SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
    if (rec != null || sessions.containsKey(nick))
        return "Duplicate ID " + nick;
    int listenPort = I2PSession.PORT_ANY;
    String slp = (String) props.remove("LISTEN_PORT");
    if (slp == null)
        slp = props.getProperty("FROM_PORT");
    if (slp != null) {
        try {
            listenPort = Integer.parseInt(slp);
            if (listenPort < 0 || listenPort > 65535)
                return "Bad LISTEN_PORT " + slp;
        // TODO enforce streaming listen port must be 0 or from port
        } catch (NumberFormatException nfe) {
            return "Bad LISTEN_PORT " + slp;
        }
    }
    int listenProtocol;
    SAMMessageSess sess;
    SAMv3Handler subhandler;
    try {
        I2PSession isess = socketMgr.getSession();
        subhandler = new SAMv3Handler(handler.getClientSocket(), handler.verMajor, handler.verMinor, handler.getBridge());
        if (style.equals("RAW")) {
            if (!props.containsKey("PORT"))
                return "RAW subsession must specify PORT";
            listenProtocol = I2PSession.PROTO_DATAGRAM_RAW;
            String spr = (String) props.remove("LISTEN_PROTOCOL");
            if (spr == null)
                spr = props.getProperty("PROTOCOL");
            if (spr != null) {
                try {
                    listenProtocol = Integer.parseInt(spr);
                    // RAW can't listen on streaming protocol
                    if (listenProtocol < 0 || listenProtocol > 255 || listenProtocol == I2PSession.PROTO_STREAMING)
                        return "Bad RAW LISTEN_PPROTOCOL " + spr;
                } catch (NumberFormatException nfe) {
                    return "Bad LISTEN_PROTOCOL " + spr;
                }
            }
            SAMv3RawSession ssess = new SAMv3RawSession(nick, props, handler, isess, listenProtocol, listenPort, dgs);
            subhandler.setSession(ssess);
            sess = ssess;
        } else if (style.equals("DATAGRAM")) {
            if (!props.containsKey("PORT"))
                return "DATAGRAM subsession must specify PORT";
            listenProtocol = I2PSession.PROTO_DATAGRAM;
            SAMv3DatagramSession ssess = new SAMv3DatagramSession(nick, props, handler, isess, listenPort, dgs);
            subhandler.setSession(ssess);
            sess = ssess;
        } else if (style.equals("STREAM")) {
            listenProtocol = I2PSession.PROTO_STREAMING;
            // FIXME need something that hangs off an existing dest
            SAMv3StreamSession ssess = new SAMv3StreamSession(nick, props, handler, socketMgr, listenPort);
            subhandler.setSession(ssess);
            sess = ssess;
        } else {
            return "Unrecognized SESSION STYLE " + style;
        }
    } catch (IOException e) {
        return e.toString();
    } catch (DataFormatException e) {
        return e.toString();
    } catch (SAMException e) {
        return e.toString();
    } catch (I2PSessionException e) {
        return e.toString();
    }
    for (SAMMessageSess s : sessions.values()) {
        if (listenProtocol == s.getListenProtocol() && listenPort == s.getListenPort())
            return "Duplicate protocol " + listenProtocol + " and port " + listenPort;
    }
    rec = new SessionRecord(getDestination().toBase64(), props, subhandler);
    try {
        SAMv3Handler.sSessionsHash.putDupDestOK(nick, rec);
        sessions.put(nick, sess);
    } catch (SessionsDB.ExistingIdException e) {
        return "Duplicate ID " + nick;
    }
    if (_log.shouldWarn())
        _log.warn("added " + style + " proto " + listenProtocol + " port " + listenPort);
    sess.start();
    // all ok
    return null;
}
Also used : IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) DataFormatException(net.i2p.data.DataFormatException) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException)

Example 30 with I2PSession

use of net.i2p.client.I2PSession in project i2p.i2p by i2p.

the class DatagramTest method testDatagram.

public void testDatagram() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    I2PClient client = I2PClientFactory.createClient();
    Destination d = client.createDestination(out);
    I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
    I2PDatagramMaker dm = new I2PDatagramMaker(session);
    byte[] dg = dm.makeI2PDatagram(DataHelper.getASCII("What's the deal with 42?"));
    I2PDatagramDissector dd = new I2PDatagramDissector();
    dd.loadI2PDatagram(dg);
    byte[] x = dd.getPayload();
    assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?")));
    x = dd.extractPayload();
    assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?")));
    assertEquals(d, dd.getSender());
    assertEquals(d, dd.extractSender());
}
Also used : Destination(net.i2p.data.Destination) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

I2PSession (net.i2p.client.I2PSession)37 Properties (java.util.Properties)13 I2PSessionException (net.i2p.client.I2PSessionException)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Destination (net.i2p.data.Destination)10 I2PClient (net.i2p.client.I2PClient)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 IOException (java.io.IOException)8 I2PException (net.i2p.I2PException)5 Hash (net.i2p.data.Hash)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 I2PSimpleClient (net.i2p.client.I2PSimpleClient)3 I2PSocket (net.i2p.client.streaming.I2PSocket)3 DataFormatException (net.i2p.data.DataFormatException)3 Log (net.i2p.util.Log)3 InterruptedIOException (java.io.InterruptedIOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 GeneralSecurityException (java.security.GeneralSecurityException)2