Skip to content

Commit 57720ed

Browse files
authored
add packetConn constructor for re-use in outline-ss-server (#300)
* Add a constructor for `packetConn` so it can be re-used in `outline-ss-server`. * Add a comment to `NewPacketConn`.
1 parent 2fdbf40 commit 57720ed

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

transport/shadowsocks/packet_listener.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ func (c *packetListener) ListenPacket(ctx context.Context) (net.PacketConn, erro
5454
if err != nil {
5555
return nil, fmt.Errorf("could not connect to endpoint: %w", err)
5656
}
57-
conn := packetConn{Conn: proxyConn, key: c.key}
58-
return &conn, nil
57+
return NewPacketConn(proxyConn, c.key), nil
5958
}
6059

6160
type packetConn struct {
@@ -65,6 +64,15 @@ type packetConn struct {
6564

6665
var _ net.PacketConn = (*packetConn)(nil)
6766

67+
// NewPacketConn wraps a [net.Conn] and returns a [net.PacketConn] that encrypts/decrypts
68+
// packets before writing/reading them to/from the underlying connection using the provided
69+
// encryption key.
70+
//
71+
// Closing the returned [net.PacketConn] will also close the underlying [net.Conn].
72+
func NewPacketConn(conn net.Conn, key *EncryptionKey) net.PacketConn {
73+
return &packetConn{Conn: conn, key: key}
74+
}
75+
6876
// WriteTo encrypts `b` and writes to `addr` through the proxy.
6977
func (c *packetConn) WriteTo(b []byte, addr net.Addr) (int, error) {
7078
socksTargetAddr := socks.ParseAddr(addr.String())

0 commit comments

Comments
 (0)