@@ -3,7 +3,6 @@ package connection
33import (
44"errors"
55"fmt"
6- "io"
76"log/slog"
87"net"
98"net/netip"
@@ -15,19 +14,8 @@ import (
1514"github.com/apoxy-dev/apoxy/pkg/netstack"
1615)
1716
18- // Connection is a simple interface implemented by connect-ip-go and custom
19- // connection types.
20- type Connection interface {
21- io.Closer
22- ReadPacket ([]byte ) (int , error )
23- WritePacket ([]byte ) ([]byte , error )
24- }
25-
26- var _ Connection = (* MuxedConnection )(nil )
27-
28- // MuxedConnection is a connection that multiplexes multiple downstream
29- // connections over a single virtual connection.
30- type MuxedConnection struct {
17+ // MuxedConn multiplexes multiple connection.Conn objects.
18+ type MuxedConn struct {
3119// Maps tunnel destination address to CONNECT-IP connection.
3220conns * triemap.TrieMap [Connection ]
3321incomingPackets chan * []byte
@@ -37,8 +25,9 @@ type MuxedConnection struct {
3725closed atomic.Bool
3826}
3927
40- func NewMuxedConnection () * MuxedConnection {
41- return & MuxedConnection {
28+ // NewMuxedConn creates a new muxedConn.
29+ func NewMuxedConn () * MuxedConn {
30+ return & MuxedConn {
4231conns : triemap .New [Connection ](),
4332incomingPackets : make (chan * []byte , 100 ),
4433packetBufferPool : sync.Pool {
@@ -50,7 +39,7 @@ func NewMuxedConnection() *MuxedConnection {
5039}
5140}
5241
53- func (m * MuxedConnection ) AddConnection (prefix netip.Prefix , conn Connection ) {
42+ func (m * MuxedConn ) AddConnection (prefix netip.Prefix , conn Connection ) {
5443if prefix .IsValid () && prefix .Addr ().Is6 () {
5544m .conns .Insert (prefix , conn )
5645go m .readPackets (conn )
@@ -59,7 +48,7 @@ func (m *MuxedConnection) AddConnection(prefix netip.Prefix, conn Connection) {
5948}
6049}
6150
62- func (m * MuxedConnection ) RemoveConnection (prefix netip.Prefix ) error {
51+ func (m * MuxedConn ) RemoveConnection (prefix netip.Prefix ) error {
6352// Has the connection already been closed?
6453if m .closed .Load () {
6554// Then this becomes a no-op.
@@ -85,7 +74,7 @@ func (m *MuxedConnection) RemoveConnection(prefix netip.Prefix) error {
8574return nil
8675}
8776
88- func (m * MuxedConnection ) Prefixes () []netip.Prefix {
77+ func (m * MuxedConn ) Prefixes () []netip.Prefix {
8978var prefixes []netip.Prefix
9079m .conns .ForEach (func (prefix netip.Prefix , value Connection ) bool {
9180prefixes = append (prefixes , prefix )
@@ -94,7 +83,7 @@ func (m *MuxedConnection) Prefixes() []netip.Prefix {
9483return prefixes
9584}
9685
97- func (m * MuxedConnection ) Close () error {
86+ func (m * MuxedConn ) Close () error {
9887var firstErr error
9988m .closeOnce .Do (func () {
10089// Close all connections in the map.
@@ -122,7 +111,7 @@ func (m *MuxedConnection) Close() error {
122111return firstErr
123112}
124113
125- func (m * MuxedConnection ) ReadPacket (pkt []byte ) (int , error ) {
114+ func (m * MuxedConn ) ReadPacket (pkt []byte ) (int , error ) {
126115p , ok := <- m .incomingPackets
127116if ! ok {
128117return 0 , net .ErrClosed
@@ -138,7 +127,7 @@ func (m *MuxedConnection) ReadPacket(pkt []byte) (int, error) {
138127return n , nil
139128}
140129
141- func (m * MuxedConnection ) WritePacket (pkt []byte ) ([]byte , error ) {
130+ func (m * MuxedConn ) WritePacket (pkt []byte ) ([]byte , error ) {
142131slog .Debug ("Write packet to connection" , slog .Int ("bytes" , len (pkt )))
143132
144133var dstIP netip.Addr
@@ -170,7 +159,7 @@ func (m *MuxedConnection) WritePacket(pkt []byte) ([]byte, error) {
170159return conn .WritePacket (pkt )
171160}
172161
173- func (m * MuxedConnection ) readPackets (conn Connection ) {
162+ func (m * MuxedConn ) readPackets (conn Connection ) {
174163for {
175164pkt := m .packetBufferPool .Get ().(* []byte )
176165
0 commit comments