mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
bug fixes
This commit is contained in:
parent
1d761e30a9
commit
6d7aaa6535
@ -63,10 +63,6 @@ func (m *ClientManager) onClientFinish() {
|
|||||||
m.access.Lock()
|
m.access.Lock()
|
||||||
defer m.access.Unlock()
|
defer m.access.Unlock()
|
||||||
|
|
||||||
if len(m.clients) < 10 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
activeClients := make([]*Client, 0, len(m.clients))
|
activeClients := make([]*Client, 0, len(m.clients))
|
||||||
|
|
||||||
for _, client := range m.clients {
|
for _, client := range m.clients {
|
||||||
@ -158,8 +154,8 @@ func fetchInput(ctx context.Context, s *Session, output buf.Writer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Client) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) bool {
|
func (m *Client) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) bool {
|
||||||
numSession := m.sessionManager.Size()
|
sm := m.sessionManager
|
||||||
if numSession >= int(m.concurrency) || numSession >= maxTotal {
|
if sm.Size() >= int(m.concurrency) || sm.Count() >= maxTotal {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +165,7 @@ func (m *Client) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) bool
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
s := m.sessionManager.Allocate()
|
s := sm.Allocate()
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ import (
|
|||||||
|
|
||||||
type SessionManager struct {
|
type SessionManager struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
count uint16
|
|
||||||
sessions map[uint16]*Session
|
sessions map[uint16]*Session
|
||||||
|
count uint16
|
||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +27,13 @@ func (m *SessionManager) Size() int {
|
|||||||
return len(m.sessions)
|
return len(m.sessions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *SessionManager) Count() int {
|
||||||
|
m.RLock()
|
||||||
|
defer m.RUnlock()
|
||||||
|
|
||||||
|
return int(m.count)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *SessionManager) Allocate() *Session {
|
func (m *SessionManager) Allocate() *Session {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
@ -71,8 +78,8 @@ func (m *SessionManager) Get(id uint16) (*Session, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *SessionManager) CloseIfNoSession() bool {
|
func (m *SessionManager) CloseIfNoSession() bool {
|
||||||
m.RLock()
|
m.Lock()
|
||||||
defer m.RUnlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
if m.closed {
|
if m.closed {
|
||||||
return true
|
return true
|
||||||
@ -87,8 +94,8 @@ func (m *SessionManager) CloseIfNoSession() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *SessionManager) Close() {
|
func (m *SessionManager) Close() {
|
||||||
m.RLock()
|
m.Lock()
|
||||||
defer m.RUnlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
if m.closed {
|
if m.closed {
|
||||||
return
|
return
|
||||||
|
@ -66,14 +66,12 @@ func (w *Writer) writeData(mb buf.MultiBuffer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
runtime.KeepAlive(meta)
|
runtime.KeepAlive(meta)
|
||||||
|
|
||||||
mb2 := buf.NewMultiBuffer()
|
|
||||||
mb2.Append(frame)
|
|
||||||
|
|
||||||
if err := frame.AppendSupplier(serial.WriteUint16(uint16(mb.Len()))); err != nil {
|
if err := frame.AppendSupplier(serial.WriteUint16(uint16(mb.Len()))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mb2 := buf.NewMultiBuffer()
|
||||||
|
mb2.Append(frame)
|
||||||
mb2.AppendMulti(mb)
|
mb2.AppendMulti(mb)
|
||||||
return w.writer.Write(mb2)
|
return w.writer.Write(mb2)
|
||||||
}
|
}
|
||||||
|
@ -96,6 +96,12 @@ func ToBytesReader(stream Reader) io.Reader {
|
|||||||
|
|
||||||
// NewWriter creates a new Writer.
|
// NewWriter creates a new Writer.
|
||||||
func NewWriter(writer io.Writer) Writer {
|
func NewWriter(writer io.Writer) Writer {
|
||||||
|
if mw, ok := writer.(MultiBufferWriter); ok {
|
||||||
|
return &writerAdapter{
|
||||||
|
writer: mw,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &BufferToBytesWriter{
|
return &BufferToBytesWriter{
|
||||||
writer: writer,
|
writer: writer,
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package buf
|
package buf
|
||||||
|
|
||||||
import (
|
import "net"
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MultiBufferWriter interface {
|
type MultiBufferWriter interface {
|
||||||
WriteMultiBuffer(MultiBuffer) (int, error)
|
WriteMultiBuffer(MultiBuffer) (int, error)
|
||||||
@ -32,17 +29,11 @@ func (b *MultiBuffer) AppendMulti(mb MultiBuffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mb *MultiBuffer) Read(b []byte) (int, error) {
|
func (mb *MultiBuffer) Read(b []byte) (int, error) {
|
||||||
if len(*mb) == 0 {
|
|
||||||
return 0, io.EOF
|
|
||||||
}
|
|
||||||
endIndex := len(*mb)
|
endIndex := len(*mb)
|
||||||
totalBytes := 0
|
totalBytes := 0
|
||||||
for i, bb := range *mb {
|
for i, bb := range *mb {
|
||||||
nBytes, err := bb.Read(b)
|
nBytes, _ := bb.Read(b)
|
||||||
totalBytes += nBytes
|
totalBytes += nBytes
|
||||||
if err != nil {
|
|
||||||
return totalBytes, err
|
|
||||||
}
|
|
||||||
b = b[nBytes:]
|
b = b[nBytes:]
|
||||||
if bb.IsEmpty() {
|
if bb.IsEmpty() {
|
||||||
bb.Release()
|
bb.Release()
|
||||||
@ -96,6 +87,7 @@ func (mb *MultiBuffer) SliceBySize(size int) MultiBuffer {
|
|||||||
endIndex = i
|
endIndex = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
sliceSize += b.Len()
|
||||||
slice.Append(b)
|
slice.Append(b)
|
||||||
}
|
}
|
||||||
*mb = (*mb)[endIndex:]
|
*mb = (*mb)[endIndex:]
|
||||||
|
@ -42,12 +42,12 @@ type bufferToBytesReader struct {
|
|||||||
|
|
||||||
func (r *bufferToBytesReader) Read(b []byte) (int, error) {
|
func (r *bufferToBytesReader) Read(b []byte) (int, error) {
|
||||||
if r.leftOver != nil {
|
if r.leftOver != nil {
|
||||||
nBytes, err := r.leftOver.Read(b)
|
nBytes, _ := r.leftOver.Read(b)
|
||||||
if r.leftOver.IsEmpty() {
|
if r.leftOver.IsEmpty() {
|
||||||
r.leftOver.Release()
|
r.leftOver.Release()
|
||||||
r.leftOver = nil
|
r.leftOver = nil
|
||||||
}
|
}
|
||||||
return nBytes, err
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mb, err := r.stream.Read()
|
mb, err := r.stream.Read()
|
||||||
@ -55,11 +55,11 @@ func (r *bufferToBytesReader) Read(b []byte) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nBytes, err := mb.Read(b)
|
nBytes, _ := mb.Read(b)
|
||||||
if !mb.IsEmpty() {
|
if !mb.IsEmpty() {
|
||||||
r.leftOver = mb
|
r.leftOver = mb
|
||||||
}
|
}
|
||||||
return nBytes, err
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *bufferToBytesReader) ReadMultiBuffer() (MultiBuffer, error) {
|
func (r *bufferToBytesReader) ReadMultiBuffer() (MultiBuffer, error) {
|
||||||
|
@ -9,11 +9,6 @@ type BufferToBytesWriter struct {
|
|||||||
|
|
||||||
// Write implements Writer.Write(). Write() takes ownership of the given buffer.
|
// Write implements Writer.Write(). Write() takes ownership of the given buffer.
|
||||||
func (w *BufferToBytesWriter) Write(mb MultiBuffer) error {
|
func (w *BufferToBytesWriter) Write(mb MultiBuffer) error {
|
||||||
if mw, ok := w.writer.(MultiBufferWriter); ok {
|
|
||||||
_, err := mw.WriteMultiBuffer(mb)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer mb.Release()
|
defer mb.Release()
|
||||||
|
|
||||||
bs := mb.ToNetBuffers()
|
bs := mb.ToNetBuffers()
|
||||||
@ -21,6 +16,15 @@ func (w *BufferToBytesWriter) Write(mb MultiBuffer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type writerAdapter struct {
|
||||||
|
writer MultiBufferWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *writerAdapter) Write(mb MultiBuffer) error {
|
||||||
|
_, err := w.writer.WriteMultiBuffer(mb)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
type bytesToBufferWriter struct {
|
type bytesToBufferWriter struct {
|
||||||
writer Writer
|
writer Writer
|
||||||
}
|
}
|
||||||
|
@ -250,10 +250,7 @@ func (w *AuthenticationWriter) WriteMultiBuffer(mb buf.MultiBuffer) (int, error)
|
|||||||
const StartIndex = 17 * 1024
|
const StartIndex = 17 * 1024
|
||||||
var totalBytes int
|
var totalBytes int
|
||||||
for {
|
for {
|
||||||
payloadLen, err := mb.Read(w.buffer[StartIndex:])
|
payloadLen, _ := mb.Read(w.buffer[StartIndex:])
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
nBytes, err := w.Write(w.buffer[StartIndex : StartIndex+payloadLen])
|
nBytes, err := w.Write(w.buffer[StartIndex : StartIndex+payloadLen])
|
||||||
totalBytes += nBytes
|
totalBytes += nBytes
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -121,10 +121,7 @@ func (w *ChunkWriter) Write(mb buf.MultiBuffer) error {
|
|||||||
defer mb.Release()
|
defer mb.Release()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
payloadLen, err := mb.Read(w.buffer[2+AuthSize:])
|
payloadLen, _ := mb.Read(w.buffer[2+AuthSize:])
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
serial.Uint16ToBytes(uint16(payloadLen), w.buffer[:0])
|
serial.Uint16ToBytes(uint16(payloadLen), w.buffer[:0])
|
||||||
w.auth.Authenticate(w.buffer[2+AuthSize : 2+AuthSize+payloadLen])(w.buffer[2:])
|
w.auth.Authenticate(w.buffer[2+AuthSize : 2+AuthSize+payloadLen])(w.buffer[2:])
|
||||||
if _, err := w.writer.Write(w.buffer[:2+AuthSize+payloadLen]); err != nil {
|
if _, err := w.writer.Write(w.buffer[:2+AuthSize+payloadLen]); err != nil {
|
||||||
|
@ -133,13 +133,13 @@ func (c *HttpConn) Read(b []byte) (int, error) {
|
|||||||
c.oneTimeReader = nil
|
c.oneTimeReader = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.readBuffer.Len() > 0 {
|
if !c.readBuffer.IsEmpty() {
|
||||||
nBytes, err := c.readBuffer.Read(b)
|
nBytes, _ := c.readBuffer.Read(b)
|
||||||
if nBytes == c.readBuffer.Len() {
|
if c.readBuffer.IsEmpty() {
|
||||||
c.readBuffer.Release()
|
c.readBuffer.Release()
|
||||||
c.readBuffer = nil
|
c.readBuffer = nil
|
||||||
}
|
}
|
||||||
return nBytes, err
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Conn.Read(b)
|
return c.Conn.Read(b)
|
||||||
|
@ -62,10 +62,7 @@ func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) {
|
|||||||
}
|
}
|
||||||
totalBytes := 0
|
totalBytes := 0
|
||||||
for !mb.IsEmpty() {
|
for !mb.IsEmpty() {
|
||||||
nBytes, err := mb.Read(c.writeBuffer)
|
nBytes, _ := mb.Read(c.writeBuffer)
|
||||||
if err != nil {
|
|
||||||
return totalBytes, err
|
|
||||||
}
|
|
||||||
totalBytes += nBytes
|
totalBytes += nBytes
|
||||||
if _, err := c.Write(c.writeBuffer[:nBytes]); err != nil {
|
if _, err := c.Write(c.writeBuffer[:nBytes]); err != nil {
|
||||||
return totalBytes, err
|
return totalBytes, err
|
||||||
|
Loading…
Reference in New Issue
Block a user