Skip to content

Commit

Permalink
refactor: use a noop metric implementation if no metrics are provided (
Browse files Browse the repository at this point in the history
…#210)

* refactor: make connection metrics optional

* Create noop metrics if nil.

* Revert some more changes.

* Add noop implementation for `ShadowsocksConnMetrics`.
  • Loading branch information
sbruens authored Sep 16, 2024
1 parent adf8a31 commit 4463b38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions service/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ import "time"
type ShadowsocksConnMetrics interface {
AddCipherSearch(accessKeyFound bool, timeToCipher time.Duration)
}

// NoOpShadowsocksConnMetrics is a [ShadowsocksConnMetrics] that doesn't do anything. Useful in tests
// or if you don't want to track metrics.
type NoOpShadowsocksConnMetrics struct{}

var _ ShadowsocksConnMetrics = (*NoOpShadowsocksConnMetrics)(nil)

func (m *NoOpShadowsocksConnMetrics) AddCipherSearch(accessKeyFound bool, timeToCipher time.Duration) {
}
6 changes: 6 additions & 0 deletions service/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type StreamAuthenticateFunc func(clientConn transport.StreamConn) (string, trans
// NewShadowsocksStreamAuthenticator creates a stream authenticator that uses Shadowsocks.
// TODO(fortuna): Offer alternative transports.
func NewShadowsocksStreamAuthenticator(ciphers CipherList, replayCache *ReplayCache, metrics ShadowsocksConnMetrics) StreamAuthenticateFunc {
if metrics == nil {
metrics = &NoOpShadowsocksConnMetrics{}
}
return func(clientConn transport.StreamConn) (string, transport.StreamConn, *onet.ConnectionError) {
// Find the cipher and acess key id.
cipherEntry, clientReader, clientSalt, timeToCipher, keyErr := findAccessKey(clientConn, remoteIP(clientConn), ciphers)
Expand Down Expand Up @@ -241,6 +244,9 @@ func StreamServe(accept StreamAcceptFunc, handle StreamHandleFunc) {
}

func (h *streamHandler) Handle(ctx context.Context, clientConn transport.StreamConn, connMetrics TCPConnMetrics) {
if connMetrics == nil {
connMetrics = &NoOpTCPConnMetrics{}
}
var proxyMetrics metrics.ProxyMetrics
measuredClientConn := metrics.MeasureConn(clientConn, &proxyMetrics.ProxyClient, &proxyMetrics.ClientProxy)
connStart := time.Now()
Expand Down
14 changes: 13 additions & 1 deletion service/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ type packetHandler struct {

// NewPacketHandler creates a UDPService
func NewPacketHandler(natTimeout time.Duration, cipherList CipherList, m UDPMetrics, ssMetrics ShadowsocksConnMetrics) PacketHandler {
return &packetHandler{natTimeout: natTimeout, ciphers: cipherList, m: m, ssm: ssMetrics, targetIPValidator: onet.RequirePublicIP}
if m == nil {
m = &NoOpUDPMetrics{}
}
if ssMetrics == nil {
ssMetrics = &NoOpShadowsocksConnMetrics{}
}
return &packetHandler{
natTimeout: natTimeout,
ciphers: cipherList,
m: m,
ssm: ssMetrics,
targetIPValidator: onet.RequirePublicIP,
}
}

// PacketHandler is a running UDP shadowsocks proxy that can be stopped.
Expand Down

0 comments on commit 4463b38

Please sign in to comment.