2023-08-31 03:37:26 +00:00
|
|
|
//go:build with_quic
|
|
|
|
|
2022-08-22 13:20:05 +00:00
|
|
|
package v2rayquic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sagernet/quic-go"
|
2024-07-11 15:34:09 +00:00
|
|
|
"github.com/sagernet/quic-go/http3"
|
2022-08-22 13:20:05 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-09-09 10:45:10 +00:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-22 13:20:05 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-09-12 13:14:11 +00:00
|
|
|
"github.com/sagernet/sing-quic"
|
2022-08-22 13:20:05 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2024-10-21 15:38:34 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-08-22 13:20:05 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
ctx context.Context
|
2024-10-21 15:38:34 +00:00
|
|
|
logger logger.ContextLogger
|
2023-08-31 03:37:26 +00:00
|
|
|
tlsConfig tls.ServerConfig
|
2022-08-22 13:20:05 +00:00
|
|
|
quicConfig *quic.Config
|
2023-02-12 21:53:03 +00:00
|
|
|
handler adapter.V2RayServerTransportHandler
|
2022-08-22 13:20:05 +00:00
|
|
|
udpListener net.PacketConn
|
2023-09-12 13:14:11 +00:00
|
|
|
quicListener qtls.Listener
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayQUICOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
|
2022-08-22 13:20:05 +00:00
|
|
|
quicConfig := &quic.Config{
|
|
|
|
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
|
|
|
|
}
|
2023-08-31 03:37:26 +00:00
|
|
|
if len(tlsConfig.NextProtos()) == 0 {
|
2024-07-11 15:34:09 +00:00
|
|
|
tlsConfig.SetNextProtos([]string{http3.NextProtoH3})
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|
|
|
|
server := &Server{
|
2023-02-12 21:53:03 +00:00
|
|
|
ctx: ctx,
|
2024-10-21 15:38:34 +00:00
|
|
|
logger: logger,
|
2023-08-31 03:37:26 +00:00
|
|
|
tlsConfig: tlsConfig,
|
2023-02-12 21:53:03 +00:00
|
|
|
quicConfig: quicConfig,
|
|
|
|
handler: handler,
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|
2022-09-09 10:45:10 +00:00
|
|
|
return server, nil
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Network() []string {
|
|
|
|
return []string{N.NetworkUDP}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Serve(listener net.Listener) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServePacket(listener net.PacketConn) error {
|
2023-08-31 03:37:26 +00:00
|
|
|
quicListener, err := qtls.Listen(listener, s.tlsConfig, s.quicConfig)
|
2022-08-22 13:20:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.udpListener = listener
|
|
|
|
s.quicListener = quicListener
|
|
|
|
go s.acceptLoop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) acceptLoop() {
|
|
|
|
for {
|
|
|
|
conn, err := s.quicListener.Accept(s.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
hErr := s.streamAcceptLoop(conn)
|
2024-10-21 15:38:34 +00:00
|
|
|
if hErr != nil && !E.IsClosedOrCanceled(hErr) {
|
|
|
|
s.logger.ErrorContext(conn.Context(), hErr)
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) streamAcceptLoop(conn quic.Connection) error {
|
|
|
|
for {
|
|
|
|
stream, err := conn.AcceptStream(s.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
go s.handler.NewConnectionEx(conn.Context(), &StreamWrapper{Conn: conn, Stream: stream}, M.SocksaddrFromNet(conn.RemoteAddr()), M.Socksaddr{}, nil)
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
2023-08-31 03:37:26 +00:00
|
|
|
return common.Close(s.udpListener, s.quicListener)
|
2022-08-22 13:20:05 +00:00
|
|
|
}
|