usingMicrosoft.AspNetCore.Http;usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Net;namespaceUEditorNetCore.Handlers{publicclassCrawlerHandler:Handler{privatestring[]Sources;privateCrawler[]Crawlers;publicCrawlerHandler(HttpContextcontext):base(context){}publicoverridevoidProcess(){Sources=Request.Form["source[]"];if(Sources==null||Sources.Length==0){WriteJson(new{state="参数错误:没有指定抓取源"});return;}Crawlers=Sources.Select(x=>newCrawler(x).Fetch()).ToArray();WriteJson(new{state="SUCCESS",list=Crawlers.Select(x=>new{state=x.State,source=x.SourceUrl,url=x.ServerUrl})});}}publicclassCrawler{publicstringSourceUrl{get;set;}publicstringServerUrl{get;set;}publicstringState{get;set;}//privateHttpServerUtilityServer{get;set;}publicCrawler(stringsourceUrl){this.SourceUrl=sourceUrl;//this.Server=server;}publicCrawlerFetch(){if(!IsExternalIPAddress(this.SourceUrl)){State="INVALID_URL";returnthis;}varrequest=HttpWebRequest.Create(this.SourceUrl)asHttpWebRequest;using(varresponse=request.GetResponseAsync().ResultasHttpWebResponse){if(response.StatusCode!=HttpStatusCode.OK){State="Url returns "+response.StatusCode+", "+response.StatusDescription;returnthis;}if(response.ContentType.IndexOf("image")==-1){State="Url is not an image";returnthis;}ServerUrl=PathFormatter.Format(Path.GetFileName(this.SourceUrl),Config.GetString("catcherPathFormat"));//varsavePath=Path.Combine(Config.WebRootPath,ServerUrl);varsavePath=Path.Combine(Config.GetString("catcherSaveAbsolutePath"),ServerUrl);//try{varstream=response.GetResponseStream();//if(Config.GetValue<bool>("catcherFtpUpload")){varfileExt=ServerUrl.Substring(savePath.LastIndexOf('.')).TrimStart('.');varkey=Config.GetString("catcherPathFormat")+"."+fileExt;//FtpUpload.UploadFile(stream,savePath,Consts.ImgFtpServer.ip,//Consts.ImgFtpServer.account,Consts.ImgFtpServer.pwd);AliyunOssUpload.UploadFile(Consts.AliyunOssServer.AccessEndpoint,Consts.AliyunOssServer.AccessKeyId,Consts.AliyunOssServer.AccessKeySecret,Consts.AliyunOssServer.BucketName,key,fileExt,stream);}else{if(!Directory.Exists(Path.GetDirectoryName(savePath))){Directory.CreateDirectory(Path.GetDirectoryName(savePath));}varreader=newBinaryReader(stream);byte[]bytes;using(varms=newMemoryStream()){byte[]buffer=newbyte[4096];intcount;while((count=reader.Read(buffer,0,buffer.Length))!=0){ms.Write(buffer,0,count);}bytes=ms.ToArray();}File.WriteAllBytes(savePath,bytes);}State="SUCCESS";}catch(Exceptione){State="抓取错误:"+e.Message;}returnthis;}}privateboolIsExternalIPAddress(stringurl){varuri=newUri(url);switch(uri.HostNameType){caseUriHostNameType.Dns:varipHostEntry=Dns.GetHostEntryAsync(uri.DnsSafeHost).Result;foreach(IPAddressipAddressinipHostEntry.AddressList){byte[]ipBytes=ipAddress.GetAddressBytes();if(ipAddress.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork){if(!IsPrivateIP(ipAddress)){returntrue;}}}break;caseUriHostNameType.IPv4:return!IsPrivateIP(IPAddress.Parse(uri.DnsSafeHost));}returnfalse;}privateboolIsPrivateIP(IPAddressmyIPAddress){if(IPAddress.IsLoopback(myIPAddress))returntrue;if(myIPAddress.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork){byte[]ipBytes=myIPAddress.GetAddressBytes();//10.0.0.0/24if(ipBytes[0]==10){returntrue;}//172.16.0.0/16elseif(ipBytes[0]==172&&ipBytes[1]==16){returntrue;}//192.168.0.0/16elseif(ipBytes[0]==192&&ipBytes[1]==168){returntrue;}//169.254.0.0/16elseif(ipBytes[0]==169&&ipBytes[1]==254){returntrue;}}returnfalse;}}}