chemicaljs 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,17 @@ import { ChemicalServer } from "chemicaljs";
19
19
  const chemical = new ChemicalServer();
20
20
  ```
21
21
 
22
+ You can pass options to disable proxy services and set the default service.
23
+
24
+ ```js
25
+ const chemical = new ChemicalServer({
26
+ default: "rammerhead",
27
+ uv: true,
28
+ scramjet: false,
29
+ rammerhead: true
30
+ });
31
+ ```
32
+
22
33
  3. Use `chemical.app` which is an express app. You may need to import express for certain APIs.
23
34
 
24
35
  ```js
@@ -74,7 +85,7 @@ If you want to set the wisp server to an external server just change the `wisp`
74
85
  await window.chemicalEncode("https://example.com")
75
86
  ```
76
87
 
77
- Optional: Change service to `ultraviolet`, `scramjet`, or `rammerhead`.
88
+ Optional: Change service to `ultraviolet`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
78
89
 
79
90
  ```js
80
91
  await window.chemicalEncode("https://example.com", "rammerhead")
@@ -116,7 +127,6 @@ Below is a simple example of a simple input that redirects to the encoded URL wh
116
127
 
117
128
  Some features that may come in the future are:
118
129
 
119
- - Server options to disable services.
120
130
  - Easy client components.
121
131
  - Easy tab cloaking components.
122
132
 
@@ -174,24 +174,30 @@ function rammerheadEncode(baseUrl) {
174
174
  })
175
175
  }
176
176
 
177
- async function chemicalEncode(url, service = "ultraviolet") {
177
+ async function chemicalEncode(url, service = defaultService) {
178
178
  switch (service) {
179
- case "ultraviolet":
180
- return (
181
- window.location.origin +
182
- window.__uv$config.prefix +
183
- window.__uv$config.encodeUrl(url)
184
- );
179
+ case "uv":
180
+ if (uvEnabled) {
181
+ return (
182
+ window.location.origin +
183
+ window.__uv$config.prefix +
184
+ window.__uv$config.encodeUrl(url)
185
+ );
186
+ }
185
187
  break;
186
188
  case "rammerhead":
187
- return window.location.origin + (await rammerheadEncode(url));
189
+ if (rammerheadEnabled) {
190
+ return window.location.origin + (await rammerheadEncode(url));
191
+ }
188
192
  break;
189
193
  case "scramjet":
190
- return (
191
- window.location.origin +
192
- window.__scramjet$config.prefix +
193
- window.__scramjet$config.codec.encode(url)
194
- );
194
+ if (scramjetEnabled) {
195
+ return (
196
+ window.location.origin +
197
+ window.__scramjet$config.prefix +
198
+ window.__scramjet$config.codec.encode(url)
199
+ );
200
+ }
195
201
  break;
196
202
  }
197
203
  }
@@ -225,10 +231,14 @@ async function loadScript(src) {
225
231
 
226
232
  (async () => {
227
233
  await loadScript("/baremux/index.js");
228
- await loadScript("/uv/uv.bundle.js");
229
- await loadScript("/uv/uv.config.js");
230
- await loadScript("/scramjet/scramjet.codecs.js");
231
- await loadScript("/scramjet/scramjet.config.js");
234
+ if (uvEnabled) {
235
+ await loadScript("/uv/uv.bundle.js");
236
+ await loadScript("/uv/uv.config.js");
237
+ }
238
+ if (scramjetEnabled) {
239
+ await loadScript("/scramjet/scramjet.codecs.js");
240
+ await loadScript("/scramjet/scramjet.config.js");
241
+ }
232
242
  await registerSW();
233
243
  chemicalLoaded = true;
234
244
  window.dispatchEvent(new Event("chemicalLoaded"));
@@ -1,19 +1,35 @@
1
- importScripts("/libcurl/index.js");
2
- importScripts("/uv/uv.bundle.js");
3
- importScripts("/uv/uv.config.js");
4
- importScripts(__uv$config.sw || "/uv/uv.sw.js");
1
+ if (uvEnabled) {
2
+ importScripts("/uv/uv.bundle.js");
3
+ importScripts("/uv/uv.config.js");
4
+ importScripts(__uv$config.sw || "/uv/uv.sw.js");
5
+ }
6
+ if (scramjetEnabled) {
7
+ importScripts("/scramjet/scramjet.codecs.js");
8
+ importScripts("/scramjet/scramjet.config.js");
9
+ importScripts(__scramjet$config.bundle || "/scramjet/scramjet.bundle.js");
10
+ importScripts(__scramjet$config.worker || "/scramjet/scramjet.worker.js");
11
+ }
5
12
 
6
13
  Object.defineProperty(self, "crossOriginIsolated", { value: true }); // Firefox fix
7
14
 
8
- const uv = new UVServiceWorker();
15
+ let uv, scramjet;
16
+
17
+ if (uvEnabled) {
18
+ uv = new UVServiceWorker();
19
+ }
20
+ if (scramjetEnabled) {
21
+ scramjet = new ScramjetServiceWorker();
22
+ }
9
23
 
10
24
  self.addEventListener("fetch", (event) => {
11
25
  event.respondWith(
12
26
  (async () => {
13
- if (uv.route(event)) {
27
+ if (uvEnabled && uv.route(event)) {
14
28
  return await uv.fetch(event);
29
+ } else if (scramjetEnabled && scramjet.route(event)) {
30
+ return await scramjet.fetch(event);
15
31
  }
16
32
  return await fetch(event.request);
17
33
  })(),
18
34
  );
19
- });
35
+ });
@@ -0,0 +1,9 @@
1
+ self.__scramjet$config = {
2
+ prefix: "/~/sj/",
3
+ codec: self.__scramjet$codecs.plain,
4
+ config: "/scramjet/scramjet.config.js",
5
+ bundle: "/scramjet/scramjet.bundle.js",
6
+ worker: "/scramjet/scramjet.worker.js",
7
+ client: "/scramjet/scramjet.client.js",
8
+ codecs: "/scramjet/scramjet.codecs.js",
9
+ };
@@ -1,5 +1,5 @@
1
1
  self.__uv$config = {
2
- prefix: "/~/",
2
+ prefix: "/~/uv/",
3
3
  encodeUrl: Ultraviolet.codec.xor.encode,
4
4
  decodeUrl: Ultraviolet.codec.xor.decode,
5
5
  handler: "/uv/uv.handler.js",
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { resolve } from "node:path";
2
+ import { readFileSync } from "node:fs";
2
3
  import { createServer } from "node:http";
3
4
  import express from "express";
4
5
  import wisp from "wisp-server-node";
@@ -9,7 +10,28 @@ import { scramjetPath } from "@mercuryworkshop/scramjet";
9
10
  import createRammerhead from "rammerhead/src/server/index.js";
10
11
 
11
12
  class ChemicalServer {
12
- constructor() {
13
+ constructor(options) {
14
+ if (options) {
15
+ if (typeof options !== "object" || Array.isArray(options)) {
16
+ options = {}
17
+ console.error("Error: ChemicalServer options invalid.")
18
+ }
19
+ } else {
20
+ options = {}
21
+ }
22
+
23
+ if (!options.uv) {
24
+ options.uv = true;
25
+ }
26
+
27
+ if (!options.scramjet) {
28
+ options.scramjet = true;
29
+ }
30
+
31
+ if (!options.rammerhead) {
32
+ options.rammerhead = true;
33
+ }
34
+
13
35
  const rh = createRammerhead()
14
36
 
15
37
  const rammerheadScopes = [
@@ -49,13 +71,48 @@ class ChemicalServer {
49
71
 
50
72
  this.server = createServer();
51
73
  this.app = express();
74
+ this.app.get("/chemical.js", async function (req, res) {
75
+ let chemicalMain = await readFileSync(resolve(import.meta.dirname, "client/chemical.js"), "utf8");
76
+
77
+ if (options.default) {
78
+ if (["uv", "rammerhead", "scramjet"].includes(options.default)) {
79
+ chemicalMain = `const defaultService = "${options.default}";\n\n` + chemicalMain
80
+ } else {
81
+ chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain
82
+ console.error("Error: Chemical default option invalid.")
83
+ }
84
+ }
85
+
86
+ chemicalMain = "const uvEnabled = " + String(options.uv) + ";\n" + chemicalMain
87
+ chemicalMain = "const scramjetEnabled = " + String(options.scramjet) + ";\n" + chemicalMain
88
+ chemicalMain = "const rammerheadEnabled = " + String(options.rammerhead) + ";\n" + chemicalMain
89
+
90
+ res.type("application/javascript");
91
+ return res.send(chemicalMain);
92
+ });
93
+ this.app.get("/chemical.sw.js", async function (req, res) {
94
+ let chemicalSW = await readFileSync(resolve(import.meta.dirname, "client/chemical.sw.js"), "utf8");
95
+
96
+ chemicalSW = "const uvEnabled = " + String(options.uv) + ";\n" + chemicalSW
97
+ chemicalSW = "const scramjetEnabled = " + String(options.scramjet) + ";\n" + chemicalSW
98
+ chemicalSW = "const rammerheadEnabled = " + String(options.rammerhead) + ";\n" + chemicalSW
99
+
100
+ res.type("application/javascript");
101
+ return res.send(chemicalSW);
102
+ });
52
103
  this.app.use(express.static(resolve(import.meta.dirname, "client")));
53
104
  this.app.use("/baremux/", express.static(baremuxPath));
54
105
  this.app.use("/epoxy/", express.static(epoxyPath));
55
- this.app.use("/uv/", express.static(uvPath));
56
- this.app.use("/scramjet/", express.static(scramjetPath));
106
+ if (options.uv) {
107
+ this.app.use("/uv/", express.static(resolve(import.meta.dirname, "config/uv")));
108
+ this.app.use("/uv/", express.static(uvPath));
109
+ }
110
+ if (options.scramjet) {
111
+ this.app.use("/scramjet/", express.static(resolve(import.meta.dirname, "config/scramjet")));
112
+ this.app.use("/scramjet/", express.static(scramjetPath));
113
+ }
57
114
  this.server.on("request", (req, res) => {
58
- if (shouldRouteRh(req)) {
115
+ if (options.rammerhead && shouldRouteRh(req)) {
59
116
  routeRhRequest(req, res);
60
117
  } else {
61
118
  this.app(req, res);
@@ -64,7 +121,7 @@ class ChemicalServer {
64
121
  this.server.on("upgrade", (req, socket, head) => {
65
122
  if (req.url && req.url.endsWith("/wisp/")) {
66
123
  wisp.routeRequest(req, socket, head);
67
- } else if (shouldRouteRh(req)) {
124
+ } else if (options.rammerhead && shouldRouteRh(req)) {
68
125
  routeRhUpgrade(req, socket, head);
69
126
  } else {
70
127
  socket.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chemicaljs",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Easily create your own web proxy with no experience required.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,7 +16,7 @@
16
16
  "@mercuryworkshop/scramjet": "^1.0.2",
17
17
  "@titaniumnetwork-dev/ultraviolet": "^3.2.0",
18
18
  "express": "^4.19.2",
19
- "rammerhead": "file:rammerhead.tgz",
19
+ "rammerhead": "https://github.com/chemicaljs/chemical/raw/main/rammerhead.tgz",
20
20
  "wisp-server-node": "^1.1.0"
21
21
  }
22
22
  }