private void handleRequest(final ProxyMessage msg) throws IOException { if (!auth.checkRequest(msg)) { throw new SocksException(SocksProxyBase.SOCKS_FAILURE); } if (msg.ip == null) { if (msg instanceof Socks5Message) { msg.ip = dnsResolver.resolveByName(msg.host); } else { throw new SocksException(SocksProxyBase.SOCKS_FAILURE); } } log(msg); switch (msg.command) { case SocksProxyBase.SOCKS_CMD_CONNECT: onConnect(msg); break; case SocksProxyBase.SOCKS_CMD_BIND: onBind(msg); break; case SocksProxyBase.SOCKS_CMD_UDP_ASSOCIATE: onUDP(msg); break; default: throw new SocksException(SocksProxyBase.SOCKS_CMD_NOT_SUPPORTED); } }
// Private methods // /////////////// private void startSession() throws IOException { sock.setSoTimeout(iddleTimeout); try { auth = auth.startSession(sock); } catch (final IOException ioe) { log.warn("Auth throwed exception:", ioe); auth = null; return; } if (auth == null) { // Authentication failed log.info("Authentication failed"); return; } in = auth.getInputStream(); out = auth.getOutputStream(); msg = readMsg(in); handleRequest(msg); }
// Runnable interface // ////////////////// @Override public void run() { switch (mode) { case START_MODE: try { startSession(); } catch (final IOException ioe) { log.error("START_MODE exception.", ioe); handleException(ioe); } finally { abort(); if (auth != null) { auth.endSession(); } log.info("Main thread(client->remote)stopped."); } break; case ACCEPT_MODE: try { doAccept(); mode = PIPE_MODE; pipe_thread1.interrupt(); // Tell other thread that connection // have // been accepted. pipe(remote_in, out); } catch (final IOException ioe) { log.error("ACCEPT_MODE exception.", ioe); handleException(ioe); } finally { abort(); log.info("Accept thread(remote->client) stopped"); } break; case PIPE_MODE: try { pipe(remote_in, out); } catch (final IOException ioe) { log.error("PIPE_MODE error", ioe); } finally { abort(); log.info("Support thread(remote->client) stopped"); } break; case ABORT_MODE: break; default: log.warn("Unexpected MODE " + mode); } }