refactor: separate some Negotiate logic

As per https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/merge_requests/392#note_3096760
in preparation for further changes to `Negotiate`.
This commit is contained in:
WofWca 2024-11-10 22:34:00 +04:00 committed by Cecylia Bocovich
parent 75e73ce397
commit f6767061e4
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90

View file

@ -127,20 +127,13 @@ func newBrokerChannelFromConfig(config ClientConfig) (*BrokerChannel, error) {
func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
*webrtc.SessionDescription, error,
) {
offerSDP, err := util.SerializeSessionDescription(offer)
if err != nil {
return nil, err
}
// Encode the client poll request.
bc.lock.Lock()
req := &messages.ClientPollRequest{
Offer: offerSDP,
NAT: bc.natType,
Fingerprint: bc.BridgeFingerprint,
}
encReq, err := req.EncodeClientPollRequest()
natType := bc.natType
bridgeFingerprint := bc.BridgeFingerprint
bc.lock.Unlock()
encReq, err := preparePollRequest(offer, natType, bridgeFingerprint)
if err != nil {
return nil, err
}
@ -163,6 +156,25 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
return util.DeserializeSessionDescription(resp.Answer)
}
// Pure function
func preparePollRequest(
offer *webrtc.SessionDescription,
natType string,
bridgeFingerprint string,
) (encReq []byte, err error) {
offerSDP, err := util.SerializeSessionDescription(offer)
if err != nil {
return nil, err
}
req := &messages.ClientPollRequest{
Offer: offerSDP,
NAT: natType,
Fingerprint: bridgeFingerprint,
}
encReq, err = req.EncodeClientPollRequest()
return
}
// SetNATType sets the NAT type of the client so we can send it to the WebRTC broker.
func (bc *BrokerChannel) SetNATType(NATType string) {
bc.lock.Lock()