コード例 #1
0
ファイル: AuthInterceptor.cs プロジェクト: zhxymh/AElf
        public override Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            try
            {
                if (context.Method != GetFullMethodName(nameof(PeerService.PeerServiceBase.DoHandshake)))
                {
                    // a method other than DoHandshake is being called

                    var peer = _peerPool.FindPeerByPublicKey(context.GetPublicKey());

                    if (peer == null && context.Method != GetFullMethodName(nameof(PeerService.PeerServiceBase.Ping)))
                    {
                        Logger.LogWarning($"Could not find peer {context.GetPublicKey()}");
                        return(Task.FromResult <TResponse>(null));
                    }

                    // check that the peers session is equal to one announced in the headers
                    if (peer != null && !peer.InboundSessionId.BytesEqual(context.GetSessionId()))
                    {
                        Logger.LogWarning($"Wrong session id, ({peer.InboundSessionId.ToHex()} vs {context.GetSessionId().ToHex()}) {context.GetPublicKey()}");
                        return(Task.FromResult <TResponse>(null));
                    }

                    context.RequestHeaders.Add(new Metadata.Entry(GrpcConstants.PeerInfoMetadataKey, $"{peer}"));
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Auth interceptor error: ");
                throw;
            }

            return(continuation(request, context));
        }
コード例 #2
0
ファイル: AuthInterceptor.cs プロジェクト: zhxymh/AElf
        public override Task <TResponse> ClientStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, ServerCallContext context,
                                                                                            ClientStreamingServerMethod <TRequest, TResponse> continuation)
        {
            try
            {
                var peer = _peerPool.FindPeerByPublicKey(context.GetPublicKey());

                if (peer == null)
                {
                    Logger.LogWarning($"Could not find peer {context.GetPublicKey()}");
                    return(Task.FromResult <TResponse>(null));
                }

                if (!peer.InboundSessionId.BytesEqual(context.GetSessionId()))
                {
                    Logger.LogWarning($"Wrong session id, ({peer.InboundSessionId.ToHex()} vs {context.GetSessionId().ToHex()}) {context.GetPublicKey()}");
                    return(Task.FromResult <TResponse>(null));
                }

                context.RequestHeaders.Add(new Metadata.Entry(GrpcConstants.PeerInfoMetadataKey, $"{peer}"));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Auth stream interceptor error: ");
                return(null);
            }

            return(continuation(requestStream, context));
        }
コード例 #3
0
        public override Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request,
                                                                                  ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            try
            {
                if (IsNeedAuth(context.Method))
                {
                    var peer = _peerPool.FindPeerByPublicKey(context.GetPublicKey());

                    if (peer == null)
                    {
                        Logger.LogWarning($"Could not find peer {context.GetPublicKey()}");
                        return(Task.FromResult <TResponse>(null));
                    }

                    // check that the peers session is equal to one announced in the headers
                    var sessionId = context.GetSessionId();

                    if (!peer.InboundSessionId.BytesEqual(sessionId))
                    {
                        if (peer.InboundSessionId == null)
                        {
                            Logger.LogWarning($"Wrong inbound session id {context.Peer}, {context.Method}");
                            return(Task.FromResult <TResponse>(null));
                        }

                        if (sessionId == null)
                        {
                            Logger.LogWarning($"Wrong context session id {context.Peer}, {context.Method}, {peer}");
                            return(Task.FromResult <TResponse>(null));
                        }

                        Logger.LogWarning(
                            $"Unequal session id, {context.Peer} ({peer.InboundSessionId.ToHex()} vs {sessionId.ToHex()}) {context.GetPublicKey()}");
                        return(Task.FromResult <TResponse>(null));
                    }

                    context.RequestHeaders.Add(new Metadata.Entry(GrpcConstants.PeerInfoMetadataKey, $"{peer}"));
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Auth interceptor error {context.Peer}, {context.Method}: ");
                throw;
            }

            return(continuation(request, context));
        }