forked from WalletConnect/relay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.ts
195 lines (169 loc) · 5.77 KB
/
http.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { EventEmitter } from "events";
import fastify, { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
import helmet from "@fastify/helmet";
import ws from "@fastify/websocket";
import pino, { Logger } from "pino";
import { getDefaultLoggerOptions, generateChildLogger } from "@walletconnect/logger";
import client from "prom-client";
import { verifyJWT } from "@walletconnect/relay-auth";
import { getAuthFromRequest, assertType, HttpError } from "./utils";
import { RedisService } from "./redis";
import { WebSocketService } from "./ws";
import { NotificationService } from "./notification";
import { HttpServiceConfig, PostSubscribeRequest, GetWebsocketHandshakeRequest } from "./types";
import {
METRICS_DURACTION_BUCKETS,
METRICS_PREFIX,
SERVER_BEAT_INTERVAL,
SERVER_CONTEXT,
SERVER_EVENTS,
} from "./constants";
import { SubscriptionService } from "./subscription";
import { MessageService } from "./message";
export class HttpService {
public events = new EventEmitter();
public app: FastifyInstance;
public logger: Logger;
public redis: RedisService;
public ws: WebSocketService;
public message: MessageService;
public subscription: SubscriptionService;
public notification: NotificationService;
public context = SERVER_CONTEXT;
public metrics;
constructor(public config: HttpServiceConfig) {
let conf = getDefaultLoggerOptions({ level: config.logger });
if (config.prettyPrint) {
conf = Object.assign(conf, {
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});
}
const logger = pino(conf);
this.config = config;
this.app = fastify({ logger });
this.logger = generateChildLogger(logger, this.context);
this.metrics = this.setMetrics();
this.redis = new RedisService(this, this.logger);
this.ws = new WebSocketService(this, this.logger);
this.message = new MessageService(this, this.logger);
this.subscription = new SubscriptionService(this, this.logger);
this.notification = new NotificationService(this, this.logger);
this.initialize();
}
public on(event: string, listener: any): void {
this.events.on(event, listener);
}
public once(event: string, listener: any): void {
this.events.once(event, listener);
}
public off(event: string, listener: any): void {
this.events.off(event, listener);
}
public removeListener(event: string, listener: any): void {
this.events.removeListener(event, listener);
}
// ---------- Private ----------------------------------------------- //
private initialize(): void {
this.logger.trace(`Initialized`);
this.registerApi();
this.setBeatInterval();
}
public async validateProjectId(
req: FastifyRequest<GetWebsocketHandshakeRequest>,
res: FastifyReply,
) {
try {
if (this.config.requiredParams.projectId) {
assertType(req, "query", "object");
assertType(req.query, "projectId");
// TODO: actually validate the ID when Cerbrus is available
}
return;
} catch (e) {
res
.status((e as HttpError).statusCode)
.send({ message: `Error: ${(e as HttpError).message}` });
}
}
public async validateAuth(req: FastifyRequest<GetWebsocketHandshakeRequest>, res: FastifyReply) {
try {
const jwt = getAuthFromRequest(req);
if (typeof jwt === "undefined") return; // no jwt provided
if (await verifyJWT(jwt)) return; // jwt is valid
throw new HttpError("failed to validate the provided header", 401);
} catch (e) {
res
.status((e as HttpError).statusCode)
.send({ message: `Error: ${(e as HttpError).message}` });
}
}
private registerApi() {
this.app.register(helmet);
this.app.register(ws);
this.app.addHook(
"preValidation",
async (request: FastifyRequest<GetWebsocketHandshakeRequest>, reply: FastifyReply) => {
if (request.raw.url !== "/") return;
await this.validateProjectId(request, reply);
await this.validateAuth(request, reply);
},
);
const server = this; //eslint-disable-line
this.app.register(async function(fastify) {
server.ws.websocketHandler(fastify);
});
this.app.get("/health", (_, res) => {
res.status(204).send();
});
this.app.get("/hello", (_, res) => {
this.metrics.hello.inc();
res
.status(200)
.send(`Hello World, this is Relay Server v${this.config.version}@${this.config.gitHash}`);
});
this.app.get("/metrics", (_, res) => {
res.headers({ "Content-Type": this.metrics.register.contentType });
this.metrics.register.metrics().then((result) => {
res.status(200).send(result);
});
});
this.app.post<PostSubscribeRequest>("/subscribe", async (req, res) => {
try {
assertType(req, "body", "object");
assertType(req.body, "topic");
assertType(req.body, "webhook");
await this.notification.register(req.body.topic, req.body.webhook);
res.status(200).send({ success: true });
} catch (e) {
res
.status((e as HttpError).statusCode)
.send({ message: `Error: ${(e as HttpError).message}` });
}
});
}
private setMetrics() {
const register = new client.Registry();
client.collectDefaultMetrics({
prefix: METRICS_PREFIX,
register,
gcDurationBuckets: METRICS_DURACTION_BUCKETS,
});
const metrics = {
register,
hello: new client.Counter({
registers: [register],
name: `${this.context}_hello_counter`,
help: "shows how much the /hello has been called",
}),
};
return metrics;
}
private setBeatInterval() {
setInterval(() => this.events.emit(SERVER_EVENTS.beat), SERVER_BEAT_INTERVAL);
}
}