mirror of
https://github.com/qi4L/JYso.git
synced 2026-09-25 08:11:53 +08:00
add ldaps
This commit is contained in:
@@ -51,6 +51,7 @@ public class LdapServer extends InMemoryOperationInterceptor {
|
||||
public static void start() {
|
||||
try {
|
||||
InMemoryDirectoryServerConfig serverConfig = new InMemoryDirectoryServerConfig("dc=example,dc=com");
|
||||
|
||||
serverConfig.setListenerConfigs(new InMemoryListenerConfig(
|
||||
"listen",
|
||||
InetAddress.getByName("0.0.0.0"),
|
||||
@@ -73,13 +74,6 @@ public class LdapServer extends InMemoryOperationInterceptor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor#processSearchResult(com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult)
|
||||
* 关键在这个类里进行了处理
|
||||
* 官方说明:在提供的搜索结果返回给客户端之前,调用应该对其执行的任何处理。
|
||||
*/
|
||||
@Override
|
||||
public void processSearchResult(InMemoryInterceptedSearchResult result) {
|
||||
String base;
|
||||
|
||||
@@ -20,6 +20,9 @@ public class Starter {
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length > 0 && args[0].equals("-j")) {
|
||||
Config.applyCmdArgs(args);
|
||||
if (Config.TLSProxy) {
|
||||
TLSProxy.start();
|
||||
}
|
||||
LdapServer.start();
|
||||
HTTPServer.start();
|
||||
RMIServer.start();
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.qi4l.JYso;
|
||||
|
||||
import com.qi4l.JYso.gadgets.Config.Config;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.security.KeyStore;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.fusesource.jansi.Ansi.ansi;
|
||||
|
||||
public class TLSProxy {
|
||||
private final String localAddr;
|
||||
private final String remoteAddr;
|
||||
private final String certFile;
|
||||
private final String keyFile;
|
||||
|
||||
public TLSProxy(String localAddr, String remoteAddr, String certFile, String keyFile) {
|
||||
this.localAddr = localAddr;
|
||||
this.remoteAddr = remoteAddr;
|
||||
this.certFile = certFile;
|
||||
this.keyFile = keyFile;
|
||||
}
|
||||
|
||||
public static void start() {
|
||||
System.out.println(ansi().render("@|green [+]|@ LDAPS Server Start Listening on >>" + Config.TLSProxy + "..."));
|
||||
new TLSProxy(Config.ip + ":" + Config.TLSProxy, Config.ip + ":" + Config.ldapPort, Config.certFile, Config.keyFile).run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
SSLServerSocketFactory sslServerSocketFactory = createSSLServerSocketFactory();
|
||||
if (sslServerSocketFactory == null) {
|
||||
System.err.println("Failed to create SSLServerSocketFactory");
|
||||
return;
|
||||
}
|
||||
|
||||
try (SSLServerSocket serverSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket()) {
|
||||
String[] addressParts = localAddr.split(":");
|
||||
serverSocket.bind(new InetSocketAddress(addressParts[0], Integer.parseInt(addressParts[1])));
|
||||
System.out.println("TLS Proxy started on " + localAddr);
|
||||
|
||||
ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
|
||||
while (true) {
|
||||
SSLSocket clientSocket = (SSLSocket) serverSocket.accept();
|
||||
System.out.println("New connection from " + clientSocket.getRemoteSocketAddress());
|
||||
executorService.submit(() -> handleConnection(clientSocket));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private SSLServerSocketFactory createSSLServerSocketFactory() {
|
||||
try {
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
KeyStore keyStore = KeyStore.getInstance("JKS");
|
||||
|
||||
try (InputStream keyInput = new FileInputStream(certFile)) {
|
||||
keyStore.load(keyInput, "".toCharArray());
|
||||
}
|
||||
|
||||
keyManagerFactory.init(keyStore, "".toCharArray());
|
||||
|
||||
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
|
||||
return sslContext.getServerSocketFactory();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleConnection(SSLSocket clientSocket) {
|
||||
try (Socket remoteSocket = new Socket(remoteAddr, getPort(remoteAddr))) {
|
||||
System.out.println("Connected to " + remoteAddr);
|
||||
ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
forwardData(clientSocket.getInputStream(), remoteSocket.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
forwardData(remoteSocket.getInputStream(), clientSocket.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void forwardData(InputStream input, OutputStream output) {
|
||||
try {
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = input.read(buffer)) != -1) {
|
||||
output.write(buffer, 0, bytesRead);
|
||||
output.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private int getPort(String address) {
|
||||
return Integer.parseInt(address.substring(address.lastIndexOf(':') + 1));
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,9 @@ public class Config {
|
||||
@Parameter(names = {"-lP", "--ldapPort"}, description = "Ldap bind port", order = 2)
|
||||
public static int ldapPort = 1389;
|
||||
|
||||
@Parameter(names = {"-lPs", "--ldapsPort"}, description = "Ldaps bind port", order = 2)
|
||||
public static int ldapsPort = 1669;
|
||||
|
||||
@Parameter(names = {"-rP", "--rmiPort"}, description = "rmi bind port", order = 2)
|
||||
public static int rmiPort = 1099;
|
||||
|
||||
@@ -36,8 +39,15 @@ public class Config {
|
||||
public static String AESkey = "123";
|
||||
@Parameter(names = {"-u", " --user"}, description = "ldap bound account", order = 5)
|
||||
public static String USER = "";
|
||||
@Parameter(names = {"-tP", " --TLSProxy"}, description = "TLS port forwarding", order = 5)
|
||||
public static boolean TLSProxy = false;
|
||||
@Parameter(names = {"-p", " --PASSWD"}, description = "ldap binding password", order = 5)
|
||||
public static String PASSWD = "";
|
||||
@Parameter(names = {"-kF", " --keyFile"}, description = "Path to the TLS private key file", order = 5)
|
||||
public static String keyFile = "";
|
||||
@Parameter(names = {"-cF", " --certFile"}, description = "Path to the TLS certificate file", order = 5)
|
||||
public static String certFile = "";
|
||||
|
||||
@Parameter(names = {"-j", "--jndi"}, description = "starter", order = 5)
|
||||
public static boolean jndi = false;
|
||||
public static String rhost;
|
||||
|
||||
Reference in New Issue
Block a user