-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrq-proxy.ts
More file actions
79 lines (70 loc) · 2.64 KB
/
rq-proxy.ts
File metadata and controls
79 lines (70 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import IRulesDataSource from "./components/interfaces/rules-data-source";
import Proxy from "./lib/proxy";
import { ProxyConfig } from "./types";
import RulesHelper from "./utils/helpers/rules-helper";
import ProxyMiddlewareManager from "./components/proxy-middleware";
import ILoggerService from "./components/interfaces/logger-service";
import IInitialState from "./components/interfaces/state";
import GlobalStateProvider, {State} from "./components/proxy-middleware/middlewares/state";
console.log("Testing2");
class RQProxy {
proxy: any;
proxyMiddlewareManager!: ProxyMiddlewareManager;
rulesHelper: RulesHelper;
loggerService: ILoggerService;
globalState: State;
constructor(
proxyConfig: ProxyConfig,
rulesDataSource: IRulesDataSource,
loggerService: ILoggerService,
initialGlobalState?: IInitialState
) {
this.initProxy(proxyConfig);
this.rulesHelper = new RulesHelper(rulesDataSource);
this.loggerService = loggerService;
this.globalState = GlobalStateProvider.initInstance(initialGlobalState?.sharedState ?? {}, initialGlobalState?.variables ?? {});
}
initProxy = (proxyConfig: ProxyConfig) => {
console.log("initProxy");
console.log(proxyConfig);
// @ts-ignore
this.proxy = new Proxy();
if(proxyConfig.onCARegenerated) {
this.proxy.onCARegenerated(proxyConfig.onCARegenerated)
}
// console.log(this.proxy);
this.proxy.listen(
{
port: proxyConfig.port,
sslCaDir: proxyConfig.certPath,
host: "0.0.0.0",
},
(err: any) => {
console.log("Proxy Listen");
if(err) {
console.log(err);
} else {
console.log("Proxy Started");
this.proxyMiddlewareManager = new ProxyMiddlewareManager(this.proxy, proxyConfig, this.rulesHelper, this.loggerService, null);
this.proxyMiddlewareManager.init();
}
}
);
// For Testing //
this.proxy.onRequest(function(ctx, callback) {
if (ctx.clientToProxyRequest.headers.host == 'example.com') {
ctx.use(Proxy.gunzip);
ctx.onResponseData(function(ctx, chunk, callback) {
chunk = new Buffer("<h1>Hello There</h1>");
return callback(null, chunk);
});
}
return callback();
});
//
}
doSomething = () => {
console.log("do something");
}
}
export default RQProxy;