aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoopyboo32 <49228220+Soopyboo32@users.noreply.github.com>2022-09-29 16:32:35 +0800
committerSoopyboo32 <49228220+Soopyboo32@users.noreply.github.com>2022-09-29 16:32:35 +0800
commit4ccb82559b645c9e06e081b41addb08a8fa45b4c (patch)
treeff58d16ef1181582d46f19fec8d61c5e3d04da17
parentb50b90740e873249904cc4ff780aaaa208160010 (diff)
downloadSoopyV2-4ccb82559b645c9e06e081b41addb08a8fa45b4c.tar.gz
SoopyV2-4ccb82559b645c9e06e081b41addb08a8fa45b4c.tar.bz2
SoopyV2-4ccb82559b645c9e06e081b41addb08a8fa45b4c.zip
meta
-rw-r--r--src/utils/certs/ISRGRootX1.cerbin0 -> 1391 bytes
-rw-r--r--src/utils/letsEncryptCerts.js32
-rw-r--r--src/utils/networkUtils.js188
3 files changed, 220 insertions, 0 deletions
diff --git a/src/utils/certs/ISRGRootX1.cer b/src/utils/certs/ISRGRootX1.cer
new file mode 100644
index 0000000..9d2132e
--- /dev/null
+++ b/src/utils/certs/ISRGRootX1.cer
Binary files differ
diff --git a/src/utils/letsEncryptCerts.js b/src/utils/letsEncryptCerts.js
new file mode 100644
index 0000000..4a639e7
--- /dev/null
+++ b/src/utils/letsEncryptCerts.js
@@ -0,0 +1,32 @@
+// Semisol was here
+let certificates = [
+ "ISRGRootX1.cer"
+] // Certificate names
+const
+ JKeyStore = Java.type("java.security.KeyStore"),
+ JPaths = Java.type("java.nio.file.Paths"),
+ JSystem = Java.type("java.lang.System"),
+ JFiles = Java.type("java.nio.file.Files"),
+ JCertificateFactory = Java.type("java.security.cert.CertificateFactory"),
+ JString = Java.type("java.lang.String"),
+ JByteArrayInputStream = Java.type("java.io.ByteArrayInputStream"),
+ JTrustManagerFactory = Java.type("javax.net.ssl.TrustManagerFactory"),
+ JSSLContext = Java.type("javax.net.ssl.SSLContext")
+let keyStore = JKeyStore.getInstance(JKeyStore.getDefaultType())
+let ksPath = JPaths.get(JSystem.getProperty("java.home"), "lib", "security", "cacerts")
+keyStore.load(JFiles.newInputStream(ksPath), new JString("changeit").toCharArray())
+let cf = JCertificateFactory.getInstance("X.509")
+for (let i of certificates) {
+ let pathStr = `${Config.modulesFolder}/SoopyV2/utils/certs/${i}`
+ let path = JPaths.get(pathStr)
+ let data = JFiles.readAllBytes(path)
+ let cert = cf.generateCertificate(new JByteArrayInputStream(data))
+ keyStore.setCertificateEntry("dev.semisol.letsencryptsupport:" + i, cert)
+}
+let tmf = JTrustManagerFactory.getInstance(JTrustManagerFactory.getDefaultAlgorithm())
+tmf.init(keyStore)
+let sslContext = JSSLContext.getInstance("TLS")
+sslContext.init(null, tmf.getTrustManagers(), null);
+JSSLContext.setDefault(sslContext)
+let socketFactory = sslContext.getSocketFactory()
+export { socketFactory as default };
diff --git a/src/utils/networkUtils.js b/src/utils/networkUtils.js
new file mode 100644
index 0000000..5ac95a4
--- /dev/null
+++ b/src/utils/networkUtils.js
@@ -0,0 +1,188 @@
+import socketFactory from "./letsEncryptCerts"
+
+if (!global.networkUtilsThingSoopyPromise) {
+
+ let jURL = Java.type("java.net.URL")
+ let jStandardCharsets = Java.type("java.nio.charset.StandardCharsets")
+ let jCollectors = Java.type("java.util.stream.Collectors")
+ let jBufferedReader = Java.type("java.io.BufferedReader")
+ let jInputStreamReader = Java.type("java.io.InputStreamReader")
+ let jString = Java.type("java.lang.String")
+ var JHttpsUrlConnection = Java.type('javax.net.ssl.HttpsURLConnection');
+
+ function getUrlContent(theUrl, { userAgent = "Mozilla/5.0", includeConnection = false, postData = undefined } = {}) {
+
+ if (global.soopyv2loggerthing) {
+ global.soopyv2loggerthing.logMessage("Loading API: " + theUrl, 4)
+ }
+
+ // if(theUrl.includes("soopy.dev")){
+ // throw new Error("Testing to ensure the module works when my server is down")
+ // }
+ // Thread.sleep(1000) //simulating high ping
+
+ let conn = new jURL(theUrl).openConnection()
+ if (conn instanceof JHttpsUrlConnection) {
+ conn.setSSLSocketFactory(socketFactory);
+ }
+ conn.setRequestProperty("User-Agent", userAgent)
+
+ if (postData) {
+ conn.setRequestMethod("POST");
+ conn.setRequestProperty("Content-Type", "application/json");
+ conn.setDoOutput(true);
+
+ let jsonInputString = new jString(JSON.stringify(postData))
+
+ let os
+ try {
+ os = conn.getOutputStream()
+ input = jsonInputString.getBytes("utf-8");
+ os.write(input, 0, input.length);
+ } finally {
+ os.close()
+ }
+ }
+
+ let stringData
+
+ if (conn.getResponseCode() < 400) {
+ stringData = new jBufferedReader(
+ new jInputStreamReader(conn.getInputStream(), jStandardCharsets.UTF_8))
+ .lines()
+ .collect(jCollectors.joining("\n"));
+
+ conn.getInputStream().close()
+ } else {
+ stringData = new jBufferedReader(
+ new jInputStreamReader(conn.getErrorStream(), jStandardCharsets.UTF_8))
+ .lines()
+ .collect(jCollectors.joining("\n"));
+
+ conn.getErrorStream().close()
+ }
+
+ if (includeConnection) {
+ return { stringData, connection: conn }
+ }
+
+ return stringData
+ }
+
+ function fetch(url, options = { userAgent: "Mozilla/5.0" }) {
+ let loadedConnection = undefined
+ let loadedString = undefined
+ let loadedJSON = undefined
+
+ let ret = {
+ loadSync() {
+ if (loadedString === undefined) {
+ options.includeConnection = true
+
+ try {
+ let data = getUrlContent(url, options)
+ loadedString = data.stringData
+ loadedConnection = data.connection
+ } catch (e) {
+ errorData = e
+ loadedString = null
+ }
+ }
+
+ return ret
+ },
+ async load(_ifError = false) {
+ if (loadedString === undefined) {
+ options.includeConnection = true
+
+ await new Promise((res, rej) => {
+ pendingRequests.push({
+ callback: (data) => {
+ loadedString = data.stringData
+ loadedConnection = data.connection
+ res()
+ },
+ errcallback: (e) => {
+ rej(e)
+ },
+ url: url,
+ options: options
+ })
+ })
+ }
+ },
+ textSync() {
+ ret.loadSync()
+
+ return loadedString
+ },
+ async text() {
+ await ret.load()
+
+ return loadedString
+ },
+ jsonSync() {
+ if (loadedJSON === undefined) {
+ let str = ret.textSync()
+
+ loadedJSON = JSON.parse(str)
+ }
+
+ return loadedJSON
+ },
+ async json() {
+ if (loadedJSON === undefined) {
+ let str = await ret.text()
+
+ loadedJSON = JSON.parse(str)
+ }
+
+ return loadedJSON
+ },
+ responseCode() {
+ return loadedConnection?.getResponseCode() || -1
+ }
+ }
+ return ret
+ }
+
+ let pendingRequests = []
+ let pendingResolves = []
+ let runningThread = false
+
+ register("tick", () => {
+ try {
+ while (pendingResolves.length > 0) {
+ let [callback, data] = pendingResolves.shift()
+
+ callback(data)
+ }
+ } catch (e) {
+ console.log(JSON.stringify(e, undefined, 2))
+ console.log(e.stack)
+ }
+
+ if (pendingRequests.length > 0 && !runningThread) {
+ runningThread = true
+ new Thread(() => {
+ while (pendingRequests.length > 0) {
+ let req = pendingRequests.shift()
+
+ try {
+ let data = getUrlContent(req.url, req.options)
+
+ pendingResolves.push([req.callback, data])
+ } catch (e) {
+ pendingResolves.push([req.errcallback, e])
+ }
+ }
+
+ runningThread = false
+ }).start()
+ }
+ })
+
+ global.networkUtilsThingSoopyPromise = fetch
+}
+
+export default global.networkUtilsThingSoopyPromise