@cloud-cli/px 3.0.1 → 3.1.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/dist/index.d.ts CHANGED
@@ -1,13 +1,19 @@
1
1
  import { init } from '@cloud-cli/cli';
2
2
  import { DomainAndTarget, Domain } from './proxy-manager.js';
3
3
  import { ProxyEntry } from './proxy.js';
4
+ declare function add(options: ProxyEntry): Promise<unknown>;
5
+ declare function remove(options: DomainAndTarget): Promise<unknown>;
6
+ declare function list(): Promise<ProxyEntry[]>;
7
+ declare function get(options: Domain): Promise<ProxyEntry[]>;
8
+ declare function domains(): Promise<string[]>;
9
+ declare function reload(): Promise<void>;
4
10
  declare const _default: {
5
- add(options: ProxyEntry): Promise<unknown>;
6
- remove(options: DomainAndTarget): Promise<unknown>;
7
- list(): Promise<ProxyEntry[]>;
8
- get(options: Domain): Promise<ProxyEntry[]>;
9
- reload(): void;
10
- domains(): Promise<string[]>;
11
- [init](): Promise<void>;
11
+ add: typeof add;
12
+ remove: typeof remove;
13
+ list: typeof list;
14
+ get: typeof get;
15
+ domains: typeof domains;
16
+ reload: typeof reload;
17
+ [init]: typeof reload;
12
18
  };
13
19
  export default _default;
package/dist/index.js CHANGED
@@ -4,32 +4,28 @@ import { ProxyManager } from './proxy-manager.js';
4
4
  import { ProxyEntry, ProxyServer } from './proxy.js';
5
5
  const px = new ProxyServer();
6
6
  const manager = new ProxyManager();
7
- export default {
8
- async add(options) {
9
- const proxy = await manager.addProxy(options);
10
- px.reload();
11
- return proxy;
12
- },
13
- async remove(options) {
14
- const removed = await manager.removeProxy(options);
15
- px.reload();
16
- return removed;
17
- },
18
- list() {
19
- return manager.getProxyList();
20
- },
21
- get(options) {
22
- return manager.getProxyListForDomain(options);
23
- },
24
- reload() {
25
- this[init]();
26
- },
27
- domains() {
28
- return manager.getDomainList();
29
- },
30
- async [init]() {
31
- Resource.use(new SQLiteDriver());
32
- await Resource.create(ProxyEntry);
33
- return px.start();
34
- },
35
- };
7
+ async function add(options) {
8
+ const proxy = await manager.addProxy(options);
9
+ px.reload();
10
+ return proxy;
11
+ }
12
+ async function remove(options) {
13
+ const removed = await manager.removeProxy(options);
14
+ px.reload();
15
+ return removed;
16
+ }
17
+ function list() {
18
+ return manager.getProxyList();
19
+ }
20
+ function get(options) {
21
+ return manager.getProxyListForDomain(options);
22
+ }
23
+ function domains() {
24
+ return manager.getDomainList();
25
+ }
26
+ async function reload() {
27
+ Resource.use(new SQLiteDriver());
28
+ await Resource.create(ProxyEntry);
29
+ return px.start();
30
+ }
31
+ export default { add, remove, list, get, domains, reload, [init]: reload };
@@ -35,12 +35,18 @@ export class ProxyManager {
35
35
  if (!options.target) {
36
36
  return reject(targetNotSpecifiedError);
37
37
  }
38
- const proxy = await Resource.find(ProxyEntry, new Query().where('domain').is(options.domain).where('target').is(options.target));
38
+ const query = new Query().where('domain').is(options.domain);
39
+ if (options.target) {
40
+ query.where('target').is(options.target);
41
+ }
42
+ const proxy = await Resource.find(ProxyEntry, query);
39
43
  if (!proxy.length) {
40
44
  return reject(notFoundError);
41
45
  }
42
46
  try {
43
- await proxy[0].remove();
47
+ for (const p of proxy) {
48
+ await p.remove();
49
+ }
44
50
  resolve(true);
45
51
  }
46
52
  catch (error) {
package/dist/proxy.d.ts CHANGED
@@ -20,7 +20,7 @@ export declare class ProxyServer {
20
20
  reload(): void;
21
21
  protected loadCertificates(): Promise<void>;
22
22
  protected loadTargets(): Promise<void>;
23
- protected serveRequest(req: IncomingMessage, res: ServerResponse): void;
23
+ protected serveRequest(req: IncomingMessage, res: ServerResponse, insecure?: boolean): void;
24
24
  protected getSslOptions(): {
25
25
  SNICallback(domain: string, cb: Function): any;
26
26
  };
package/dist/proxy.js CHANGED
@@ -45,7 +45,7 @@ export class ProxyServer {
45
45
  this.reload();
46
46
  this.servers.forEach((server) => server.close());
47
47
  this.servers = [
48
- createHttpServer((req, res) => this.serveRequest(req, res)).listen(80),
48
+ createHttpServer((req, res) => this.serveRequest(req, res, true)).listen(80),
49
49
  createHttpsServer(this.getSslOptions(), (req, res) => this.serveRequest(req, res)).listen(443),
50
50
  ];
51
51
  }
@@ -73,7 +73,7 @@ export class ProxyServer {
73
73
  this.targets = {};
74
74
  targets.forEach((entry) => (this.targets[entry.domain] = entry));
75
75
  }
76
- serveRequest(req, res) {
76
+ serveRequest(req, res, insecure = false) {
77
77
  const origin = getOrigin(req);
78
78
  const proxyEntry = this.targets[origin?.hostname];
79
79
  if (!origin.hostname || !proxyEntry) {
@@ -81,6 +81,13 @@ export class ProxyServer {
81
81
  res.end();
82
82
  return;
83
83
  }
84
+ if (proxyEntry.redirect && insecure) {
85
+ const newURL = new URL(req.url, `https://${req.headers.host}`);
86
+ res.setHeader('Location', String(newURL));
87
+ res.writeHead(301, 'HTTPS is better');
88
+ res.end();
89
+ return;
90
+ }
84
91
  const target = proxyEntry.target;
85
92
  const url = new URL(req.url, target);
86
93
  const isCorsPreflight = req.method === 'OPTIONS' && proxyEntry.cors;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/px",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "description": "Reverse Proxy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",