// /** // * File: TCP.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using System.Net.Sockets; namespace ln.skyscanner.crawl.service { public class TCP : CrawlService { public int[] Ports { get; } public TCP(int[] ports) :base("tcp") { Ports = ports; } public override bool Check(Crawl crawl) { if (!crawl.CheckRequiredOption("ping")) return false; int n = 0; foreach (int port in Ports) { crawl.UpdateState(this, (double)n / (double)Ports.Length); try { using (TcpClient tcpClient = new TcpClient()) { IAsyncResult ar = tcpClient.BeginConnect(crawl.Host.PrimaryIP.ToString(), port, null, null); System.Threading.WaitHandle wh = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1), false)) { tcpClient.Close(); throw new TimeoutException(); } tcpClient.EndConnect(ar); } catch (Exception) { throw; } tcpClient.Close(); crawl.Host.SetHint(String.Format("tcp.{0}", port), true); } } catch (Exception) { crawl.Host.SetHint(String.Format("tcp.{0}", port), false); } } return true; } public override bool HostProvidesOption(Crawl crawl,params object[] parameters) { if (parameters.Length == 0) return false; foreach (object oport in parameters) { int port = (int)oport; if (!crawl.Host.GetHint(String.Format("tcp.{0}", port),false)) return false; } return true; } } }