github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/bufio/only.go (about) 1 package bufio 2 3 import ( 4 "io" 5 "net" 6 "time" 7 ) 8 9 type ReadOnlyConn struct { 10 reader io.Reader 11 } 12 13 func NewReadOnlyConn(reader io.Reader) net.Conn { 14 return &ReadOnlyConn{reader} 15 } 16 17 func (c *ReadOnlyConn) Read(b []byte) (n int, err error) { 18 return c.reader.Read(b) 19 } 20 21 func (c *ReadOnlyConn) Write(b []byte) (n int, err error) { 22 return 0, io.ErrClosedPipe 23 } 24 25 func (c *ReadOnlyConn) Close() error { 26 return nil 27 } 28 29 func (c *ReadOnlyConn) LocalAddr() net.Addr { 30 return nil 31 } 32 33 func (c *ReadOnlyConn) RemoteAddr() net.Addr { 34 return nil 35 } 36 37 func (c *ReadOnlyConn) SetDeadline(t time.Time) error { 38 return nil 39 } 40 41 func (c *ReadOnlyConn) SetWriteDeadline(t time.Time) error { 42 return nil 43 } 44 45 func (c *ReadOnlyConn) SetReadDeadline(t time.Time) error { 46 return nil 47 } 48 49 func (c *ReadOnlyConn) Upstream() any { 50 return c.reader 51 } 52 53 type WriteOnlyConn struct { 54 writer io.Writer 55 } 56 57 func NewWriteOnlyConn(writer io.Writer) net.Conn { 58 return &WriteOnlyConn{writer} 59 } 60 61 func (c *WriteOnlyConn) Read(b []byte) (n int, err error) { 62 return 0, io.ErrClosedPipe 63 } 64 65 func (c *WriteOnlyConn) Write(b []byte) (n int, err error) { 66 return c.writer.Write(b) 67 } 68 69 func (c *WriteOnlyConn) Close() error { 70 return nil 71 } 72 73 func (c *WriteOnlyConn) LocalAddr() net.Addr { 74 return nil 75 } 76 77 func (c *WriteOnlyConn) RemoteAddr() net.Addr { 78 return nil 79 } 80 81 func (c *WriteOnlyConn) SetDeadline(t time.Time) error { 82 return nil 83 } 84 85 func (c *WriteOnlyConn) SetWriteDeadline(t time.Time) error { 86 return nil 87 } 88 89 func (c *WriteOnlyConn) SetReadDeadline(t time.Time) error { 90 return nil 91 } 92 93 func (c *WriteOnlyConn) Upstream() any { 94 return c.writer 95 }