use of sockslib.client.SocksProxy in project nzbhydra2 by theotherp.
the class Socks5Server method initProxy.
private void initProxy(Arguments arguments, SocksServerBuilder builder) throws IllegalArgumentException, IOException {
String proxyValue = arguments.getValue(Arrays.asList("-P", "--proxy"), null);
String regex = "((\\w+):(\\w+)@)?([.\\w]+):(\\d+)";
if (proxyValue != null) {
if (proxyValue.matches(regex)) {
SocksProxy proxy = null;
String host = null;
int port = 1080;
Credentials credentials = null;
String[] values = proxyValue.split("@");
String[] address = null;
String[] user = null;
if (values.length == 1) {
address = values[0].split(":");
} else {
user = values[0].split(":");
address = values[1].split(":");
credentials = new UsernamePasswordCredentials(user[0], user[1]);
}
host = address[0];
port = Integer.parseInt(address[1]);
String proxySslValue = arguments.getValue(Arrays.asList("-S", "--proxySsl"), null);
if (proxySslValue != null) {
proxy = new SSLSocks5(new InetSocketAddress(host, port), SSLConfiguration.load(proxySslValue));
}
SocksProxy tempProxy = initProxySSL(arguments, builder, new InetSocketAddress(host, port));
if (tempProxy != null) {
proxy = tempProxy;
}
if (proxy == null) {
proxy = new Socks5(new InetSocketAddress(host, port));
}
if (credentials != null) {
proxy.setCredentials(credentials);
}
builder.setProxy(proxy);
} else {
logger.error("[-P] or [--proxy] value: [username:password@]host:port");
throw new IllegalArgumentException();
}
}
}
use of sockslib.client.SocksProxy in project nzbhydra2 by theotherp.
the class TCPTimeServer method run.
@Override
public void run() {
int port = DEFAULT_PORT;
SocksProxy socksProxy = null;
String alwaysResponse = null;
String proxyHost = null;
String proxyUsername = null;
String proxyPassword = null;
if (args != null) {
for (String arg : args) {
if (arg.equals("-h") || arg.equals("--help")) {
showHelp();
System.exit(0);
} else if (arg.startsWith("--port=")) {
try {
port = Arguments.intValueOf(arg);
} catch (NumberFormatException e) {
logger.error("Value of [--port] should be a number");
System.exit(-1);
}
} else if (arg.startsWith("--always-response=")) {
alwaysResponse = Arguments.valueOf(arg);
} else {
logger.error("Unknown argument [{}]", arg);
return;
}
}
}
try {
server = new ServerSocket(port);
logger.info("TCP time server created at {}", server.getInetAddress(), port);
while (!stop) {
Socket socket = server.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int b;
while ((b = (byte) inputStream.read()) != -1) {
if (b == '\n') {
break;
}
byteArrayOutputStream.write(b);
}
byte[] buffer = byteArrayOutputStream.toByteArray();
String receive = new String(buffer);
logger.info("Client from {} send:{}", socket.getRemoteSocketAddress(), receive);
if (receive.equals("shutdown")) {
break;
}
String response = new Date().toString();
if (alwaysResponse != null) {
response = alwaysResponse;
}
response += "\n";
outputStream.write(response.getBytes());
outputStream.flush();
inputStream.close();
outputStream.close();
socket.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
Aggregations