/
lookup-service.js
144 lines (122 loc) · 4.8 KB
/
lookup-service.js
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
// -*- tab-width:4; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*
* Copyright (c) 2011 Dhruv Matani
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
var util = require('util');
var SRV = require('dns-srv');
var dutil = require('./dutil.js');
var events = require('events');
var path = require('path');
var filename = path.basename(path.normalize(__filename));
var log = require('./log.js').getLogger(filename);
/* The XMPPLookupService tries to resolve the host name to connect to
* in various ways. The order in which it tries is as follows:
*
* 1. Try to directly connect to a host if the route parameter is passed
*
* 2. Try to do an SRV record lookup for _xmpp-client._tcp record on the
* target domain passed in as domain_name.
*
* A 'connect' event is raised on the passed 'socket' if connection succeeds.
* If all attempts fail, an 'error' event is raised on the 'socket'.
*
* Expects:
* --------
*
* stream: An object that MUST have the following fields: 'to' and MAY have
* the following fields: 'route'
*
*/
function XMPPLookupService(port, stream) {
this._domain_name = stream.to;
this._port = port;
this._route = stream.route;
var _special = {
"gmail.com": "talk.google.com",
"chat.facebook.com": "chat.facebook.com"
};
if (_special.hasOwnProperty(this._domain_name)) {
if (!this._route) {
this._route = {
protocol: "xmpp",
host: _special[this._domain_name],
port: this._port
};
}
}
}
util.inherits(XMPPLookupService, events.EventEmitter);
dutil.copy(XMPPLookupService.prototype, {
connect: function(socket) {
var self = this;
// We first save all the user's handlers.
//
// NOTE: NEVER re-attach OR trigger event handlers in a
// nextTick() function. ALWAYS do it in the same tick since
// there might be pending events and the semantics might need
// a sequential ordering on the delivery of these events (for
// example the 'connect' and the 'data' events need to come in
// the order they arrived).
function _on_socket_connect(e) {
log.trace('Connection to %s succeeded', self._domain_name);
// Re-trigger the connect event.
self.emit('connect', e);
}
function try_connect_route() {
// First just connect to the server if this._route is defined.
if (self._route) {
log.trace('try_connect_route - %s:%s', self._route.host, self._route.port);
// socket.setTimeout(10000);
socket.connect(self._route.port, self._route.host);
}
else {
// Trigger the 'error' event.
socket.emit('error');
}
}
function try_connect_SRV_lookup() {
log.trace('try_connect_SRV_lookup - %s, %s',self._domain_name, self._port);
// Then try a normal SRV lookup.
var attempt = SRV.connect(socket, ['_xmpp-client._tcp'],
self._domain_name, self._port);
}
function give_up_trying_to_connect(e) {
log.warn('Giving up connection attempts to %s',self._domain_name);
// Trigger the error event.
self.emit('error', 'host-unknown');
}
var cstates = [
try_connect_route,
try_connect_SRV_lookup,
give_up_trying_to_connect
];
function _on_socket_error(e) {
var next = cstates.shift();
next(e);
}
socket.on('error', _on_socket_error);
socket.on('connect', _on_socket_connect);
// Start the avalanche.
_on_socket_error();
}
});
exports.LookupService = XMPPLookupService;