2022-08-22 18:53:47 +08:00
|
|
|
package v2raygrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2022-08-22 21:20:05 +08:00
|
|
|
"os"
|
2022-09-25 19:29:29 +08:00
|
|
|
"strings"
|
2023-03-11 15:49:02 +08:00
|
|
|
"time"
|
2022-08-22 18:53:47 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-09-09 18:45:10 +08:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2024-10-21 23:38:34 +08:00
|
|
|
"github.com/sagernet/sing-box/log"
|
2022-08-22 20:20:56 +08:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-04-10 13:00:57 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2024-10-21 23:38:34 +08:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-08-22 18:53:47 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
|
2023-04-11 11:38:53 +08:00
|
|
|
"golang.org/x/net/http2"
|
2022-08-22 18:53:47 +08:00
|
|
|
"google.golang.org/grpc"
|
2023-03-11 15:49:02 +08:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2022-09-25 19:29:29 +08:00
|
|
|
gM "google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/peer"
|
2022-08-22 18:53:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
ctx context.Context
|
2024-10-21 23:38:34 +08:00
|
|
|
logger logger.ContextLogger
|
|
|
|
handler adapter.V2RayServerTransportHandler
|
2022-08-22 18:53:47 +08:00
|
|
|
server *grpc.Server
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
|
2022-08-22 18:53:47 +08:00
|
|
|
var serverOptions []grpc.ServerOption
|
|
|
|
if tlsConfig != nil {
|
2023-04-11 11:38:53 +08:00
|
|
|
if !common.Contains(tlsConfig.NextProtos(), http2.NextProtoTLS) {
|
|
|
|
tlsConfig.SetNextProtos(append([]string{"h2"}, tlsConfig.NextProtos()...))
|
|
|
|
}
|
2022-11-09 11:43:03 +08:00
|
|
|
serverOptions = append(serverOptions, grpc.Creds(NewTLSTransportCredentials(tlsConfig)))
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|
2023-03-11 15:49:02 +08:00
|
|
|
if options.IdleTimeout > 0 {
|
|
|
|
serverOptions = append(serverOptions, grpc.KeepaliveParams(keepalive.ServerParameters{
|
|
|
|
Time: time.Duration(options.IdleTimeout),
|
|
|
|
Timeout: time.Duration(options.PingTimeout),
|
|
|
|
}))
|
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
server := &Server{ctx, logger, handler, grpc.NewServer(serverOptions...)}
|
2022-08-22 20:20:56 +08:00
|
|
|
RegisterGunServiceCustomNameServer(server.server, server, options.ServiceName)
|
2022-09-09 18:45:10 +08:00
|
|
|
return server, nil
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Tun(server GunService_TunServer) error {
|
2024-10-21 23:38:34 +08:00
|
|
|
conn := NewGRPCConn(server)
|
|
|
|
var source M.Socksaddr
|
2022-09-25 19:29:29 +08:00
|
|
|
if remotePeer, loaded := peer.FromContext(server.Context()); loaded {
|
2024-10-21 23:38:34 +08:00
|
|
|
source = M.SocksaddrFromNet(remotePeer.Addr)
|
2022-09-25 19:29:29 +08:00
|
|
|
}
|
|
|
|
if grpcMetadata, loaded := gM.FromIncomingContext(server.Context()); loaded {
|
|
|
|
forwardFrom := strings.Join(grpcMetadata.Get("X-Forwarded-For"), ",")
|
|
|
|
if forwardFrom != "" {
|
|
|
|
for _, from := range strings.Split(forwardFrom, ",") {
|
|
|
|
originAddr := M.ParseSocksaddr(from)
|
|
|
|
if originAddr.IsValid() {
|
2024-10-21 23:38:34 +08:00
|
|
|
source = originAddr.Unwrap()
|
2022-09-25 19:29:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
done := make(chan struct{})
|
|
|
|
go s.handler.NewConnectionEx(log.ContextWithNewID(s.ctx), conn, source, M.Socksaddr{}, N.OnceClose(func(it error) {
|
|
|
|
close(done)
|
|
|
|
}))
|
|
|
|
<-done
|
2022-08-22 18:53:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) mustEmbedUnimplementedGunServiceServer() {
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:20:05 +08:00
|
|
|
func (s *Server) Network() []string {
|
|
|
|
return []string{N.NetworkTCP}
|
|
|
|
}
|
|
|
|
|
2022-08-22 18:53:47 +08:00
|
|
|
func (s *Server) Serve(listener net.Listener) error {
|
|
|
|
return s.server.Serve(listener)
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:20:05 +08:00
|
|
|
func (s *Server) ServePacket(listener net.PacketConn) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
2022-08-22 18:53:47 +08:00
|
|
|
func (s *Server) Close() error {
|
|
|
|
s.server.Stop()
|
|
|
|
return nil
|
|
|
|
}
|