forked from rejetto/hfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.ts
83 lines (69 loc) · 2.56 KB
/
connections.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
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
import { Socket } from 'net'
import events from './events'
import { Context } from 'koa'
export class Connection {
readonly started = new Date()
sent = 0 // socket-scoped, not request-scoped
got = 0
outSpeed?: number
inSpeed?: number
ctx?: Context
country?: string
private _cachedIp?: string
[rest:symbol]: any // let other modules add extra data, but using symbols to avoid name collision
constructor(readonly socket: Socket) {
all.push(this)
socket.on('close', () => {
all.splice(all.indexOf(this), 1)
events.emit('connectionClosed', this)
})
events.emit('connection', this)
}
get ip() { // prioritize ctx.ip as it supports proxies, but fallback for when ctx is not yet available
return this.ctx?.ip || (this._cachedIp ??= normalizeIp(this.socket.remoteAddress||''))
}
get secure() {
return (this.socket as any).server.cert > ''
}
}
export function normalizeIp(ip: string) {
return ip.replace(/^::ffff:/,'') // simplify ipv6-mapped addresses
}
const all: Connection[] = []
export function newConnection(socket: Socket) {
new Connection(socket)
}
export function getConnections(): Readonly<typeof all> {
return all
}
export function socket2connection(socket: Socket) {
return all.find(x => // socket exposed by Koa is TLSSocket which encapsulates simple Socket, and I've found no way to access it for simple comparison
x.socket.remotePort === socket.remotePort // but we can still match them because IP:PORT is key
&& x.socket.remoteAddress === socket.remoteAddress )
}
export function getConnection(ctx: Context) {
return ctx.state.connection
}
export function updateConnectionForCtx(ctx: Context ) {
const conn = getConnection(ctx)
if (conn)
updateConnection(conn, { ctx })
return conn
}
export function updateConnection(conn: Connection, change: Partial<Connection>, changeState?: true | Partial<Context['state']>) {
const { ctx } = conn
if (changeState && ctx) {
Object.assign(ctx.state, changeState)
Object.assign(change, { ctx })
}
Object.assign(conn, change)
events.emit('connectionUpdated', conn, change)
}
export function disconnect(what: Context | Socket, debugLog='') {
if ('socket' in what)
what = what.socket
if (debugLog)
console.debug("disconnection:", debugLog, what.remoteAddress)
return what.destroy()
}