Search in sources :

Example 6 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKS5Server method setupServer.

protected void setupServer() throws SOCKSException {
    if (setupCompleted) {
        return;
    }
    DataInputStream in;
    DataOutputStream out;
    try {
        in = new DataInputStream(clientSock.getInputStream());
        out = new DataOutputStream(clientSock.getOutputStream());
        init(in, out);
        if (manageRequest(in, out) == Command.UDP_ASSOCIATE)
            handleUDP(in, out);
    } catch (IOException e) {
        throw new SOCKSException("Connection error", e);
    }
    setupCompleted = true;
}
Also used : DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 7 with SOCKSException

use of net.i2p.socks.SOCKSException 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;
}
Also used : Destination(net.i2p.data.Destination) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) SOCKSException(net.i2p.socks.SOCKSException) I2PSocket(net.i2p.client.streaming.I2PSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) IOException(java.io.IOException) Properties(java.util.Properties)

Example 8 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKSServerFactory method createSOCKSServer.

/**
 * Create a new SOCKS server, using the provided socket (that must
 * be connected to a client) to select the proper SOCKS protocol
 * version.  This method wil strip the SOCKS VER field from the
 * provided sockets's input stream.
 *
 * @param s a Socket used to choose the SOCKS server type
 * @param props non-null
 */
public static SOCKSServer createSOCKSServer(I2PAppContext ctx, Socket s, Properties props) throws SOCKSException {
    SOCKSServer serv;
    try {
        DataInputStream in = new DataInputStream(s.getInputStream());
        int socksVer = in.readByte();
        switch(socksVer) {
            case 0x04:
                // SOCKS version 4/4a
                if (Boolean.parseBoolean(props.getProperty(I2PTunnelHTTPClientBase.PROP_AUTH)) && props.containsKey(I2PTunnelHTTPClientBase.PROP_USER) && props.containsKey(I2PTunnelHTTPClientBase.PROP_PW)) {
                    throw new SOCKSException("SOCKS 4/4a not supported when authorization is required");
                }
                serv = new SOCKS4aServer(ctx, s, props);
                break;
            case 0x05:
                // SOCKS version 5
                serv = new SOCKS5Server(ctx, s, props);
                break;
            case 'C':
            case 'G':
            case 'H':
            case 'P':
                DataOutputStream out = new DataOutputStream(s.getOutputStream());
                out.write(DataHelper.getASCII(ERR_REQUEST_DENIED));
                throw new SOCKSException("HTTP request to socks");
            default:
                throw new SOCKSException("SOCKS protocol version not supported (" + Integer.toHexString(socksVer) + ")");
        }
    } catch (IOException e) {
        // _log.debug("error reading SOCKS protocol version");
        throw new SOCKSException("Connection error", e);
    }
    return serv;
}
Also used : DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 9 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class I2PSOCKSIRCTunnel method clientConnectionRun.

/**
 *  Same as in I2PSOCKSTunnel, but run the filters from I2PTunnelIRCClient
 *  instead of I2PTunnelRunner
 */
@Override
protected void clientConnectionRun(Socket s) {
    I2PSocket destSock = null;
    try {
        // _log.error("SOCKS IRC Tunnel Start");
        try {
            s.setSoTimeout(INITIAL_SO_TIMEOUT);
        } catch (SocketException ioe) {
        }
        SOCKSServer serv = SOCKSServerFactory.createSOCKSServer(_context, s, getTunnel().getClientOptions());
        Socket clientSock = serv.getClientSocket();
        try {
            s.setSoTimeout(0);
        } catch (SocketException ioe) {
        }
        destSock = serv.getDestinationI2PSocket(this);
        StringBuffer expectedPong = new StringBuffer();
        int id = __clientId.incrementAndGet();
        Thread in = new I2PAppThread(new IrcInboundFilter(clientSock, destSock, expectedPong, _log), "SOCKS IRC Client " + id + " in", true);
        in.start();
        // Thread out = new I2PAppThread(new IrcOutboundFilter(clientSock, destSock, expectedPong, _log),
        // "SOCKS IRC Client " + id + " out", true);
        Runnable out = new IrcOutboundFilter(clientSock, destSock, expectedPong, _log);
        // we are called from an unlimited thread pool, so run inline
        // out.start();
        out.run();
    } catch (SOCKSException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Error from SOCKS connection", e);
    } finally {
        // only because we are running it inline
        closeSocket(s);
        if (destSock != null)
            try {
                destSock.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : SocketException(java.net.SocketException) I2PSocket(net.i2p.client.streaming.I2PSocket) SOCKSException(net.i2p.socks.SOCKSException) IOException(java.io.IOException) I2PAppThread(net.i2p.util.I2PAppThread) I2PAppThread(net.i2p.util.I2PAppThread) IrcOutboundFilter(net.i2p.i2ptunnel.irc.IrcOutboundFilter) IrcInboundFilter(net.i2p.i2ptunnel.irc.IrcInboundFilter) Socket(java.net.Socket) I2PSocket(net.i2p.client.streaming.I2PSocket)

Example 10 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKS4aServer method confirmConnection.

protected void confirmConnection() throws SOCKSException {
    DataOutputStream out;
    try {
        out = new DataOutputStream(clientSock.getOutputStream());
        sendRequestReply(Reply.SUCCEEDED, InetAddress.getByName("127.0.0.1"), 1, out);
    } catch (IOException e) {
        throw new SOCKSException("Connection error", e);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) IOException(java.io.IOException)

Aggregations

SOCKSException (net.i2p.socks.SOCKSException)12 IOException (java.io.IOException)11 DataOutputStream (java.io.DataOutputStream)8 I2PSocket (net.i2p.client.streaming.I2PSocket)5 DataInputStream (java.io.DataInputStream)4 Properties (java.util.Properties)3 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)3 Destination (net.i2p.data.Destination)3 Socket (java.net.Socket)2 SocketException (java.net.SocketException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 I2PException (net.i2p.I2PException)2 Outproxy (net.i2p.app.Outproxy)2 DataFormatException (net.i2p.data.DataFormatException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1