chemicaljs 1.2.4 → 1.3.0

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.
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "chemical-example-vite",
3
+ "description": "This is an example project for Chemical.",
4
+ "type": "module",
5
+ "scripts": {
6
+ "start": "node index.js",
7
+ "dev": "vite",
8
+ "build": "vite build"
9
+ },
10
+ "dependencies": {
11
+ "chemicaljs": "^1.2.5",
12
+ "express": "^4.19.2",
13
+ "preact": "^10.22.0"
14
+ },
15
+ "devDependencies": {
16
+ "@preact/preset-vite": "^2.8.2",
17
+ "vite": "^5.3.1"
18
+ }
19
+ }
@@ -0,0 +1,14 @@
1
+ export function App() {
2
+ async function keyDown(e) {
3
+ if (e.key == "Enter" && window.chemicalLoaded && e.target.value) {
4
+ window.location = await window.chemicalEncode(e.target.value)
5
+ }
6
+ }
7
+
8
+ return (
9
+ <>
10
+ <h1>Chemical Example</h1>
11
+ <input onKeyDown={keyDown} placeholder="Enter URL" />
12
+ </>
13
+ )
14
+ }
@@ -0,0 +1,4 @@
1
+ import { render } from "preact"
2
+ import { App } from "./app.jsx"
3
+
4
+ render(<App />, document.getElementById("app"))
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from "vite"
2
+ import preact from "@preact/preset-vite"
3
+ import { ChemicalPluginVite } from "chemicaljs"
4
+
5
+ export default defineConfig({
6
+ plugins: [
7
+ preact(),
8
+ ChemicalPluginVite()
9
+ ],
10
+ })
package/index.js CHANGED
@@ -132,4 +132,139 @@ class ChemicalServer {
132
132
  }
133
133
  }
134
134
 
135
- export { ChemicalServer };
135
+ const ChemicalPluginVite = (options) => ({
136
+ name: "chemical-plugin-vite",
137
+ configureServer(server) {
138
+ if (options) {
139
+ if (typeof options !== "object" || Array.isArray(options)) {
140
+ options = {}
141
+ console.error("Error: ChemicalServer options invalid.")
142
+ }
143
+ } else {
144
+ options = {}
145
+ }
146
+
147
+ if (options.uv == undefined) {
148
+ options.uv = true;
149
+ }
150
+
151
+ if (options.scramjet == undefined) {
152
+ options.scramjet = true;
153
+ }
154
+
155
+ if (options.rammerhead == undefined) {
156
+ options.rammerhead = true;
157
+ }
158
+
159
+ const rh = createRammerhead()
160
+
161
+ const rammerheadScopes = [
162
+ "/rammerhead.js",
163
+ "/hammerhead.js",
164
+ "/transport-worker.js",
165
+ "/task.js",
166
+ "/iframe-task.js",
167
+ "/worker-hammerhead.js",
168
+ "/messaging",
169
+ "/sessionexists",
170
+ "/deletesession",
171
+ "/newsession",
172
+ "/editsession",
173
+ "/needpassword",
174
+ "/syncLocalStorage",
175
+ "/api/shuffleDict"
176
+ ]
177
+
178
+ const rammerheadSession = /^\/[a-z0-9]{32}/
179
+
180
+ const shouldRouteRh = req => {
181
+ const url = new URL(req.url, "http://0.0.0.0")
182
+ return (
183
+ rammerheadScopes.includes(url.pathname) ||
184
+ rammerheadSession.test(url.pathname)
185
+ )
186
+ }
187
+
188
+ const routeRhRequest = (req, res) => {
189
+ rh.emit("request", req, res)
190
+ }
191
+
192
+ const routeRhUpgrade = (req, socket, head) => {
193
+ rh.emit("upgrade", req, socket, head)
194
+ }
195
+
196
+ const app = express();
197
+ app.get("/chemical.js", async function (req, res) {
198
+ let chemicalMain = await readFileSync(resolve(import.meta.dirname, "client/chemical.js"), "utf8");
199
+
200
+ if (options.default) {
201
+ if (["uv", "rammerhead", "scramjet"].includes(options.default)) {
202
+ chemicalMain = `const defaultService = "${options.default}";\n\n` + chemicalMain
203
+ } else {
204
+ chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain
205
+ console.error("Error: Chemical default option invalid.")
206
+ }
207
+ } else {
208
+ chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
209
+ }
210
+
211
+ chemicalMain = "const uvEnabled = " + String(options.uv) + ";\n" + chemicalMain
212
+ chemicalMain = "const scramjetEnabled = " + String(options.scramjet) + ";\n" + chemicalMain
213
+ chemicalMain = "const rammerheadEnabled = " + String(options.rammerhead) + ";\n" + chemicalMain
214
+
215
+ res.type("application/javascript");
216
+ return res.send(chemicalMain);
217
+ });
218
+ app.get("/chemical.sw.js", async function (req, res) {
219
+ let chemicalSW = await readFileSync(resolve(import.meta.dirname, "client/chemical.sw.js"), "utf8");
220
+
221
+ chemicalSW = "const uvEnabled = " + String(options.uv) + ";\n" + chemicalSW
222
+ chemicalSW = "const scramjetEnabled = " + String(options.scramjet) + ";\n" + chemicalSW
223
+ chemicalSW = "const rammerheadEnabled = " + String(options.rammerhead) + ";\n" + chemicalSW
224
+
225
+ res.type("application/javascript");
226
+ return res.send(chemicalSW);
227
+ });
228
+ app.use(express.static(resolve(import.meta.dirname, "client")));
229
+ app.use("/baremux/", express.static(baremuxPath));
230
+ app.use("/epoxy/", express.static(epoxyPath));
231
+ if (options.uv) {
232
+ app.use("/uv/", express.static(resolve(import.meta.dirname, "config/uv")));
233
+ app.use("/uv/", express.static(uvPath));
234
+ }
235
+ if (options.scramjet) {
236
+ app.use("/scramjet/", express.static(resolve(import.meta.dirname, "config/scramjet")));
237
+ app.use("/scramjet/", express.static(scramjetPath));
238
+ }
239
+
240
+ server.middlewares.use(app);
241
+
242
+ server.middlewares.use((req, res, next) => {
243
+ if (options.rammerhead && shouldRouteRh(req)) {
244
+ routeRhRequest(req, res);
245
+ } else {
246
+ next();
247
+ }
248
+ });
249
+
250
+ const upgraders = server.httpServer.listeners("upgrade")
251
+
252
+ for (const upgrader of upgraders) {
253
+ server.httpServer.off("upgrade", upgrader)
254
+ }
255
+
256
+ server.httpServer.on("upgrade", (req, socket, head) => {
257
+ if (req.url && req.url.endsWith("/wisp/")) {
258
+ wisp.routeRequest(req, socket, head)
259
+ } else if (options.rammerhead && shouldRouteRh(req)) {
260
+ routeRhUpgrade(req, socket, head)
261
+ } else {
262
+ for (const upgrader of upgraders) {
263
+ upgrader(req, socket, head)
264
+ }
265
+ }
266
+ })
267
+ }
268
+ })
269
+
270
+ export { ChemicalServer, ChemicalPluginVite };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chemicaljs",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "description": "Easily create your own web proxy with no experience required.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "dependencies": {
14
14
  "@mercuryworkshop/bare-mux": "^2.0.1",
15
- "@mercuryworkshop/epoxy-transport": "^2.1.2",
15
+ "@mercuryworkshop/epoxy-transport": "^2.1.3",
16
16
  "@mercuryworkshop/scramjet": "^1.0.2",
17
17
  "@titaniumnetwork-dev/ultraviolet": "^3.2.1",
18
18
  "express": "^4.19.2",