show timestamps

This commit is contained in:
Alexander Lehmann 2023-05-05 21:21:05 +02:00
parent 932ed3db5c
commit a7491ffc88
2 changed files with 91 additions and 16 deletions

12
pom.xml
View File

@ -62,7 +62,19 @@
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId> <artifactId>netty-tcnative-boringssl-static</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId> <groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId> <artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version> <version>2.4.0-b180830.0359</version>

View File

@ -1,5 +1,6 @@
package cx.lehmann.gemini.gemini; package cx.lehmann.gemini.gemini;
import java.security.KeyStore;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate; import java.security.cert.Certificate;
@ -7,7 +8,18 @@ import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.List; import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import io.vertx.core.AbstractVerticle; import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise; import io.vertx.core.Promise;
@ -16,36 +28,59 @@ import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.NetSocket; import io.vertx.core.net.NetSocket;
//import io.vertx.core.net.OpenSSLEngineOptions; //import io.vertx.core.net.OpenSSLEngineOptions;
import io.vertx.core.net.PemKeyCertOptions; import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.core.net.PemTrustOptions; //import io.vertx.core.net.PemTrustOptions;
import io.vertx.core.net.TrustOptions; import io.vertx.core.net.TrustOptions;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
import java.util.ArrayList;
//import sun.security.x509.X509Cert; //import sun.security.x509.X509Cert;
public class MainVerticle extends AbstractVerticle { public class MainVerticle extends AbstractVerticle {
NetSocket conn2=null; List<NetSocket> clients=new ArrayList<>();
// X509TrustManager tm=new MyTrustManager();
@Override @Override
public void start(Promise<Void> startPromise) throws Exception { public void start(Promise<Void> startPromise) throws Exception {
NetServerOptions options=new NetServerOptions(); NetServerOptions options=new NetServerOptions();
String certPath="c:/temp/cert.pem"; vertx.exceptionHandler(ex -> {ex.printStackTrace();});
// TrustOptions trustOptions=new PemTrustOptions(); // String certPath="c:/temp/cert.pem";
String certPath="/home/lehmann/gemini-chat/cert.pem";
// TrustOptions trustOptions=new MyTrustOptions(vertx);
TrustManagerFactory trustMgrFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keystore=KeyStore.getInstance("JKS");
trustMgrFactory.init(keystore);
TrustManager tms[] = trustMgrFactory.getTrustManagers();
TrustManager tm=tms[0];
TrustManager trustManager = new MyTrustManager(tm);
options.setPemKeyCertOptions(new PemKeyCertOptions() options.setPemKeyCertOptions(new PemKeyCertOptions()
.setCertPath(certPath) .setCertPath(certPath)
.setKeyPath(certPath)) .setKeyPath(certPath))
.setSsl(true) .setSsl(true)
// .setTrustOptions(trustOptions) // .setTrustOptions(trustOptions)
.setTrustOptions(TrustOptions.wrap(trustManager))
// .setOpenSslEngineOptions(new OpenSSLEngineOptions()) // .setOpenSslEngineOptions(new OpenSSLEngineOptions())
.setClientAuth(ClientAuth.REQUIRED); .setClientAuth(ClientAuth.REQUEST)
;
// SSLContext sc = SSLContext.getInstance("SSL");
// sc.init(null, new X509TrustManager[] { tm }, null);
// HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
vertx.createNetServer(options).connectHandler(conn -> { vertx.createNetServer(options).connectHandler(conn -> {
conn.handler(event -> { conn.handler(event -> {
System.out.println("accepted connection:"+conn.remoteAddress()); System.out.println(new Date().toString()+"accepted connection:"+conn.remoteAddress());
String url=event.toString("UTF-8"); String url=event.toString("UTF-8");
if(!url.endsWith("\r\n")) { if(!url.endsWith("\r\n")) {
conn.write("40 format error\r\n"); conn.write("40 format error\r\n");
@ -53,9 +88,18 @@ public class MainVerticle extends AbstractVerticle {
} else { } else {
url=url.substring(0, url.length()-2); url=url.substring(0, url.length()-2);
System.out.println("url:"+url); System.out.println("url:"+url);
String path=url.substring(18); String path=url.substring(9);
if(path.indexOf('/')>=0) {
path=path.substring(path.indexOf('/'));
} else {
path="";
}
System.out.println("path:"+path); System.out.println("path:"+path);
if (path.startsWith("/post")) { if(path.equals("")) {
conn.write("30 gemini://gemini.lehmann.cx:11965/\r\n");
conn.close();
}
else if(path.startsWith("/post")) {
System.out.println("post"); System.out.println("post");
try { try {
List<Certificate> certs = conn.peerCertificates(); List<Certificate> certs = conn.peerCertificates();
@ -70,26 +114,45 @@ public class MainVerticle extends AbstractVerticle {
conn.write("20 text/gemini\r\n"); conn.write("20 text/gemini\r\n");
conn.write("message was sent\n"); conn.write("message was sent\n");
conn.write("=> /post post another message\n"); conn.write("=> /post post another message\n");
conn2.write(clientHash+":"+message+"\n");
String decodedMessage;
try {
decodedMessage=URLDecoder.decode(message, "utf-8");
}
catch(UnsupportedEncodingException ex) {
decodedMessage="error";
}
String quotedMessage=decodedMessage.replace("\n", "\n ");
for (NetSocket socket:clients) {
socket.write(clientHash+":"+quotedMessage+"\n");
}
} }
} catch (SSLPeerUnverifiedException | CertificateEncodingException | NoSuchAlgorithmException ex) { } catch (SSLPeerUnverifiedException | CertificateEncodingException | NoSuchAlgorithmException ex) {
ex.printStackTrace(); // ex.printStackTrace();
conn.write("60 cert required\r\n");
} }
conn.close(); conn.close();
} else { } else {
System.out.println("conn2"); System.out.println("conn2");
conn2=conn; clients.add(conn);
conn2.write("20 text/gemini\r\n"); conn.write("20 text/gemini\r\n");
conn.write("to post messages, go to\n");
conn.write("=> post post page\n");
conn.write("preferably in a new window\n");
conn.write("chat start\n");
} }
} }
} }
); );
}).listen(1965, server -> { }).listen(11965, server -> {
if (server.succeeded()) { if (server.succeeded()) {
startPromise.complete(); startPromise.complete();
System.out.println("Gemini server started on port 1965"); System.out.println("Gemini server started on port 11965 at "+new Date().toString());
} else { } else {
startPromise.fail(server.cause()); server.cause().printStackTrace();
startPromise.fail(server.cause());
} }
}); });
} }