2024-04-26 17:00:56 +03:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2024-04-29 01:37:07 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2024-04-26 17:00:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Scheduler struct {
|
2024-09-04 07:06:51 +03:00
|
|
|
Buffer chan buf.MultiBuffer
|
|
|
|
Trigger chan int
|
|
|
|
Error chan error
|
2024-09-22 05:02:08 +03:00
|
|
|
TimeoutCounter int
|
|
|
|
TimeoutLock *sync.Mutex
|
2024-09-04 07:06:51 +03:00
|
|
|
closed chan int
|
|
|
|
bufferReadLock *sync.Mutex
|
|
|
|
writer buf.Writer
|
|
|
|
addons *Addons
|
|
|
|
trafficState *TrafficState
|
|
|
|
writeOnceUserUUID *[]byte
|
|
|
|
ctx context.Context
|
2024-04-26 17:00:56 +03:00
|
|
|
}
|
|
|
|
|
2024-09-22 05:02:08 +03:00
|
|
|
func TriggerScheduler(scheduler *Scheduler) buf.CopyOption {
|
|
|
|
return func(handler *buf.CopyHandler) {
|
|
|
|
handler.OnData = append(handler.OnData, func(buf.MultiBuffer) {
|
|
|
|
scheduler.Trigger <- 2 // send fake buffer if no pending
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 07:06:51 +03:00
|
|
|
func NewScheduler(w buf.Writer, addon *Addons, state *TrafficState, userUUID *[]byte, context context.Context) *Scheduler {
|
2024-04-26 17:00:56 +03:00
|
|
|
var s = Scheduler{
|
|
|
|
Buffer: make(chan buf.MultiBuffer, 100),
|
|
|
|
Trigger: make(chan int),
|
|
|
|
Error: make(chan error, 100),
|
2024-09-22 05:02:08 +03:00
|
|
|
TimeoutCounter: 0,
|
|
|
|
TimeoutLock: new(sync.Mutex),
|
2024-09-04 07:06:51 +03:00
|
|
|
closed: make(chan int),
|
2024-04-26 17:00:56 +03:00
|
|
|
bufferReadLock: new(sync.Mutex),
|
|
|
|
writer: w,
|
|
|
|
addons: addon,
|
|
|
|
trafficState: state,
|
2024-09-04 07:06:51 +03:00
|
|
|
writeOnceUserUUID: userUUID,
|
2024-04-26 17:00:56 +03:00
|
|
|
ctx: context,
|
|
|
|
}
|
2024-09-22 05:02:08 +03:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func(s *Scheduler) Start() {
|
2024-04-26 17:00:56 +03:00
|
|
|
go s.mainLoop()
|
2024-09-22 05:02:08 +03:00
|
|
|
if s.addons.Scheduler != nil && !s.addons.Scheduler.PingPong {
|
2024-04-26 17:00:56 +03:00
|
|
|
go s.exampleIndependentScheduler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(s *Scheduler) mainLoop() {
|
|
|
|
for trigger := range s.Trigger {
|
2024-09-04 07:06:51 +03:00
|
|
|
if len(s.closed) > 0 {
|
|
|
|
return
|
|
|
|
}
|
2024-09-22 05:02:08 +03:00
|
|
|
if trigger == -1 && s.addons.Scheduler != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if trigger == 2 && (s.addons.Scheduler == nil || !s.addons.Scheduler.PingPong) {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-26 17:00:56 +03:00
|
|
|
go func() { // each trigger has independent delay, trigger does not block
|
|
|
|
var d = 0 * time.Millisecond
|
|
|
|
if s.addons.Delay != nil {
|
|
|
|
l, err := rand.Int(rand.Reader, big.NewInt(int64(s.addons.Delay.MaxMillis - s.addons.Delay.MinMillis)))
|
|
|
|
if err != nil {
|
2024-04-29 01:37:07 +03:00
|
|
|
errors.LogInfoInner(s.ctx, err, "failed to generate delay", trigger)
|
2024-04-26 17:00:56 +03:00
|
|
|
}
|
2024-04-29 01:37:07 +03:00
|
|
|
d = time.Duration(uint32(l.Int64()) + s.addons.Delay.MinMillis) * time.Millisecond
|
|
|
|
time.Sleep(d)
|
2024-04-26 17:00:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.bufferReadLock.Lock() // guard against multiple trigger threads
|
|
|
|
var sending = len(s.Buffer)
|
|
|
|
if sending > 0 {
|
2024-04-29 01:37:07 +03:00
|
|
|
errors.LogDebug(s.ctx, "Scheduler Trigger for ", sending, " buffer(s) with ", d, " ", trigger)
|
2024-07-02 04:27:24 +03:00
|
|
|
for i := 0; i<sending; i++ {
|
2024-09-04 07:06:51 +03:00
|
|
|
err := s.writer.WriteMultiBuffer(<-s.Buffer)
|
|
|
|
if err != nil {
|
|
|
|
s.Error <- err
|
|
|
|
s.closed <- 1
|
|
|
|
return
|
|
|
|
}
|
2024-07-02 04:27:24 +03:00
|
|
|
}
|
2024-09-04 07:06:51 +03:00
|
|
|
} else if trigger > 0 && s.trafficState.IsPadding && ShouldStartSeed(s.addons, s.trafficState) && !ShouldStopSeed(s.addons, s.trafficState) {
|
2024-07-02 04:27:24 +03:00
|
|
|
errors.LogDebug(s.ctx, "Scheduler Trigger for fake buffer with ", d, " ", trigger)
|
2024-09-04 07:06:51 +03:00
|
|
|
s.trafficState.NumberOfPacketSent += 1
|
2024-07-02 04:27:24 +03:00
|
|
|
mb := make(buf.MultiBuffer, 1)
|
2024-09-04 07:06:51 +03:00
|
|
|
mb[0] = XtlsPadding(nil, CommandPaddingContinue, s.writeOnceUserUUID, true, s.addons, s.ctx)
|
|
|
|
s.trafficState.ByteSent += int64(mb.Len())
|
|
|
|
if s.trafficState.StartTime.IsZero() {
|
|
|
|
s.trafficState.StartTime = time.Now()
|
|
|
|
}
|
|
|
|
err := s.writer.WriteMultiBuffer(mb)
|
|
|
|
if err != nil {
|
|
|
|
s.Error <- err
|
|
|
|
s.closed <- 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if buffered, ok := s.writer.(*buf.BufferedWriter); ok {
|
|
|
|
buffered.SetBuffered(false)
|
|
|
|
}
|
2024-04-26 17:00:56 +03:00
|
|
|
}
|
|
|
|
s.bufferReadLock.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(s *Scheduler) exampleIndependentScheduler() {
|
|
|
|
for {
|
2024-09-04 07:06:51 +03:00
|
|
|
if len(s.closed) > 0 {
|
|
|
|
return
|
|
|
|
}
|
2024-07-02 04:27:24 +03:00
|
|
|
s.Trigger <- 1 // send fake buffer if no pending
|
2024-09-04 07:06:51 +03:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2024-04-26 17:00:56 +03:00
|
|
|
}
|
|
|
|
}
|