mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Remove "active" property of proxyPairs
Use their existence in the proxy pair list to indicate they are active.
This commit is contained in:
parent
d186fcd401
commit
789285e0df
2 changed files with 25 additions and 42 deletions
|
@ -85,9 +85,6 @@ class ProxyPair {
|
||||||
prepareDataChannel(channel) {
|
prepareDataChannel(channel) {
|
||||||
channel.onopen = () => {
|
channel.onopen = () => {
|
||||||
log('WebRTC DataChannel opened!');
|
log('WebRTC DataChannel opened!');
|
||||||
if (!this.active) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
snowflake.ui.setActive(true);
|
snowflake.ui.setActive(true);
|
||||||
// This is the point when the WebRTC datachannel is done, so the next step
|
// This is the point when the WebRTC datachannel is done, so the next step
|
||||||
// is to establish websocket to the server.
|
// is to establish websocket to the server.
|
||||||
|
@ -179,17 +176,13 @@ class ProxyPair {
|
||||||
if (this.webrtcIsReady()) {
|
if (this.webrtcIsReady()) {
|
||||||
this.client.close();
|
this.client.close();
|
||||||
}
|
}
|
||||||
this.client = null;
|
|
||||||
if (this.peerConnOpen()) {
|
if (this.peerConnOpen()) {
|
||||||
this.pc.close();
|
this.pc.close();
|
||||||
}
|
}
|
||||||
this.pc = null;
|
|
||||||
if (this.relayIsReady()) {
|
if (this.relayIsReady()) {
|
||||||
this.relay.close();
|
this.relay.close();
|
||||||
}
|
}
|
||||||
this.relay = null;
|
|
||||||
this.onCleanup();
|
this.onCleanup();
|
||||||
this.active = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flush() {
|
flush() {
|
||||||
|
@ -253,8 +246,6 @@ ProxyPair.prototype.relay = null; // websocket
|
||||||
|
|
||||||
ProxyPair.prototype.timer = 0;
|
ProxyPair.prototype.timer = 0;
|
||||||
|
|
||||||
ProxyPair.prototype.active = false; // Whether serving a client.
|
|
||||||
|
|
||||||
ProxyPair.prototype.flush_timeout_id = null;
|
ProxyPair.prototype.flush_timeout_id = null;
|
||||||
|
|
||||||
ProxyPair.prototype.onCleanup = null;
|
ProxyPair.prototype.onCleanup = null;
|
||||||
|
|
|
@ -43,10 +43,6 @@ class Snowflake {
|
||||||
// Initialize WebRTC PeerConnection, which requires beginning the signalling
|
// Initialize WebRTC PeerConnection, which requires beginning the signalling
|
||||||
// process. |pollBroker| automatically arranges signalling.
|
// process. |pollBroker| automatically arranges signalling.
|
||||||
beginWebRTC() {
|
beginWebRTC() {
|
||||||
log('ProxyPair Slots: ' + this.proxyPairs.length);
|
|
||||||
log('Snowflake IDs: ' + (this.proxyPairs.map(function(p) {
|
|
||||||
return p.id;
|
|
||||||
})).join(' | '));
|
|
||||||
this.pollBroker();
|
this.pollBroker();
|
||||||
return this.pollInterval = setInterval((() => {
|
return this.pollInterval = setInterval((() => {
|
||||||
return this.pollBroker();
|
return this.pollBroker();
|
||||||
|
@ -58,13 +54,13 @@ class Snowflake {
|
||||||
pollBroker() {
|
pollBroker() {
|
||||||
var msg, pair, recv;
|
var msg, pair, recv;
|
||||||
// Poll broker for clients.
|
// Poll broker for clients.
|
||||||
pair = this.nextAvailableProxyPair();
|
pair = this.makeProxyPair();
|
||||||
if (!pair) {
|
if (!pair) {
|
||||||
log('At client capacity.');
|
log('At client capacity.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
log('Polling broker..');
|
||||||
// Do nothing until a new proxyPair is available.
|
// Do nothing until a new proxyPair is available.
|
||||||
pair.active = true;
|
|
||||||
msg = 'Polling for client ... ';
|
msg = 'Polling for client ... ';
|
||||||
if (this.retries > 0) {
|
if (this.retries > 0) {
|
||||||
msg += '[retries: ' + this.retries + ']';
|
msg += '[retries: ' + this.retries + ']';
|
||||||
|
@ -72,35 +68,23 @@ class Snowflake {
|
||||||
this.ui.setStatus(msg);
|
this.ui.setStatus(msg);
|
||||||
recv = this.broker.getClientOffer(pair.id);
|
recv = this.broker.getClientOffer(pair.id);
|
||||||
recv.then((desc) => {
|
recv.then((desc) => {
|
||||||
if (pair.active) {
|
if (!this.receiveOffer(pair, desc)) {
|
||||||
if (!this.receiveOffer(pair, desc)) {
|
return pair.close();
|
||||||
return pair.active = false;
|
|
||||||
}
|
|
||||||
//set a timeout for channel creation
|
|
||||||
return setTimeout((() => {
|
|
||||||
if (!pair.webrtcIsReady()) {
|
|
||||||
log('proxypair datachannel timed out waiting for open');
|
|
||||||
pair.close();
|
|
||||||
return pair.active = false;
|
|
||||||
}
|
|
||||||
}), 20000); // 20 second timeout
|
|
||||||
}
|
}
|
||||||
|
//set a timeout for channel creation
|
||||||
|
return setTimeout((() => {
|
||||||
|
if (!pair.webrtcIsReady()) {
|
||||||
|
log('proxypair datachannel timed out waiting for open');
|
||||||
|
return pair.close();
|
||||||
|
}
|
||||||
|
}), 20000); // 20 second timeout
|
||||||
}, function() {
|
}, function() {
|
||||||
return pair.active = false;
|
//on error, close proxy pair
|
||||||
|
return pair.close();
|
||||||
});
|
});
|
||||||
return this.retries++;
|
return this.retries++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the first ProxyPair that's available to connect.
|
|
||||||
nextAvailableProxyPair() {
|
|
||||||
if (this.proxyPairs.length < this.config.connectionsPerClient) {
|
|
||||||
return this.makeProxyPair(this.relayAddr);
|
|
||||||
}
|
|
||||||
return this.proxyPairs.find(function(pp) {
|
|
||||||
return !pp.active;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
receiveOffer(pair, desc) {
|
receiveOffer(pair, desc) {
|
||||||
var e, offer, sdp;
|
var e, offer, sdp;
|
||||||
try {
|
try {
|
||||||
|
@ -127,19 +111,27 @@ class Snowflake {
|
||||||
return pair.pc.setLocalDescription(sdp).catch(fail);
|
return pair.pc.setLocalDescription(sdp).catch(fail);
|
||||||
};
|
};
|
||||||
fail = function() {
|
fail = function() {
|
||||||
pair.active = false
|
pair.close();
|
||||||
return dbg('webrtc: Failed to create or set Answer');
|
return dbg('webrtc: Failed to create or set Answer');
|
||||||
};
|
};
|
||||||
return pair.pc.createAnswer().then(next).catch(fail);
|
return pair.pc.createAnswer().then(next).catch(fail);
|
||||||
}
|
}
|
||||||
|
|
||||||
makeProxyPair(relay) {
|
makeProxyPair() {
|
||||||
|
if (this.proxyPairs.length >= this.config.connectionsPerClient) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
var pair;
|
var pair;
|
||||||
pair = new ProxyPair(relay, this.rateLimit, this.config.pcConfig);
|
pair = new ProxyPair(this.relayAddr, this.rateLimit, this.config.pcConfig);
|
||||||
this.proxyPairs.push(pair);
|
this.proxyPairs.push(pair);
|
||||||
|
|
||||||
|
log('Snowflake IDs: ' + (this.proxyPairs.map(function(p) {
|
||||||
|
return p.id;
|
||||||
|
})).join(' | '));
|
||||||
|
|
||||||
pair.onCleanup = () => {
|
pair.onCleanup = () => {
|
||||||
var ind;
|
var ind;
|
||||||
// Delete from the list of active proxy pairs.
|
// Delete from the list of proxy pairs.
|
||||||
ind = this.proxyPairs.indexOf(pair);
|
ind = this.proxyPairs.indexOf(pair);
|
||||||
if (ind > -1) {
|
if (ind > -1) {
|
||||||
return this.proxyPairs.splice(ind, 1);
|
return this.proxyPairs.splice(ind, 1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue