mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-26 03:21:43 +00:00
https://github.com/XTLS/Xray-core/pull/6748#issuecomment-5752780799 https://github.com/XTLS/Xray-core/issues/7240#issuecomment-6439951754 https://github.com/XTLS/Xray-core/pull/4231#issuecomment-6679992732 https://github.com/XTLS/Xray-core/pull/7355#issuecomment-6739539943 https://github.com/XTLS/Xray-core/pull/7359#issuecomment-6742147165 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6784892866 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6812285253 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6842747639 https://github.com/XTLS/Xray-core/pull/4231#issuecomment-7012427059 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7108550396 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7129493942 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7130454962 --------- Co-authored-by: Codex <codex@users.noreply.github.com> Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package reverse
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/session"
|
|
"github.com/xtls/xray-core/proxy/vless/multiplex"
|
|
"github.com/xtls/xray-core/transport"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// StartIntranet maintains Reverse carriers until the returned cancellation function is called.
|
|
// The opener derives each physical connection's context from the supplied context.
|
|
func StartIntranet(ctx context.Context, dispatch func(context.Context, net.Destination, *transport.Link) error, open func(context.Context) (context.Context, *transport.Link, error)) func() {
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
go func() {
|
|
const interval = 2 * time.Second
|
|
timer := time.NewTimer(interval)
|
|
defer timer.Stop()
|
|
var workers []*intranetWorker
|
|
for ; ; timer.Reset(interval) {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-timer.C:
|
|
}
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
var connections uint32
|
|
active := workers[:0]
|
|
for _, w := range workers {
|
|
if w.state.Load() == int32(Control_ACTIVE) && !w.worker.Closed() {
|
|
active = append(active, w)
|
|
connections += w.worker.ActiveConnections()
|
|
}
|
|
}
|
|
clear(workers[len(active):])
|
|
workers = active
|
|
if len(workers) != 0 && connections/uint32(len(workers)) <= 16 {
|
|
continue
|
|
}
|
|
physical, link, err := open(ctx)
|
|
if err != nil {
|
|
errors.LogWarningInner(ctx, err, "failed to create intranet worker")
|
|
continue
|
|
}
|
|
w := &intranetWorker{dispatch: dispatch}
|
|
w.worker = multiplex.NewServerWorker(session.ContextWithIsReverseMux(physical, true), w.handle, link)
|
|
workers = append(workers, w)
|
|
}
|
|
}()
|
|
return cancel
|
|
}
|
|
|
|
type intranetWorker struct {
|
|
worker *multiplex.ServerWorker
|
|
dispatch func(context.Context, net.Destination, *transport.Link) error
|
|
state atomic.Int32
|
|
}
|
|
|
|
// handle forwards user traffic or consumes the native Reverse control stream.
|
|
func (w *intranetWorker) handle(ctx context.Context, dest net.Destination, link *transport.Link) error {
|
|
if !isInternalDomain(dest) {
|
|
return w.dispatch(ctx, dest, link)
|
|
}
|
|
defer common.Close(link.Writer)
|
|
defer common.Interrupt(link.Reader)
|
|
for {
|
|
mb, readErr := link.Reader.ReadMultiBuffer()
|
|
for _, b := range mb {
|
|
var ctl Control
|
|
if err := proto.Unmarshal(b.Bytes(), &ctl); err != nil {
|
|
buf.ReleaseMulti(mb)
|
|
errors.LogInfoInner(ctx, err, "failed to parse proto message")
|
|
return nil
|
|
}
|
|
w.state.Store(int32(ctl.State))
|
|
}
|
|
buf.ReleaseMulti(mb)
|
|
if readErr != nil {
|
|
return nil
|
|
}
|
|
}
|
|
}
|