示例#1
0
        /// <summary>
        /// Return true if ipAddressOrHostName is valid ip address or host name.
        /// Ip address and host name will be returned in out parameters.
        /// </summary>
        /// <param name="ipAddressOrHostName"></param>
        /// <param name="ipAddress"></param>
        /// <param name="hostName"></param>
        /// <returns></returns>
        private static bool SolveIpAddressFromHostNameOrIpAddress(string ipAddressOrHostName, out string ipAddress, out string hostName)
        {
            ipAddress = string.Empty;
            hostName  = string.Empty;
            var ok = Conversion.IsValidIpAddress(ipAddressOrHostName);

            // no host name
            if (ok)
            {
                ipAddress = ipAddressOrHostName;
                return(true);
            }

            var list = DnsWrapper.GetHostIp4Addresses(ipAddressOrHostName);

            if (list == null || list.Count == 0)
            {
                return(false);
            }

            hostName = ipAddressOrHostName;
            foreach (var address in list)
            {
                string addr = address.ToString();
                if (Conversion.IsValidIpAddress(addr))
                {
                    ipAddress = addr;
                }
            }
            return(true);
        }
示例#2
0
        private ITransport MakeTransport(GraylogSinkOptions options)
        {
            switch (options.TransportType)
            {
            case SerilogTransportType.Udp:

                IDnsInfoProvider dns        = new DnsWrapper();
                IPAddress[]      ipAddreses = Task.Run(() => dns.GetHostAddresses(options.HostnameOrAddress)).Result;
                IPAddress        ipAddress  = ipAddreses.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);
                var ipEndpoint = new IPEndPoint(ipAddress, options.Port);

                IDataToChunkConverter chunkConverter = new DataToChunkConverter(new ChunkSettings
                {
                    MessageIdGeneratorType = options.MessageGeneratorType
                }, new MessageIdGeneratorResolver());

                var udpClient    = new UdpTransportClient(ipEndpoint);
                var udpTransport = new UdpTransport(udpClient, chunkConverter);
                return(udpTransport);

            case SerilogTransportType.Http:
                var httpClient    = new HttpTransportClient($"{options.HostnameOrAddress}:{options.Port}/gelf");
                var httpTransport = new HttpTransport(httpClient);
                return(httpTransport);

            default:
                throw new ArgumentOutOfRangeException(nameof(options), options.TransportType, null);
            }
        }
        public ITransport MakeTransport()
        {
            switch (_options.TransportType)
            {
            case SinkTransportType.Udp:

                IDnsInfoProvider dns        = new DnsWrapper();
                IPAddress[]      ipAddreses = Task.Run(() => dns.GetHostAddresses(_options.HostnameOrAddress)).Result;
                IPAddress        ipAddress  = ipAddreses.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);

                var ipEndpoint = new IPEndPoint(ipAddress ?? throw new InvalidOperationException(), _options.Port);


                var chunkSettings = new ChunkSettings(_options.MessageGeneratorType, _options.MaxMessageSizeInUdp);
                IDataToChunkConverter chunkConverter = new DataToChunkConverter(chunkSettings, new MessageIdGeneratorResolver());

                var udpClient    = new UdpTransportClient(ipEndpoint);
                var udpTransport = new UdpTransport(udpClient, chunkConverter);
                return(udpTransport);

            case SinkTransportType.Http:
                var httpClient    = new HttpTransportClient($"{_options.HostnameOrAddress}:{_options.Port}/gelf");
                var httpTransport = new HttpTransport(httpClient);
                return(httpTransport);

            default:
                throw new ArgumentOutOfRangeException(nameof(_options), _options.TransportType, null);
            }
        }
示例#4
0
        public GraylogSink(GraylogSinkOptions options)
        {
            IDnsInfoProvider dns = new DnsWrapper();

            IPAddress[] ipAddreses = Task.Run(() => dns.GetHostAddresses(options.HostnameOrAdress)).Result;

            IPAddress ipAdress = ipAddreses.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);

            var ipEndpoint = new IPEndPoint(ipAdress, options.Port);

            IDataToChunkConverter chunkConverter = new DataToChunkConverter(new ChunkSettings
            {
                MessageIdGeneratorType = options.MessageGeneratorType
            }, new MessageIdGeneratorResolver());

            var client = new UdpTransportClient(ipEndpoint);

            _transport = options.Transport ?? new UdpTransport(client, chunkConverter);

            string hostName = Dns.GetHostName();

            IDictionary <BuilderType, IMessageBuilder> builders = new Dictionary <BuilderType, IMessageBuilder>
            {
                [BuilderType.Exception] = new ExceptionMessageBuilder(hostName, options),
                [BuilderType.Message]   = new GelfMessageBuilder(hostName, options)
            };

            _converter = options.GelfConverter ?? new GelfConverter(builders);
        }
        public async void Test()
        {
            var traget = new DnsWrapper();

            IPAddress[] actual = await traget.GetHostAddresses("github.com");

            Assert.NotEmpty(actual);
        }
        private async Task <IPAddress> GetIpAddress(string hostnameOrAddress)
        {
            IDnsInfoProvider dns = new DnsWrapper();

            IPAddress[] ipAddreses = await dns.GetHostAddresses(hostnameOrAddress).ConfigureAwait(false);

            IPAddress ipAddress = ipAddreses.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);

            return(ipAddress);
        }
 public void Send(byte[] datagram, int bytes, Uri endPoint)
 {
     using (var udpClient = new UdpClient())
     {
         var dns       = new DnsWrapper();
         var addresses = dns.GetHostAddresses(endPoint.Host);
         var ip        = addresses.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
         int result    = udpClient.SendAsync(datagram, bytes, new IPEndPoint(ip, endPoint.Port)).Result;
     }
 }
示例#8
0
        public void TestAddressList()
        {
            var hostName = DnsWrapper.GetHostName;

            Assert.True(!String.IsNullOrWhiteSpace(hostName));

            var addresses = DnsWrapper.GetHostIp4Addresses(hostName);


            Assert.True(addresses.Count > 0);
        }
示例#9
0
        public GraylogSink(GraylogSinkOptions options)
        {
            _transport = options.Transport;

            if (_transport == null)
            {
                if (options.UseHttpTransport)
                {
                    _converter = new HttpGelfConverter(options.Facility, options.ShortMessageMaxLength);
                    _transport = new HttpTransport(options.HostnameOrAdress);   // should be in the form http://HostNameOrIp:Port//Gelf
                }
                else
                {
                    IDnsInfoProvider dns        = new DnsWrapper();
                    IPAddress[]      ipAddreses = Task.Run(() => dns.GetHostAddresses(options.HostnameOrAdress)).Result;

                    IPAddress ipAdress = ipAddreses.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);

                    var ipEndpoint = new IPEndPoint(ipAdress, options.Port);

                    IDataToChunkConverter chunkConverter = new DataToChunkConverter(new ChunkSettings
                    {
                        MessageIdGeneratorType = options.MessageGeneratorType
                    }, new MessageIdGeneratorResolver());

                    var client = new UdpTransportClient(ipEndpoint);
                    _transport = new UdpTransport(client, chunkConverter);
                }
            }

            string hostName = Dns.GetHostName();

            IDictionary <BuilderType, IMessageBuilder> builders = new Dictionary <BuilderType, IMessageBuilder>
            {
                [BuilderType.Exception] = new ExceptionMessageBuilder(hostName, options),
                [BuilderType.Message]   = new GelfMessageBuilder(hostName, options)
            };

            _converter = options.GelfConverter ?? new GelfConverter(builders);
        }
示例#10
0
 public void RemoveDns()
 {
     DnsWrapper.RemoveDnsRule();
 }
示例#11
0
 public void ApplyDns(params string[] dns)
 {
     DnsWrapper.AddDnsRule(dns);
 }