Exemplo n.º 1
0
        private async Task RegisterApplicationInstance(CancellationToken cancellationToken)
        {
            if (RuntimeEnvironment.ServiceId.HasValue && !RuntimeEnvironment.ServiceInstanceId.HasValue)
            {
                var osInfoRequest = new AgentOsInfoRequest
                {
                    HostName  = DnsHelpers.GetHostName(),
                    IpAddress = DnsHelpers.GetIpV4s(),
                    OsName    = PlatformInformation.GetOSName(),
                    ProcessNo = Process.GetCurrentProcess().Id
                };
                var value = await Polling(3,
                                          () => skyApmClient.RegisterApplicationInstanceAsync(RuntimeEnvironment.ServiceId.Value,
                                                                                              RuntimeEnvironment.InstanceId,
                                                                                              DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), osInfoRequest, cancellationToken),
                                          cancellationToken);

                if (value.HasValue && RuntimeEnvironment is RuntimeEnvironment environment)
                {
                    environment.ServiceInstanceId = value;
                    Logger.LogInformation(
                        $"Registered Application Instance[Id={environment.ServiceInstanceId.Value}].");
                }
            }
        }
Exemplo n.º 2
0
        private async Task RegisterServiceInstanceAsync(CancellationToken cancellationToken)
        {
            if (RuntimeEnvironment.ServiceId.HasValue && !RuntimeEnvironment.ServiceInstanceId.HasValue)
            {
                var properties = new AgentOsInfoRequest
                {
                    HostName = DnsHelpers.GetHostName(),
                    IpAddress = DnsHelpers.GetIpV4s(),
                    OsName = PlatformInformation.GetOSName(),
                    ProcessNo = Process.GetCurrentProcess().Id,
                    Language = "dotnet"
                };
                var request = new ServiceInstanceRequest
                {
                    ServiceId = RuntimeEnvironment.ServiceId.Value,
                    InstanceUUID = RuntimeEnvironment.InstanceId.ToString("N"),
                    Properties = properties
                };
                var value = await Polling(3,
                    () => _serviceRegister.RegisterServiceInstanceAsync(request, cancellationToken),
                    cancellationToken);

                if (value.HasValue && RuntimeEnvironment is RuntimeEnvironment)
                {
                    var environment = (RuntimeEnvironment)RuntimeEnvironment;

                    environment.ServiceInstanceId = value;
                    Logger.Information($"Registered ServiceInstance[Id={environment.ServiceInstanceId.Value}].");
                }
            }
        }
Exemplo n.º 3
0
        private async Task ReportServiceInstancePropertiesAsync(CancellationToken cancellationToken)
        {
            var properties = new AgentOsInfoRequest
            {
                HostName  = DnsHelpers.GetHostName(),
                IpAddress = DnsHelpers.GetIpV4s(),
                OsName    = PlatformInformation.GetOSName(),
                ProcessNo = Process.GetCurrentProcess().Id,
                Language  = "dotnet"
            };
            var request = new ServiceInstancePropertiesRequest
            {
                ServiceId         = _config.ServiceName,
                ServiceInstanceId = _config.ServiceInstanceName,
                Properties        = properties
            };
            var result = await Polling(3,
                                       () => _serviceRegister.ReportInstancePropertiesAsync(request, cancellationToken),
                                       cancellationToken);

            if (result && RuntimeEnvironment is RuntimeEnvironment environment)
            {
                environment.Initialized = true;
                Logger.Information($"Reported Service Instance Properties[Service={request.ServiceId},InstanceId={request.ServiceInstanceId}].");
            }
        }
Exemplo n.º 4
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(_algorithmName);
            sb.Append(" ");
            sb.Append(_timeSigned);
            sb.Append(" ");
            sb.Append(_fudge);
            sb.Append(" ");
            sb.Append(_mac.Length);
            sb.Append(" ");
            sb.Append(Convert.ToBase64String(Mac));
            sb.Append(" ");
            sb.Append(_error);
            sb.Append(" ");

            if (_otherData == null)
            {
                sb.Append(0);
            }
            else
            {
                sb.Append(_otherData.Length);
                sb.Append(" ");

                if (_error == RCode.BADTIME)
                {
                    if (_otherData.Length != 6)
                    {
                        sb.Append("<invalid BADTIME other data>");
                    }
                    else
                    {
                        long time = ((long)(_otherData[0] & 0xFF) << 40) +
                                    ((long)(_otherData[1] & 0xFF) << 32) +
                                    ((_otherData[2] & 0xFF) << 24) +
                                    ((_otherData[3] & 0xFF) << 16) +
                                    ((_otherData[4] & 0xFF) << 8) +
                                    ((_otherData[5] & 0xFF));

                        sb.Append("<server time: ");
                        sb.Append(DnsHelpers.ConvertFromDnsTime(time));
                        sb.Append(">");
                    }
                }
                else
                {
                    sb.Append("<");
                    sb.Append(Convert.ToBase64String(_otherData));
                    sb.Append(">");
                }
            }

            return(sb.ToString());
        }
Exemplo n.º 5
0
        public override byte[] GetMessageBytes()
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] data = DnsHeader.GetMessageBytes();
            memoryStream.Write(data, 0, data.Length);

            long rLengthPosition = memoryStream.Position;

            data = DnsHelpers.CanonicaliseDnsName(_algorithmName, false);
            memoryStream.Write(data, 0, data.Length);

            int  timeHigh;
            long timeLow;

            DnsHelpers.ConvertToDnsTime(_timeSigned.ToUniversalTime(), out timeHigh, out timeLow);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)timeHigh) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((uint)timeLow) >> 32));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_fudge) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_mac.Length) >> 16));
            memoryStream.Write(data, 0, data.Length);

            memoryStream.Write(_mac, 0, _mac.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_originalId) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_error) >> 16));
            memoryStream.Write(data, 0, data.Length);


            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_otherData.Length) >> 16));
            memoryStream.Write(data, 0, data.Length);

            if (_otherData.Length != 0)
            {
                memoryStream.Write(_otherData, 0, _otherData.Length);
            }

            // Add the rdata lenght
            long rlength = memoryStream.Position - rLengthPosition;

            memoryStream.Seek(rLengthPosition - 2, SeekOrigin.Begin);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)rlength) >> 16));
            memoryStream.Write(data, 0, data.Length);

            return(memoryStream.ToArray());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Apply a TSIG record to the request message.
        /// </summary>
        /// <param name="dnsQueryRequest">The <see cref="DnsQueryRequest"/> to add the security headers too.</param>
        /// <returns>A <see cref="DnsQueryRequest"/> instance with additional security attributes assigned.</returns>
        public DnsQueryRequest SecureMessage(DnsQueryRequest dnsQueryRequest)
        {
            DateTime signDateTime = DateTime.Now;
            int      timeHigh;
            long     timeLow;

            byte[] messageBytes = dnsQueryRequest.GetMessageBytes();
            Trace.WriteLine(String.Format("Message Header Bytes: {0}", DnsHelpers.DumpArrayToString(messageBytes)));

            MemoryStream memoryStream = new MemoryStream();

            memoryStream.Write(messageBytes, 0, messageBytes.Length);

            // the shared key name
            byte[] data = DnsHelpers.CanonicaliseDnsName(_name, false);
            memoryStream.Write(data, 0, data.Length);
            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)NsClass.ANY) >> 16));
            memoryStream.Write(data, 0, data.Length);
            // the TTL value
            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((uint)0) >> 32));
            memoryStream.Write(data, 0, data.Length);
            // the algorithim name
            data = DnsHelpers.CanonicaliseDnsName(_algorithimName, true);
            memoryStream.Write(data, 0, data.Length);

            DnsHelpers.ConvertToDnsTime(signDateTime.ToUniversalTime(), out timeHigh, out timeLow);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)timeHigh) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((uint)timeLow) >> 32));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_fudge) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)RCode.NoError) >> 16));
            memoryStream.Write(data, 0, data.Length);

            // no other data
            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)0) >> 16));
            memoryStream.Write(data, 0, data.Length);

            byte[] dataToHash = memoryStream.ToArray();
            Trace.WriteLine(String.Format("Data to hash: {0}", DnsHelpers.DumpArrayToString(dataToHash)));
            byte[] mac = _hmac.ComputeHash(dataToHash);
            Trace.WriteLine(String.Format("hash: {0}", DnsHelpers.DumpArrayToString(mac)));

            dnsQueryRequest.AdditionalRRecords.Add(new TSigRecord(_name, _algorithimName, RCode.NoError, _fudge, dnsQueryRequest.TransactionID, new byte[] { }, mac, signDateTime));

            return(dnsQueryRequest);
        }
Exemplo n.º 7
0
        public override void ParseRecord(ref MemoryStream memoryStream)
        {
            Byte[] dataUInt16 = new byte[2];
            Byte[] dataUInt32 = new byte[4];

            _algorithmName = ParseName(ref memoryStream);

            memoryStream.Read(dataUInt16, 0, dataUInt16.Length);
            long timeHigh = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataUInt16, 0));

            memoryStream.Read(dataUInt32, 0, dataUInt32.Length);
            long timeLow = (uint)IPAddress.NetworkToHostOrder((int)BitConverter.ToUInt32(dataUInt32, 0));

            _timeSigned = DnsHelpers.ConvertFromDnsTime(timeLow, timeHigh);

            memoryStream.Read(dataUInt16, 0, dataUInt16.Length);
            _fudge = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataUInt16, 0));

            memoryStream.Read(dataUInt16, 0, dataUInt16.Length);
            Int32 macLen = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataUInt16, 0));

            _mac = new byte[macLen];
            memoryStream.Read(_mac, 0, macLen);

            memoryStream.Read(dataUInt16, 0, dataUInt16.Length);
            _originalId = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataUInt16, 0));

            memoryStream.Read(dataUInt16, 0, dataUInt16.Length);
            _error = (RCode)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataUInt16, 0));

            memoryStream.Read(dataUInt16, 0, dataUInt16.Length);
            Int32 otherLen = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataUInt16, 0));

            if (otherLen > 0)
            {
                _otherData = new byte[otherLen];
                memoryStream.Read(_otherData, 0, otherLen);
            }
            else
            {
                _otherData = null;
            }

            _answer = ToString();
        }
Exemplo n.º 8
0
        internal byte[] GetMessageBytes()
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] data = new byte[2];

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_transactionId) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_flags) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_questions) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_answerRRs) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_authorityRRs) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_additionalRecords.Count) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = DnsHelpers.CanonicaliseDnsName(_name, false);
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_nsType) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_nsClass) >> 16));
            memoryStream.Write(data, 0, data.Length);

            foreach (IDnsRecord dnsRecord in AdditionalRRecords)
            {
                data = dnsRecord.GetMessageBytes();
                memoryStream.Write(data, 0, data.Length);
            }

            Trace.WriteLine(String.Format("The message bytes: {0}", DnsHelpers.DumpArrayToString(memoryStream.ToArray())));

            return(memoryStream.ToArray());
        }
Exemplo n.º 9
0
        internal byte[] GetMessageBytes()
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] data = DnsHelpers.CanonicaliseDnsName(_name, false);
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_nsType) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_nsClass) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((ushort)_timeToLive) >> 32));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_dataLength) >> 16));
            memoryStream.Write(data, 0, data.Length);

            return(memoryStream.ToArray());
        }
Exemplo n.º 10
0
        private int instance(Channel connection, string instanceUUID, int serviceId)
        {
            var serviceInstanceId = 0;

            var properties = new AgentOsInfoRequest
            {
                HostName  = DnsHelpers.GetHostName(),
                IpAddress = DnsHelpers.GetIpV4s(),
                OsName    = PlatformInformation.GetOSName(),
                ProcessNo = Process.GetCurrentProcess().Id,
                Language  = "dotnet"
            };
            var request = new ServiceInstanceRequest
            {
                ServiceId    = serviceId,
                InstanceUUID = instanceUUID,
                Properties   = properties
            };

            var client   = new Register.RegisterClient(connection);
            var instance = new ServiceInstance
            {
                ServiceId    = request.ServiceId,
                InstanceUUID = request.InstanceUUID,
                Time         = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            };

            instance.Properties.Add(new KeyStringValuePair
            {
                Key = OS_NAME, Value = request.Properties.OsName
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = HOST_NAME, Value = request.Properties.HostName
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = PROCESS_NO, Value = request.Properties.ProcessNo.ToString()
            });
            instance.Properties.Add(new KeyStringValuePair
            {
                Key = LANGUAGE, Value = request.Properties.Language
            });
            foreach (var ip in request.Properties.IpAddress)
            {
                instance.Properties.Add(new KeyStringValuePair {
                    Key = IPV4, Value = ip
                });
            }

            var serviceInstances = new ServiceInstances();

            serviceInstances.Instances.Add(instance);

            try
            {
                var value = Task.Run(async() =>
                {
                    return(await Polly.Polling(3,
                                               () => client.doServiceInstanceRegisterAsync(serviceInstances)));
                }).Result;

                //var reply = Task.Run(async () =>
                //{
                //    return await client.doServiceInstanceRegisterAsync(serviceInstances);
                //}).Result;
                //foreach (var serviceInstance in reply.ServiceInstances)
                //{
                //    serviceInstanceId = serviceInstance.Value;
                //}

                serviceInstanceId = value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(serviceInstanceId);
        }