-
Notifications
You must be signed in to change notification settings - Fork 402
/
prefix.go
51 lines (42 loc) · 1.02 KB
/
prefix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package server
import (
"bytes"
"io"
"net"
)
type prefixConn struct {
io.Reader
net.Conn
}
func newPrefixConn(data []byte, conn net.Conn) *prefixConn {
return &prefixConn{
Reader: io.MultiReader(bytes.NewReader(data), conn),
Conn: conn,
}
}
func (pc *prefixConn) Read(p []byte) (n int, err error) {
return pc.Reader.Read(p)
}
// PrefixedListener injects prefix bytes to the beginning of every new connection.
type PrefixedListener struct {
net.Listener
prefix []byte
}
// NewPrefixedListener creates a new PrefixedListener.
func NewPrefixedListener(prefix []byte, listener net.Listener) net.Listener {
return &PrefixedListener{
Listener: listener,
prefix: prefix,
}
}
// Accept implements function of net.Listener.
func (p *PrefixedListener) Accept() (net.Conn, error) {
conn, err := p.Listener.Accept()
if err != nil {
return conn, err
}
return newPrefixConn(p.prefix, conn), nil
}
var _ net.Listener = &PrefixedListener{}