Fix close conn

This commit is contained in:
世界 2022-05-01 07:53:26 +08:00
parent a5d5d79e29
commit 118c423774
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
11 changed files with 179 additions and 51 deletions

View file

@ -41,15 +41,15 @@ func NewRawReader(upstream io.Reader, cipher cipher.AEAD, buffer []byte, nonce [
}
}
func (r *Reader) Upstream() io.Reader {
func (r *Reader) UpstreamReader() io.Reader {
return r.upstream
}
func (r *Reader) Replaceable() bool {
func (r *Reader) ReaderReplaceable() bool {
return false
}
func (r *Reader) SetUpstream(reader io.Reader) {
func (r *Reader) SetReader(reader io.Reader) {
r.upstream = reader
}
@ -226,11 +226,11 @@ func NewRawWriter(upstream io.Writer, cipher cipher.AEAD, maxPacketSize int, buf
}
}
func (w *Writer) Upstream() io.Writer {
func (w *Writer) UpstreamWriter() io.Writer {
return w.upstream
}
func (w *Writer) Replaceable() bool {
func (w *Writer) WriterReplaceable() bool {
return false
}
@ -299,11 +299,11 @@ type BufferedWriter struct {
index int
}
func (w *BufferedWriter) Upstream() io.Writer {
func (w *BufferedWriter) UpstreamWriter() io.Writer {
return w.upstream
}
func (w *BufferedWriter) Replaceable() bool {
func (w *BufferedWriter) WriterReplaceable() bool {
return w.index == 0
}

View file

@ -298,6 +298,28 @@ func (c *clientConn) ReadFrom(r io.Reader) (n int64, err error) {
return c.writer.ReadFrom(r)
}
func (c *clientConn) UpstreamReader() io.Reader {
if c.reader == nil {
return c.Conn
}
return c.reader
}
func (c *clientConn) ReaderReplaceable() bool {
return c.reader != nil
}
func (c *clientConn) UpstreamWriter() io.Writer {
if c.writer == nil {
return c.Conn
}
return c.writer
}
func (c *clientConn) WriterReplaceable() bool {
return c.writer != nil
}
type clientPacketConn struct {
*Method
net.Conn
@ -329,3 +351,19 @@ func (c *clientPacketConn) ReadPacket(buffer *buf.Buffer) (*M.AddrPort, error) {
}
return socks.AddressSerializer.ReadAddrPort(buffer)
}
func (c *clientPacketConn) UpstreamReader() io.Reader {
return c.Conn
}
func (c *clientPacketConn) ReaderReplaceable() bool {
return false
}
func (c *clientPacketConn) UpstreamWriter() io.Writer {
return c.Conn
}
func (c *clientPacketConn) WriterReplaceable() bool {
return false
}

View file

@ -168,22 +168,34 @@ func (c *serverConn) WriteTo(w io.Writer) (n int64, err error) {
return c.reader.WriteTo(w)
}
func (c *serverConn) UpstreamReader() io.Reader {
if c.reader == nil {
return c.Conn
}
return c.reader
}
func (c *serverConn) ReaderReplaceable() bool {
return c.reader != nil
}
func (c *serverConn) UpstreamWriter() io.Writer {
if c.writer == nil {
return c.Conn
}
return c.writer
}
func (c *serverConn) WriterReplaceable() bool {
return c.writer != nil
}
func (s *Service) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
if buffer.Len() < s.keySaltLength {
return E.New("bad packet")
}
key := Kdf(s.key, buffer.To(s.keySaltLength), s.keySaltLength)
c := s.constructor(common.Dup(key))
/*data := buf.New()
packet, err := c.Open(data.Index(0), rw.ZeroBytes[:c.NonceSize()], buffer.From(s.keySaltLength), nil)
if err != nil {
return err
}
data.Truncate(len(packet))
metadata.Protocol = "shadowsocks"
return s.udp.NewPacket(metadata.Source.String(), func() socks.PacketWriter {
return &serverPacketWriter{s, conn, metadata.Source}
}, data, metadata)*/
packet, err := c.Open(buffer.Index(s.keySaltLength), rw.ZeroBytes[:c.NonceSize()], buffer.From(s.keySaltLength), nil)
if err != nil {
return err

View file

@ -377,6 +377,28 @@ func (c *clientConn) ReadFrom(r io.Reader) (n int64, err error) {
return c.writer.ReadFrom(r)
}
func (c *clientConn) UpstreamReader() io.Reader {
if c.reader == nil {
return c.Conn
}
return c.reader
}
func (c *clientConn) ReaderReplaceable() bool {
return c.reader != nil
}
func (c *clientConn) UpstreamWriter() io.Writer {
if c.writer == nil {
return c.Conn
}
return c.writer
}
func (c *clientConn) WriterReplaceable() bool {
return c.writer != nil
}
type clientPacketConn struct {
net.Conn
method *Method
@ -582,3 +604,19 @@ func (m *Method) newUDPSession() *udpSession {
}
return session
}
func (c *clientPacketConn) UpstreamReader() io.Reader {
return c.Conn
}
func (c *clientPacketConn) ReaderReplaceable() bool {
return false
}
func (c *clientPacketConn) UpstreamWriter() io.Writer {
return c.Conn
}
func (c *clientPacketConn) WriterReplaceable() bool {
return false
}

View file

@ -207,6 +207,28 @@ func (c *serverConn) WriteTo(w io.Writer) (n int64, err error) {
return c.reader.WriteTo(w)
}
func (c *serverConn) UpstreamReader() io.Reader {
if c.reader == nil {
return c.Conn
}
return c.reader
}
func (c *serverConn) ReaderReplaceable() bool {
return c.reader != nil
}
func (c *serverConn) UpstreamWriter() io.Writer {
if c.writer == nil {
return c.Conn
}
return c.writer
}
func (c *serverConn) WriterReplaceable() bool {
return c.writer != nil
}
func (s *Service) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
var packetHeader []byte
if s.udpCipher != nil {