mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Implemented new broker messages for browser proxy
This commit is contained in:
parent
c4ae64905b
commit
b4b538a17f
3 changed files with 33 additions and 23 deletions
|
@ -33,7 +33,7 @@ HTTP 200 OK
|
||||||
HTTP 200 OK
|
HTTP 200 OK
|
||||||
|
|
||||||
{
|
{
|
||||||
Status: "no proxies"
|
Status: "no match"
|
||||||
}
|
}
|
||||||
|
|
||||||
3) If the request is malformed:
|
3) If the request is malformed:
|
||||||
|
|
|
@ -46,10 +46,16 @@ class Broker {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (xhr.status) {
|
switch (xhr.status) {
|
||||||
case Broker.STATUS.OK:
|
case Broker.CODE.OK:
|
||||||
return fulfill(xhr.responseText); // Should contain offer.
|
var response = JSON.parse(xhr.responseText);
|
||||||
case Broker.STATUS.GATEWAY_TIMEOUT:
|
if (response.Status == Broker.STATUS.MATCH) {
|
||||||
|
return fulfill(response.Offer); // Should contain offer.
|
||||||
|
} else if (response.Status == Broker.STATUS.TIMEOUT) {
|
||||||
return reject(Broker.MESSAGE.TIMEOUT);
|
return reject(Broker.MESSAGE.TIMEOUT);
|
||||||
|
} else {
|
||||||
|
log('Broker ERROR: Unexpected ' + response.Status);
|
||||||
|
return reject(Broker.MESSAGE.UNEXPECTED);
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
log('Broker ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
|
log('Broker ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
|
||||||
snowflake.ui.setStatus(' failure. Please refresh.');
|
snowflake.ui.setStatus(' failure. Please refresh.');
|
||||||
|
@ -57,7 +63,8 @@ class Broker {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this._xhr = xhr; // Used by spec to fake async Broker interaction
|
this._xhr = xhr; // Used by spec to fake async Broker interaction
|
||||||
return this._postRequest(id, xhr, 'proxy', id);
|
var data = {"Version": "1.0", "Sid": id}
|
||||||
|
return this._postRequest(xhr, 'proxy', JSON.stringify(data));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,26 +80,24 @@ class Broker {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (xhr.status) {
|
switch (xhr.status) {
|
||||||
case Broker.STATUS.OK:
|
case Broker.CODE.OK:
|
||||||
dbg('Broker: Successfully replied with answer.');
|
dbg('Broker: Successfully replied with answer.');
|
||||||
return dbg(xhr.responseText);
|
return dbg(xhr.responseText);
|
||||||
case Broker.STATUS.GONE:
|
|
||||||
return dbg('Broker: No longer valid to reply with answer.');
|
|
||||||
default:
|
default:
|
||||||
dbg('Broker ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
|
dbg('Broker ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
|
||||||
return snowflake.ui.setStatus(' failure. Please refresh.');
|
return snowflake.ui.setStatus(' failure. Please refresh.');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return this._postRequest(id, xhr, 'answer', JSON.stringify(answer));
|
var data = {"Version": "1.0", "Sid": id, "Answer": JSON.stringify(answer)};
|
||||||
|
return this._postRequest(xhr, 'answer', JSON.stringify(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
// urlSuffix for the broker is different depending on what action
|
// urlSuffix for the broker is different depending on what action
|
||||||
// is desired.
|
// is desired.
|
||||||
_postRequest(id, xhr, urlSuffix, payload) {
|
_postRequest(xhr, urlSuffix, payload) {
|
||||||
var err;
|
var err;
|
||||||
try {
|
try {
|
||||||
xhr.open('POST', this.url + urlSuffix);
|
xhr.open('POST', this.url + urlSuffix);
|
||||||
xhr.setRequestHeader('X-Session-ID', id);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
err = error;
|
err = error;
|
||||||
/*
|
/*
|
||||||
|
@ -109,10 +114,15 @@ class Broker {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Broker.STATUS = {
|
Broker.CODE = {
|
||||||
OK: 200,
|
OK: 200,
|
||||||
GONE: 410,
|
BAD_REQUEST: 400,
|
||||||
GATEWAY_TIMEOUT: 504
|
INTERNAL_SERVER_ERROR: 500
|
||||||
|
};
|
||||||
|
|
||||||
|
Broker.STATUS = {
|
||||||
|
MATCH: "client match",
|
||||||
|
TIMEOUT: "no match"
|
||||||
};
|
};
|
||||||
|
|
||||||
Broker.MESSAGE = {
|
Broker.MESSAGE = {
|
||||||
|
|
|
@ -35,8 +35,8 @@ describe('Broker', function() {
|
||||||
// fake successful request and response from broker.
|
// fake successful request and response from broker.
|
||||||
spyOn(b, '_postRequest').and.callFake(function() {
|
spyOn(b, '_postRequest').and.callFake(function() {
|
||||||
b._xhr.readyState = b._xhr.DONE;
|
b._xhr.readyState = b._xhr.DONE;
|
||||||
b._xhr.status = Broker.STATUS.OK;
|
b._xhr.status = Broker.CODE.OK;
|
||||||
b._xhr.responseText = 'fake offer';
|
b._xhr.responseText = '{"Status":"client match","Offer":"fake offer"}';
|
||||||
return b._xhr.onreadystatechange();
|
return b._xhr.onreadystatechange();
|
||||||
});
|
});
|
||||||
poll = b.getClientOffer();
|
poll = b.getClientOffer();
|
||||||
|
@ -46,7 +46,7 @@ describe('Broker', function() {
|
||||||
expect(desc).toEqual('fake offer');
|
expect(desc).toEqual('fake offer');
|
||||||
return done();
|
return done();
|
||||||
}).catch(function() {
|
}).catch(function() {
|
||||||
fail('should not reject on Broker.STATUS.OK');
|
fail('should not reject on Broker.CODE.OK');
|
||||||
return done();
|
return done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -57,14 +57,15 @@ describe('Broker', function() {
|
||||||
// fake timed-out request from broker
|
// fake timed-out request from broker
|
||||||
spyOn(b, '_postRequest').and.callFake(function() {
|
spyOn(b, '_postRequest').and.callFake(function() {
|
||||||
b._xhr.readyState = b._xhr.DONE;
|
b._xhr.readyState = b._xhr.DONE;
|
||||||
b._xhr.status = Broker.STATUS.GATEWAY_TIMEOUT;
|
b._xhr.status = Broker.CODE.OK;
|
||||||
|
b._xhr.responseText = '{"Status":"no match"}';
|
||||||
return b._xhr.onreadystatechange();
|
return b._xhr.onreadystatechange();
|
||||||
});
|
});
|
||||||
poll = b.getClientOffer();
|
poll = b.getClientOffer();
|
||||||
expect(poll).not.toBeNull();
|
expect(poll).not.toBeNull();
|
||||||
expect(b._postRequest).toHaveBeenCalled();
|
expect(b._postRequest).toHaveBeenCalled();
|
||||||
return poll.then(function(desc) {
|
return poll.then(function(desc) {
|
||||||
fail('should not fulfill on Broker.STATUS.GATEWAY_TIMEOUT');
|
fail('should not fulfill with "Status: no match"');
|
||||||
return done();
|
return done();
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).toBe(Broker.MESSAGE.TIMEOUT);
|
expect(err).toBe(Broker.MESSAGE.TIMEOUT);
|
||||||
|
@ -101,7 +102,7 @@ describe('Broker', function() {
|
||||||
var b = new Broker('fake');
|
var b = new Broker('fake');
|
||||||
spyOn(b, '_postRequest');
|
spyOn(b, '_postRequest');
|
||||||
b.sendAnswer('fake id', 123);
|
b.sendAnswer('fake id', 123);
|
||||||
expect(b._postRequest).toHaveBeenCalledWith('fake id', jasmine.any(Object), 'answer', '123');
|
expect(b._postRequest).toHaveBeenCalledWith(jasmine.any(Object), 'answer', '{"Version":"1.0","Sid":"fake id","Answer":"123"}');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('POST XMLHttpRequests to the broker', function() {
|
it('POST XMLHttpRequests to the broker', function() {
|
||||||
|
@ -110,9 +111,8 @@ describe('Broker', function() {
|
||||||
spyOn(b._xhr, 'open');
|
spyOn(b._xhr, 'open');
|
||||||
spyOn(b._xhr, 'setRequestHeader');
|
spyOn(b._xhr, 'setRequestHeader');
|
||||||
spyOn(b._xhr, 'send');
|
spyOn(b._xhr, 'send');
|
||||||
b._postRequest(0, b._xhr, 'test', 'data');
|
b._postRequest(b._xhr, 'test', 'data');
|
||||||
expect(b._xhr.open).toHaveBeenCalled();
|
expect(b._xhr.open).toHaveBeenCalled();
|
||||||
expect(b._xhr.setRequestHeader).toHaveBeenCalled();
|
|
||||||
expect(b._xhr.send).toHaveBeenCalled();
|
expect(b._xhr.send).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue