mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Stop storing version in ClientPollRequest
This continues to asserts the known version while decoding. The client will only ever generate the latest version while encoding and if the response needs to change, the impetus will be a new feature, set in the deserialized request, which can be used as a distinguisher.
This commit is contained in:
parent
b73add1550
commit
281d917beb
6 changed files with 22 additions and 53 deletions
|
@ -146,9 +146,8 @@ func clientOffers(i *IPC, w http.ResponseWriter, r *http.Request) {
|
||||||
if len(body) > 0 && body[0] == '{' {
|
if len(body) > 0 && body[0] == '{' {
|
||||||
isLegacy = true
|
isLegacy = true
|
||||||
req := messages.ClientPollRequest{
|
req := messages.ClientPollRequest{
|
||||||
Offer: string(body),
|
Offer: string(body),
|
||||||
NAT: r.Header.Get("Snowflake-NAT-Type"),
|
NAT: r.Header.Get("Snowflake-NAT-Type"),
|
||||||
Version: messages.ClientVersion1_0,
|
|
||||||
}
|
}
|
||||||
body, err = req.EncodeClientPollRequest()
|
body, err = req.EncodeClientPollRequest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -129,15 +129,9 @@ func (i *IPC) ClientOffers(arg messages.Arg, response *[]byte) error {
|
||||||
return sendClientResponse(&messages.ClientPollResponse{Error: err.Error()}, response)
|
return sendClientResponse(&messages.ClientPollResponse{Error: err.Error()}, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
var offer *ClientOffer
|
offer := &ClientOffer{
|
||||||
switch req.Version {
|
natType: req.NAT,
|
||||||
case messages.ClientVersion1_0:
|
sdp: []byte(req.Offer),
|
||||||
offer = &ClientOffer{
|
|
||||||
natType: req.NAT,
|
|
||||||
sdp: []byte(req.Offer),
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
panic("unknown version")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only hand out known restricted snowflakes to unrestricted clients
|
// Only hand out known restricted snowflakes to unrestricted clients
|
||||||
|
@ -162,13 +156,8 @@ func (i *IPC) ClientOffers(arg messages.Arg, response *[]byte) error {
|
||||||
i.ctx.metrics.clientRestrictedDeniedCount++
|
i.ctx.metrics.clientRestrictedDeniedCount++
|
||||||
}
|
}
|
||||||
i.ctx.metrics.lock.Unlock()
|
i.ctx.metrics.lock.Unlock()
|
||||||
switch req.Version {
|
resp := &messages.ClientPollResponse{Error: messages.StrNoProxies}
|
||||||
case messages.ClientVersion1_0:
|
return sendClientResponse(resp, response)
|
||||||
resp := &messages.ClientPollResponse{Error: messages.StrNoProxies}
|
|
||||||
return sendClientResponse(resp, response)
|
|
||||||
default:
|
|
||||||
panic("unknown version")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, find the most available snowflake proxy, and pass the offer to it.
|
// Otherwise, find the most available snowflake proxy, and pass the offer to it.
|
||||||
|
@ -185,24 +174,14 @@ func (i *IPC) ClientOffers(arg messages.Arg, response *[]byte) error {
|
||||||
i.ctx.metrics.clientProxyMatchCount++
|
i.ctx.metrics.clientProxyMatchCount++
|
||||||
i.ctx.metrics.promMetrics.ClientPollTotal.With(prometheus.Labels{"nat": offer.natType, "status": "matched"}).Inc()
|
i.ctx.metrics.promMetrics.ClientPollTotal.With(prometheus.Labels{"nat": offer.natType, "status": "matched"}).Inc()
|
||||||
i.ctx.metrics.lock.Unlock()
|
i.ctx.metrics.lock.Unlock()
|
||||||
switch req.Version {
|
resp := &messages.ClientPollResponse{Answer: answer}
|
||||||
case messages.ClientVersion1_0:
|
err = sendClientResponse(resp, response)
|
||||||
resp := &messages.ClientPollResponse{Answer: answer}
|
|
||||||
err = sendClientResponse(resp, response)
|
|
||||||
default:
|
|
||||||
panic("unknown version")
|
|
||||||
}
|
|
||||||
// Initial tracking of elapsed time.
|
// Initial tracking of elapsed time.
|
||||||
i.ctx.metrics.clientRoundtripEstimate = time.Since(startTime) / time.Millisecond
|
i.ctx.metrics.clientRoundtripEstimate = time.Since(startTime) / time.Millisecond
|
||||||
case <-time.After(time.Second * ClientTimeout):
|
case <-time.After(time.Second * ClientTimeout):
|
||||||
log.Println("Client: Timed out.")
|
log.Println("Client: Timed out.")
|
||||||
switch req.Version {
|
resp := &messages.ClientPollResponse{Error: messages.StrTimedOut}
|
||||||
case messages.ClientVersion1_0:
|
err = sendClientResponse(resp, response)
|
||||||
resp := &messages.ClientPollResponse{Error: messages.StrTimedOut}
|
|
||||||
err = sendClientResponse(resp, response)
|
|
||||||
default:
|
|
||||||
panic("unknown version")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i.ctx.snowflakeLock.Lock()
|
i.ctx.snowflakeLock.Lock()
|
||||||
|
|
|
@ -116,9 +116,8 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
|
||||||
// Encode the client poll request.
|
// Encode the client poll request.
|
||||||
bc.lock.Lock()
|
bc.lock.Lock()
|
||||||
req := &messages.ClientPollRequest{
|
req := &messages.ClientPollRequest{
|
||||||
Offer: offerSDP,
|
Offer: offerSDP,
|
||||||
NAT: bc.natType,
|
NAT: bc.natType,
|
||||||
Version: messages.ClientVersion1_0,
|
|
||||||
}
|
}
|
||||||
encReq, err := req.EncodeClientPollRequest()
|
encReq, err := req.EncodeClientPollRequest()
|
||||||
bc.lock.Unlock()
|
bc.lock.Unlock()
|
||||||
|
|
|
@ -43,9 +43,8 @@ func (t errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
// offer.
|
// offer.
|
||||||
func makeEncPollReq(offer string) []byte {
|
func makeEncPollReq(offer string) []byte {
|
||||||
encPollReq, err := (&messages.ClientPollRequest{
|
encPollReq, err := (&messages.ClientPollRequest{
|
||||||
Offer: offer,
|
Offer: offer,
|
||||||
NAT: nat.NATUnknown,
|
NAT: nat.NATUnknown,
|
||||||
Version: messages.ClientVersion1_0,
|
|
||||||
}).EncodeClientPollRequest()
|
}).EncodeClientPollRequest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/nat"
|
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/nat"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ClientVersion1_0 = "1.0"
|
const ClientVersion = "1.0"
|
||||||
|
|
||||||
/* Client--Broker protocol v1.x specification:
|
/* Client--Broker protocol v1.x specification:
|
||||||
|
|
||||||
|
@ -50,21 +50,17 @@ for the error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type ClientPollRequest struct {
|
type ClientPollRequest struct {
|
||||||
Offer string `json:"offer"`
|
Offer string `json:"offer"`
|
||||||
NAT string `json:"nat"`
|
NAT string `json:"nat"`
|
||||||
Version string `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encodes a poll message from a snowflake client
|
// Encodes a poll message from a snowflake client
|
||||||
func (req *ClientPollRequest) EncodeClientPollRequest() ([]byte, error) {
|
func (req *ClientPollRequest) EncodeClientPollRequest() ([]byte, error) {
|
||||||
if req.Version != ClientVersion1_0 {
|
|
||||||
return nil, fmt.Errorf("unsupported message version")
|
|
||||||
}
|
|
||||||
body, err := json.Marshal(req)
|
body, err := json.Marshal(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return append([]byte(req.Version+"\n"), body...), nil
|
return append([]byte(ClientVersion+"\n"), body...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decodes a poll message from a snowflake client
|
// Decodes a poll message from a snowflake client
|
||||||
|
@ -78,9 +74,7 @@ func DecodeClientPollRequest(data []byte) (*ClientPollRequest, error) {
|
||||||
|
|
||||||
var message ClientPollRequest
|
var message ClientPollRequest
|
||||||
|
|
||||||
if string(parts[0]) == ClientVersion1_0 {
|
if string(parts[0]) != ClientVersion {
|
||||||
message.Version = ClientVersion1_0
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("unsupported message version")
|
return nil, fmt.Errorf("unsupported message version")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -327,9 +327,8 @@ func TestDecodeClientPollRequest(t *testing.T) {
|
||||||
func TestEncodeClientPollRequests(t *testing.T) {
|
func TestEncodeClientPollRequests(t *testing.T) {
|
||||||
Convey("Context", t, func() {
|
Convey("Context", t, func() {
|
||||||
req1 := &ClientPollRequest{
|
req1 := &ClientPollRequest{
|
||||||
NAT: "unknown",
|
NAT: "unknown",
|
||||||
Offer: "fake",
|
Offer: "fake",
|
||||||
Version: ClientVersion1_0,
|
|
||||||
}
|
}
|
||||||
b, err := req1.EncodeClientPollRequest()
|
b, err := req1.EncodeClientPollRequest()
|
||||||
So(err, ShouldEqual, nil)
|
So(err, ShouldEqual, nil)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue