107 lines
3.6 KiB
Java
107 lines
3.6 KiB
Java
package cx.lehmann.gemini.gemini;
|
|
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.cert.Certificate;
|
|
import java.security.cert.CertificateEncodingException;
|
|
import java.security.cert.X509Certificate;
|
|
import java.util.List;
|
|
|
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
|
|
|
import io.vertx.core.AbstractVerticle;
|
|
import io.vertx.core.Promise;
|
|
import io.vertx.core.http.ClientAuth;
|
|
import io.vertx.core.net.NetServerOptions;
|
|
import io.vertx.core.net.NetSocket;
|
|
//import io.vertx.core.net.OpenSSLEngineOptions;
|
|
import io.vertx.core.net.PemKeyCertOptions;
|
|
import io.vertx.core.net.PemTrustOptions;
|
|
import io.vertx.core.net.TrustOptions;
|
|
|
|
import javax.xml.bind.DatatypeConverter;
|
|
|
|
//import sun.security.x509.X509Cert;
|
|
|
|
public class MainVerticle extends AbstractVerticle {
|
|
|
|
NetSocket conn2=null;
|
|
|
|
@Override
|
|
public void start(Promise<Void> startPromise) throws Exception {
|
|
NetServerOptions options=new NetServerOptions();
|
|
|
|
String certPath="c:/temp/cert.pem";
|
|
|
|
// TrustOptions trustOptions=new PemTrustOptions();
|
|
options.setPemKeyCertOptions(new PemKeyCertOptions()
|
|
.setCertPath(certPath)
|
|
.setKeyPath(certPath))
|
|
.setSsl(true)
|
|
// .setTrustOptions(trustOptions)
|
|
// .setOpenSslEngineOptions(new OpenSSLEngineOptions())
|
|
.setClientAuth(ClientAuth.REQUIRED);
|
|
|
|
|
|
vertx.createNetServer(options).connectHandler(conn -> {
|
|
conn.handler(event -> {
|
|
System.out.println("accepted connection:"+conn.remoteAddress());
|
|
String url=event.toString("UTF-8");
|
|
if(!url.endsWith("\r\n")) {
|
|
conn.write("40 format error\r\n");
|
|
conn.close();
|
|
} else {
|
|
url=url.substring(0, url.length()-2);
|
|
System.out.println("url:"+url);
|
|
String path=url.substring(18);
|
|
System.out.println("path:"+path);
|
|
if (path.startsWith("/post")) {
|
|
System.out.println("post");
|
|
try {
|
|
List<Certificate> certs = conn.peerCertificates();
|
|
Certificate cert = certs.get(0);
|
|
X509Certificate certX509 = (X509Certificate) cert;
|
|
String clientHash = getThumbprint(certX509);
|
|
System.out.println("cert");
|
|
if (!url.contains("?")) {
|
|
conn.write("10 please enter your chat message\r\n");
|
|
} else {
|
|
String message = url.substring(url.indexOf('?') + 1);
|
|
conn.write("20 text/gemini\r\n");
|
|
conn.write("message was sent\n");
|
|
conn.write("=> /post post another message\n");
|
|
conn2.write(clientHash+":"+message+"\n");
|
|
}
|
|
} catch (SSLPeerUnverifiedException | CertificateEncodingException | NoSuchAlgorithmException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
conn.close();
|
|
} else {
|
|
System.out.println("conn2");
|
|
conn2=conn;
|
|
conn2.write("20 text/gemini\r\n");
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}).listen(1965, server -> {
|
|
if (server.succeeded()) {
|
|
startPromise.complete();
|
|
System.out.println("Gemini server started on port 1965");
|
|
} else {
|
|
startPromise.fail(server.cause());
|
|
}
|
|
});
|
|
}
|
|
|
|
private static String getThumbprint(X509Certificate cert)
|
|
throws NoSuchAlgorithmException, CertificateEncodingException {
|
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
byte[] der = cert.getEncoded();
|
|
md.update(der);
|
|
byte[] digest = md.digest();
|
|
String digestHex = DatatypeConverter.printHexBinary(digest);
|
|
return digestHex.toLowerCase();
|
|
}
|
|
}
|