Repository navigation
Expand file tree
/
Copy pathswitcher.ts
More file actions
149 lines (128 loc) · 3.74 KB
/
Copy pathswitcher.ts
File metadata and controls
149 lines (128 loc) · 3.74 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
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
import { EventEmitter } from 'events';
import * as util from 'util';
import { WSProcessor } from './wsprocessor';
import { TCPProcessor } from './tcpprocessor';
import { getLogger } from 'pinus-logger';
import * as net from 'net';
import * as tls from 'tls';
import { TlsOptions } from 'tls';
import * as WebSocket from 'ws';
import { TcpSocket } from './tcpsocket';
import { IHybridSocket } from './IHybridSocket';
let logger = getLogger('pinus', __filename);
let HTTP_METHODS = [
'GET', 'POST', 'DELETE', 'PUT', 'HEAD'
];
let ST_STARTED = 1;
let ST_CLOSED = 2;
let DEFAULT_TIMEOUT = 90;
export interface HybridSwitcherOptions
{
closeMethod ?: 'end';
timeout ?: number;
setNoDelay ?: boolean;
ssl ?: TlsOptions;
}
export interface IHybridSwitcher
{
on(evt : 'connection' , listener : (socket : IHybridSocket)=>void):void;
}
/**
* Switcher for tcp and websocket protocol
*
* @param {Object} server tcp server instance from node.js net module
*/
export class HybridSwitcher extends EventEmitter implements IHybridSwitcher
{
server: net.Server;
wsprocessor: WSProcessor;
tcpprocessor: TCPProcessor;
id: number;
timeout: number;
setNoDelay: boolean;
state: number;
constructor(server : net.Server, opts : HybridSwitcherOptions)
{
super();
this.server = server;
this.wsprocessor = new WSProcessor();
this.tcpprocessor = new TCPProcessor(opts.closeMethod);
this.id = 1;
this.timeout = (opts.timeout || DEFAULT_TIMEOUT) * 1000;
this.setNoDelay = opts.setNoDelay;
if (!opts.ssl)
{
this.server.on('connection', this.newSocket.bind(this));
} else
{
this.server.on('secureConnection', this.newSocket.bind(this));
this.server.on('clientError', function (e, tlsSo)
{
logger.warn('an ssl error occured before handshake established: ', e);
tlsSo.destroy();
});
}
this.wsprocessor.on('connection', this.emit.bind(this, 'connection'));
this.tcpprocessor.on('connection', this.emit.bind(this, 'connection'));
this.state = ST_STARTED;
};
newSocket(socket : net.Socket)
{
if (this.state !== ST_STARTED)
{
return;
}
socket.setTimeout(this.timeout, function ()
{
logger.warn('connection is timeout without communication, the remote ip is %s && port is %s',
socket.remoteAddress, socket.remotePort);
socket.destroy();
});
let self = this;
socket.once('data', (data)=>
{
// FIXME: handle incomplete HTTP method
if (isHttp(data))
{
this.processHttp(self.wsprocessor, socket, data);
} else
{
if (!!self.setNoDelay)
{
socket.setNoDelay(true);
}
this.processTcp(self.tcpprocessor, socket, data);
}
});
};
close()
{
if (this.state !== ST_STARTED)
{
return;
}
this.state = ST_CLOSED;
this.wsprocessor.close();
this.tcpprocessor.close();
};
private processHttp(processor: WSProcessor, socket: net.Socket, data: Buffer)
{
processor.add(socket, data);
}
private processTcp(processor: TCPProcessor, socket: net.Socket, data: Buffer)
{
processor.add(socket, data);
}
}
let isHttp = function (data : Buffer)
{
let head = data.toString('utf8', 0, 4);
for (let i = 0, l = HTTP_METHODS.length; i < l; i++)
{
if (head.indexOf(HTTP_METHODS[i]) === 0)
{
return true;
}
}
return false;
};