Skip to content

Commit

Permalink
Replace MustParseAddr with a new toIPAddr helper function.
Browse files Browse the repository at this point in the history
`MustParseAddr` is meant to be used for tests only, according to the
docs.
  • Loading branch information
sbruens committed Mar 21, 2024
1 parent b14464d commit 2d1252b
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/outline-ss-server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ type outlineMetrics struct {
var _ service.TCPMetrics = (*outlineMetrics)(nil)
var _ service.UDPMetrics = (*outlineMetrics)(nil)

// Converts a [net.Addr] to a [netip.Addr].
func toIPAddr(addr net.Addr) (d *netip.Addr, err error) {
hostname, _, _ := net.SplitHostPort(addr.String())
ip, err := netip.ParseAddr(hostname)
if err != nil {
return nil, fmt.Errorf("failed to convert client IP address: %w", err)
}
return &ip, err
}

type ReportTunnelTimeFunc func(IPKey, ipinfo.IPInfo, time.Duration)

type activeClient struct {
Expand Down Expand Up @@ -109,8 +119,11 @@ func (t *tunnelTimeTracker) reportDuration(c *activeClient, now time.Time) {

// Registers a new active connection for a client [net.Addr] and access key.
func (t *tunnelTimeTracker) startConnection(clientInfo ipinfo.IPInfo, clientAddr net.Addr, accessKey string) {
hostname, _, _ := net.SplitHostPort(clientAddr.String())
ipKey := IPKey{ip: netip.MustParseAddr(hostname), accessKey: accessKey}
ip, err := toIPAddr(clientAddr)
if err != nil {
return
}
ipKey := IPKey{*ip, accessKey}

t.mu.Lock()
defer t.mu.Unlock()
Expand All @@ -128,8 +141,11 @@ func (t *tunnelTimeTracker) startConnection(clientInfo ipinfo.IPInfo, clientAddr

// Removes an active connection for a client [net.Addr] and access key.
func (t *tunnelTimeTracker) stopConnection(clientAddr net.Addr, accessKey string) {
hostname, _, _ := net.SplitHostPort(clientAddr.String())
ipKey := IPKey{ip: netip.MustParseAddr(hostname), accessKey: accessKey}
ip, err := toIPAddr(clientAddr)
if err != nil {
return
}
ipKey := IPKey{*ip, accessKey}

t.mu.Lock()
defer t.mu.Unlock()
Expand Down

0 comments on commit 2d1252b

Please sign in to comment.