private void CopyCommandLineArgs() { if (CommandLineParser.CommandLineArgs.TryGetValue("organizationid", out var orgID)) { OrganizationID = orgID; } if (CommandLineParser.CommandLineArgs.TryGetValue("serverurl", out var serverUrl)) { ServerUrl = serverUrl; } if (CommandLineParser.CommandLineArgs.TryGetValue("devicegroup", out var deviceGroup)) { DeviceGroup = deviceGroup; } if (CommandLineParser.CommandLineArgs.TryGetValue("devicealias", out var deviceAlias)) { DeviceAlias = deviceAlias; } if (CommandLineParser.CommandLineArgs.TryGetValue("deviceuuid", out var deviceUuid)) { DeviceUuid = deviceUuid; } if (ServerUrl?.EndsWith("/") == true) { ServerUrl = ServerUrl.Substring(0, ServerUrl.LastIndexOf("/")); } }
public void PreviousDirectory() { if (ServerUrl.Substring(ServerUrl.Length - 1) == "/") { ServerUrl = ServerUrl.Substring(0, ServerUrl.Length - 1); } var index = ServerUrl.LastIndexOf("/"); if (index > 0) { ServerUrl = ServerUrl.Substring(0, index + 1); } }
public Crawler Fetch() { if (!IsExternalIPAddress(this.SourceUrl)) { State = "INVALID_URL"; return(this); } var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest; using (var response = request.GetResponseAsync().Result as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { State = "Url returns " + response.StatusCode + ", " + response.StatusDescription; return(this); } if (response.ContentType.IndexOf("image") == -1) { State = "Url is not an image"; return(this); } ServerUrl = PathFormatter.Format(Path.GetFileName(this.SourceUrl), Config.GetString("catcherPathFormat")); //var savePath = Path.Combine(Config.WebRootPath, ServerUrl); var savePath = Path.Combine(Config.GetString("catcherSaveAbsolutePath"), ServerUrl); // try { var stream = response.GetResponseStream(); // if (Config.GetValue <bool>("catcherFtpUpload")) { var fileExt = ServerUrl.Substring(savePath.LastIndexOf('.')).TrimStart('.'); var key = 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)); } var reader = new BinaryReader(stream); byte[] bytes; using (var ms = new MemoryStream()) { byte[] buffer = new byte[4096]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { ms.Write(buffer, 0, count); } bytes = ms.ToArray(); } File.WriteAllBytes(savePath, bytes); } State = "SUCCESS"; } catch (Exception e) { State = "抓取错误:" + e.Message; } return(this); } }
/// <summary> /// Validates the SSL server certificate. /// </summary> /// <param name="sender">An object that contains state information for this /// validation.</param> /// <param name="cert">The certificate used to authenticate the remote party.</param> /// <param name="chain">The chain of certificate authorities associated with the /// remote certificate.</param> /// <param name="sslPolicyErrors">One or more errors associated with the remote /// certificate.</param> /// <returns>Returns a boolean value that determines whether the specified /// certificate is accepted for authentication; true to accept or false to /// reject.</returns> public override bool ValidateServerCertficate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslPolicyErrors) { if (sslPolicyErrors == SslPolicyErrors.None) { // Good certificate. return(true); } string msg = string.Format(P4VS.Resources.SSLCertificateHandler_SSLCertificateError, sslPolicyErrors); P4VS.P4VsOutputWindow.AppendMessage(msg); P4VS.FileLogger.LogMessage(3, "SwarmAPI", msg); string certHash = cert.GetCertHashString(); if (certHash == CertHash) { // same certificate we looked at before if (ExceptionApproved == true) { // Already approved return(true); } if (ExceptionRejected == true) { //Already rejected return(false); } } if ((!ExceptionRejected) && ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == 0)) { // likely a self signed certificate. string serverName = ServerUrl; int slashIdx = ServerUrl.LastIndexOf('/'); if (slashIdx > 0) { serverName = ServerUrl.Substring(slashIdx + 1); } string key = string.Format("SSlCertificateException_{0}", serverName.Replace(':', '_')); CertHash = P4VS.Preferences.LocalSettings.GetString(key, null); if ((CertHash == null) || (certHash != CertHash)) { // haven't seen one for this URL or it's a different certificate SSLCertificateErrorDlg dlg = new SSLCertificateErrorDlg(); dlg.CertificateText = cert.ToString(true); List <string> certErrors = new List <string>(); if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) != 0) { certErrors.Add(Resources.P4ScmProvider_RemoteCertificateNameMismatch); } if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0) { foreach (X509ChainStatus err in chain.ChainStatus) { certErrors.Add(err.StatusInformation); } } dlg.CertificateErrors = certErrors; if (System.Windows.Forms.DialogResult.Yes == dlg.ShowDialog()) { // add the exeption P4VS.Preferences.LocalSettings[key] = certHash; ExceptionApproved = true; return(true); } ExceptionRejected = true; return(false); } ExceptionApproved = true; return(true); } // Return true => allow unauthenticated server, // false => disallow unauthenticated server. return(false); }