-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13614c0
commit 92cd3c1
Showing
5 changed files
with
122 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package mux | ||
|
||
import ( | ||
"encoding/binary" | ||
"os" | ||
|
||
"github.com/sagernet/sing/common/buf" | ||
M "github.com/sagernet/sing/common/metadata" | ||
N "github.com/sagernet/sing/common/network" | ||
) | ||
|
||
var _ N.PacketReadWaiter = (*clientPacketConn)(nil) | ||
|
||
func (c *clientPacketConn) InitializeReadWaiter(newBuffer func() *buf.Buffer, _ bool) bool { | ||
c.newBuffer = newBuffer | ||
return false | ||
} | ||
|
||
func (c *clientPacketConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) { | ||
if c.newBuffer == nil { | ||
return nil, M.Socksaddr{}, os.ErrInvalid | ||
} | ||
if !c.responseRead { | ||
err = c.readResponse() | ||
if err != nil { | ||
return | ||
} | ||
c.responseRead = true | ||
} | ||
var length uint16 | ||
err = binary.Read(c.conn, binary.BigEndian, &length) | ||
if err != nil { | ||
return | ||
} | ||
buffer = c.newBuffer() | ||
_, err = buffer.ReadFullFrom(c.conn, int(length)) | ||
if err != nil { | ||
buffer.Release() | ||
return nil, M.Socksaddr{}, err | ||
} | ||
return | ||
} | ||
|
||
var _ N.PacketReadWaiter = (*clientPacketAddrConn)(nil) | ||
|
||
func (c *clientPacketAddrConn) InitializeReadWaiter(newBuffer func() *buf.Buffer, _ bool) bool { | ||
c.newBuffer = newBuffer | ||
return false | ||
} | ||
|
||
func (c *clientPacketAddrConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) { | ||
if c.newBuffer == nil { | ||
return nil, M.Socksaddr{}, os.ErrInvalid | ||
} | ||
if !c.responseRead { | ||
err = c.readResponse() | ||
if err != nil { | ||
return | ||
} | ||
c.responseRead = true | ||
} | ||
destination, err = M.SocksaddrSerializer.ReadAddrPort(c.conn) | ||
if err != nil { | ||
return | ||
} | ||
var length uint16 | ||
err = binary.Read(c.conn, binary.BigEndian, &length) | ||
if err != nil { | ||
return | ||
} | ||
buffer = c.newBuffer() | ||
_, err = buffer.ReadFullFrom(c.conn, int(length)) | ||
if err != nil { | ||
buffer.Release() | ||
return nil, M.Socksaddr{}, err | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters