void UploaderThread() { while (file.Position < filesz) { Int64 blksz; Int64 nchunk = 512 * 1024; if (nchunk > filesz - file.Position) nchunk = filesz - file.Position; byte[] data = new byte[nchunk]; blksz = file.Read(data, 0, data.Length); currentsz += blksz; counter++; UploadData ud = new UploadData(); ud.Counter = counter; ud.data = data; ud.Size = blksz; ud.MD5 = MD5Utilities.CalcMD5(data); if (this.net.UploadData(ud) == false) { lasterror = this.net.GetLastError(); this.net.CloseConnection(); file.Close(); this.BeginInvoke(new voidy(Reset)); this.BeginInvoke(new voidy(OnError)); return; } if (CancelThread == true) { lasterror = "Canceled"; this.net.UploadCancel(); this.net.CloseConnection(); file.Close(); this.BeginInvoke(new voidy(Reset)); this.BeginInvoke(new voidy(OnCancel)); return; } this.BeginInvoke(new voidy(UpdatePromille)); } file.Close(); NewUploadDataID nid = this.net.UploadFinalise(); if (nid == null) { lasterror = this.net.GetLastError(); this.net.CloseConnection(); this.BeginInvoke(new voidy(Reset)); this.BeginInvoke(new voidy(OnError)); return; } this.net.CloseConnection(); this.BeginInvoke(new voidy(Reset)); this.BeginInvoke(new newd(OnSuccess), nid); }
private void CompareFiles() { ListView.SelectedListViewItemCollection files = listViewMain.SelectedItems; string sourceDir = $"{comboBoxFolder.Text}\\{treeViewMain.SelectedNode.FullPath}"; string sourceFile = $"{sourceDir}\\{files[0].Text}"; string vvDir = $"{(string)vvConfig.GetValue("VVPath")}\\{treeViewMain.SelectedNode.FullPath}\\{files[0].Text}"; string vvFile = $"{vvDir}\\{((VVItem)listBoxVV.SelectedItem).ItemName}"; if (File.Exists(sourceFile)) { // check if files are exactly the same FileInfo sourceInfo = new FileInfo(sourceFile); FileInfo vvInfo = new FileInfo(vvFile); if (sourceInfo.Length == vvInfo.Length && MD5Utilities.CalcMD5(sourceFile) == MD5Utilities.CalcMD5(vvFile)) { MessageBox.Show("Files are identical", _appName, MessageBoxButtons.OK); } else { // launch ExternalCompareApp process with the two files string compareApp = Properties.Settings.Default.ExternalCompareApp; string compareOptions = Properties.Settings.Default.ExternalCompareAppOptions; Process p = Process.Start(compareApp, $"{compareOptions} \"{sourceFile}\" \"{vvFile}\""); p.WaitForExit(); int result = p.ExitCode; } } else { // launch file viewer (default = notepad) to show the deleted file string compareApp = Properties.Settings.Default.ExternalFileViewer; Process p = Process.Start(compareApp, $"\"{vvFile}\""); p.WaitForExit(); int result = p.ExitCode; } }
public RESTStatus UploadData(SQLLib sql, UploadData data, NetworkConnectionInfo ni) { if (ni.Upload == null) { ni.Error = "Upload is not running"; ni.ErrorID = ErrorFlags.UploadNotRunning; return(RESTStatus.Fail); } if (data.Size < 0) { ni.Error = "Negative SZ"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (data.Size > Consts.MaxFileChunk) { ni.Error = "Chunk too large"; ni.ErrorID = ErrorFlags.ChunkTooLarge; return(RESTStatus.Fail); } if (data == null) { ni.Error = "No data"; ni.ErrorID = ErrorFlags.NoData; return(RESTStatus.Fail); } if (data.data.Length != data.Size) { ni.Error = "Faulty sizes"; ni.ErrorID = ErrorFlags.FaultySizes; return(RESTStatus.Fail); } if (ni.Upload.CurrentPosition + data.Size > ni.Upload.Size) { ni.Error = "Too many data"; ni.ErrorID = ErrorFlags.TooManyData; return(RESTStatus.Fail); } if (ni.Upload.Counter + 1 != data.Counter) { ni.Error = "Invalid count"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (MD5Utilities.CalcMD5(data.data).ToLower() != data.MD5.ToLower()) { ni.Error = "MD5 error"; ni.ErrorID = ErrorFlags.CheckSumError; return(RESTStatus.Fail); } ni.Upload.Data.Write(data.data, 0, data.data.Length); ni.Upload.Counter++; ni.Upload.CurrentPosition += data.Size; return(RESTStatus.Success); }
public RESTStatus ComputerLogin(SQLLib sql, RecoveryLogon logon, NetworkConnectionInfo ni, string IPAddress) { RD = new RecoveryData(); if (Fox_LicenseGenerator.SDCLicensing.ValidLicense == false) { RD.Worked = false; return(RESTStatus.Fail); } if (Fox_LicenseGenerator.SDCLicensing.TestExpiry() == false) { RD.Worked = false; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(logon.UCID) == true) { RD.Worked = false; return(RESTStatus.Fail); } if (Settings.Default.UseContract == true) { if (string.IsNullOrWhiteSpace(logon.ContractID) == true || string.IsNullOrWhiteSpace(logon.ContractPassword) == true) { RD.Worked = false; return(RESTStatus.Fail); } } string newID = NetworkConnection.NewSession(); ni = NetworkConnection.GetSession(newID); if (NetworkConnectionProcessor.InitNi(ni) == false) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.ServerError); } sql = ni.sql; if (Settings.Default.UseContract == true) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT Count(*) FROM Contracts WHERE ContractID=@id AND ContractPassword=@pw AND Disabled=0", new SQLParam("@id", logon.ContractID), new SQLParam("@pw", logon.ContractPassword))) == 0) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE ContractID=@id AND UCID=@u", new SQLParam("@id", logon.ContractID), new SQLParam("@u", logon.UCID))) == 0) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE UCID=@u", new SQLParam("@u", logon.UCID))) == 0) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM ComputerAccounts WHERE UCID=@u", new SQLParam("@u", logon.UCID)); dr.Read(); string Check = Convert.ToString(dr["CPUName"]).Trim(); Check += Convert.ToString(dr["ComputerModel"]).Trim(); Check += Convert.ToString(dr["BIOS"]).Trim(); string MD5 = MD5Utilities.CalcMD5(Check); if (MD5.ToLower() != logon.MoreMachineHash.ToLower()) { dr.Close(); NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } RD.MachineID = Convert.ToString(dr["MachineID"]); RD.MachinePassword = Convert.ToString(dr["Password"]); RD.Worked = true; dr.Close(); NetworkConnection.DeleteSession(newID); return(RESTStatus.Success); }
static void DownloadThreadRunner() { try { int Direction = -1; lock (FileLock) { if (FilesystemData.FileTransferStatus.ServerID == null) { return; } Direction = FilesystemData.FileTransferStatus.Direction; } #region Client to Server if (Direction == 1) { string LocalFilename = ""; string MD5; Int64 ServerID = 0; Int64 CurrentSZ; Int64 TotalSZ; bool OverrideMetered; bool ReqOnly; DateTime LastModified; try { lock (FileLock) { LocalFilename = FilesystemData.FileTransferStatus.RemoteFileLocation; ServerID = FilesystemData.FileTransferStatus.ServerID.Value; CurrentSZ = FilesystemData.FileTransferStatus.ProgressSize; TotalSZ = FilesystemData.FileTransferStatus.Size; MD5 = FilesystemData.FileTransferStatus.MD5CheckSum; OverrideMetered = FilesystemData.FileTransferStatus.OverrideMeteredConnection; ReqOnly = FilesystemData.FileTransferStatus.RequestOnly; LastModified = FilesystemData.FileTransferStatus.LastModfied; } //won't start when in metered connection! if (OverrideMetered == false) { try { if (MeteredConnection.IsMeteredConnection() == true) { FoxEventLog.VerboseWriteEventLog("Upload paused = metered connection detected", System.Diagnostics.EventLogEntryType.Information); return; } } catch { } } Int64 ReallyCurrentSZ = 0; try { if (File.Exists(LocalFilename) == true) { FileInfo fi = new FileInfo(LocalFilename); ReallyCurrentSZ = fi.Length; } else { FoxEventLog.WriteEventLog("File " + LocalFilename + " does not exist for upload.", System.Diagnostics.EventLogEntryType.Error); return; } } catch (Exception ee) { FoxEventLog.WriteEventLog("Checking upload file " + LocalFilename + " ID: " + ServerID.ToString() + " failed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); return; } downloadnet = Utilities.ConnectNetwork(7); if (downloadnet == null) { return; } if (ReqOnly == true) { Status.UpdateMessage(7, "Checking " + LocalFilename); MD5 = MD5Utilities.CalcMD5File(LocalFilename); FileInfo fi = new FileInfo(LocalFilename); lock (FileLock) { FilesystemData.FileTransferStatus.MD5CheckSum = MD5; FilesystemData.FileTransferStatus.LastModfied = fi.LastWriteTimeUtc; FilesystemData.WriteFileTransferStatus(); } Int64?NewID = downloadnet.File_Agent_NewUploadReq(LocalFilename, OverrideMetered, MD5); if (NewID == null) { FoxEventLog.VerboseWriteEventLog("Cannot create a new upload req for " + LocalFilename, System.Diagnostics.EventLogEntryType.Warning); return; } if (downloadnet.File_Agent_CancelUpload(ServerID) == false) { FoxEventLog.VerboseWriteEventLog("Cannot delete temp upload req for " + LocalFilename + " ID: " + ServerID.ToString(), System.Diagnostics.EventLogEntryType.Warning); return; } lock (FileLock) { //wait for the new upload req ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); UnlockTimer = true; return; } } else { FileStream fs = null; do { try { if (CancelAndDeleteDL == true) { return; } FileInfo fi = new FileInfo(LocalFilename); if (roughDT(fi.LastWriteTimeUtc, LastModified) == false || fi.Length != TotalSZ) { if (downloadnet.File_Agent_CancelUpload(ServerID) == false) { FoxEventLog.VerboseWriteEventLog("Cannot cancel upload req for " + LocalFilename + " ID: " + ServerID.ToString() + ", due file changes", System.Diagnostics.EventLogEntryType.Warning); lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); } return; } else { FoxEventLog.VerboseWriteEventLog("Cannot upload req for " + LocalFilename + " ID: " + ServerID.ToString() + ": File has been changed!", System.Diagnostics.EventLogEntryType.Warning); } return; } try { if (fs == null) { fs = File.Open(LocalFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); } } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot upload file " + LocalFilename + " ID: " + ServerID.ToString() + " failed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Warning); return; } try { fs.Seek(CurrentSZ, SeekOrigin.Begin); } catch { FoxEventLog.WriteEventLog("Cannot properly seek in file " + LocalFilename + " ID: " + ServerID.ToString() + " Pos: 0x" + CurrentSZ.ToString("X"), System.Diagnostics.EventLogEntryType.Warning); return; } byte[] data = null; try { int read = 1024 * 1024; data = new byte[read]; read = fs.Read(data, 0, read); if (data.Length != read) { byte[] ddd = new byte[read]; Array.Copy(data, ddd, read); data = ddd; } } catch { FoxEventLog.WriteEventLog("Cannot read file " + LocalFilename + " ID: " + ServerID.ToString() + " Pos: 0x" + CurrentSZ.ToString("X"), System.Diagnostics.EventLogEntryType.Warning); return; } Status.UpdateMessage(7, "Uploading " + LocalFilename + "\r\n" + CommonUtilities.NiceSize(CurrentSZ) + " of " + CommonUtilities.NiceSize(TotalSZ)); bool res = downloadnet.File_Agent_AppendUpload(ServerID, data); if (res == false) { FoxEventLog.VerboseWriteEventLog("Cannot upload append req for " + LocalFilename + " ID: " + ServerID.ToString(), System.Diagnostics.EventLogEntryType.Warning); return; } CurrentSZ += data.Length; lock (FileLock) { FilesystemData.FileTransferStatus.ProgressSize = CurrentSZ; FilesystemData.WriteFileTransferStatus(); } if (CurrentSZ == TotalSZ) { FoxEventLog.WriteEventLog("Upload file success " + LocalFilename + " ID: " + ServerID.ToString(), System.Diagnostics.EventLogEntryType.Information); lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); } UnlockTimer = true; return; } } finally { if (fs != null) { fs.Close(); fs = null; } } } while (true); } } catch (Exception ee) { FoxEventLog.WriteEventLog("Downloading file " + LocalFilename + " ID: " + ServerID.ToString() + " crashed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } finally { Status.UpdateMessage(7); } } #endregion #region Server to Client if (Direction == 0) { string LocalFilename; string MD5; Int64 ServerID; Int64 CurrentSZ; Int64 TotalSZ; bool OverrideMetered; lock (FileLock) { LocalFilename = FilesystemData.FileTransferStatus.RemoteFileLocation; ServerID = FilesystemData.FileTransferStatus.ServerID.Value; CurrentSZ = FilesystemData.FileTransferStatus.ProgressSize; TotalSZ = FilesystemData.FileTransferStatus.Size; MD5 = FilesystemData.FileTransferStatus.MD5CheckSum; OverrideMetered = FilesystemData.FileTransferStatus.OverrideMeteredConnection; } //won't start when in metered connection! if (OverrideMetered == false) { try { if (MeteredConnection.IsMeteredConnection() == true) { FoxEventLog.VerboseWriteEventLog("Download paused = metered connection detected", System.Diagnostics.EventLogEntryType.Information); return; } } catch { } } Int64 ReallyCurrentSZ = 0; try { string Dir = Path.GetDirectoryName(LocalFilename); if (Directory.Exists(Dir) == false) { Directory.CreateDirectory(Dir); } if (File.Exists(LocalFilename) == true) { FileInfo fi = new FileInfo(LocalFilename); ReallyCurrentSZ = fi.Length; } else { ReallyCurrentSZ = 0; } } catch (Exception ee) { FoxEventLog.WriteEventLog("Checking download file " + LocalFilename + " ID: " + ServerID.ToString() + " failed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } if (ReallyCurrentSZ != CurrentSZ) { FoxEventLog.WriteEventLog("File Size does not match: deleting the file " + LocalFilename, System.Diagnostics.EventLogEntryType.Warning); try { File.Delete(LocalFilename); } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); return; } } downloadnet = Utilities.ConnectNetwork(7); if (downloadnet == null) { return; } try { downloadnet.DownloadNotify += Downloadnet_DownloadNotify; if (downloadnet.DownloadFile("api/agent/filefiledownload/" + ServerID.ToString(), LocalFilename, TotalSZ) == false) { FileInfo fi = new FileInfo(LocalFilename); lock (FileLock) { FilesystemData.FileTransferStatus.ProgressSize = fi.Length; FilesystemData.WriteFileTransferStatus(); } } else { if (downloadnet.StopDownload == false) { Status.UpdateMessage(7, "Checking " + FilesystemData.FileTransferStatus.RemoteFileLocation + "..."); string CalcMD5 = MD5Utilities.CalcMD5File(LocalFilename); if (MD5.ToLower() != CalcMD5.ToLower()) { FoxEventLog.WriteEventLog("File MD5 does not match: deleting the file " + LocalFilename, System.Diagnostics.EventLogEntryType.Warning); try { File.Delete(LocalFilename); } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); return; } } else { //success! FoxEventLog.WriteEventLog("File download success: " + LocalFilename, System.Diagnostics.EventLogEntryType.Information); downloadnet.File_Agent_CancelUpload(ServerID); lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); } UnlockTimer = true; return; } } else { if (CancelAndDeleteDL == true) { FoxEventLog.VerboseWriteEventLog("File " + LocalFilename + " canceled by server req.", System.Diagnostics.EventLogEntryType.Information); try { File.Delete(LocalFilename); } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } lock (FileLock) { ClearDataFSD(); FilesystemData.WriteFileTransferStatus(); return; } } } } } catch (Exception ee) { FoxEventLog.WriteEventLog("Downloading file " + LocalFilename + " ID: " + ServerID.ToString() + " crashed:\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } finally { downloadnet.DownloadNotify -= Downloadnet_DownloadNotify; Status.UpdateMessage(7); } } #endregion } finally { try { if (downloadnet != null) { downloadnet.CloseConnection(); } } catch { } downloadnet = null; } }
public static void SMain() { #if !DEBUG AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; #endif FoxEventLog.RegisterEventLog(); if (UsePipeAction == false && UseScreenAction == false && UseDNSAutoConfig == false && UseLoginRecovery == false) //Pipe Actions can also be run in user-space ... { if (IsSystemUser() != true) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } } if (LoadDLL() == false) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } #if !DEBUG if (TestIntegrity(null) == false) { FoxEventLog.WriteEventLog("Integrity check failed!", EventLogEntryType.Error); service.Stop(); return; } #endif if (UseDNSAutoConfig == true) { try { List <List <string> > Query = CPP.DNSQueryTXT("sdc-contract.my-vulpes-config.lu"); if (Query == null) { return; } string ContractID = null; string ContractPassword = null; string UseOnPrem = null; string OnPremURL = null; foreach (List <string> Q in Query) { if (Q == null) { continue; } foreach (string QR in Q) { if (string.IsNullOrWhiteSpace(QR) == true) { continue; } if (QR.ToLower().StartsWith("contractid=") == true) { ContractID = QR.Substring(11).Trim(); } if (QR.ToLower().StartsWith("contractpassword="******"useonprem=") == true) { UseOnPrem = QR.Substring(10).Trim(); } if (QR.ToLower().StartsWith("onpremurl=") == true) { OnPremURL = QR.Substring(10).Trim(); } } } using (RegistryKey k = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Fox\\SDC")) { if (string.IsNullOrWhiteSpace(ContractID) == false && string.IsNullOrWhiteSpace(ContractPassword) == false) { k.SetValue("ContractID", ContractID, RegistryValueKind.String); k.SetValue("ContractPassword", ContractPassword, RegistryValueKind.String); } int UseOnPremInt; if (int.TryParse(UseOnPrem, out UseOnPremInt) == true) { if (UseOnPremInt == 1 && string.IsNullOrWhiteSpace(OnPremURL) == false) { k.SetValue("UseOnPremServer", 1, RegistryValueKind.DWord); k.SetValue("Server", OnPremURL, RegistryValueKind.String); } else { k.SetValue("UseOnPremServer", 0, RegistryValueKind.DWord); } } else { k.SetValue("UseOnPremServer", 0, RegistryValueKind.DWord); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); } return; } if (UseLoginRecovery == true) { if (SystemInfos.CollectSystemInfo() != 0) { return; } #if !DEBUG if (SystemInfos.SysInfo.RunningInWindowsPE == false || SystemInfos.SysInfo.RunningInWindowsPE == null) { return; } #endif RecoveryLogon reclogon = new RecoveryLogon(); reclogon.UCID = SystemInfos.SysInfo.UCID; reclogon.ContractID = SystemInfos.ContractID; reclogon.ContractPassword = SystemInfos.ContractPassword; string Check = SystemInfos.SysInfo.CPUName.Trim(); Check += SystemInfos.SysInfo.ComputerModel == "" ? "N/A" : SystemInfos.SysInfo.ComputerModel.Trim(); Check += SystemInfos.SysInfo.BIOS == "" ? "N/A" : SystemInfos.SysInfo.BIOS.Trim(); reclogon.MoreMachineHash = MD5Utilities.CalcMD5(Check); Network net = Utilities.NoConnectNetwork(); RecoveryData rd = net.GetRecoveryLogon(reclogon); if (rd == null) { return; } if (rd.Worked == false) { return; } string Registry = "Windows Registry Editor Version 5.00\r\n\r\n[HKEY_LOCAL_MACHINE\\SOFTWARE\\Fox\\SDC]\r\n\"ID\"=\"" + rd.MachineID + "\"\r\n\"PassID\"=\"" + rd.MachinePassword + "\""; try { File.WriteAllText(Environment.ExpandEnvironmentVariables("%SYSTEMROOT%\\Fox SDC MachinePW.reg"), Registry, Encoding.Unicode); } catch { } return; } if (UsePipeAction == false && UseScreenAction == false) { using (RegistryKey installer = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + VulpesBranding.MSIGUID, false)) { if (installer == null) { FoxEventLog.WriteEventLog("Missing installer key in HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + VulpesBranding.MSIGUID + ". This can lead to erratic behavoir of the program!", EventLogEntryType.Warning); } } if (CPP.SetToken() == false) { FoxEventLog.WriteEventLog("Cannot setup token - 0x" + Marshal.GetLastWin32Error().ToString("X") + " - " + new Win32Exception(Marshal.GetLastWin32Error()).Message, System.Diagnostics.EventLogEntryType.Error); return; } } if (UsePipeAction == true) { try { Process2ProcessCommClient.RunPipeClient(); } catch (Exception ee) { FoxEventLog.WriteEventLog("Pipe SEH " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } return; } if (UseScreenAction == true) { try { if (SystemInfos.CollectSystemInfo() != 0) { return; } MainScreenSystemClient.RunPipeClient(); } catch (Exception ee) { FoxEventLog.WriteEventLog("Screen Pipe SEH " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } return; } if (SystemInfos.CollectSystemInfo() != 0) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } RegistryData.InstallPath = AppPath; if (ApplicationCertificate.LoadCertificate() == false) { FoxEventLog.WriteEventLog("Cannot load certificate", System.Diagnostics.EventLogEntryType.Error); #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } if (FilesystemData.LoadCertificates() == false) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } if (FilesystemData.LoadPolicies() == false) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } FilesystemData.LoadLocalPackageData(); FilesystemData.LoadLocalPackages(); FilesystemData.LoadUserPackageData(); FilesystemData.LoadEventLogList(); SyncPolicy.ApplyPolicy(SyncPolicy.ApplyPolicyFunction.ApplySystem); PipeCommunicationSRV.StartPipeSrv(); try { string p = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (p.EndsWith("\\") == false) { p += "\\"; } using (RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { reg.SetValue("FoxSDCAgent", p + "FoxSDC_Agent_UI.exe", RegistryValueKind.String); reg.SetValue("FoxSDCAgentApply", p + "FoxSDC_ApplyUserSettings.exe", RegistryValueKind.String); reg.Close(); } } catch { } try { using (RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true)) { object o = reg.GetValue("SoftwareSASGeneration", null); int rvalue = 0; if (o != null) { rvalue = Convert.ToInt32(o); } if (rvalue != 1 && rvalue != 3) { reg.SetValue("SoftwareSASGeneration", 1); } reg.Close(); } } catch { } Threads.StartAllThreads(); }
public RESTStatus AppendUpload(SQLLib sql, FileUploadAppendData upload, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (upload == null) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } lock (ni.sqllock) { if (Computers.MachineExists(sql, upload.MachineID) == false) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } } if (upload.Data.Length != upload.Size) { ni.Error = "Invalid SZ"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(upload.MD5) == true) { ni.Error = "No MD5"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (upload.MD5.ToLower() != MD5Utilities.CalcMD5(upload.Data).ToLower()) { ni.Error = "MD5 Error"; ni.ErrorID = ErrorFlags.CheckSumError; return(RESTStatus.Fail); } lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM FileTransfers WHERE ID=@id AND MachineID=@mid AND [Size]!=[ProgressSize] AND Direction=3", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))) == 0) { ni.Error = "Invalid ID"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } } Int64 TotalSZ; Int64 ProgressSize; lock (ni.sqllock) { TotalSZ = Convert.ToInt64(sql.ExecSQLScalar("SELECT [Size] FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } lock (ni.sqllock) { ProgressSize = Convert.ToInt64(sql.ExecSQLScalar("SELECT ProgressSize FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (ProgressSize + upload.Size > TotalSZ) { ni.Error = "Too many data"; ni.ErrorID = ErrorFlags.ChunkTooLarge; return(RESTStatus.Fail); } string Filename = null; lock (ni.sqllock) { Filename = Settings.Default.DataPath + Convert.ToString(sql.ExecSQLScalar("SELECT ServerFile FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (File.Exists(Filename) == false) { if (ProgressSize > 0) { ni.Error = "FS Error - Missing"; ni.ErrorID = ErrorFlags.FileSystemError; return(RESTStatus.Fail); } } using (FileStream str = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { str.Seek(0, SeekOrigin.End); str.Write(upload.Data, 0, upload.Size); } FileInfo fi = new FileInfo(Filename); if (ProgressSize + upload.Size != fi.Length) { ni.Error = "FS Error - Final SZ Error"; ni.ErrorID = ErrorFlags.FileSystemError; return(RESTStatus.Fail); } lock (ni.sqllock) { sql.ExecSQL("UPDATE FileTransfers SET ProgressSize=@psz, DTUpdated=getutcdate() WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@psz", fi.Length), new SQLParam("@id", upload.ID)); } if (fi.Length == TotalSZ) { string MD5 = MD5Utilities.CalcMD5File(Filename); string MD5DB = ""; lock (ni.sqllock) { MD5DB = Convert.ToString(sql.ExecSQLScalar("SELECT MD5Sum FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (MD5.ToLower() != MD5DB.ToLower()) { ni.Error = "Final MD5 error"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } lock (ni.sqllock) { sql.ExecSQL("UPDATE FileTransfers SET Direction=0 WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID)); } } return(RESTStatus.Success); }
static void DownloadThready(object dd) { if (!(dd is UploadDownloadData)) { return; } try { UploadDownloadData d = (UploadDownloadData)dd; Debug.Assert(d.UploadID != null); #region Download Code HttpWebRequest client = (HttpWebRequest)WebRequest.Create(net.ConnectedURL + "api/agent/filefiledownload/" + d.UploadID.Value.ToString()); client.Pipelined = false; client.ServicePoint.Expect100Continue = false; client.AllowAutoRedirect = true; if (net.Session != "") { client.Headers.Add("Authorization", "Bearer " + net.Session); } #if DEBUG client.ReadWriteTimeout = 5000; client.Timeout = 5000; #else client.ReadWriteTimeout = 60000; client.Timeout = 60000; #endif client.UserAgent = "FoxSDC Client"; client.Method = "GET"; Int64 SeekTo = 0; FileMode fm = FileMode.Create; if (File.Exists(d.RemoteFilename) == true) { FileInfo f = new FileInfo(d.RemoteFilename); SeekTo = f.Length; fm = FileMode.Open; } using (Stream FileStream = File.Open(d.RemoteFilename, fm, FileAccess.ReadWrite, FileShare.Read)) { FileStream.Seek(SeekTo, SeekOrigin.Begin); HttpWebResponse resp = (HttpWebResponse)client.GetResponse(); using (Stream HTTPStream = resp.GetResponseStream()) { const int ReadBufferSZ = 2048; d.ProgressSize = 0; StopDownload = false; byte[] data = new byte[ReadBufferSZ]; int ReadSZ = HTTPStream.Read(data, 0, ReadBufferSZ); while (ReadSZ > 0) { d.ProgressSize += ReadSZ; FileStream.Write(data, 0, ReadSZ); ReadSZ = HTTPStream.Read(data, 0, ReadBufferSZ); if (StopDownload == true) { break; } } } } if (StopDownload == true) { return; } if (MD5Utilities.CalcMD5File(d.RemoteFilename).ToLower() != d.MD5CheckSum.ToLower()) { File.Delete(d.RemoteFilename); d.Failed = true; d.ErrorText = "MD5 mismatch"; d.DownloadThread = null; return; } net.File_MGMT_CancelUpload(d.UploadID.Value); d.DownloadThread = null; lock (DataLock) { Data.Remove(d); } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); } #endregion }
static void Thready() { do { UploadDownloadData d = null; lock (DataLock) { if (Data.Count == 0) { d = null; Thread.Sleep(500); continue; } foreach (UploadDownloadData dd in Data) { if (dd.Failed == false) { d = dd; } } } if (d == null) { Thread.Sleep(1000); } else { //check if another download thread is running foreach (UploadDownloadData dd in Data) { if (dd.ID == d.ID) { continue; } if (dd.DownloadThread != null) { StopDownload = true; dd.DownloadThread.Join(60000); dd.DownloadThread = null; } } //process element switch (d.Direction) { case Direction.DownloadFromServer: if (d.DownloadThread != null) { if (d.DownloadThread.IsAlive == true) { Thread.Sleep(1000); break; } } Debug.Assert(d.MD5CheckSum != null); Debug.Assert(d.RemoteFilename != null); if (d.ProgressSize != null) { if (d.ProgressSize != d.Size) { d.ProgressSize = null; } } if (d.UploadID == null) { d.Failed = true; d.ErrorText = "Missing Download ID"; continue; } try { // u.RemoteFilename - here // u.LocalFilename - agent if (d.DownloadThread == null) { StopDownload = false; d.DownloadThread = new Thread(new ParameterizedThreadStart(DownloadThready)); d.DownloadThread.Start(d); } } catch (Exception ee) { d.Failed = true; d.ErrorText = "Cannot start download: " + ee.Message; continue; } finally { } break; case Direction.UploadToServer: if (string.IsNullOrWhiteSpace(d.MD5CheckSum) == true) { d.MD5CheckSum = MD5Utilities.CalcMD5File(d.LocalFilename); } else { if (d.FileStream == null) { try { d.FileStream = File.Open(d.LocalFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); } catch (Exception ee) { d.Failed = true; d.ErrorText = "Cannot open " + d.LocalFilename + ": " + ee.Message; continue; } } else { #region Upload code if (d.UploadID == null) { d.UploadID = net.File_MGMT_NewUploadReq(d.LocalFilename, d.RemoteFilename, d.MachineID, d.MD5CheckSum, d.IgnoreMeteredConnection); if (d.UploadID == null) { d.FileStream.Close(); d.FileStream = null; d.Failed = true; d.ErrorText = net.GetLastError(); continue; } d.ProgressSize = 0; } else { int read = 1024 * 1024; byte[] data = new byte[read]; read = d.FileStream.Read(data, 0, read); if (data.Length != read) { byte[] ddd = new byte[read]; Array.Copy(data, ddd, read); data = ddd; } bool res = net.File_MGMT_AppendUpload(d.MachineID, d.UploadID.Value, data); if (res == false) { d.FileStream.Close(); d.FileStream = null; d.Failed = true; d.ErrorText = net.GetLastError(); net.File_MGMT_CancelUpload(d.UploadID.Value); continue; } d.ProgressSize += read; if (d.ProgressSize == d.Size) { d.FileStream.Close(); d.FileStream = null; lock (DataLock) { Data.Remove(d); } } } #endregion } } break; } } } while (CancelThread == false); lock (DataLock) { foreach (UploadDownloadData d in Data) { if (d.UploadID != null) { switch (d.Direction) { case Direction.UploadToServer: net.File_MGMT_CancelUpload(d.UploadID.Value); break; case Direction.DownloadFromServer: // u.RemoteFilename - here // u.LocalFilename - agent try { File.Delete(d.RemoteFilename); } catch { } break; } } } } ThreadClosed = true; }