Ejemplo n.º 1
0
 internal async Task onConnectionException(InConnection inc, Exception e)
 {
     Logger.exception(e, Logging.Level.Error, $"Handling {inc}");
     if (inc.IsHandled == false)
     {
         await inc.SetResult(new ConnectResultBase(this, ConnectResultEnum.Failed) {
             FailedReason = $"exception: {e.GetType()}: {e.Message}"
         });
     }
 }
Ejemplo n.º 2
0
 public virtual Task HandleConnection(InConnection connection)
 {
     if (connection is InConnectionTcp tcp)
     {
         return(this.HandleTcpConnection(tcp));
     }
     else
     {
         throw new NotSupportedException("cannot handle this type of connection.");
     }
 }
Ejemplo n.º 3
0
 public async Task HandleInConnection(InConnection inc, IConnectionHandler outAdapter)
 {
     try {
         if (outAdapter == null)
         {
             warning($"'{inc.InAdapter.Name}' {inc} -> (no out adapter)");
             return;
         }
         onConnectionBegin(inc, outAdapter);
         int redirectCount = 0;
         while (true)
         {
             inc.RunningHandler = outAdapter;
             if (outAdapter is IConnectionHandler2 ich2)
             {
                 await ich2.HandleConnection(inc).CAF();
             }
             else if (inc is InConnectionTcp tcp)
             {
                 await outAdapter.HandleTcpConnection(tcp).CAF();
             }
             else
             {
                 throw new Exception($"'{outAdapter.Name}' cannot handle this type of connection.");
             }
             if (inc.IsHandled || !inc.IsRedirected)
             {
                 break;
             }
             if (++redirectCount >= 10)
             {
                 error($"'{inc.InAdapter.Name}' {inc} too many redirects, last redirect: {outAdapter.Name}");
                 return;
             }
             var nextAdapter = inc.Redirected?.Adapter as IConnectionHandler;
             if (nextAdapter == null)
             {
                 warning($"'{inc.InAdapter.Name}' {inc} was redirected by '{outAdapter.Name}'" +
                         $" to '{inc.Redirected}' which can not be found.");
                 return;
             }
             outAdapter = nextAdapter;
             if (LoggingLevel <= Logging.Level.None)
             {
                 debug($"'{inc.InAdapter.Name}' {inc} was redirected to '{inc.Redirected}'");
             }
             inc.Redirected = null;
         }
     } catch (Exception e) {
         await onConnectionException(inc, e).CAF();
     } finally {
         await onConnectionEnd(inc).CAF();
     }
 }
Ejemplo n.º 4
0
        protected Task HandleIncommingConnection(InConnection inConnection, AdapterRef outRef)
        {
            if (rdns?.Adapter is DnsInAdapter dnsIn)
            {
                try {
                    dnsIn.HandleRdns(inConnection);
                } catch (Exception e) {
                    Logger.exception(e, Logging.Level.Error, "rdns handling");
                }
            }

            return(Controller.HandleInConnection(inConnection, outRef.Adapter as IConnectionHandler));
        }
Ejemplo n.º 5
0
 public Task HandleInConnection(InConnection inConnection)
 {
     if (inConnection == null)
     {
         throw new ArgumentNullException(nameof(inConnection));
     }
     if (inConnection.InAdapter is IInAdapter ina)
     {
         return(HandleInConnection(inConnection, [email protected] as IConnectionHandler));
     }
     else
     {
         throw new ArgumentException($"InConnection.InAdapter({inConnection.InAdapter}) does not implement IInAdapter.");
     }
 }
Ejemplo n.º 6
0
 public Task HandleConnection(InConnection connection)
 {
     if (connection is InConnectionDns dns)
     {
         return(ResolveName(dns));
     }
     else if (connection is InConnectionTcp)
     {
         throw new NotSupportedException("This is just a dns client and cannot handle regular connection.");
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Ejemplo n.º 7
0
 public Task HandleConnection(InConnection connection)
 {
     if (connection is InConnectionTcp tcp)
     {
         return(HandleTcpConnection(tcp));
     }
     else if (connection is InConnectionDns dns)
     {
         return(HandleDnsConnection(dns));
     }
     else
     {
         throw new NotSupportedException("cannot handle this type of connection");
     }
 }
Ejemplo n.º 8
0
 private void onConnectionBegin(InConnection inc, IAdapter outAdapter)
 {
     if (LoggingLevel <= Logging.Level.None)
     {
         debug($"'{inc.InAdapter.Name}' {inc} -> '{outAdapter.Name}'");
     }
     try {
         lock (InConnectionsLock) {
             inc.InAdapter.GetAdapter().CreatedConnections++;
             _totalHandledConnections++;
             InConnections.Add(inc.Id, inc);
             NewConnection?.Invoke(inc);
         }
     } catch (Exception e) {
         Logger.exception(e, Logging.Level.Error, "event NewConnection");
     }
 }
Ejemplo n.º 9
0
        public Task HandleInConnection(InConnection inc, AdapterRef outAdapterRef)
        {
            if (outAdapterRef == null)
            {
                throw new ArgumentNullException(nameof(outAdapterRef));
            }
            IConnectionHandler adapter = outAdapterRef.Adapter as IConnectionHandler;

            if (adapter == null)
            {
                if (LoggingLevel <= Logging.Level.Warning)
                {
                    warning($"null out adapter reference ({outAdapterRef})");
                }
            }
            return(HandleInConnection(inc, adapter));
        }
Ejemplo n.º 10
0
        public Task HandleInConnection(InConnection inc, string outAdapterName)
        {
            if (string.IsNullOrEmpty(outAdapterName))
            {
                throw new ArgumentException("outAdapterName is null or empty");
            }
            IConnectionHandler selectedAdapter = FindOutAdapter(outAdapterName);

            if (selectedAdapter == null)
            {
                if (LoggingLevel <= Logging.Level.Warning)
                {
                    warning($"out adapter '{outAdapterName}' not found");
                }
            }
            return(HandleInConnection(inc, selectedAdapter));
        }
Ejemplo n.º 11
0
 public void HandleRdns(InConnection x)
 {
     if (IPAddress.TryParse(x.Dest.Host, out var ip))
     {
         string host = null;
         if (ip.AddressFamily == AddressFamily.InterNetwork)
         {
             host = cacheDns.QueryByIp((uint)ip.Address);
         }
         else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
         {
             host = cacheDns.QueryByIp6(new Ip6(ip));
         }
         if (host != null)
         {
             x.DestOriginalName = host;
         }
     }
 }
Ejemplo n.º 12
0
 public override Task HandleConnection(InConnection connection)
 {
     if (connection is InConnectionDns dns)
     {
         if (redirect_dns != null)
         {
             dns.RedirectTo(redirect_dns);
             return(NaiveUtils.CompletedTask);
         }
         return(ResolveNameCore(dns));
     }
     if (connection is InConnectionUdp udp)
     {
         var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         return(connection.SetResult(new ConnectResultUdp(this, ConnectResultEnum.OK)
         {
             UdpSocket = GetMyStreamFromSocket(socket) as IUdpSocket
         }));
     }
     return(base.HandleConnection(connection));
 }
Ejemplo n.º 13
0
        internal async Task onConnectionEnd(InConnection inc)
        {
            inc.RunningHandler = null;
            if (LoggingLevel <= Logging.Level.None)
            {
                debug($"{inc} End.");
            }
            await inc.Finish();

            try {
                lock (InConnectionsLock) {
                    if (inc.ConnectResult?.Result != ConnectResultEnum.OK)
                    {
                        _failedConnections++;
                    }
                    InConnections.Remove(inc.Id);
                    EndConnection?.Invoke(inc);
                }
            } catch (Exception e) {
                Logger.exception(e, Logging.Level.Error, "event EndConnection");
            }
        }
Ejemplo n.º 14
0
        public Task HandleConnection(InConnection connection)
        {
            var sw = Stopwatch.StartNew();

            if (Handle(connection, out var redir))
            {
                connection.RedirectTo(redir);
            }
            else
            {
                connection.RedirectTo(@default);
            }
            if (logging)
            {
                var ms   = sw.ElapsedMilliseconds;
                var dest = connection.GetDestStringWithOriginalName();
                if (log_uri && connection.Url != null)
                {
                    dest = connection.Url;
                }
                Logger.info($"{connection.Redirected} <- {dest} - '{connection.InAdapter?.Name}' ({ms} ms)");
            }
            return(AsyncHelper.CompletedTask);
        }
Ejemplo n.º 15
0
 protected Task HandleIncommingConnection(InConnection inConnection)
 {
     return(HandleIncommingConnection(inConnection, @out));
 }