github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/transport/internet/request/assembly/hub.go (about)

     1  package assembly
     2  
     3  import (
     4  	"context"
     5  	gonet "net"
     6  	"time"
     7  
     8  	"github.com/v2fly/v2ray-core/v5/transport/internet/transportcommon"
     9  
    10  	"github.com/v2fly/v2ray-core/v5/common"
    11  	"github.com/v2fly/v2ray-core/v5/common/net"
    12  	"github.com/v2fly/v2ray-core/v5/common/serial"
    13  	"github.com/v2fly/v2ray-core/v5/transport/internet"
    14  	"github.com/v2fly/v2ray-core/v5/transport/internet/request"
    15  )
    16  
    17  type server struct {
    18  	tripper   request.RoundTripperServer
    19  	assembler request.SessionAssemblerServer
    20  	addConn   internet.ConnHandler
    21  
    22  	streamSettings *internet.MemoryStreamConfig
    23  	addr           net.Address
    24  	port           net.Port
    25  }
    26  
    27  func (s server) Listen(ctx context.Context) (net.Listener, error) {
    28  	return transportcommon.ListenWithSecuritySettings(ctx, s.addr, s.port, s.streamSettings)
    29  }
    30  
    31  func (s server) AutoImplListener() request.Listener {
    32  	return s
    33  }
    34  
    35  func (s server) Close() error {
    36  	if err := s.tripper.Close(); err != nil {
    37  		return newError("failed to close tripper").Base(err)
    38  	}
    39  	return nil
    40  }
    41  
    42  func (s server) Addr() net.Addr {
    43  	// Unimplemented
    44  	return nil
    45  }
    46  
    47  type serverConnection struct {
    48  	request.Session
    49  }
    50  
    51  func (s serverConnection) LocalAddr() gonet.Addr {
    52  	return &net.UnixAddr{Name: "unimplemented"}
    53  }
    54  
    55  func (s serverConnection) RemoteAddr() gonet.Addr {
    56  	return &net.UnixAddr{Name: "unimplemented"}
    57  }
    58  
    59  func (s serverConnection) SetDeadline(t time.Time) error {
    60  	// Unimplemented
    61  	return nil
    62  }
    63  
    64  func (s serverConnection) SetReadDeadline(t time.Time) error {
    65  	// Unimplemented
    66  	return nil
    67  }
    68  
    69  func (s serverConnection) SetWriteDeadline(t time.Time) error {
    70  	// Unimplemented
    71  	return nil
    72  }
    73  
    74  func (s server) OnNewSession(ctx context.Context, sess request.Session, opts ...request.SessionOption) error {
    75  	s.addConn(&serverConnection{sess})
    76  	return nil
    77  }
    78  
    79  func (s server) SessionReceiver() request.SessionReceiver {
    80  	return s
    81  }
    82  
    83  func (s server) TripperReceiver() request.TripperReceiver {
    84  	return s.assembler
    85  }
    86  
    87  func listenRequest(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
    88  	transportConfiguration := streamSettings.ProtocolSettings.(*Config)
    89  	serverAssembly := &server{addConn: addConn}
    90  
    91  	assemblerConfigInstance, err := serial.GetInstanceOf(transportConfiguration.Assembler)
    92  	if err != nil {
    93  		return nil, newError("failed to get config instance of assembler").Base(err)
    94  	}
    95  	assembler, err := common.CreateObject(ctx, assemblerConfigInstance)
    96  	if err != nil {
    97  		return nil, newError("failed to create assembler").Base(err)
    98  	}
    99  	if typedAssembler, ok := assembler.(request.SessionAssemblerServer); !ok {
   100  		return nil, newError("failed to type assert assembler to SessionAssemblerServer")
   101  	} else {
   102  		serverAssembly.assembler = typedAssembler
   103  	}
   104  
   105  	roundtripperConfigInstance, err := serial.GetInstanceOf(transportConfiguration.Roundtripper)
   106  	if err != nil {
   107  		return nil, newError("failed to get config instance of roundtripper").Base(err)
   108  	}
   109  	roundtripper, err := common.CreateObject(ctx, roundtripperConfigInstance)
   110  	if err != nil {
   111  		return nil, newError("failed to create roundtripper").Base(err)
   112  	}
   113  	if typedRoundtripper, ok := roundtripper.(request.RoundTripperServer); !ok {
   114  		return nil, newError("failed to type assert roundtripper to RoundTripperServer")
   115  	} else {
   116  		serverAssembly.tripper = typedRoundtripper
   117  	}
   118  
   119  	serverAssembly.addr = address
   120  	serverAssembly.port = port
   121  	serverAssembly.streamSettings = streamSettings
   122  
   123  	serverAssembly.assembler.OnTransportServerAssemblyReady(serverAssembly)
   124  	serverAssembly.tripper.OnTransportServerAssemblyReady(serverAssembly)
   125  
   126  	if err := serverAssembly.tripper.Start(); err != nil {
   127  		return nil, newError("failed to start tripper").Base(err)
   128  	}
   129  
   130  	return serverAssembly, nil
   131  }
   132  
   133  func init() {
   134  	common.Must(internet.RegisterTransportListener(protocolName, listenRequest))
   135  }