mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-16 20:27:04 +03:00
Some checks failed
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Test / check-assets (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Scheduled assets update / geodat (push) Has been cancelled
* Fix ss2022 gouroutine leak * ErrReadTimeout
82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package singbridge
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/transport"
|
|
)
|
|
|
|
func CopyConn(ctx context.Context, inboundConn net.Conn, link *transport.Link, serverConn net.Conn) error {
|
|
conn := &PipeConnWrapper{
|
|
W: link.Writer,
|
|
Conn: inboundConn,
|
|
}
|
|
if ir, ok := link.Reader.(io.Reader); ok {
|
|
conn.R = ir
|
|
} else {
|
|
conn.R = &buf.BufferedReader{Reader: link.Reader}
|
|
}
|
|
return ReturnError(bufio.CopyConn(ctx, conn, serverConn))
|
|
}
|
|
|
|
type PipeConnWrapper struct {
|
|
R io.Reader
|
|
W buf.Writer
|
|
net.Conn
|
|
}
|
|
|
|
func (w *PipeConnWrapper) Close() error {
|
|
return nil
|
|
}
|
|
|
|
// This Read implemented a timeout to avoid goroutine leak.
|
|
// as a temporarily solution
|
|
func (w *PipeConnWrapper) Read(b []byte) (n int, err error) {
|
|
type readResult struct {
|
|
n int
|
|
err error
|
|
}
|
|
c := make(chan readResult, 1)
|
|
go func() {
|
|
n, err := w.R.Read(b)
|
|
c <- readResult{n: n, err: err}
|
|
}()
|
|
select {
|
|
case result := <-c:
|
|
return result.n, result.err
|
|
case <-time.After(300 * time.Second):
|
|
common.Close(w.R)
|
|
common.Interrupt(w.R)
|
|
return 0, buf.ErrReadTimeout
|
|
}
|
|
}
|
|
|
|
func (w *PipeConnWrapper) Write(p []byte) (n int, err error) {
|
|
n = len(p)
|
|
var mb buf.MultiBuffer
|
|
pLen := len(p)
|
|
for pLen > 0 {
|
|
buffer := buf.New()
|
|
if pLen > buf.Size {
|
|
_, err = buffer.Write(p[:buf.Size])
|
|
p = p[buf.Size:]
|
|
} else {
|
|
buffer.Write(p)
|
|
}
|
|
pLen -= int(buffer.Len())
|
|
mb = append(mb, buffer)
|
|
}
|
|
err = w.W.WriteMultiBuffer(mb)
|
|
if err != nil {
|
|
n = 0
|
|
buf.ReleaseMulti(mb)
|
|
}
|
|
return
|
|
}
|