commit 5dd19adec63e216b659aa60ea7af57f678a657ba from: Omar Polo date: Sat Oct 16 11:44:38 2021 UTC use com.omarpolo/gemini I've released a gemini library (based on the deleted Request.java) so use it. commit - c7b163126c640f439a8cca24ec4ea916c47db419 commit + 5dd19adec63e216b659aa60ea7af57f678a657ba blob - b3890fc326491cafe5f119eb9bce756c494acd20 (mode 644) blob + /dev/null --- Makefile +++ /dev/null @@ -1,14 +0,0 @@ -JAVAC = javac - -CLASSES = java/com/omarpolo/gemini/Request.class - -.PHONY: all clean - -all: ${CLASSES} - -clean: - find java -type f -iname '*.class' -exec rm {} + - -.SUFFIXES: .java .class -.java.class: - ${JAVAC} $< blob - 4707374513c2755776e8ee2f9a520d283ce92555 blob + 134a53705de48cb6333ae8fb121ca8a8832e04e0 --- README.md +++ README.md @@ -1,9 +1,3 @@ # blog -## How to build - -Issue - - make - -then do the usual clojure stuff. +just another random tech blog blob - 88dd338d51d66669a13af58ddebe22d7ef4542fa blob + 08cbbae20f3cc79c3a2356f4ac265945cc09fbd5 --- deps.edn +++ deps.edn @@ -2,9 +2,10 @@ ring {:mvn/version "1.8.0"} commonmark-hiccup {:mvn/version "0.1.0"} org.clojure/data.xml {:mvn/version "0.2.0-alpha6"} - com.omarpolo/gemtext {:mvn/version "0.1.5"}} + com.omarpolo/gemtext {:mvn/version "0.1.5"} + com.omarpolo/gemini {:mvn/version "0.2.0"}} - :paths ["classes" "src" "resources" "java"] + :paths ["classes" "src" "resources"] :aliases {:act {:main-opts ["-m" "blog.core"]}}} blob - 195ef9647afca4eccc7f8d34f99c6d95aea998a6 (mode 644) blob + /dev/null --- java/com/omarpolo/gemini/Request.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.omarpolo.gemini; - -import javax.net.ssl.*; -import java.io.*; -import java.net.*; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.cert.X509Certificate; -import java.util.Collections; -import java.util.NoSuchElementException; -import java.util.Scanner; - -public class Request implements AutoCloseable { - - private final BufferedReader in; - private final PrintWriter out; - private final SSLSocket sock; - - private final int code; - private final String meta; - - public static class DummyManager extends X509ExtendedTrustManager { - - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) { - } - - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) { - } - - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - } - - public static class MalformedResponse extends Exception {} - - public Request(String uri) throws IOException, MalformedResponse, URISyntaxException { - this(new URI(uri)); - } - - public Request(URI url) throws IOException, MalformedResponse { - this(url.getHost(), url.getPort(), url.toString() + "\r\n"); - } - - public Request(String host, int port, String req) throws IOException, MalformedResponse { - if (port == -1) { - port = 1965; - } - - sock = connect(host, port); - - var outStream = sock.getOutputStream(); - out = new PrintWriter( - new BufferedWriter(new OutputStreamWriter(outStream))); - - out.print(req); - out.flush(); - - var inStream = sock.getInputStream(); - in = new BufferedReader(new InputStreamReader(inStream)); - - var reply = in.readLine(); - - if (reply.length() > 1027) { - throw new MalformedResponse(); - } - - var s = new Scanner(new StringReader(reply)); - try { - code = s.nextInt(); - s.skip(" "); - meta = s.nextLine(); - } catch (NoSuchElementException e) { - throw new MalformedResponse(); - } - } - - public SSLSocket connect(String host, int port) throws IOException { - try { - var params = new SSLParameters(); - params.setServerNames(Collections.singletonList(new SNIHostName(host))); - - var ctx = SSLContext.getInstance("TLS"); - ctx.init(null, new DummyManager[]{new DummyManager()}, new SecureRandom()); - var factory = (SSLSocketFactory) ctx.getSocketFactory(); - - var socket = (SSLSocket) factory.createSocket(host, port); - socket.setSSLParameters(params); - socket.startHandshake(); - return socket; - } - catch (NoSuchAlgorithmException | KeyManagementException e) { - throw new RuntimeException("Unexpected failure", e); - } - } - - public int getCode() { - return code; - } - - public String getMeta() { - return meta; - } - - public BufferedReader body() { - return in; - } - - public void close() throws IOException { - in.close(); - out.close(); - sock.close(); - } -} blob - 4cb2fa898d7ff0d5655872a37d9e76215d70b46e blob + 50cc57fe3b4b5c475b1cb8c3aee7bbf41637c25d --- src/blog/core.clj +++ src/blog/core.clj @@ -243,8 +243,7 @@ Disallow: /cgi/man/ (defn antenna "Ping antenna" [] - (net-gemini/head "warmedal.se" 1965 - (str "gemini://warmedal.se/~antenna/submit?gemini://gemini.omarpolo.com"))) + (net-gemini/ping-antenna "gemini://gemini.omarpolo.com")) (defn stop-jetty [] (.stop @j) blob - 758ee354614329ac319d96cd0b8c6a0c97636e55 blob + 466daedcbb0582cc46bc1d36c63bf33740d6b487 --- src/blog/net_gemini.clj +++ src/blog/net_gemini.clj @@ -1,13 +1,15 @@ (ns blog.net-gemini - (:import (com.omarpolo.gemini Request))) + (:require [gemini.core :as gemini])) -(defn head [host port req] - (with-open [res (Request. host port (str req "\r\n"))] - {:code (.getCode res) - :meta (.getMeta res)})) +(def antenna-uri "gemini://warmedal.se/~antenna") +(defn ping-antenna + "Sends the given `url` to antenna." + [url] + (gemini/with-request [req {:request (str antenna-uri "/submit?" url) + :follow-redirects? true}] + (gemini/body-as-string! req))) + (comment - (with-open [res (Request. "gemini://localhost/index.gmi")] - {:code (.getCode res) - :meta (.getMeta res)}) + (ping-antenna "gemini://gemini.omarpolo.com") )