mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-25 11:01:44 +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>
102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package multiplex
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/log"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/session"
|
|
"github.com/xtls/xray-core/transport"
|
|
)
|
|
|
|
type ServerWorker struct {
|
|
*carrier
|
|
dispatch func(context.Context, net.Destination, *transport.Link) error
|
|
}
|
|
|
|
func NewServerWorker(ctx context.Context, dispatch func(context.Context, net.Destination, *transport.Link) error, link *transport.Link) *ServerWorker {
|
|
w := &ServerWorker{carrier: newCarrier(ctx, *link, 60*time.Second, false), dispatch: dispatch}
|
|
go w.run(ctx, w.handleStatusNew)
|
|
return w
|
|
}
|
|
|
|
// A non-nil dispatch return rejects admission; accepted backend failures use the link.
|
|
func (w *ServerWorker) dispatchFlow(ctx context.Context, dest net.Destination, flow *inboundFlow, output buf.Writer) {
|
|
if err := w.dispatch(ctx, dest, &transport.Link{Reader: flow, Writer: output}); err != nil {
|
|
if flow.GlobalID != [8]byte{} {
|
|
XUDPManager.Lock()
|
|
if XUDPManager.Map[flow.GlobalID] == flow {
|
|
delete(XUDPManager.Map, flow.GlobalID)
|
|
}
|
|
XUDPManager.Unlock()
|
|
}
|
|
w.cancel(err)
|
|
flow.Interrupt()
|
|
flow.finish(true)
|
|
errors.LogInfoInner(ctx, err, "failed to dispatch request to ", dest)
|
|
}
|
|
}
|
|
|
|
func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata, reader *buf.BufferedReader) error {
|
|
w.RLock()
|
|
previous := w.sessions[meta.SessionID]
|
|
w.RUnlock()
|
|
if previous != nil {
|
|
previous.abort()
|
|
}
|
|
ctx = session.SubContextFromMuxInbound(ctx)
|
|
if meta.Inbound != nil && meta.Inbound.Source.IsValid() && meta.Inbound.Local.IsValid() {
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil {
|
|
newInbound := *inbound
|
|
newInbound.Source = meta.Inbound.Source
|
|
newInbound.Local = meta.Inbound.Local
|
|
ctx = session.ContextWithInbound(ctx, &newInbound)
|
|
}
|
|
}
|
|
errors.LogInfo(ctx, "received request for ", meta.Target)
|
|
msg := &log.AccessMessage{
|
|
To: meta.Target,
|
|
Status: log.AccessAccepted,
|
|
}
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil && inbound.Source.IsValid() {
|
|
msg.From = inbound.Source
|
|
msg.Email = inbound.User.Email
|
|
}
|
|
ctx = log.ContextWithAccessMessage(ctx, msg)
|
|
|
|
if network := session.AllowedNetworkFromContext(ctx); network != net.Network_Unknown && meta.Target.Network != network {
|
|
return errors.New("unexpected network ", meta.Target.Network)
|
|
}
|
|
|
|
if meta.GlobalID != [8]byte{} {
|
|
return w.bindXUDP(ctx, meta, reader)
|
|
}
|
|
|
|
s := &Session{
|
|
parent: w.carrier, phase: outputOpen,
|
|
ID: meta.SessionID, target: net.Destination{Network: meta.Target.Network},
|
|
}
|
|
s.flow = newInboundFlow(ctx, [8]byte{})
|
|
s.flow.binding = s
|
|
w.Lock()
|
|
admitted := w.admitLocked(s)
|
|
w.Unlock()
|
|
if !admitted {
|
|
s.flow.Interrupt()
|
|
return errors.New("failed to add new session")
|
|
}
|
|
var output buf.Writer = s
|
|
if meta.Target.Network == net.Network_UDP {
|
|
output = &flowOutput{s.flow}
|
|
}
|
|
go w.dispatchFlow(ctx, meta.Target, s.flow, output)
|
|
if !meta.Option.Has(OptionData) {
|
|
return nil
|
|
}
|
|
|
|
return copyFramePayload(reader, s, meta.Target)
|
|
}
|