-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>io.github.greyp9</groupId> | ||
<artifactId>tls-client</artifactId> | ||
<version>0.0.0</version> | ||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format> | ||
</properties> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit</groupId> | ||
<artifactId>junit-bom</artifactId> | ||
<version>5.10.2</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.github.greyp9</groupId> | ||
<artifactId>arwo</artifactId> | ||
<version>0.2.0-SNAPSHOT</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>2.0.13</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>2.0.13</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.14.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.bouncycastle</groupId> | ||
<artifactId>bcprov-jdk18on</artifactId> | ||
<version>1.78.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.nifi</groupId> | ||
<artifactId>nifi-security-utils-api</artifactId> | ||
<version>1.25.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.nifi</groupId> | ||
<artifactId>nifi-property-utils</artifactId> | ||
<version>1.25.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.nifi</groupId> | ||
<artifactId>nifi-security-utils</artifactId> | ||
<version>1.25.0</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<!-- https://repo.maven.apache.org/maven2/org/apache/maven/plugins/ --> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.13.0</version> <!-- 2024-03-15 --> | ||
<configuration/> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.2.5</version> <!-- 2024-01-06 --> | ||
<configuration> | ||
<redirectTestOutputToFile>false</redirectTestOutputToFile> | ||
<systemPropertyVariables> | ||
<java.util.logging.config.file>src/test/resources/logging.properties</java.util.logging.config.file> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<version>3.7.0</version> <!-- 2024-06-09 --> | ||
<executions> | ||
<execution> | ||
<id>with-version</id> | ||
<phase>initialize</phase> | ||
<goals> | ||
<goal>copy-dependencies</goal> | ||
</goals> | ||
<configuration> | ||
<includeScope>runtime</includeScope> | ||
<outputDirectory>${project.build.directory}/lib</outputDirectory> | ||
<stripVersion>false</stripVersion> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.4.1</version> <!-- 2024-04-16 --> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<classpathPrefix>lib/</classpathPrefix> | ||
<mainClass>io.github.greyp9.tls.client.TlsClient</mainClass> | ||
</manifest> | ||
<manifestEntries> | ||
<Specification-Version>${project.version}</Specification-Version> | ||
<Implementation-Version>${maven.build.timestamp}</Implementation-Version> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
140 changes: 140 additions & 0 deletions
140
dev/tls/client/src/main/java/io/github/greyp9/tls/client/TlsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package io.github.greyp9.tls.client; | ||
|
||
import io.github.greyp9.arwo.core.charset.UTF8Codec; | ||
import io.github.greyp9.arwo.core.date.DurationU; | ||
import io.github.greyp9.arwo.core.input.runnable.InputStreamRunnable; | ||
import io.github.greyp9.arwo.core.lang.NumberU; | ||
import io.github.greyp9.arwo.core.tls.context.TLSContext; | ||
import io.github.greyp9.arwo.core.tls.context.TLSContextFactory; | ||
import io.github.greyp9.arwo.core.tls.manage.TLSKeyManager; | ||
import io.github.greyp9.arwo.core.tls.manage.TLSTrustManager; | ||
import io.github.greyp9.arwo.core.value.Value; | ||
import io.github.greyp9.arwo.core.vm.thread.ThreadU; | ||
import org.apache.nifi.security.util.KeystoreType; | ||
import org.apache.nifi.security.util.SslContextFactory; | ||
import org.apache.nifi.security.util.StandardTlsConfiguration; | ||
import org.apache.nifi.security.util.TlsConfiguration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.Socket; | ||
import java.security.GeneralSecurityException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import javax.net.ssl.SSLContext; | ||
|
||
public class TlsClient { | ||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private final File tlsDir; | ||
private final int port; | ||
|
||
public TlsClient(final File tlsDir, final int port) { | ||
this.tlsDir = tlsDir; | ||
this.port = port; | ||
} | ||
|
||
public void run() throws GeneralSecurityException, IOException { | ||
final String keystore = new File(tlsDir, "ks.p12").getAbsolutePath(); | ||
final String truststore = new File(tlsDir, "ts.jks").getAbsolutePath(); | ||
final String password = "123456"; | ||
//final SSLContext sslContextClient = getSSLContextNiFi(keystore, truststore); | ||
final SSLContext sslContextClient = getSSLContextArwo(keystore, truststore, password); | ||
logger.info(sslContextClient.toString()); | ||
|
||
final long idleInterval = (DurationU.Const.ONE_SECOND_MILLIS / 2); | ||
final AtomicReference<String> signal = new AtomicReference<>(); | ||
final AtomicReference<String> referenceText = new AtomicReference<>(); | ||
ExecutorService executorService = Executors.newFixedThreadPool(2, Executors.defaultThreadFactory()); | ||
executorService.execute(new InputStreamRunnable(System.in, referenceText, idleInterval)); | ||
final Runnable runnableTerminal = () -> signal.set("INTERRUPTED"); | ||
|
||
while (Value.isEmpty(signal.get())) { | ||
Value.doIf(ThreadU.sleepMillis(idleInterval), runnableTerminal); | ||
final String text = referenceText.getAndSet(null); | ||
if (Value.isData(text)) { | ||
if (text.contains("q")) { | ||
runnableTerminal.run(); | ||
} else { | ||
logger.info(text); | ||
sendMessage(sslContextClient, text); | ||
executorService.execute(new InputStreamRunnable(System.in, referenceText, idleInterval)); | ||
} | ||
} | ||
} | ||
logger.info("SIGNAL=[{}]", signal); | ||
executorService.shutdown(); | ||
} | ||
|
||
private SSLContext getSSLContextNiFi(final String keystore, final String truststore, final String password) | ||
throws GeneralSecurityException { | ||
final TlsConfiguration tlsConfiguration = new StandardTlsConfiguration( | ||
keystore, password, KeystoreType.PKCS12, | ||
truststore, password, KeystoreType.JKS); | ||
return SslContextFactory.createSslContext(tlsConfiguration); | ||
} | ||
|
||
private SSLContext getSSLContextArwo(final String keystore, final String truststore, final String password) | ||
throws GeneralSecurityException, IOException { | ||
final TLSContextFactory tlsContextFactory = new TLSContextFactory(); | ||
final TLSKeyManager keyManager = tlsContextFactory.getKeyManager("PKCS12", keystore, password.toCharArray()); | ||
final TLSTrustManager trustManager = tlsContextFactory.getTrustManager("JKS", truststore, password.toCharArray()); | ||
final TLSContext tlsContext = new TLSContext(keyManager, trustManager, "TLSv1.2"); | ||
//final TLSContext tlsContext = new TLSContext(null, trustManager, "TLSv1.2"); | ||
return tlsContext.getContext(); | ||
} | ||
|
||
private void sendMessage(final SSLContext sslContext, final String message) { | ||
try (final Socket socket = sslContext.getSocketFactory().createSocket("localhost", port)) { | ||
logger.info("GOT A SERVER CONNECTION: {}", socket); | ||
// send message | ||
final String payload = String.format("hello from client - message=[%s]", message); | ||
final byte[] payloadBytes = UTF8Codec.toBytes(payload); | ||
final InputStream is = new BufferedInputStream(socket.getInputStream()); | ||
final OutputStream os = new BufferedOutputStream(socket.getOutputStream()); | ||
os.write(payloadBytes, 0, payloadBytes.length); | ||
os.flush(); | ||
logger.info(String.format("CLIENT SENT: %s", payload)); | ||
// receive service response | ||
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
while (bos.size() == 0) { | ||
final int i = is.read(); | ||
if (i > 0) { | ||
bos.write(i); | ||
} | ||
while (is.available() > 0) { | ||
bos.write(is.read()); | ||
} | ||
ThreadU.sleepMillis(20L); | ||
} | ||
logger.info(String.format("CLIENT RECEIVED: %s", UTF8Codec.toString(bos.toByteArray()))); | ||
} catch (final Exception e) { | ||
logger.error(e.getMessage(), e); | ||
} | ||
} | ||
|
||
public static void main(final String[] args) { | ||
final String tlsDir = System.getProperty("tls.dir"); | ||
Value.require(Value.isData(tlsDir), () -> new IllegalArgumentException("specify '-Dtls.dir'")); | ||
final File folder = new File(System.getProperty("tls.dir")); | ||
Value.require(folder.exists(), () -> new IllegalArgumentException("tlsDir must refer to an existing folder")); | ||
|
||
Value.require((args.length >= 1), () -> new IllegalArgumentException("usage: TlsClient [port]")); | ||
final int port = NumberU.toInt(args[0], 0); | ||
Value.require((port > 0), () -> new IllegalArgumentException("port should be number")); | ||
|
||
try { | ||
new TlsClient(folder, port).run(); | ||
} catch (GeneralSecurityException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
org.slf4j.simpleLogger.showDateTime=true | ||
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd'T'HH:mm:ss:SSS'Z' | ||
org.slf4j.simpleLogger.showThreadName=false | ||
|
||
org.slf4j.simpleLogger.log=INFO |
Oops, something went wrong.