use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class SocksCommandSender method checkServerReply.
private static void checkServerReply(final InputStream inputStream) throws IOException {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int temp = 0;
for (int i = 0; i < 4; i++) {
temp = inputStream.read();
byteArrayOutputStream.write(temp);
}
final byte addressType = (byte) temp;
switch(addressType) {
case ADDRESS_TYPE_IPV4:
for (int i = 0; i < 6; i++) {
byteArrayOutputStream.write(inputStream.read());
}
break;
case ADDRESS_TYPE_DOMAIN_NAME:
temp = inputStream.read();
byteArrayOutputStream.write(temp);
for (int i = 0; i < temp + 2; i++) {
byteArrayOutputStream.write(inputStream.read());
}
break;
case ADDRESS_TYPE_IPV6:
for (int i = 0; i < 18; i++) {
byteArrayOutputStream.write(inputStream.read());
}
break;
default:
throw new SocksException("Address type not support, type value: " + addressType);
}
final byte[] receivedData = byteArrayOutputStream.toByteArray();
LOGGER.trace("{}", MiscUtil.buildLogString(receivedData, true));
final byte[] addressBytes;
final byte[] portBytes = new byte[2];
if (receivedData[3] == ADDRESS_TYPE_IPV4) {
addressBytes = new byte[4];
System.arraycopy(receivedData, 4, addressBytes, 0, addressBytes.length);
final int a = MiscUtil.toInt(addressBytes[0]);
final int b = MiscUtil.toInt(addressBytes[1]);
final int c = MiscUtil.toInt(addressBytes[2]);
final int d = MiscUtil.toInt(addressBytes[3]);
portBytes[0] = receivedData[8];
portBytes[1] = receivedData[9];
LOGGER.debug("Server replied:Address as IPv4:{}.{}.{}.{}, port:{}", a, b, c, d, (MiscUtil.toInt(portBytes[0]) << 8) | (MiscUtil.toInt(portBytes[1])));
} else if (receivedData[3] == ADDRESS_TYPE_DOMAIN_NAME) {
int size = receivedData[4];
size = size & 0xFF;
addressBytes = new byte[size];
System.arraycopy(receivedData, 4, addressBytes, 0, size);
portBytes[0] = receivedData[4 + size];
portBytes[1] = receivedData[5 + size];
LOGGER.debug("Server replied:Address as host:{}, port:{}", new String(addressBytes, UTF_8), (MiscUtil.toInt(portBytes[0]) << 8) | (MiscUtil.toInt(portBytes[1])));
} else if (receivedData[3] == ADDRESS_TYPE_IPV6) {
addressBytes = new byte[16];
System.arraycopy(receivedData, 4, addressBytes, 0, addressBytes.length);
LOGGER.debug("Server replied:Address as IPv6:{}", new String(addressBytes, UTF_8));
}
final byte serverReply = receivedData[1];
if (serverReply != REP_SUCCEEDED) {
throw SocksException.serverReplyException(serverReply);
}
LOGGER.debug("SOCKS server response success");
}
use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class AnonymousSocks5Server method start.
/**
* Binds the port and starts a thread to listen to incoming proxy connections from JavaMail.
*/
public void start() {
if (running) {
throw new IllegalStateException("server already running!");
}
running = true;
try {
this.threadPool = Executors.newFixedThreadPool(100);
this.serverSocket = new ServerSocket();
this.serverSocket.setReuseAddress(true);
this.serverSocket.bind(new InetSocketAddress(proxyBridgePort));
} catch (final IOException e) {
throw new SocksException("error preparing socks5bridge server for authenticated proxy session", e);
}
new Thread(this).start();
}
Aggregations