public void Configuration(IAppBuilder app) { var logger = LogManager.GetLogger(this.GetType()); logger.Info("Starting..."); // Setup WebAPI configuration var configuration = new HttpConfiguration(); configuration.Routes.Add("API Default", new HttpRoute("api/{Controller}/{action}")); // Configure OWIN pipeline // Configure NInject for WebAPI app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(configuration); // Register the WebAPI to the pipeline. It is required for Configuration WebAPI app.UseWebApi(configuration); var options = new FileServerOptions { RequestPath = new PathString("/admin"), FileSystem = new PhysicalFileSystem(@".\public"), }; app.UseFileServer(options); var config = Owin.ApiGateway.Configuration.Configuration.Current ?? Owin.ApiGateway.Configuration.Configuration.Load(); app.UseConfigurationManager(this.GetCurrentConfiguration, logger); var requestResponseLogStoreWriter = new SqlServerRequestResponseLogWriter(logger); var requestResponseLogger = new BackgroundThreadLogger(requestResponseLogStoreWriter, logger); app.UseRequestResponseLogger(logger, requestResponseLogger); app.UseCache(new MemoryCacheProvider(), logger); app.UseRoutingManagerMiddleware(logger, this.GetCurrentConfiguration); app.UseProxy(logger, new ProxyOptions { VerboseMode = false }); // configure and start endpoints health checking IServiceProbe serviceProbe = new ServiceProbe(); serviceProbe.Initialize(this.GetCurrentConfiguration, logger); serviceProbe.Start(); configuration.Formatters.Clear(); configuration.Formatters.Add(new JsonMediaTypeFormatter()); configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; }
private string GrabBanner(ServiceProbe serviceProbe, IPAddress ipAddress, ushort port) { var sendString = Regex.Unescape(serviceProbe.Probe); var bytes = Encoding.ASCII.GetBytes(sendString); try { if (serviceProbe.ProtocolType == Layer4ProtocolType.TCP) { using (var client = new TcpClient()) { client.ReceiveTimeout = 5000; var endPoint = new IPEndPoint(ipAddress, port); client.Connect(endPoint); var networkStream = client.GetStream(); if (bytes.Length > 0) { networkStream.Write(bytes, 0, bytes.Length); } var receivedBytes = new byte[client.ReceiveBufferSize]; var bytesRead = networkStream.Read(receivedBytes, 0, client.ReceiveBufferSize); var result = Encoding.ASCII.GetString(receivedBytes); return(result); } } else if (serviceProbe.ProtocolType == Layer4ProtocolType.UDP) { using (var client = new UdpClient()) { client.Client.SendTimeout = 5000; client.Client.ReceiveTimeout = 5000; var endPoint = new IPEndPoint(ipAddress, port); client.Connect(endPoint); client.Send(bytes, bytes.Length); var receivedBytes = client.Receive(ref endPoint); var result = Encoding.ASCII.GetString(receivedBytes); return(result); } } } catch (Exception) { return(string.Empty); } throw new NotImplementedException($"Unknown protocol '{serviceProbe.ProtocolType}'"); }
public NmapServiceProbeParser() { var lines = File.ReadAllLines("nmap-service-probes.txt"); var protocolType = Layer4ProtocolType.TCP; var name = string.Empty; var probe = string.Empty; var ports = new List <ushort>(); var sslPorts = new List <ushort>(); foreach (var line in lines) { if (line.StartsWith("Probe")) { if (name != string.Empty) { var serviceProbe = new ServiceProbe(protocolType, name, probe, ports, sslPorts); this.serviceProbes.Add(serviceProbe); } ports = new List <ushort>(); sslPorts = new List <ushort>(); var protocolAndName = line.Split(' '); var protocol = protocolAndName[1].ToLower(); if (protocol == Layer4ProtocolType.TCP.ToString().ToLower()) { protocolType = Layer4ProtocolType.TCP; } else if (protocol == Layer4ProtocolType.UDP.ToString().ToLower()) { protocolType = Layer4ProtocolType.UDP; } name = protocolAndName[2]; var startIndex = line.IndexOf("q|") + 2; var endIndex = line.LastIndexOf("|"); probe = line.Substring(startIndex, endIndex - startIndex); } else if (line.StartsWith("ports")) { var portRange = line.Replace("ports ", string.Empty); ports.AddRange(this.ParsePorts(portRange)); } else if (line.StartsWith("sslports")) { var portRange = line.Replace("sslports ", string.Empty); sslPorts.AddRange(this.ParsePorts(portRange)); } } }
public void ServiceInstanceIsMarkedAsDown_MonitoringEndpointReturnsSuccessMessage_ServiceInstanceIsMarkedAsUp() { var configuration = new Configuration.Configuration(); configuration.Endpoints.Add(new Configuration.RoutingEndpoint { Id = "service1", Instances = new Configuration.Instances { Instance = new System.Collections.Generic.List <Configuration.Instance> { new Configuration.Instance { Status = ApiGateway.Configuration.InstanceStatuses.Down, Url = "http://service1.com/requestPath" } } }, HealthCheck = new Configuration.HealthCheckConfiguration { MonitoringPath = "/status", ResponseShouldContainString = "OK" } }); // Arrange Func <Configuration.Configuration> configurationGen = () => { return(configuration); }; var logMock = new Mock <ILog>(); ILog logger = logMock.Object; var responseHandler = new FakeResponseHandler(); responseHandler.AddFakeResponseGenerator(new System.Uri("http://service1.com/status"), () => { var r = new HttpResponseMessage(HttpStatusCode.OK); r.Content = new StringContent("Service is OK."); return(r); }); // Act var serviceProbe = new ServiceProbe(httpClientMessageHandler: responseHandler); serviceProbe.Initialize(configurationGen, logger); serviceProbe.TestEndpoint(configuration.Endpoints.First()); // Assert Assert.AreEqual(Configuration.InstanceStatuses.Up, configuration.Endpoints.First().Instances.Instance.First().Status); }
private string GrabSslBanner(ServiceProbe serviceProbe, IPAddress ipAddress, ushort port) { var sendString = Regex.Unescape(serviceProbe.Probe); var bytes = Encoding.ASCII.GetBytes(sendString); try { if (serviceProbe.ProtocolType == Layer4ProtocolType.TCP) { using (var client = new TcpClient()) { client.ReceiveTimeout = 5000; var endPoint = new IPEndPoint(ipAddress, port); client.Connect(endPoint); var networkStream = new SslStream(client.GetStream(), false, (w, x, y, z) => true); networkStream.AuthenticateAsClient(ipAddress.ToString()); if (bytes.Length > 0) { networkStream.Write(bytes, 0, bytes.Length); } var receivedBytes = new byte[client.ReceiveBufferSize]; var bytesRead = 0; try { bytesRead = networkStream.Read(receivedBytes, 0, client.ReceiveBufferSize); } catch (Exception) { } var certificateDescription = this.ExtractCertificateInfo(networkStream); var results = new List <string>(); results.Add(Encoding.ASCII.GetString(receivedBytes)); results.Add(certificateDescription); var result = string.Join("\r\n\r\n###############\r\n\r\n", results); return(result); } } else if (serviceProbe.ProtocolType == Layer4ProtocolType.UDP) { using (var client = new UdpClient()) { client.Client.SendTimeout = 5000; client.Client.ReceiveTimeout = 5000; var endPoint = new IPEndPoint(ipAddress, port); client.Connect(endPoint); client.Send(bytes, bytes.Length); var receivedBytes = client.Receive(ref endPoint); var result = Encoding.ASCII.GetString(receivedBytes); return(result); } } } catch (Exception) { return(string.Empty); } throw new NotImplementedException($"Unknown protocol '{serviceProbe.ProtocolType}'"); }