Files
sing-box-extended-mirror/protocol/bond/conn.go
T

160 lines
3.2 KiB
Go

package bond
import (
"bytes"
"encoding/binary"
"errors"
"io"
"math"
"net"
"time"
)
type bondedConn struct {
conns []net.Conn
downloadRatios []uint8
uploadRatios []uint8
readBuffer *bytes.Buffer
}
func NewBondedConn(conns []net.Conn, downloadRatios, uploadRatios []uint8) *bondedConn {
return &bondedConn{
conns: conns,
downloadRatios: downloadRatios,
uploadRatios: uploadRatios,
readBuffer: bytes.NewBuffer(make([]byte, 0, 4096)),
}
}
func (c *bondedConn) Read(buffer []byte) (int, error) {
if len(buffer) == 0 {
return 0, nil
}
for c.readBuffer.Len() == 0 {
c.readBuffer.Reset()
var header [2]byte
_, err := io.ReadFull(c.conns[0], header[:])
if err != nil {
return 0, err
}
size := int(binary.BigEndian.Uint16(header[:]))
for i, length := range splitByRatios(size, c.downloadRatios) {
if length == 0 {
continue
}
if _, err = io.CopyN(c.readBuffer, c.conns[i], int64(length)); err != nil {
c.readBuffer.Reset()
return 0, err
}
}
}
return c.readBuffer.Read(buffer)
}
func (c *bondedConn) Write(buffer []byte) (int, error) {
written := 0
for written < len(buffer) {
size := min(len(buffer)-written, math.MaxUint16)
var header [2]byte
binary.BigEndian.PutUint16(header[:], uint16(size))
count, err := c.conns[0].Write(header[:])
if err != nil {
return written, err
}
if count != len(header) {
return written, io.ErrShortWrite
}
for i, length := range splitByRatios(size, c.uploadRatios) {
for length > 0 {
count, err = c.conns[i].Write(buffer[written : written+length])
written += count
length -= count
if err != nil {
return written, err
}
if count == 0 {
return written, io.ErrShortWrite
}
}
}
}
return written, nil
}
func (c *bondedConn) Close() error {
errs := make([]error, 0)
for _, conn := range c.conns {
err := conn.Close()
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}
func (c *bondedConn) LocalAddr() net.Addr {
return nil
}
func (c *bondedConn) RemoteAddr() net.Addr {
return nil
}
func (c *bondedConn) SetDeadline(t time.Time) error {
errs := make([]error, 0)
for _, conn := range c.conns {
err := conn.SetDeadline(t)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}
func (c *bondedConn) SetReadDeadline(t time.Time) error {
errs := make([]error, 0)
for _, conn := range c.conns {
err := conn.SetReadDeadline(t)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}
func (c *bondedConn) SetWriteDeadline(t time.Time) error {
errs := make([]error, 0)
for _, conn := range c.conns {
err := conn.SetWriteDeadline(t)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}
func splitByRatios(number int, ratios []uint8) []int {
result := make([]int, len(ratios))
remaining := number
for i := 0; i < len(ratios)-1; i++ {
part := number * int(ratios[i]) / 100
result[i] = part
remaining -= part
}
result[len(ratios)-1] = remaining
return result
}