mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Update broker--proxy protocol with proxy type
Proxies now include information about what type they are when they poll for client offers. The broker saves this information along with snowflake ids and outputs it on the /debug page.
This commit is contained in:
parent
7092b2cb2c
commit
7277bb37cd
6 changed files with 75 additions and 40 deletions
|
@ -6,16 +6,18 @@ package messages
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const version = "1.0"
|
||||
const version = "1.1"
|
||||
|
||||
/* Version 1.0 specification:
|
||||
/* Version 1.1 specification:
|
||||
|
||||
== ProxyPollRequest ==
|
||||
{
|
||||
Sid: [generated session id of proxy]
|
||||
Version: 1.0
|
||||
Sid: [generated session id of proxy],
|
||||
Version: 1.1,
|
||||
Type: [badge|webext|standalone]
|
||||
}
|
||||
|
||||
== ProxyPollResponse ==
|
||||
|
@ -41,11 +43,11 @@ HTTP 400 BadRequest
|
|||
|
||||
== ProxyAnswerRequest ==
|
||||
{
|
||||
Sid: [generated session id of proxy]
|
||||
Version: 1.0
|
||||
Sid: [generated session id of proxy],
|
||||
Version: 1.1,
|
||||
Answer:
|
||||
{
|
||||
type: answer
|
||||
type: answer,
|
||||
sdp: [WebRTC SDP]
|
||||
}
|
||||
}
|
||||
|
@ -73,34 +75,38 @@ HTTP 400 BadRequest
|
|||
type ProxyPollRequest struct {
|
||||
Sid string
|
||||
Version string
|
||||
Type string
|
||||
}
|
||||
|
||||
func EncodePollRequest(sid string) ([]byte, error) {
|
||||
func EncodePollRequest(sid string, ptype string) ([]byte, error) {
|
||||
return json.Marshal(ProxyPollRequest{
|
||||
Sid: sid,
|
||||
Version: version,
|
||||
Type: ptype,
|
||||
})
|
||||
}
|
||||
|
||||
// Decodes a poll message from a snowflake proxy and returns the
|
||||
// sid of the proxy on success and an error if it failed
|
||||
func DecodePollRequest(data []byte) (string, error) {
|
||||
func DecodePollRequest(data []byte) (string, string, error) {
|
||||
var message ProxyPollRequest
|
||||
|
||||
err := json.Unmarshal(data, &message)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if message.Version != "1.0" {
|
||||
return "", fmt.Errorf("using unknown version")
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Version 1.0 requires an Sid
|
||||
majorVersion := strings.Split(message.Version, ".")[0]
|
||||
if majorVersion != "1" {
|
||||
return "", "", fmt.Errorf("using unknown version")
|
||||
}
|
||||
|
||||
// Version 1.x requires an Sid
|
||||
if message.Sid == "" {
|
||||
return "", fmt.Errorf("no supplied session id")
|
||||
return "", "", fmt.Errorf("no supplied session id")
|
||||
}
|
||||
|
||||
return message.Sid, nil
|
||||
return message.Sid, message.Type, nil
|
||||
}
|
||||
|
||||
type ProxyPollResponse struct {
|
||||
|
@ -153,7 +159,7 @@ type ProxyAnswerRequest struct {
|
|||
|
||||
func EncodeAnswerRequest(answer string, sid string) ([]byte, error) {
|
||||
return json.Marshal(ProxyAnswerRequest{
|
||||
Version: "1.0",
|
||||
Version: "1.1",
|
||||
Sid: sid,
|
||||
Answer: answer,
|
||||
})
|
||||
|
@ -167,7 +173,9 @@ func DecodeAnswerRequest(data []byte) (string, string, error) {
|
|||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if message.Version != "1.0" {
|
||||
|
||||
majorVersion := strings.Split(message.Version, ".")[0]
|
||||
if majorVersion != "1" {
|
||||
return "", "", fmt.Errorf("using unknown version")
|
||||
}
|
||||
|
||||
|
|
|
@ -11,45 +11,60 @@ import (
|
|||
func TestDecodeProxyPollRequest(t *testing.T) {
|
||||
Convey("Context", t, func() {
|
||||
for _, test := range []struct {
|
||||
sid string
|
||||
data string
|
||||
err error
|
||||
sid string
|
||||
ptype string
|
||||
data string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
//Version 1.0 proxy message
|
||||
"ymbcCMto7KHNGYlp",
|
||||
"",
|
||||
`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.0"}`,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
//Version 1.1 proxy message
|
||||
"ymbcCMto7KHNGYlp",
|
||||
"standalone",
|
||||
`{"Sid":"ymbcCMto7KHNGYlp","Version":"1.1","Type":"standalone"}`,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
//Version 0.X proxy message:
|
||||
"",
|
||||
"",
|
||||
"ymbcCMto7KHNGYlp",
|
||||
&json.SyntaxError{},
|
||||
},
|
||||
{
|
||||
"",
|
||||
"",
|
||||
`{"Sid":"ymbcCMto7KHNGYlp"}`,
|
||||
fmt.Errorf(""),
|
||||
},
|
||||
{
|
||||
"",
|
||||
"",
|
||||
"{}",
|
||||
fmt.Errorf(""),
|
||||
},
|
||||
{
|
||||
"",
|
||||
"",
|
||||
`{"Version":"1.0"}`,
|
||||
fmt.Errorf(""),
|
||||
},
|
||||
{
|
||||
"",
|
||||
"",
|
||||
`{"Version":"2.0"}`,
|
||||
fmt.Errorf(""),
|
||||
},
|
||||
} {
|
||||
sid, err := DecodePollRequest([]byte(test.data))
|
||||
sid, ptype, err := DecodePollRequest([]byte(test.data))
|
||||
So(sid, ShouldResemble, test.sid)
|
||||
So(ptype, ShouldResemble, test.ptype)
|
||||
So(err, ShouldHaveSameTypeAs, test.err)
|
||||
}
|
||||
|
||||
|
@ -58,10 +73,11 @@ func TestDecodeProxyPollRequest(t *testing.T) {
|
|||
|
||||
func TestEncodeProxyPollRequests(t *testing.T) {
|
||||
Convey("Context", t, func() {
|
||||
b, err := EncodePollRequest("ymbcCMto7KHNGYlp")
|
||||
b, err := EncodePollRequest("ymbcCMto7KHNGYlp", "standalone")
|
||||
So(err, ShouldEqual, nil)
|
||||
sid, err := DecodePollRequest(b)
|
||||
sid, ptype, err := DecodePollRequest(b)
|
||||
So(sid, ShouldEqual, "ymbcCMto7KHNGYlp")
|
||||
So(ptype, ShouldEqual, "standalone")
|
||||
So(err, ShouldEqual, nil)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue