-
Notifications
You must be signed in to change notification settings - Fork 8
/
dns_blackhole_checker.js
187 lines (160 loc) · 5.38 KB
/
dns_blackhole_checker.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
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
(function() {
/** @enum */
var BlackholeState = {
PENDING: 0,
OPEN: 1,
BLOCKED: 2
};
/**
* A SignalStrategy wrapper that performs DNS blackhole check.
*
* @param {remoting.SignalStrategy} signalStrategy
* @constructor
* @implements {remoting.SignalStrategy}
*/
remoting.DnsBlackholeChecker = function(signalStrategy) {
/** @private */
this.signalStrategy_ = signalStrategy;
this.signalStrategy_.setStateChangedCallback(
this.onWrappedSignalStrategyStateChanged_.bind(this));
/** @type {?function(remoting.SignalStrategy.State):void} @private */
this.onStateChangedCallback_ = null;
/** @private */
this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
/** @private */
this.blackholeState_ = BlackholeState.PENDING;
/** @type {?XMLHttpRequest} @private */
this.xhr_ = null;
};
/**
* @const
* @private
*/
remoting.DnsBlackholeChecker.URL_TO_REQUEST_ =
"https://chromoting-client.talkgadget.google.com/talkgadget/oauth/" +
"chrome-remote-desktop-client";
/**
* @param {?function(remoting.SignalStrategy.State):void} onStateChangedCallback
* Callback to call on state change.
*/
remoting.DnsBlackholeChecker.prototype.setStateChangedCallback = function(
onStateChangedCallback) {
this.onStateChangedCallback_ = onStateChangedCallback;
};
/**
* @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
* incoming messages.
*/
remoting.DnsBlackholeChecker.prototype.setIncomingStanzaCallback =
function(onIncomingStanzaCallback) {
this.signalStrategy_.setIncomingStanzaCallback(onIncomingStanzaCallback);
};
/** @return {remoting.SignalStrategy.Type} The signal strategy type. */
remoting.DnsBlackholeChecker.prototype.getType = function() {
return this.signalStrategy_.getType();
};
/**
* @param {string} server
* @param {string} username
* @param {string} authToken
*/
remoting.DnsBlackholeChecker.prototype.connect = function(server,
username,
authToken) {
base.debug.assert(this.onStateChangedCallback_ != null);
this.signalStrategy_.connect(server, username, authToken);
this.xhr_ =
remoting.xhr.get(remoting.DnsBlackholeChecker.URL_TO_REQUEST_,
this.onHttpRequestDone_.bind(this));
};
remoting.DnsBlackholeChecker.prototype.getState = function() {
return this.state_;
};
remoting.DnsBlackholeChecker.prototype.getError = function() {
if (this.blackholeState_ == BlackholeState.BLOCKED) {
return remoting.Error.NOT_AUTHORIZED;
}
return this.signalStrategy_.getError();
};
remoting.DnsBlackholeChecker.prototype.getJid = function() {
base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED);
return this.signalStrategy_.getJid();
};
remoting.DnsBlackholeChecker.prototype.dispose = function() {
if (this.xhr_) {
this.xhr_.abort();
this.xhr_ = null;
}
base.dispose(this.signalStrategy_);
this.setState_(remoting.SignalStrategy.State.CLOSED);
};
/**
* @param {remoting.LogToServer} logToServer The LogToServer instance for the
* connection.
*/
remoting.DnsBlackholeChecker.prototype.sendConnectionSetupResults = function(
logToServer) {
this.signalStrategy_.sendConnectionSetupResults(logToServer)
};
/** @param {string} message */
remoting.DnsBlackholeChecker.prototype.sendMessage = function(message) {
base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED);
this.signalStrategy_.sendMessage(message);
};
/** @param {remoting.SignalStrategy.State} state */
remoting.DnsBlackholeChecker.prototype.onWrappedSignalStrategyStateChanged_ =
function(state) {
switch (this.blackholeState_) {
case BlackholeState.PENDING:
// Stay in HANDSHAKE state if we are still waiting for the HTTP request.
if (state != remoting.SignalStrategy.State.CONNECTED) {
this.setState_(state);
}
break;
case BlackholeState.OPEN:
this.setState_(state);
break;
case BlackholeState.BLOCKED:
// In case DNS blackhole is active the external state stays FAILED.
break;
}
};
/**
* @param {XMLHttpRequest} xhr
* @private
*/
remoting.DnsBlackholeChecker.prototype.onHttpRequestDone_ = function(xhr) {
this.xhr_ = null;
if (xhr.status >= 200 && xhr.status <= 299) {
console.log("DNS blackhole check succeeded.");
this.blackholeState_ = BlackholeState.OPEN;
if (this.signalStrategy_.getState() ==
remoting.SignalStrategy.State.CONNECTED) {
this.setState_(remoting.SignalStrategy.State.CONNECTED);
}
} else {
console.error("DNS blackhole check failed: " + xhr.status + " " +
xhr.statusText + ". Response URL: " + xhr.responseURL +
". Response Text: " + xhr.responseText);
this.blackholeState_ = BlackholeState.BLOCKED;
base.dispose(this.signalStrategy_);
this.setState_(remoting.SignalStrategy.State.FAILED);
}
}
/**
* @param {remoting.SignalStrategy.State} newState
* @private
*/
remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
if (this.state_ != newState) {
this.state_ = newState;
this.onStateChangedCallback_(this.state_);
}
};
}());