use of net.i2p.client.I2PSession in project i2p.i2p by i2p.
the class I2PTunnelServer method connectManager.
/**
* Warning, blocks while connecting to router and building tunnels;
*
* @throws IllegalArgumentException if the I2CP configuration is b0rked so
* badly that we cant create a socketManager
* @since 0.9.8
*/
private void connectManager() {
int retries = 0;
while (sockMgr.getSession().isClosed()) {
try {
sockMgr.getSession().connect();
// Now connect the subsessions, if any
List<I2PSession> subs = sockMgr.getSubsessions();
if (!subs.isEmpty()) {
for (I2PSession sub : subs) {
try {
sub.connect();
if (_log.shouldInfo())
_log.info("Connected subsession " + sub);
} catch (I2PSessionException ise) {
// not fatal?
String msg = "Unable to connect subsession " + sub;
this.l.log(msg);
_log.error(msg, ise);
}
}
}
} catch (I2PSessionException ise) {
// try to make this error sensible as it will happen...
String portNum = getTunnel().port;
if (portNum == null)
portNum = "7654";
String msg;
if (getTunnel().getContext().isRouterContext())
msg = "Unable to build tunnels for the server at " + remoteHost.getHostAddress() + ':' + remotePort;
else
msg = "Unable to connect to the router at " + getTunnel().host + ':' + portNum + " and build tunnels for the server at " + remoteHost.getHostAddress() + ':' + remotePort;
if (++retries < MAX_RETRIES) {
msg += ", retrying in " + (RETRY_DELAY / 1000) + " seconds";
this.l.log(msg);
_log.error(msg);
} else {
msg += ", giving up";
this.l.log(msg);
_log.log(Log.CRIT, msg, ise);
throw new IllegalArgumentException(msg, ise);
}
try {
Thread.sleep(RETRY_DELAY);
} catch (InterruptedException ie) {
}
}
}
l.log("Tunnels ready for server at " + remoteHost.getHostAddress() + ':' + remotePort);
notifyEvent("openServerResult", "ok");
open = true;
}
use of net.i2p.client.I2PSession in project i2p.i2p by i2p.
the class I2PTunnelServer method close.
/**
* Note that the tunnel can be reopened after this by calling startRunning().
* This does not release all resources. In particular, the I2PSocketManager remains
* and it may have timer threads that continue running.
*
* To release all resources permanently, call destroy().
*/
public synchronized boolean close(boolean forced) {
if (!open)
return true;
if (task != null) {
task.close(forced);
}
synchronized (lock) {
if (!forced && sockMgr.listSockets().size() != 0) {
l.log("There are still active connections!");
for (I2PSocket skt : sockMgr.listSockets()) {
l.log("->" + skt);
}
return false;
}
l.log("Stopping tunnels for server at " + this.remoteHost + ':' + this.remotePort);
open = false;
try {
if (i2pss != null) {
i2pss.close();
i2pss = null;
}
I2PSession session = sockMgr.getSession();
getTunnel().removeSession(session);
session.destroySession();
} catch (I2PException ex) {
_log.error("Error destroying the session", ex);
// System.exit(1);
}
// l.log("Server shut down.");
if (_usePool && _executor != null) {
_executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
_executor.shutdownNow();
}
return true;
}
}
use of net.i2p.client.I2PSession in project i2p.i2p by i2p.
the class TunnelController method getDestination.
/**
* Returns null if not running.
* @return Destination or null
* @since 0.9.17
*/
public Destination getDestination() {
if (_tunnel != null) {
List<I2PSession> sessions = _tunnel.getSessions();
for (int i = 0; i < sessions.size(); i++) {
I2PSession session = sessions.get(i);
Destination dest = session.getMyDestination();
if (dest != null)
return dest;
}
}
return null;
}
use of net.i2p.client.I2PSession in project i2p.i2p by i2p.
the class I2PSocketEepGet method sendRequest.
/**
* Look up the address, get a socket from the I2PSocketManager supplied in the constructor,
* and send the request.
*
* @param timeout ignored
*/
@Override
protected void sendRequest(SocketTimeout timeout) throws IOException {
if (_outputStream == null) {
File outFile = new File(_outputFile);
if (outFile.exists())
_alreadyTransferred = outFile.length();
}
if (_proxyIn != null)
try {
_proxyIn.close();
} catch (IOException ioe) {
}
if (_proxyOut != null)
try {
_proxyOut.close();
} catch (IOException ioe) {
}
if (_socket != null)
try {
_socket.close();
} catch (IOException ioe) {
}
try {
URI url = new URI(_actualURL);
if ("http".equals(url.getScheme())) {
String host = url.getHost();
if (host == null)
throw new MalformedURLException("no hostname: " + _actualURL);
int port = url.getPort();
if (port <= 0 || port > 65535)
port = 80;
// as the naming service accepts B64KEY (but not B64KEY.i2p atm)
if ("i2p".equals(host)) {
String file = url.getRawPath();
try {
int slash = 1 + file.substring(1).indexOf('/');
host = file.substring(1, slash);
_actualURL = "http://" + host + file.substring(slash);
String query = url.getRawQuery();
if (query != null)
_actualURL = _actualURL + '?' + query;
} catch (IndexOutOfBoundsException ioobe) {
throw new MalformedURLException("Bad /i2p/ format: " + _actualURL);
}
}
// Use existing I2PSession for lookups.
// This is much more efficient than using the naming service
Destination dest;
I2PSession sess = _socketManager.getSession();
if (sess != null && !sess.isClosed()) {
try {
if (host.length() == 60 && host.endsWith(".b32.i2p")) {
byte[] b = Base32.decode(host.substring(0, 52));
if (b != null) {
Hash h = Hash.create(b);
dest = sess.lookupDest(h, 20 * 1000);
} else {
dest = null;
}
} else {
dest = sess.lookupDest(host, 20 * 1000);
}
} catch (I2PSessionException ise) {
dest = null;
}
} else {
dest = _context.namingService().lookup(host);
}
if (dest == null)
throw new UnknownHostException("Unknown or non-i2p host: " + host);
// Set the timeouts, using the other existing options in the socket manager
// This currently duplicates what SocketTimeout is doing in EepGet,
// but when that's ripped out of EepGet to use setsotimeout, we'll need this.
Properties props = new Properties();
props.setProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT, "" + CONNECT_TIMEOUT);
props.setProperty(I2PSocketOptions.PROP_READ_TIMEOUT, "" + INACTIVITY_TIMEOUT);
// This is important - even if the underlying socket doesn't have a connect delay,
// we want to set it for this connection, so the request headers will go out
// in the SYN packet, saving one RTT.
props.setProperty(PROP_CONNECT_DELAY, CONNECT_DELAY);
I2PSocketOptions opts = _socketManager.buildOptions(props);
opts.setPort(port);
_socket = _socketManager.connect(dest, opts);
} else {
throw new MalformedURLException("Unsupported protocol: " + _actualURL);
}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Bad URL");
ioe.initCause(use);
throw ioe;
} catch (I2PException ie) {
throw new IOException("I2P error", ie);
}
_proxyIn = _socket.getInputStream();
_proxyOut = _socket.getOutputStream();
// SocketTimeout doesn't take an I2PSocket, but no matter, because we
// always close our socket in fetch() above.
// timeout.setSocket(_socket);
String req = getRequest();
_proxyOut.write(DataHelper.getUTF8(req));
_proxyOut.flush();
}
use of net.i2p.client.I2PSession in project i2p.i2p by i2p.
the class I2PSocketManagerFactory method createManager.
/**
* Create a socket manager using the destination loaded from the given private key
* stream and connected to the I2CP router on the specified machine on the given
* port.
*
* Blocks for a long time while the router builds tunnels if connect is true.
*
* @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
* non-null. Caller must close.
* @param i2cpHost I2CP host null to use default, ignored if in router context
* @param i2cpPort I2CP port <= 0 to use default, ignored if in router context
* @param opts Streaming and I2CP options, may be null
* @param connect true to connect (blocking)
* @return the newly created socket manager, non-null (throws on error)
* @since 0.9.7
*/
private static I2PSocketManager createManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort, Properties opts, boolean connect) throws I2PSessionException {
I2PClient client = I2PClientFactory.createClient();
if (opts == null)
opts = new Properties();
Properties syscopy = (Properties) System.getProperties().clone();
for (Map.Entry<Object, Object> e : syscopy.entrySet()) {
String name = (String) e.getKey();
if (opts.getProperty(name) == null)
opts.setProperty(name, (String) e.getValue());
}
// as of 0.8.1 (I2CP default is BestEffort)
if (opts.getProperty(I2PClient.PROP_RELIABILITY) == null)
opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_NONE);
if (i2cpHost != null)
opts.setProperty(I2PClient.PROP_TCP_HOST, i2cpHost);
if (i2cpPort > 0)
opts.setProperty(I2PClient.PROP_TCP_PORT, "" + i2cpPort);
I2PSession session = client.createSession(myPrivateKeyStream, opts);
if (connect)
session.connect();
I2PSocketManager sockMgr = createManager(session, opts, "manager");
return sockMgr;
}
Aggregations