2022-07-29 16:29:22 +00:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
2023-11-08 04:09:22 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-29 16:29:22 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-04-20 03:16:22 +00:00
|
|
|
"github.com/sagernet/sing-mux"
|
2023-11-08 04:09:22 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-07-29 16:29:22 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
2023-11-08 04:09:22 +00:00
|
|
|
type Client = mux.Client
|
|
|
|
|
|
|
|
func NewClientWithOptions(dialer N.Dialer, logger logger.Logger, options option.OutboundMultiplexOptions) (*Client, error) {
|
2022-07-29 16:29:22 +00:00
|
|
|
if !options.Enabled {
|
2022-08-04 01:11:39 +00:00
|
|
|
return nil, nil
|
2022-07-29 16:29:22 +00:00
|
|
|
}
|
2023-11-08 04:09:22 +00:00
|
|
|
var brutalOptions mux.BrutalOptions
|
|
|
|
if options.Brutal != nil && options.Brutal.Enabled {
|
|
|
|
brutalOptions = mux.BrutalOptions{
|
|
|
|
Enabled: true,
|
|
|
|
SendBPS: uint64(options.Brutal.UpMbps * C.MbpsToBps),
|
|
|
|
ReceiveBPS: uint64(options.Brutal.DownMbps * C.MbpsToBps),
|
|
|
|
}
|
|
|
|
if brutalOptions.SendBPS < mux.BrutalMinSpeedBPS {
|
|
|
|
return nil, E.New("brutal: invalid upload speed")
|
|
|
|
}
|
|
|
|
if brutalOptions.ReceiveBPS < mux.BrutalMinSpeedBPS {
|
|
|
|
return nil, E.New("brutal: invalid download speed")
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 03:16:22 +00:00
|
|
|
return mux.NewClient(mux.Options{
|
|
|
|
Dialer: dialer,
|
2023-11-08 04:09:22 +00:00
|
|
|
Logger: logger,
|
2023-04-20 03:16:22 +00:00
|
|
|
Protocol: options.Protocol,
|
|
|
|
MaxConnections: options.MaxConnections,
|
|
|
|
MinStreams: options.MinStreams,
|
|
|
|
MaxStreams: options.MaxStreams,
|
|
|
|
Padding: options.Padding,
|
2023-11-08 04:09:22 +00:00
|
|
|
Brutal: brutalOptions,
|
2023-04-20 03:16:22 +00:00
|
|
|
})
|
2022-07-29 16:29:22 +00:00
|
|
|
}
|