use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class ConnectionAcceptor method run2.
private void run2() {
while (!stop) {
I2PServerSocket serverSocket = _util.getServerSocket();
while ((serverSocket == null) && (!stop)) {
if (!(_util.isConnecting() || _util.connected())) {
stop = true;
break;
}
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException ie) {
}
serverSocket = _util.getServerSocket();
}
if (stop)
break;
try {
I2PSocket socket = serverSocket.accept();
if (socket == null) {
continue;
} else {
if (socket.getPeerDestination().equals(_util.getMyDestination())) {
_log.error("Incoming connection from myself");
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
Hash h = socket.getPeerDestination().calculateHash();
if (socket.getLocalPort() == 80) {
_badCounter.increment(h);
if (_log.shouldLog(Log.WARN))
_log.error("Dropping incoming HTTP from " + h);
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
int bad = _badCounter.count(h);
if (bad >= MAX_BAD) {
if (_log.shouldLog(Log.WARN))
_log.warn("Rejecting connection from " + h + " after " + bad + " failures, max is " + MAX_BAD);
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
Thread t = new I2PAppThread(new Handler(socket), "I2PSnark incoming connection");
t.start();
}
} catch (I2PException ioe) {
int level = stop ? Log.WARN : Log.ERROR;
if (_log.shouldLog(level))
_log.log(level, "Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
} catch (ConnectException ioe) {
// which does not currently call our halt(), although it should
if (_log.shouldWarn())
_log.warn("Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
} catch (IOException ioe) {
int level = stop ? Log.WARN : Log.ERROR;
if (_log.shouldLog(level))
_log.log(level, "Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
}
// catch oom?
}
if (_log.shouldLog(Log.WARN))
_log.warn("ConnectionAcceptor closed");
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class Peer method disconnect.
void disconnect() {
if (!_disconnected.compareAndSet(false, true))
return;
PeerState s = state;
if (s != null) {
// try to save partial piece
if (this.deregister) {
PeerListener p = s.listener;
if (p != null) {
List<Request> pcs = s.returnPartialPieces();
if (!pcs.isEmpty())
p.savePartialPieces(this, pcs);
// now covered by savePartialPieces
// p.markUnrequested(this);
}
}
state = null;
PeerConnectionIn in = s.in;
if (in != null)
in.disconnect();
// this is blocking in streaming, so do this after closing the socket
// so it won't really block
// PeerConnectionOut out = s.out;
// if (out != null)
// out.disconnect();
PeerListener pl = s.listener;
if (pl != null)
pl.disconnected(this);
}
I2PSocket csock = sock;
sock = null;
if ((csock != null) && (!csock.isClosed())) {
try {
csock.close();
} catch (IOException ioe) {
_log.warn("Error disconnecting " + toString(), ioe);
}
}
if (s != null) {
// this is blocking in streaming, so do this after closing the socket
// so it won't really block
PeerConnectionOut out = s.out;
if (out != null)
out.disconnect();
}
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class SOCKS4aServer method getDestinationI2PSocket.
/**
* Get an I2PSocket that can be used to send/receive 8-bit clean data
* to/from the destination of the SOCKS connection.
*
* @return an I2PSocket connected with the destination
*/
public I2PSocket getDestinationI2PSocket(I2PSOCKSTunnel t) throws SOCKSException {
setupServer();
if (connHostName == null) {
_log.error("BUG: destination host name has not been initialized!");
throw new SOCKSException("BUG! See the logs!");
}
if (connPort == 0) {
_log.error("BUG: destination port has not been initialized!");
throw new SOCKSException("BUG! See the logs!");
}
// for errors
DataOutputStream out;
try {
out = new DataOutputStream(clientSock.getOutputStream());
} catch (IOException e) {
throw new SOCKSException("Connection error", e);
}
// FIXME: here we should read our config file, select an
// outproxy, and instantiate the proper socket class that
// handles the outproxy itself (SOCKS4a, SOCKS4a, HTTP CONNECT...).
I2PSocket destSock;
try {
if (connHostName.toLowerCase(Locale.US).endsWith(".i2p")) {
Destination dest = _context.namingService().lookup(connHostName);
if (dest == null) {
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException("Host not found");
}
if (_log.shouldDebug())
_log.debug("connecting to " + connHostName + "...");
Properties overrides = new Properties();
I2PSocketOptions sktOpts = t.buildOptions(overrides);
sktOpts.setPort(connPort);
destSock = t.createI2PSocket(dest, sktOpts);
} else if ("localhost".equals(connHostName) || "127.0.0.1".equals(connHostName)) {
String err = "No localhost accesses allowed through the Socks Proxy";
_log.error(err);
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException(err);
/**
**
* } else if (connPort == 80) {
* // rewrite GET line to include hostname??? or add Host: line???
* // or forward to local eepProxy (but that's a Socket not an I2PSocket)
* // use eepProxy configured outproxies?
* String err = "No handler for HTTP outproxy implemented - to: " + connHostName;
* _log.error(err);
* try {
* sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
* } catch (IOException ioe) {}
* throw new SOCKSException(err);
***
*/
} else {
Outproxy outproxy = getOutproxyPlugin();
if (outproxy != null) {
try {
destSock = new SocketWrapper(outproxy.connect(connHostName, connPort));
} catch (IOException ioe) {
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe2) {
}
throw new SOCKSException("connect failed via outproxy plugin", ioe);
}
} else {
List<String> proxies = t.getProxies(connPort);
if (proxies == null || proxies.isEmpty()) {
String err = "No outproxy configured for port " + connPort + " and no default configured either - host: " + connHostName;
_log.error(err);
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException(err);
}
int p = _context.random().nextInt(proxies.size());
String proxy = proxies.get(p);
Destination dest = _context.namingService().lookup(proxy);
if (dest == null) {
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException("Outproxy not found");
}
if (_log.shouldDebug())
_log.debug("connecting to port " + connPort + " proxy " + proxy + " for " + connHostName + "...");
// this isn't going to work, these need to be socks outproxies so we need
// to do a socks session to them?
destSock = t.createI2PSocket(dest);
}
}
confirmConnection();
_log.debug("connection confirmed - exchanging data...");
} catch (DataFormatException e) {
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException("Error in destination format", e);
} catch (IOException e) {
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException("Error connecting", e);
} catch (I2PException e) {
try {
sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
} catch (IOException ioe) {
}
throw new SOCKSException("Error connecting", e);
}
return destSock;
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p by i2p.
the class SOCKS5Server method outproxyConnect.
/**
* Act as a SOCKS 5 client to connect to an outproxy
* @return open socket or throws error
* @since 0.8.2
*/
private I2PSocket outproxyConnect(I2PSOCKSTunnel tun, String proxy) throws IOException, I2PException {
Properties overrides = new Properties();
overrides.setProperty("option.i2p.streaming.connectDelay", "1000");
I2PSocketOptions proxyOpts = tun.buildOptions(overrides);
Destination dest = _context.namingService().lookup(proxy);
if (dest == null)
throw new SOCKSException("Outproxy not found");
I2PSocket destSock = tun.createI2PSocket(dest, proxyOpts);
OutputStream out = null;
InputStream in = null;
try {
out = destSock.getOutputStream();
in = destSock.getInputStream();
boolean authAvail = Boolean.parseBoolean(props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH));
String configUser = null;
String configPW = null;
if (authAvail) {
configUser = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER_PREFIX + proxy);
configPW = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_PW_PREFIX + proxy);
if (configUser == null || configPW == null) {
configUser = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER);
configPW = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_PW);
}
}
SOCKS5Client.connect(in, out, connHostName, connPort, configUser, configPW);
} catch (IOException e) {
try {
destSock.close();
} catch (IOException ioe) {
}
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
throw e;
}
// that's it, caller will send confirmation to our client
return destSock;
}
use of net.i2p.client.streaming.I2PSocket in project i2p.i2p-bote by i2p.
the class SeedlessAnnounce method doSeedlessAnnounce.
private synchronized void doSeedlessAnnounce() {
List<String> seedlessServers = seedlessScrapeServers.getSeedlessServers();
if (seedlessServers.isEmpty()) {
// try again in a minute.
log.error("SeedlessServers.isEmpty, will retry shortly.");
lastSeedlessAnnounce = System.currentTimeMillis() - (interval - TimeUnit.MINUTES.toMillis(1));
return;
}
// Announce to 10 servers.
// We do this over the i2pSocket.
int successful = Math.min(10, seedlessServers.size());
log.debug("Try to announce to " + successful + " Seedless Servers");
Collections.shuffle(seedlessServers, new Random());
Iterator<String> it = seedlessServers.iterator();
String line;
I2PSocket I2P;
InputStream Iin;
OutputStream Iout;
BufferedReader data;
Boolean didsomething = false;
BufferedWriter output;
while (successful > 0 && it.hasNext()) {
lastSeedlessAnnounce = System.currentTimeMillis();
String b32 = it.next();
Destination dest = null;
I2P = null;
try {
lastSeedlessAnnounce = System.currentTimeMillis();
// deprecated dest = I2PTunnel.destFromName(b32);
dest = I2PAppContext.getGlobalContext().namingService().lookup(b32);
lastSeedlessAnnounce = System.currentTimeMillis();
if (dest == null) {
log.debug("Could not find the destination: <" + b32 + ">");
continue;
}
line = dest.toBase64();
dest = new Destination();
dest.fromBase64(line);
I2P = socketManager.connect(dest);
// I2P.setReadTimeout(0); // temp bugfix, this *SHOULD* be the default
// make readers/writers
Iin = I2P.getInputStream();
Iout = I2P.getOutputStream();
output = new BufferedWriter(new OutputStreamWriter(Iout));
output.write(announceString);
output.flush();
data = new BufferedReader(new InputStreamReader(Iin));
// Check for success.
line = data.readLine();
if (line != null) {
if (line.contains(" 200 ")) {
log.debug("Announced to " + b32);
successful--;
didsomething = true;
} else {
log.debug("Announce to " + b32 + " Failed with Error " + line);
log.debug("We sent " + announceString);
}
}
while ((line = data.readLine()) != null) {
}
} catch (DataFormatException ex) {
log.debug("Not base64!", ex);
} catch (ConnectException ex) {
log.debug("ConnectException", ex);
} catch (NoRouteToHostException ex) {
log.debug("NoRouteToHostException", ex);
} catch (InterruptedIOException ex) {
log.debug("InterruptedIOException", ex);
} catch (IOException ex) {
log.debug("IOException", ex);
ex.printStackTrace();
} catch (I2PException ex) {
log.debug("I2PException", ex);
}
if (I2P != null) {
try {
I2P.close();
} catch (IOException ex) {
// don't care.
}
}
}
if (!didsomething) {
// try again in 1 minute.
lastSeedlessAnnounce = System.currentTimeMillis() - (interval - TimeUnit.MINUTES.toMillis(1));
return;
}
lastSeedlessAnnounce = System.currentTimeMillis();
}
Aggregations