-
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
1739640
commit ae2745a
Showing
6 changed files
with
264 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package mux | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
|
||
"github.com/sagernet/sing/common" | ||
"github.com/sagernet/sing/common/buf" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
"github.com/sagernet/sing/common/rw" | ||
) | ||
|
||
const ( | ||
BrutalExchangeDomain = "_BrutalBwExchange" | ||
BrutalMinSpeedBPS = 65536 | ||
) | ||
|
||
func WriteBrutalRequest(writer io.Writer, receiveBPS uint64) error { | ||
return binary.Write(writer, binary.BigEndian, receiveBPS) | ||
} | ||
|
||
func ReadBrutalRequest(reader io.Reader) (uint64, error) { | ||
var receiveBPS uint64 | ||
err := binary.Read(reader, binary.BigEndian, &receiveBPS) | ||
return receiveBPS, err | ||
} | ||
|
||
func WriteBrutalResponse(writer io.Writer, receiveBPS uint64, ok bool, message string) error { | ||
buffer := buf.New() | ||
defer buffer.Release() | ||
common.Must(binary.Write(buffer, binary.BigEndian, ok)) | ||
if ok { | ||
common.Must(binary.Write(buffer, binary.BigEndian, receiveBPS)) | ||
} else { | ||
err := rw.WriteVString(buffer, message) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return common.Error(writer.Write(buffer.Bytes())) | ||
} | ||
|
||
func ReadBrutalResponse(reader io.Reader) (uint64, error) { | ||
var ok bool | ||
err := binary.Read(reader, binary.BigEndian, &ok) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if ok { | ||
var receiveBPS uint64 | ||
err = binary.Read(reader, binary.BigEndian, &receiveBPS) | ||
return receiveBPS, err | ||
} else { | ||
var message string | ||
message, err = rw.ReadVString(reader) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return 0, E.New("remote error: ", message) | ||
} | ||
} |
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,54 @@ | ||
package mux | ||
|
||
import ( | ||
"net" | ||
"os" | ||
"reflect" | ||
"syscall" | ||
"unsafe" | ||
_ "unsafe" | ||
|
||
"github.com/sagernet/sing/common" | ||
"github.com/sagernet/sing/common/control" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
BrutalAvailable = true | ||
TCP_BRUTAL_PARAMS = 23301 | ||
) | ||
|
||
type TCPBrutalParams struct { | ||
Rate uint64 | ||
CwndGain uint32 | ||
} | ||
|
||
//go:linkname setsockopt syscall.setsockopt | ||
func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) | ||
|
||
func SetBrutalOptions(conn net.Conn, sendBPS uint64) error { | ||
syscallConn, loaded := common.Cast[syscall.Conn](conn) | ||
if !loaded { | ||
return E.New("cannot convert from ", reflect.TypeOf(conn), " to syscall.Conn") | ||
} | ||
return control.Conn(syscallConn, func(fd uintptr) error { | ||
err := unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "brutal") | ||
if err != nil { | ||
return E.Extend( | ||
os.NewSyscallError("setsockopt IPPROTO_TCP TCP_CONGESTION brutal", err), | ||
"please make sure you have installed the tcp-brutal kernel module", | ||
) | ||
} | ||
params := TCPBrutalParams{ | ||
Rate: sendBPS, | ||
CwndGain: 20, // hysteria2 default | ||
} | ||
err = setsockopt(int(fd), unix.IPPROTO_TCP, TCP_BRUTAL_PARAMS, unsafe.Pointer(¶ms), unsafe.Sizeof(params)) | ||
if err != nil { | ||
return os.NewSyscallError("setsockopt IPPROTO_TCP TCP_BRUTAL_PARAMS", err) | ||
} | ||
return nil | ||
}) | ||
} |
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,15 @@ | ||
//go:build !linux | ||
|
||
package mux | ||
|
||
import ( | ||
"net" | ||
|
||
E "github.com/sagernet/sing/common/exceptions" | ||
) | ||
|
||
const BrutalAvailable = false | ||
|
||
func SetBrutalOptions(conn net.Conn, sendBPS uint64) error { | ||
return E.New("TCP Brutal is only supported on Linux") | ||
} |
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
Oops, something went wrong.