From 2a212640ce2e73c067a2e3d1435b72a377c07753 Mon Sep 17 00:00:00 2001 From: theodorsm Date: Thu, 19 Dec 2024 17:26:13 +0100 Subject: [PATCH] Remove code duplication and run goimports --- broker/bridge-list_test.go | 6 +++-- broker/broker.go | 6 +++-- broker/ipc.go | 1 + broker/metrics.go | 2 ++ broker/snowflake-broker_test.go | 1 + broker/sqs.go | 1 + broker/sqs_test.go | 1 + client/lib/lib_test.go | 1 + client/lib/rendezvous.go | 18 ++------------- client/lib/rendezvous_sqs.go | 1 + client/lib/rendezvous_test.go | 1 + client/lib/snowflake.go | 12 +++++----- client/lib/webrtc.go | 30 +------------------------ common/covertdtls/covert_dtls_config.go | 3 ++- common/event/bus_test.go | 3 ++- common/event/interface.go | 1 + common/messages/client.go | 1 + common/namematcher/matcher_test.go | 6 +++-- common/nat/nat.go | 3 ++- common/utls/client_hello_id.go | 3 ++- common/utls/client_hello_id_version.go | 3 ++- common/utls/roundtripper.go | 3 ++- probetest/probetest.go | 1 + proxy/lib/proxy-go_test.go | 1 + proxy/main.go | 2 ++ server/server.go | 7 ++++-- 26 files changed, 53 insertions(+), 65 deletions(-) diff --git a/broker/bridge-list_test.go b/broker/bridge-list_test.go index d74a4be..7a65d6f 100644 --- a/broker/bridge-list_test.go +++ b/broker/bridge-list_test.go @@ -3,9 +3,11 @@ package main import ( "bytes" "encoding/hex" - . "github.com/smartystreets/goconvey/convey" - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/bridgefingerprint" "testing" + + . "github.com/smartystreets/goconvey/convey" + + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/bridgefingerprint" ) const DefaultBridges = `{"displayName":"default", "webSocketAddress":"wss://snowflake.torproject.org", "fingerprint":"2B280B23E1107BB62ABFC40DDCC8824814F80A72"} diff --git a/broker/broker.go b/broker/broker.go index 482f0d2..9f7e7bb 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -27,9 +27,11 @@ import ( "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/namematcher" "golang.org/x/crypto/acme/autocert" + + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" + + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/namematcher" ) type BrokerContext struct { diff --git a/broker/ipc.go b/broker/ipc.go index bde72b0..a10f475 100644 --- a/broker/ipc.go +++ b/broker/ipc.go @@ -10,6 +10,7 @@ import ( "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/bridgefingerprint" "github.com/prometheus/client_golang/prometheus" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" ) diff --git a/broker/metrics.go b/broker/metrics.go index 4e85183..95d9667 100644 --- a/broker/metrics.go +++ b/broker/metrics.go @@ -16,7 +16,9 @@ import ( "github.com/prometheus/client_golang/prometheus" "gitlab.torproject.org/tpo/anti-censorship/geoip" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safeprom" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" ) diff --git a/broker/snowflake-broker_test.go b/broker/snowflake-broker_test.go index f8a45b9..03cd431 100644 --- a/broker/snowflake-broker_test.go +++ b/broker/snowflake-broker_test.go @@ -15,6 +15,7 @@ import ( "time" . "github.com/smartystreets/goconvey/convey" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/amp" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" ) diff --git a/broker/sqs.go b/broker/sqs.go index 614dafe..482c130 100644 --- a/broker/sqs.go +++ b/broker/sqs.go @@ -10,6 +10,7 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/aws/aws-sdk-go-v2/service/sqs/types" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/sqsclient" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/util" diff --git a/broker/sqs_test.go b/broker/sqs_test.go index 7c70390..507f5bb 100644 --- a/broker/sqs_test.go +++ b/broker/sqs_test.go @@ -15,6 +15,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/sqs/types" "github.com/golang/mock/gomock" . "github.com/smartystreets/goconvey/convey" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/sqsclient" ) diff --git a/client/lib/lib_test.go b/client/lib/lib_test.go index 5eab4a0..08c4d3a 100644 --- a/client/lib/lib_test.go +++ b/client/lib/lib_test.go @@ -7,6 +7,7 @@ import ( "time" . "github.com/smartystreets/goconvey/convey" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/event" ) diff --git a/client/lib/rendezvous.go b/client/lib/rendezvous.go index 08711da..f8d7544 100644 --- a/client/lib/rendezvous.go +++ b/client/lib/rendezvous.go @@ -197,18 +197,7 @@ func NewWebRTCDialerWithEvents(broker *BrokerChannel, iceServers []webrtc.ICESer func NewWebRTCDialerWithEventsAndProxy(broker *BrokerChannel, iceServers []webrtc.ICEServer, max int, eventLogger event.SnowflakeEventReceiver, proxy *url.URL, ) *WebRTCDialer { - config := webrtc.Configuration{ - ICEServers: iceServers, - } - - return &WebRTCDialer{ - BrokerChannel: broker, - webrtcConfig: &config, - max: max, - - eventLogger: eventLogger, - proxy: proxy, - } + return NewCovertWebRTCDialerWithEventsAndProxy(broker, iceServers, max, eventLogger, proxy, nil) } // NewWebRTCDialerWithEventsAndProxy constructs a new WebRTCDialer setting DTLS mimicking and randomization. @@ -235,10 +224,7 @@ func NewCovertWebRTCDialerWithEventsAndProxy(broker *BrokerChannel, iceServers [ func (w WebRTCDialer) Catch() (*WebRTCPeer, error) { // TODO: [#25591] Fetch ICE server information from Broker. // TODO: [#25596] Consider TURN servers here too. - if w.covertDTLSConfig != nil { - return NewCovertWebRTCPeerWithEventsAndProxy(w.webrtcConfig, w.BrokerChannel, w.eventLogger, w.proxy, w.covertDTLSConfig) - } - return NewWebRTCPeerWithEventsAndProxy(w.webrtcConfig, w.BrokerChannel, w.eventLogger, w.proxy) + return NewCovertWebRTCPeerWithEventsAndProxy(w.webrtcConfig, w.BrokerChannel, w.eventLogger, w.proxy, w.covertDTLSConfig) } // GetMax returns the maximum number of snowflakes to collect. diff --git a/client/lib/rendezvous_sqs.go b/client/lib/rendezvous_sqs.go index 6b1c073..e493af6 100644 --- a/client/lib/rendezvous_sqs.go +++ b/client/lib/rendezvous_sqs.go @@ -15,6 +15,7 @@ import ( "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/aws/aws-sdk-go-v2/service/sqs/types" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/sqsclient" sqscreds "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/sqscreds/lib" ) diff --git a/client/lib/rendezvous_test.go b/client/lib/rendezvous_test.go index 17f7cd2..739285d 100644 --- a/client/lib/rendezvous_test.go +++ b/client/lib/rendezvous_test.go @@ -16,6 +16,7 @@ import ( "github.com/golang/mock/gomock" "github.com/pion/webrtc/v4" . "github.com/smartystreets/goconvey/convey" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/amp" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/nat" diff --git a/client/lib/snowflake.go b/client/lib/snowflake.go index 04e0310..7a4809c 100644 --- a/client/lib/snowflake.go +++ b/client/lib/snowflake.go @@ -166,18 +166,18 @@ func NewSnowflakeClient(config ClientConfig) (*Transport, error) { } eventsLogger := event.NewSnowflakeEventDispatcher() - var transport *Transport + + var covertDTLSConfig covertdtls.CovertDTLSConfig if config.CovertDTLSConfig != "" { - covertDTLSConfig := covertdtls.ParseConfigString(config.CovertDTLSConfig) + covertDTLSConfig = covertdtls.ParseConfigString(config.CovertDTLSConfig) if config.CovertDTLSFingerprint != "" { - covertDTLSConfig.Fingerprint = fingerprints.ClientHelloFingerprint(*&config.CovertDTLSFingerprint) + covertDTLSConfig.Fingerprint = fingerprints.ClientHelloFingerprint(config.CovertDTLSFingerprint) } - transport = &Transport{dialer: NewCovertWebRTCDialerWithEventsAndProxy(broker, iceServers, max, eventsLogger, config.CommunicationProxy, &covertDTLSConfig), eventDispatcher: eventsLogger} - } else { - transport = &Transport{dialer: NewWebRTCDialerWithEventsAndProxy(broker, iceServers, max, eventsLogger, config.CommunicationProxy), eventDispatcher: eventsLogger} } + transport := &Transport{dialer: NewCovertWebRTCDialerWithEventsAndProxy(broker, iceServers, max, eventsLogger, config.CommunicationProxy, &covertDTLSConfig), eventDispatcher: eventsLogger} + return transport, nil } diff --git a/client/lib/webrtc.go b/client/lib/webrtc.go index 606a0b2..c6f57a4 100644 --- a/client/lib/webrtc.go +++ b/client/lib/webrtc.go @@ -73,35 +73,7 @@ func NewWebRTCPeerWithEventsAndProxy( config *webrtc.Configuration, broker *BrokerChannel, eventsLogger event.SnowflakeEventReceiver, proxy *url.URL, ) (*WebRTCPeer, error) { - if eventsLogger == nil { - eventsLogger = event.NewSnowflakeEventDispatcher() - } - - connection := new(WebRTCPeer) - { - var buf [8]byte - if _, err := rand.Read(buf[:]); err != nil { - panic(err) - } - connection.id = "snowflake-" + hex.EncodeToString(buf[:]) - } - connection.closed = make(chan struct{}) - - // Override with something that's not NullLogger to have real logging. - connection.bytesLogger = &bytesNullLogger{} - - // Pipes remain the same even when DataChannel gets switched. - connection.recvPipe, connection.writePipe = io.Pipe() - - connection.eventsLogger = eventsLogger - connection.proxy = proxy - - err := connection.connect(config, broker, nil) - if err != nil { - connection.Close() - return nil, err - } - return connection, nil + return NewCovertWebRTCPeerWithEventsAndProxy(config, broker, eventsLogger, proxy, nil) } // NewCovertWebRTCPeerWithEventsAndProxy constructs a WebRTC PeerConnection to a snowflake proxy using DTLS mimicking or randomization. diff --git a/common/covertdtls/covert_dtls_config.go b/common/covertdtls/covert_dtls_config.go index 74e6a92..b24dd3c 100644 --- a/common/covertdtls/covert_dtls_config.go +++ b/common/covertdtls/covert_dtls_config.go @@ -1,8 +1,9 @@ package covertdtls import ( - "github.com/theodorsm/covert-dtls/pkg/fingerprints" "strings" + + "github.com/theodorsm/covert-dtls/pkg/fingerprints" ) type CovertDTLSConfig struct { diff --git a/common/event/bus_test.go b/common/event/bus_test.go index df40d3d..0380f29 100644 --- a/common/event/bus_test.go +++ b/common/event/bus_test.go @@ -1,8 +1,9 @@ package event import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) type stubReceiver struct { diff --git a/common/event/interface.go b/common/event/interface.go index 6cfe7de..7445469 100644 --- a/common/event/interface.go +++ b/common/event/interface.go @@ -5,6 +5,7 @@ import ( "time" "github.com/pion/webrtc/v4" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" ) diff --git a/common/messages/client.go b/common/messages/client.go index da6359e..752a626 100644 --- a/common/messages/client.go +++ b/common/messages/client.go @@ -7,6 +7,7 @@ import ( "bytes" "encoding/json" "fmt" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/bridgefingerprint" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/nat" diff --git a/common/namematcher/matcher_test.go b/common/namematcher/matcher_test.go index 08d089c..5c4a68e 100644 --- a/common/namematcher/matcher_test.go +++ b/common/namematcher/matcher_test.go @@ -1,8 +1,10 @@ package namematcher -import "testing" +import ( + "testing" -import . "github.com/smartystreets/goconvey/convey" + . "github.com/smartystreets/goconvey/convey" +) func TestMatchMember(t *testing.T) { testingVector := []struct { diff --git a/common/nat/nat.go b/common/nat/nat.go index 7e6da00..000b220 100644 --- a/common/nat/nat.go +++ b/common/nat/nat.go @@ -16,12 +16,13 @@ package nat import ( "errors" "fmt" - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/proxy" "log" "net" "net/url" "time" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/proxy" + "github.com/pion/stun/v3" ) diff --git a/common/utls/client_hello_id.go b/common/utls/client_hello_id.go index c134ecf..b0890f3 100644 --- a/common/utls/client_hello_id.go +++ b/common/utls/client_hello_id.go @@ -2,8 +2,9 @@ package utls import ( "errors" - utls "github.com/refraction-networking/utls" "strings" + + utls "github.com/refraction-networking/utls" ) // ported from https://github.com/max-b/snowflake/commit/9dded063cb74c6941a16ad90b9dd0e06e618e55e diff --git a/common/utls/client_hello_id_version.go b/common/utls/client_hello_id_version.go index f17de21..2e79c13 100644 --- a/common/utls/client_hello_id_version.go +++ b/common/utls/client_hello_id_version.go @@ -1,8 +1,9 @@ package utls import ( - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/version" "strings" + + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/version" ) func generateVersionOutput() string { diff --git a/common/utls/roundtripper.go b/common/utls/roundtripper.go index 10c9155..46cfe13 100644 --- a/common/utls/roundtripper.go +++ b/common/utls/roundtripper.go @@ -5,13 +5,14 @@ import ( "crypto/tls" "errors" "fmt" - "golang.org/x/net/proxy" "net" "net/http" "net/url" "sync" "time" + "golang.org/x/net/proxy" + utls "github.com/refraction-networking/utls" "golang.org/x/net/http2" ) diff --git a/probetest/probetest.go b/probetest/probetest.go index fb59cb7..bda8720 100644 --- a/probetest/probetest.go +++ b/probetest/probetest.go @@ -21,6 +21,7 @@ import ( "time" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/util" diff --git a/proxy/lib/proxy-go_test.go b/proxy/lib/proxy-go_test.go index e8e50db..d22421b 100644 --- a/proxy/lib/proxy-go_test.go +++ b/proxy/lib/proxy-go_test.go @@ -12,6 +12,7 @@ import ( "github.com/pion/webrtc/v4" . "github.com/smartystreets/goconvey/convey" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/util" ) diff --git a/proxy/main.go b/proxy/main.go index 16b89c3..9b45ed6 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -12,7 +12,9 @@ import ( "time" "github.com/theodorsm/covert-dtls/pkg/fingerprints" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/covertdtls" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/event" "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/version" diff --git a/server/server.go b/server/server.go index 3bd624f..09c7fb3 100644 --- a/server/server.go +++ b/server/server.go @@ -18,11 +18,14 @@ import ( "sync" "syscall" - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" - "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/version" "golang.org/x/crypto/acme/autocert" + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog" + + "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/version" + pt "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib" + sf "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/server/lib" )