diff options
author | Shelikhoo <xiaokangwang@outlook.com> | 2022-05-17 15:53:15 +0100 |
---|---|---|
committer | Shelikhoo <xiaokangwang@outlook.com> | 2022-06-16 14:06:58 +0100 |
commit | f789dce6d2b5e6e7d02eef6b168c31ed2ddd149e (patch) | |
tree | f595eedc305d6d124ef926039d9cc6e0782310ea /broker | |
parent | dd61e2be0f65aed72b0740aa22debab7246ebc48 (diff) | |
download | snowflake-f789dce6d2b5e6e7d02eef6b168c31ed2ddd149e.tar.gz snowflake-f789dce6d2b5e6e7d02eef6b168c31ed2ddd149e.zip |
Represent Bridge Fingerprint As String
Diffstat (limited to 'broker')
-rw-r--r-- | broker/bridge-list.go | 22 | ||||
-rw-r--r-- | broker/bridge-list_test.go | 9 | ||||
-rw-r--r-- | broker/broker.go | 5 | ||||
-rw-r--r-- | broker/ipc.go | 17 | ||||
-rw-r--r-- | broker/snowflake-broker_test.go | 2 |
5 files changed, 36 insertions, 19 deletions
diff --git a/broker/bridge-list.go b/broker/bridge-list.go index e77db65..ca2c041 100644 --- a/broker/bridge-list.go +++ b/broker/bridge-list.go @@ -2,27 +2,26 @@ package main import ( "bufio" - "encoding/hex" "encoding/json" "errors" + "git.torproject.org/pluggable-transports/snowflake.git/v2/common/bridgefingerprint" "io" "sync" ) var ErrBridgeNotFound = errors.New("bridge not found") -var ErrBridgeFingerprintInvalid = errors.New("bridge fingerprint invalid") func NewBridgeListHolder() BridgeListHolderFileBased { return &bridgeListHolder{} } type bridgeListHolder struct { - bridgeInfo map[[20]byte]BridgeInfo + bridgeInfo map[bridgefingerprint.Fingerprint]BridgeInfo accessBridgeInfo sync.RWMutex } type BridgeListHolder interface { - GetBridgeInfo(fingerprint [20]byte) (BridgeInfo, error) + GetBridgeInfo(bridgefingerprint.Fingerprint) (BridgeInfo, error) } type BridgeListHolderFileBased interface { @@ -36,7 +35,7 @@ type BridgeInfo struct { Fingerprint string `json:"fingerprint"` } -func (h *bridgeListHolder) GetBridgeInfo(fingerprint [20]byte) (BridgeInfo, error) { +func (h *bridgeListHolder) GetBridgeInfo(fingerprint bridgefingerprint.Fingerprint) (BridgeInfo, error) { h.accessBridgeInfo.RLock() defer h.accessBridgeInfo.RUnlock() if bridgeInfo, ok := h.bridgeInfo[fingerprint]; ok { @@ -46,7 +45,7 @@ func (h *bridgeListHolder) GetBridgeInfo(fingerprint [20]byte) (BridgeInfo, erro } func (h *bridgeListHolder) LoadBridgeInfo(reader io.Reader) error { - bridgeInfoMap := map[[20]byte]BridgeInfo{} + bridgeInfoMap := map[bridgefingerprint.Fingerprint]BridgeInfo{} inputScanner := bufio.NewScanner(reader) for inputScanner.Scan() { inputLine := inputScanner.Bytes() @@ -54,13 +53,14 @@ func (h *bridgeListHolder) LoadBridgeInfo(reader io.Reader) error { if err := json.Unmarshal(inputLine, &bridgeInfo); err != nil { return err } - var bridgeHash [20]byte - if n, err := hex.Decode(bridgeHash[:], []byte(bridgeInfo.Fingerprint)); err != nil { + + var bridgeFingerprint bridgefingerprint.Fingerprint + var err error + if bridgeFingerprint, err = bridgefingerprint.FingerprintFromHexString(bridgeInfo.Fingerprint); err != nil { return err - } else if n != 20 { - return ErrBridgeFingerprintInvalid } - bridgeInfoMap[bridgeHash] = bridgeInfo + + bridgeInfoMap[bridgeFingerprint] = bridgeInfo } h.accessBridgeInfo.Lock() defer h.accessBridgeInfo.Unlock() diff --git a/broker/bridge-list_test.go b/broker/bridge-list_test.go index 73da43c..4b53821 100644 --- a/broker/bridge-list_test.go +++ b/broker/bridge-list_test.go @@ -3,6 +3,7 @@ package main import ( "bytes" "encoding/hex" + "git.torproject.org/pluggable-transports/snowflake.git/v2/common/bridgefingerprint" . "github.com/smartystreets/goconvey/convey" "testing" ) @@ -34,7 +35,9 @@ func TestBridgeLoad(t *testing.T) { So(n, ShouldEqual, 20) So(err, ShouldBeNil) } - bridgeInfo, err := bridgeList.GetBridgeInfo(bridgeFingerprint) + Fingerprint, err := bridgefingerprint.FingerprintFromBytes(bridgeFingerprint[:]) + So(err, ShouldBeNil) + bridgeInfo, err := bridgeList.GetBridgeInfo(Fingerprint) So(err, ShouldBeNil) So(bridgeInfo.DisplayName, ShouldEqual, "default") So(bridgeInfo.WebSocketAddress, ShouldEqual, "wss://snowflake.torproject.org") @@ -50,7 +53,9 @@ func TestBridgeLoad(t *testing.T) { So(n, ShouldEqual, 20) So(err, ShouldBeNil) } - bridgeInfo, err := bridgeList.GetBridgeInfo(bridgeFingerprint) + Fingerprint, err := bridgefingerprint.FingerprintFromBytes(bridgeFingerprint[:]) + So(err, ShouldBeNil) + bridgeInfo, err := bridgeList.GetBridgeInfo(Fingerprint) So(err, ShouldBeNil) So(bridgeInfo.DisplayName, ShouldEqual, "imaginary-8") So(bridgeInfo.WebSocketAddress, ShouldEqual, "wss://imaginary-8-snowflake.torproject.org") diff --git a/broker/broker.go b/broker/broker.go index 8ca0120..9162370 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -10,6 +10,7 @@ import ( "container/heap" "crypto/tls" "flag" + "git.torproject.org/pluggable-transports/snowflake.git/v2/common/bridgefingerprint" "io" "log" "net/http" @@ -44,7 +45,7 @@ type BrokerContext struct { presumedPatternForLegacyClient string } -func (ctx *BrokerContext) GetBridgeInfo(fingerprint [20]byte) (BridgeInfo, error) { +func (ctx *BrokerContext) GetBridgeInfo(fingerprint bridgefingerprint.Fingerprint) (BridgeInfo, error) { return ctx.bridgeList.GetBridgeInfo(fingerprint) } @@ -178,7 +179,7 @@ func (ctx *BrokerContext) CheckProxyRelayPattern(pattern string, nonSupported bo type ClientOffer struct { natType string sdp []byte - fingerprint [20]byte + fingerprint []byte } func main() { diff --git a/broker/ipc.go b/broker/ipc.go index c86d1a7..f5d4747 100644 --- a/broker/ipc.go +++ b/broker/ipc.go @@ -4,6 +4,7 @@ import ( "container/heap" "encoding/hex" "fmt" + "git.torproject.org/pluggable-transports/snowflake.git/v2/common/bridgefingerprint" "log" "net" "time" @@ -130,7 +131,11 @@ func (i *IPC) ProxyPolls(arg messages.Arg, response *[]byte) error { i.ctx.metrics.promMetrics.ProxyPollTotal.With(prometheus.Labels{"nat": natType, "status": "matched"}).Inc() var relayURL string - if info, err := i.ctx.bridgeList.GetBridgeInfo(offer.fingerprint); err != nil { + bridgeFingerprint, err := bridgefingerprint.FingerprintFromBytes(offer.fingerprint) + if err != nil { + return messages.ErrBadRequest + } + if info, err := i.ctx.bridgeList.GetBridgeInfo(bridgeFingerprint); err != nil { return err } else { relayURL = info.WebSocketAddress @@ -172,12 +177,18 @@ func (i *IPC) ClientOffers(arg messages.Arg, response *[]byte) error { if err != nil { return sendClientResponse(&messages.ClientPollResponse{Error: err.Error()}, response) } - copy(offer.fingerprint[:], fingerprint) - if _, err := i.ctx.GetBridgeInfo(offer.fingerprint); err != nil { + BridgeFingerprint, err := bridgefingerprint.FingerprintFromBytes(fingerprint) + if err != nil { + return sendClientResponse(&messages.ClientPollResponse{Error: err.Error()}, response) + } + + if _, err := i.ctx.GetBridgeInfo(BridgeFingerprint); err != nil { return err } + offer.fingerprint = BridgeFingerprint.ToBytes() + // Only hand out known restricted snowflakes to unrestricted clients var snowflakeHeap *SnowflakeHeap if offer.natType == NATUnrestricted { diff --git a/broker/snowflake-broker_test.go b/broker/snowflake-broker_test.go index aee8578..a72f3ac 100644 --- a/broker/snowflake-broker_test.go +++ b/broker/snowflake-broker_test.go @@ -258,7 +258,7 @@ func TestBroker(t *testing.T) { // Pass a fake client offer to this proxy p := <-ctx.proxyPolls So(p.id, ShouldEqual, "ymbcCMto7KHNGYlp") - p.offerChannel <- &ClientOffer{sdp: []byte("fake offer"), fingerprint: defaultBridge} + p.offerChannel <- &ClientOffer{sdp: []byte("fake offer"), fingerprint: defaultBridge[:]} <-done So(w.Code, ShouldEqual, http.StatusOK) So(w.Body.String(), ShouldEqual, `{"Status":"client match","Offer":"fake offer","NAT":"","RelayURL":"wss://snowflake.torproject.net/"}`) |