1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 00:07:09 -05:00
v2fly/common/mux/server.go

253 lines
6.4 KiB
Go
Raw Normal View History

2018-10-23 08:35:32 -04:00
package mux
import (
"context"
"io"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/errors"
"github.com/v2fly/v2ray-core/v5/common/log"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/session"
"github.com/v2fly/v2ray-core/v5/features/routing"
"github.com/v2fly/v2ray-core/v5/transport"
"github.com/v2fly/v2ray-core/v5/transport/pipe"
2018-10-23 08:35:32 -04:00
)
type Server struct {
dispatcher routing.Dispatcher
}
// NewServer creates a new mux.Server.
func NewServer(ctx context.Context) *Server {
s := &Server{}
core.RequireFeatures(ctx, func(d routing.Dispatcher) {
s.dispatcher = d
})
return s
}
// Type implements common.HasType.
func (s *Server) Type() interface{} {
return s.dispatcher.Type()
}
// Dispatch implements routing.Dispatcher
2018-11-03 07:36:29 -04:00
func (s *Server) Dispatch(ctx context.Context, dest net.Destination) (*transport.Link, error) {
2018-10-23 08:35:32 -04:00
if dest.Address != muxCoolAddress {
return s.dispatcher.Dispatch(ctx, dest)
}
opts := pipe.OptionsFromContext(ctx)
uplinkReader, uplinkWriter := pipe.New(opts...)
downlinkReader, downlinkWriter := pipe.New(opts...)
2018-11-03 07:36:29 -04:00
_, err := NewServerWorker(ctx, s.dispatcher, &transport.Link{
2018-10-27 03:08:32 -04:00
Reader: uplinkReader,
Writer: downlinkWriter,
})
if err != nil {
return nil, err
2018-10-23 08:35:32 -04:00
}
2018-10-27 03:08:32 -04:00
2018-11-03 07:36:29 -04:00
return &transport.Link{Reader: downlinkReader, Writer: uplinkWriter}, nil
2018-10-23 08:35:32 -04:00
}
// Start implements common.Runnable.
func (s *Server) Start() error {
return nil
}
// Close implements common.Closable.
func (s *Server) Close() error {
return nil
}
type ServerWorker struct {
dispatcher routing.Dispatcher
2018-11-03 07:36:29 -04:00
link *transport.Link
2018-10-23 08:35:32 -04:00
sessionManager *SessionManager
}
2018-11-03 07:36:29 -04:00
func NewServerWorker(ctx context.Context, d routing.Dispatcher, link *transport.Link) (*ServerWorker, error) {
2018-10-27 03:08:32 -04:00
worker := &ServerWorker{
dispatcher: d,
link: link,
sessionManager: NewSessionManager(),
}
go worker.run(ctx)
return worker, nil
}
2018-10-23 08:35:32 -04:00
func handle(ctx context.Context, s *Session, output buf.Writer) {
writer := NewResponseWriter(s.ID, output, s.transferType)
if err := buf.Copy(s.input, writer); err != nil {
newError("session ", s.ID, " ends.").Base(err).WriteToLog(session.ExportIDToError(ctx))
writer.hasError = true
}
writer.Close()
s.Close()
}
2018-10-27 18:03:11 -04:00
func (w *ServerWorker) ActiveConnections() uint32 {
return uint32(w.sessionManager.Size())
}
2018-10-29 07:51:56 -04:00
func (w *ServerWorker) Closed() bool {
return w.sessionManager.Closed()
}
2018-10-23 08:35:32 -04:00
func (w *ServerWorker) handleStatusKeepAlive(meta *FrameMetadata, reader *buf.BufferedReader) error {
if meta.Option.Has(OptionData) {
2018-10-24 06:33:42 -04:00
return buf.Copy(NewStreamReader(reader), buf.Discard)
2018-10-23 08:35:32 -04:00
}
return nil
}
func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata, reader *buf.BufferedReader) error {
newError("received request for ", meta.Target).WriteToLog(session.ExportIDToError(ctx))
{
msg := &log.AccessMessage{
To: meta.Target,
Status: log.AccessAccepted,
Reason: "",
}
if inbound := session.InboundFromContext(ctx); inbound != nil && inbound.Source.IsValid() {
msg.From = inbound.Source
msg.Email = inbound.User.Email
2018-10-23 08:35:32 -04:00
}
2019-06-12 01:04:34 -04:00
ctx = log.ContextWithAccessMessage(ctx, msg)
2018-10-23 08:35:32 -04:00
}
link, err := w.dispatcher.Dispatch(ctx, meta.Target)
if err != nil {
if meta.Option.Has(OptionData) {
2018-10-24 06:33:42 -04:00
buf.Copy(NewStreamReader(reader), buf.Discard)
2018-10-23 08:35:32 -04:00
}
return newError("failed to dispatch request.").Base(err)
}
s := &Session{
input: link.Reader,
output: link.Writer,
parent: w.sessionManager,
ID: meta.SessionID,
transferType: protocol.TransferTypeStream,
}
if meta.Target.Network == net.Network_UDP {
s.transferType = protocol.TransferTypePacket
}
w.sessionManager.Add(s)
go handle(ctx, s, w.link.Writer)
if !meta.Option.Has(OptionData) {
return nil
}
rr := s.NewReader(reader)
if err := buf.Copy(rr, s.output); err != nil {
2018-10-24 06:33:42 -04:00
buf.Copy(rr, buf.Discard)
2018-12-31 15:25:10 -05:00
common.Interrupt(s.input)
2018-10-23 08:35:32 -04:00
return s.Close()
}
return nil
}
func (w *ServerWorker) handleStatusKeep(meta *FrameMetadata, reader *buf.BufferedReader) error {
if !meta.Option.Has(OptionData) {
return nil
}
2018-10-24 06:33:42 -04:00
s, found := w.sessionManager.Get(meta.SessionID)
if !found {
2018-11-07 06:46:20 -05:00
// Notify remote peer to close this session.
closingWriter := NewResponseWriter(meta.SessionID, w.link.Writer, protocol.TransferTypeStream)
closingWriter.Close()
2018-10-27 03:08:32 -04:00
return buf.Copy(NewStreamReader(reader), buf.Discard)
2018-10-23 08:35:32 -04:00
}
2018-10-24 06:33:42 -04:00
rr := s.NewReader(reader)
err := buf.Copy(rr, s.output)
if err != nil && buf.IsWriteError(err) {
2018-11-07 06:46:20 -05:00
newError("failed to write to downstream writer. closing session ", s.ID).Base(err).WriteToLog()
// Notify remote peer to close this session.
closingWriter := NewResponseWriter(meta.SessionID, w.link.Writer, protocol.TransferTypeStream)
closingWriter.Close()
2018-10-24 06:33:42 -04:00
drainErr := buf.Copy(rr, buf.Discard)
2018-12-31 15:25:10 -05:00
common.Interrupt(s.input)
2018-10-24 06:33:42 -04:00
s.Close()
return drainErr
}
return err
2018-10-23 08:35:32 -04:00
}
func (w *ServerWorker) handleStatusEnd(meta *FrameMetadata, reader *buf.BufferedReader) error {
if s, found := w.sessionManager.Get(meta.SessionID); found {
if meta.Option.Has(OptionError) {
2018-12-31 15:25:10 -05:00
common.Interrupt(s.input)
common.Interrupt(s.output)
2018-10-23 08:35:32 -04:00
}
s.Close()
}
if meta.Option.Has(OptionData) {
2018-10-24 06:33:42 -04:00
return buf.Copy(NewStreamReader(reader), buf.Discard)
2018-10-23 08:35:32 -04:00
}
return nil
}
func (w *ServerWorker) handleFrame(ctx context.Context, reader *buf.BufferedReader) error {
var meta FrameMetadata
err := meta.Unmarshal(reader)
if err != nil {
return newError("failed to read metadata").Base(err)
}
switch meta.SessionStatus {
case SessionStatusKeepAlive:
err = w.handleStatusKeepAlive(&meta, reader)
case SessionStatusEnd:
err = w.handleStatusEnd(&meta, reader)
case SessionStatusNew:
err = w.handleStatusNew(ctx, &meta, reader)
case SessionStatusKeep:
err = w.handleStatusKeep(&meta, reader)
default:
status := meta.SessionStatus
return newError("unknown status: ", status).AtError()
}
if err != nil {
return newError("failed to process data").Base(err)
}
return nil
}
func (w *ServerWorker) run(ctx context.Context) {
input := w.link.Reader
reader := &buf.BufferedReader{Reader: input}
defer w.sessionManager.Close()
2018-10-23 08:35:32 -04:00
for {
select {
case <-ctx.Done():
return
default:
err := w.handleFrame(ctx, reader)
if err != nil {
if errors.Cause(err) != io.EOF {
newError("unexpected EOF").Base(err).WriteToLog(session.ExportIDToError(ctx))
2018-12-31 15:25:10 -05:00
common.Interrupt(input)
2018-10-23 08:35:32 -04:00
}
return
}
}
}
}