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
|
||||
|
||||
{
|
||||
Status: "no proxies"
|
||||
Status: "no match"
|
||||
}
|
||||
|
||||
3) If the request is malformed:
|
||||
|
|
|
@ -46,10 +46,16 @@ class Broker {
|
|||
return;
|
||||
}
|
||||
switch (xhr.status) {
|
||||
case Broker.STATUS.OK:
|
||||
return fulfill(xhr.responseText); // Should contain offer.
|
||||
case Broker.STATUS.GATEWAY_TIMEOUT:
|
||||
return reject(Broker.MESSAGE.TIMEOUT);
|
||||
case Broker.CODE.OK:
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
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);
|
||||
} else {
|
||||
log('Broker ERROR: Unexpected ' + response.Status);
|
||||
return reject(Broker.MESSAGE.UNEXPECTED);
|
||||
}
|
||||
default:
|
||||
log('Broker ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
|
||||
snowflake.ui.setStatus(' failure. Please refresh.');
|
||||
|
@ -57,7 +63,8 @@ class Broker {
|
|||
}
|
||||
};
|
||||
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;
|
||||
}
|
||||
switch (xhr.status) {
|
||||
case Broker.STATUS.OK:
|
||||
case Broker.CODE.OK:
|
||||
dbg('Broker: Successfully replied with answer.');
|
||||
return dbg(xhr.responseText);
|
||||
case Broker.STATUS.GONE:
|
||||
return dbg('Broker: No longer valid to reply with answer.');
|
||||
default:
|
||||
dbg('Broker ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
|
||||
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
|
||||
// is desired.
|
||||
_postRequest(id, xhr, urlSuffix, payload) {
|
||||
_postRequest(xhr, urlSuffix, payload) {
|
||||
var err;
|
||||
try {
|
||||
xhr.open('POST', this.url + urlSuffix);
|
||||
xhr.setRequestHeader('X-Session-ID', id);
|
||||
} catch (error) {
|
||||
err = error;
|
||||
/*
|
||||
|
@ -109,10 +114,15 @@ class Broker {
|
|||
|
||||
}
|
||||
|
||||
Broker.STATUS = {
|
||||
Broker.CODE = {
|
||||
OK: 200,
|
||||
GONE: 410,
|
||||
GATEWAY_TIMEOUT: 504
|
||||
BAD_REQUEST: 400,
|
||||
INTERNAL_SERVER_ERROR: 500
|
||||
};
|
||||
|
||||
Broker.STATUS = {
|
||||
MATCH: "client match",
|
||||
TIMEOUT: "no match"
|
||||
};
|
||||
|
||||
Broker.MESSAGE = {
|
||||
|
|
|
@ -35,8 +35,8 @@ describe('Broker', function() {
|
|||
// fake successful request and response from broker.
|
||||
spyOn(b, '_postRequest').and.callFake(function() {
|
||||
b._xhr.readyState = b._xhr.DONE;
|
||||
b._xhr.status = Broker.STATUS.OK;
|
||||
b._xhr.responseText = 'fake offer';
|
||||
b._xhr.status = Broker.CODE.OK;
|
||||
b._xhr.responseText = '{"Status":"client match","Offer":"fake offer"}';
|
||||
return b._xhr.onreadystatechange();
|
||||
});
|
||||
poll = b.getClientOffer();
|
||||
|
@ -46,7 +46,7 @@ describe('Broker', function() {
|
|||
expect(desc).toEqual('fake offer');
|
||||
return done();
|
||||
}).catch(function() {
|
||||
fail('should not reject on Broker.STATUS.OK');
|
||||
fail('should not reject on Broker.CODE.OK');
|
||||
return done();
|
||||
});
|
||||
});
|
||||
|
@ -57,14 +57,15 @@ describe('Broker', function() {
|
|||
// fake timed-out request from broker
|
||||
spyOn(b, '_postRequest').and.callFake(function() {
|
||||
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();
|
||||
});
|
||||
poll = b.getClientOffer();
|
||||
expect(poll).not.toBeNull();
|
||||
expect(b._postRequest).toHaveBeenCalled();
|
||||
return poll.then(function(desc) {
|
||||
fail('should not fulfill on Broker.STATUS.GATEWAY_TIMEOUT');
|
||||
fail('should not fulfill with "Status: no match"');
|
||||
return done();
|
||||
}, function(err) {
|
||||
expect(err).toBe(Broker.MESSAGE.TIMEOUT);
|
||||
|
@ -101,7 +102,7 @@ describe('Broker', function() {
|
|||
var b = new Broker('fake');
|
||||
spyOn(b, '_postRequest');
|
||||
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() {
|
||||
|
@ -110,9 +111,8 @@ describe('Broker', function() {
|
|||
spyOn(b._xhr, 'open');
|
||||
spyOn(b._xhr, 'setRequestHeader');
|
||||
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.setRequestHeader).toHaveBeenCalled();
|
||||
expect(b._xhr.send).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue