mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-04 10:48:49 +00:00
Add brutal infra
This commit is contained in:
@@ -170,7 +170,7 @@ func (f *DialingWorkerFactory) Create() (*ClientWorker, error) {
|
|||||||
|
|
||||||
type ClientStrategy struct {
|
type ClientStrategy struct {
|
||||||
MaxConcurrency uint32
|
MaxConcurrency uint32
|
||||||
MaxConnection uint32
|
MaxReuseTimes uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientWorker struct {
|
type ClientWorker struct {
|
||||||
@@ -288,7 +288,7 @@ func fetchInput(ctx context.Context, s *Session, output buf.Writer) {
|
|||||||
|
|
||||||
func (m *ClientWorker) IsClosing() bool {
|
func (m *ClientWorker) IsClosing() bool {
|
||||||
sm := m.sessionManager
|
sm := m.sessionManager
|
||||||
if m.strategy.MaxConnection > 0 && sm.Count() >= int(m.strategy.MaxConnection) {
|
if m.strategy.MaxReuseTimes > 0 && sm.Count() >= int(m.strategy.MaxReuseTimes) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func TestClientWorkerClose(t *testing.T) {
|
|||||||
Writer: w1,
|
Writer: w1,
|
||||||
}, mux.ClientStrategy{
|
}, mux.ClientStrategy{
|
||||||
MaxConcurrency: 4,
|
MaxConcurrency: 4,
|
||||||
MaxConnection: 4,
|
MaxReuseTimes: 4,
|
||||||
})
|
})
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ func TestClientWorkerClose(t *testing.T) {
|
|||||||
Writer: w2,
|
Writer: w2,
|
||||||
}, mux.ClientStrategy{
|
}, mux.ClientStrategy{
|
||||||
MaxConcurrency: 4,
|
MaxConcurrency: 4,
|
||||||
MaxConnection: 4,
|
MaxReuseTimes: 4,
|
||||||
})
|
})
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
|
|
||||||
|
|||||||
@@ -56,11 +56,12 @@ func (m *SessionManager) Allocate(Strategy *ClientStrategy) *Session {
|
|||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
MaxConcurrency := int(Strategy.MaxConcurrency)
|
MaxConcurrency := int(Strategy.MaxConcurrency)
|
||||||
MaxConnection := uint16(Strategy.MaxConnection)
|
MaxReuseTimes := uint16(Strategy.MaxReuseTimes)
|
||||||
|
|
||||||
if m.closed || (MaxConcurrency > 0 && len(m.sessions) >= MaxConcurrency) || (MaxConnection > 0 && m.count >= MaxConnection) {
|
if m.closed || (MaxConcurrency > 0 && len(m.sessions) >= MaxConcurrency) || (MaxReuseTimes > 0 && m.count >= MaxReuseTimes) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
errors.LogInfo(context.Background(), "Allocated mux.cool session id ", m.count, "/", MaxReuseTimes)
|
||||||
|
|
||||||
m.count++
|
m.count++
|
||||||
s := &Session{
|
s := &Session{
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package brutal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/xtls/xray-core/common/net"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:linkname setsockopt syscall.setsockopt
|
||||||
|
func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TCP_BRUTAL_PARAMS = 23301
|
||||||
|
)
|
||||||
|
|
||||||
|
type TCPBrutalParams struct {
|
||||||
|
Rate uint64
|
||||||
|
CwndGain uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func setBrutalFD(fd uintptr, sendBPS uint64) error {
|
||||||
|
err := unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "brutal")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetBrutal(conn *net.TCPConn, sendBPS uint64) error {
|
||||||
|
syscallConn, err := conn.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
syscallConn.Control(func(fd uintptr) {
|
||||||
|
err = setBrutalFD(fd, sendBPS)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package brutal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/xtls/xray-core/common/errors"
|
||||||
|
"github.com/xtls/xray-core/common/net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetBrutal(conn *net.TCPConn, sendBPS uint64) error {
|
||||||
|
return errors.New("brutal not available on this platform")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user