2019-02-11 13:42:09 +00:00
|
|
|
|
using Renci.SshNet;
|
2019-01-15 14:55:35 +00:00
|
|
|
|
using Renci.SshNet.Common;
|
2018-10-13 23:03:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
2019-01-15 15:27:00 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2018-10-13 23:03:50 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2019-01-15 15:27:00 +00:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2018-10-13 23:03:50 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace BackupManager
|
|
|
|
|
{
|
|
|
|
|
public static class Program
|
|
|
|
|
{
|
|
|
|
|
public readonly static Stopwatch sw = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
private const string CONFIG_NAME = @"FlashiiBackupManager.v1.xml";
|
|
|
|
|
|
|
|
|
|
public static bool IsWindows
|
|
|
|
|
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
|
|
|
|
|
|
public readonly static DateTimeOffset Startup = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
|
|
public static string Basename
|
|
|
|
|
=> $@"{Environment.MachineName} {Startup.Year:0000}-{Startup.Month:00}-{Startup.Day:00} {Startup.Hour:00}{Startup.Minute:00}{Startup.Second:00}";
|
|
|
|
|
public static string DatabaseDumpName
|
|
|
|
|
=> $@"{Basename}.sql.gz";
|
|
|
|
|
public static string UserDataName
|
|
|
|
|
=> $@"{Basename}.zip";
|
|
|
|
|
|
|
|
|
|
private static Config Config;
|
|
|
|
|
private readonly static string ConfigPath = Path.Combine(
|
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
|
|
|
|
|
CONFIG_NAME
|
|
|
|
|
);
|
|
|
|
|
|
2019-08-16 19:13:18 +00:00
|
|
|
|
public static string FSPath => string.IsNullOrWhiteSpace(Config.FileSystemPathV2)
|
|
|
|
|
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups")
|
|
|
|
|
: Config.FileSystemPathV2;
|
|
|
|
|
|
2019-01-15 14:55:35 +00:00
|
|
|
|
private static object BackupStorage;
|
|
|
|
|
|
|
|
|
|
private static SftpClient SFTP;
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
|
|
|
|
public static bool Headless;
|
|
|
|
|
|
|
|
|
|
public static string WindowsToUnixPath(this string path)
|
|
|
|
|
{
|
|
|
|
|
return IsWindows ? path.Replace('\\', '/') : path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Stream ToXml(this object obj, bool pretty = false)
|
|
|
|
|
{
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
XmlSerializer xs = new XmlSerializer(obj.GetType());
|
|
|
|
|
|
|
|
|
|
using (XmlWriter xw = XmlWriter.Create(ms, new XmlWriterSettings { Indent = pretty }))
|
|
|
|
|
xs.Serialize(xw, obj);
|
|
|
|
|
|
|
|
|
|
ms.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T FromXml<T>(Stream xml)
|
|
|
|
|
{
|
|
|
|
|
if (xml.CanSeek)
|
|
|
|
|
xml.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
XmlSerializer xs = new XmlSerializer(typeof(T));
|
|
|
|
|
return (T)xs.Deserialize(xml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SaveConfig()
|
|
|
|
|
{
|
|
|
|
|
Log(@"Saving configuration...");
|
|
|
|
|
using (FileStream fs = new FileStream(ConfigPath, FileMode.Create, FileAccess.Write))
|
|
|
|
|
using (Stream cs = Config.ToXml(true))
|
|
|
|
|
cs.CopyTo(fs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void LoadConfig()
|
|
|
|
|
{
|
|
|
|
|
Log(@"Loading configuration...");
|
|
|
|
|
using (FileStream fs = File.OpenRead(ConfigPath))
|
|
|
|
|
Config = FromXml<Config>(fs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
Headless = args.Contains(@"-cron") || args.Contains(@"-headless");
|
|
|
|
|
|
|
|
|
|
Log(@"Flashii Backup Manager");
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(ConfigPath))
|
|
|
|
|
{
|
|
|
|
|
Config = new Config();
|
|
|
|
|
SaveConfig();
|
|
|
|
|
Error(@"No configuration file exists, created a blank one. Be sure to fill it out properly.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadConfig();
|
2019-01-15 15:27:00 +00:00
|
|
|
|
|
2019-01-15 14:55:35 +00:00
|
|
|
|
switch (Config.StorageMethod)
|
|
|
|
|
{
|
|
|
|
|
case StorageMethod.Sftp:
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Config.SftpHost) || string.IsNullOrWhiteSpace(Config.SftpUsername))
|
|
|
|
|
{
|
|
|
|
|
sw.Stop();
|
|
|
|
|
Config.SftpHost = Config.SftpHost ?? @"";
|
|
|
|
|
Config.SftpPort = Config.SftpPort < 1 ? (ushort)22 : Config.SftpPort;
|
|
|
|
|
Config.SftpUsername = Config.SftpUsername ?? @"";
|
|
|
|
|
Config.SftpPassphrase = Config.SftpPassphrase ?? @"";
|
|
|
|
|
Config.SftpPrivateKey = Config.SftpPrivateKey ?? @"";
|
|
|
|
|
Config.SftpTrustedHost = Config.SftpTrustedHost ?? @"";
|
|
|
|
|
Config.SftpBackupDirectoryPath = Config.SftpBackupDirectoryPath ?? @"";
|
|
|
|
|
SaveConfig();
|
|
|
|
|
Error(@"No Sftp host/auth details found in the configuration.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Config.SftpPrivateKey))
|
|
|
|
|
SFTP = new SftpClient(Config.SftpHost, Config.SftpPort, Config.SftpUsername, new PrivateKeyFile(Config.SftpPrivateKey, Config.SftpPassphrase ?? string.Empty));
|
|
|
|
|
else
|
|
|
|
|
SFTP = new SftpClient(Config.SftpHost, Config.SftpPort, Config.SftpUsername, Config.SftpPassphrase ?? string.Empty);
|
|
|
|
|
|
|
|
|
|
using (ManualResetEvent mre = new ManualResetEvent(false))
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Config.SftpTrustedHost))
|
|
|
|
|
SFTP.HostKeyReceived += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
string checkString = e.HostKeyName + @"#" + Convert.ToBase64String(e.HostKey) + @"#" + Convert.ToBase64String(e.FingerPrint);
|
|
|
|
|
e.CanTrust = Config.SftpTrustedHost.SequenceEqual(checkString);
|
|
|
|
|
mre.Set();
|
|
|
|
|
};
|
|
|
|
|
else
|
|
|
|
|
mre.Set();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SFTP.Connect();
|
|
|
|
|
} catch (SshConnectionException)
|
|
|
|
|
{
|
|
|
|
|
Error(@"Error during SFTP connect, it's possible the server key changed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mre.WaitOne();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2019-02-11 14:16:55 +00:00
|
|
|
|
|
|
|
|
|
case StorageMethod.FileSystem:
|
2019-08-16 19:13:18 +00:00
|
|
|
|
if (!Directory.Exists(FSPath))
|
|
|
|
|
Directory.CreateDirectory(FSPath);
|
2019-02-11 14:16:55 +00:00
|
|
|
|
break;
|
2019-01-15 14:55:35 +00:00
|
|
|
|
}
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
|
|
|
|
GetBackupStorage();
|
|
|
|
|
|
|
|
|
|
Log(@"Database backup...");
|
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
string sqldump = CreateMySqlDump();
|
|
|
|
|
|
|
|
|
|
using (Stream s = File.OpenRead(sqldump))
|
2018-10-13 23:03:50 +00:00
|
|
|
|
using (Stream g = GZipEncodeStream(s))
|
|
|
|
|
{
|
2019-01-15 14:55:35 +00:00
|
|
|
|
object f = Upload(DatabaseDumpName, @"application/sql+gzip", g);
|
|
|
|
|
|
|
|
|
|
switch (f)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
Log($@"MySQL dump uploaded.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
File.Delete(sqldump);
|
|
|
|
|
|
2018-10-13 23:03:50 +00:00
|
|
|
|
if (Directory.Exists(Config.MisuzuPath))
|
|
|
|
|
{
|
|
|
|
|
Log(@"Filesystem backup...");
|
2019-08-16 19:13:18 +00:00
|
|
|
|
string mszConfig = Path.Combine(Config.MisuzuPath, @"config/config.ini");
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
|
|
|
|
if (!File.Exists(mszConfig))
|
|
|
|
|
Error(@"Could not find Misuzu config.");
|
|
|
|
|
|
2019-08-16 19:13:18 +00:00
|
|
|
|
string mszStore = Path.Combine(Config.MisuzuPath, @"store");
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(mszStore))
|
|
|
|
|
Error(@"Could not find Misuzu storage directory.");
|
|
|
|
|
|
|
|
|
|
string archivePath = CreateMisuzuDataBackup(mszConfig, mszStore);
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = File.OpenRead(archivePath))
|
|
|
|
|
{
|
2019-01-15 14:55:35 +00:00
|
|
|
|
object f = Upload(UserDataName, @"application/zip", fs);
|
|
|
|
|
|
|
|
|
|
switch (f)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
Log($@"Misuzu data uploaded.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Delete(archivePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SaveConfig();
|
|
|
|
|
sw.Stop();
|
2019-02-11 14:19:18 +00:00
|
|
|
|
Log($@"Done! Took {sw.Elapsed}.", true);
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 14:19:18 +00:00
|
|
|
|
public static void Log(object line, bool forceSatori = false)
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
2019-02-11 15:29:50 +00:00
|
|
|
|
if (!Headless)
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
2019-02-11 15:29:50 +00:00
|
|
|
|
if (sw?.IsRunning == true)
|
|
|
|
|
{
|
|
|
|
|
ConsoleColor fg = Console.ForegroundColor;
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
|
|
|
Console.Write(sw.ElapsedMilliseconds.ToString().PadRight(10));
|
|
|
|
|
Console.ForegroundColor = fg;
|
|
|
|
|
}
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
2019-02-11 15:29:50 +00:00
|
|
|
|
Console.WriteLine(line);
|
|
|
|
|
}
|
2019-01-15 15:27:00 +00:00
|
|
|
|
|
2019-02-11 15:29:50 +00:00
|
|
|
|
if (forceSatori || (!Headless && !(Config?.SatoriErrorsOnly ?? true)))
|
2019-01-15 15:27:00 +00:00
|
|
|
|
SatoriBroadcast(line.ToString());
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Error(object line, int exit = 0x00DEAD00)
|
|
|
|
|
{
|
|
|
|
|
if (!Headless)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
Log(line);
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 15:27:00 +00:00
|
|
|
|
SatoriBroadcast(line.ToString(), true);
|
|
|
|
|
|
2019-01-15 14:55:35 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-10-13 23:03:50 +00:00
|
|
|
|
Environment.Exit(exit);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 14:55:35 +00:00
|
|
|
|
public static object Upload(string name, string type, Stream stream)
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
Log($@"Uploading '{name}'...");
|
2019-01-15 14:55:35 +00:00
|
|
|
|
|
2019-02-11 14:16:55 +00:00
|
|
|
|
switch (Config.StorageMethod)
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
2019-02-11 14:16:55 +00:00
|
|
|
|
case StorageMethod.Sftp:
|
|
|
|
|
SFTP.UploadFile(stream, (BackupStorage as string) + @"/" + name);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case StorageMethod.FileSystem:
|
|
|
|
|
string filename = Path.Combine(BackupStorage as string, name);
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = File.OpenWrite(filename))
|
|
|
|
|
stream.CopyTo(fs);
|
2019-01-15 14:55:35 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string CreateMisuzuDataBackup(string configPath, string storePath)
|
|
|
|
|
{
|
|
|
|
|
Log(@"Creating Zip archive containing non-volatile Misuzu data...");
|
|
|
|
|
|
|
|
|
|
string tmpName = Path.GetTempFileName();
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = File.OpenWrite(tmpName))
|
|
|
|
|
using (ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create))
|
|
|
|
|
{
|
|
|
|
|
za.CreateEntryFromFile(configPath, @"config/config.ini", CompressionLevel.Optimal);
|
|
|
|
|
|
|
|
|
|
string[] storeFiles = Directory.GetFiles(storePath, @"*", SearchOption.AllDirectories);
|
|
|
|
|
|
|
|
|
|
foreach (string file in storeFiles)
|
|
|
|
|
za.CreateEntryFromFile(
|
|
|
|
|
file,
|
|
|
|
|
@"store/" + file.Replace(storePath, string.Empty).WindowsToUnixPath().Trim('/'),
|
|
|
|
|
CompressionLevel.Optimal
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tmpName;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
public static string CreateMySqlDump()
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
Log(@"Dumping MySQL Databases...");
|
2019-08-16 03:09:17 +00:00
|
|
|
|
string sqldefaults = Path.GetTempFileName();
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
using (FileStream fs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite))
|
2018-10-13 23:03:50 +00:00
|
|
|
|
using (StreamWriter sw = new StreamWriter(fs))
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine(@"[client]");
|
|
|
|
|
sw.WriteLine($@"user={Config.MySqlUser}");
|
|
|
|
|
sw.WriteLine($@"password={Config.MySqlPass}");
|
2019-02-11 14:16:55 +00:00
|
|
|
|
sw.WriteLine(@"default-character-set=utf8mb4");
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
string sqldump = Path.GetTempFileName();
|
|
|
|
|
|
2019-02-11 14:16:55 +00:00
|
|
|
|
StringBuilder mysqldumpArgs = new StringBuilder();
|
2019-08-16 03:09:17 +00:00
|
|
|
|
mysqldumpArgs.AppendFormat(@"--defaults-file={0} ", sqldefaults);
|
2019-02-11 14:16:55 +00:00
|
|
|
|
mysqldumpArgs.Append(@"--single-transaction ");
|
|
|
|
|
mysqldumpArgs.Append(@"--tz-utc --triggers ");
|
|
|
|
|
mysqldumpArgs.Append(@"--routines --hex-blob ");
|
|
|
|
|
mysqldumpArgs.Append(@"--add-locks --order-by-primary ");
|
2019-08-16 03:09:17 +00:00
|
|
|
|
mysqldumpArgs.AppendFormat(@"--result-file={0} ", sqldump);
|
2019-02-11 14:16:55 +00:00
|
|
|
|
mysqldumpArgs.Append(@"-l -Q -q -B "); // lock, quote names, quick, databases list
|
|
|
|
|
mysqldumpArgs.Append(Config.MySqlDatabases);
|
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
Log($@"mysqldump args: {mysqldumpArgs}");
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-15 14:55:35 +00:00
|
|
|
|
Process p = Process.Start(new ProcessStartInfo
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
FileName = IsWindows ? Config.MySqlDumpPathWindows : Config.MySqlDumpPath,
|
2019-02-11 14:16:55 +00:00
|
|
|
|
Arguments = mysqldumpArgs.ToString(),
|
2018-10-13 23:03:50 +00:00
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true,
|
2019-01-15 14:55:35 +00:00
|
|
|
|
});
|
2018-10-13 23:03:50 +00:00
|
|
|
|
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
|
2019-08-16 03:09:17 +00:00
|
|
|
|
File.Delete(sqldefaults);
|
|
|
|
|
|
|
|
|
|
return sqldump;
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Stream GZipEncodeStream(Stream input)
|
|
|
|
|
{
|
|
|
|
|
Log(@"Compressing stream...");
|
|
|
|
|
MemoryStream output = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
using (GZipStream gz = new GZipStream(output, CompressionLevel.Optimal, true))
|
|
|
|
|
input.CopyTo(gz);
|
|
|
|
|
|
|
|
|
|
output.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void GetBackupStorage(string name = null)
|
|
|
|
|
{
|
2019-01-15 14:55:35 +00:00
|
|
|
|
switch (Config.StorageMethod)
|
2018-10-13 23:03:50 +00:00
|
|
|
|
{
|
2019-01-15 14:55:35 +00:00
|
|
|
|
case StorageMethod.Sftp:
|
|
|
|
|
string directory = (BackupStorage = name ?? Config.SftpBackupDirectoryPath) as string;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SFTP.ListDirectory(directory);
|
|
|
|
|
} catch (SftpPathNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
SFTP.CreateDirectory(directory);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2019-02-11 14:16:55 +00:00
|
|
|
|
|
|
|
|
|
case StorageMethod.FileSystem:
|
2019-08-16 19:13:18 +00:00
|
|
|
|
BackupStorage = name ?? FSPath;
|
2019-02-11 14:16:55 +00:00
|
|
|
|
break;
|
2019-01-15 14:55:35 +00:00
|
|
|
|
}
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
2019-01-15 15:27:00 +00:00
|
|
|
|
|
|
|
|
|
public static void SatoriBroadcast(string text, bool error = false)
|
|
|
|
|
{
|
2019-08-16 03:09:17 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
2019-01-15 15:27:00 +00:00
|
|
|
|
if (string.IsNullOrEmpty(text)
|
|
|
|
|
|| Config == null
|
|
|
|
|
|| string.IsNullOrWhiteSpace(Config.SatoriHost)
|
|
|
|
|
|| string.IsNullOrWhiteSpace(Config.SatoriSecret)
|
|
|
|
|
|| Config.SatoriPort < 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IPAddress ip = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ip = IPAddress.Parse(Config.SatoriHost);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ip = Dns.GetHostAddresses(Config.SatoriHost).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
ip = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ip == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
EndPoint endPoint = new IPEndPoint(ip, Config.SatoriPort);
|
|
|
|
|
|
|
|
|
|
StringBuilder textBuilder = new StringBuilder();
|
|
|
|
|
textBuilder.Append(@"[b]Backup System[/b]: ");
|
|
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
|
textBuilder.Append(@"[color=red]");
|
|
|
|
|
|
|
|
|
|
textBuilder.Append(text);
|
|
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
|
textBuilder.Append(@"[/color]");
|
|
|
|
|
|
|
|
|
|
text = textBuilder.ToString();
|
|
|
|
|
|
|
|
|
|
StringBuilder messageBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(Config.SatoriSecret)))
|
|
|
|
|
{
|
|
|
|
|
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(text));
|
|
|
|
|
|
|
|
|
|
foreach (byte b in hash)
|
|
|
|
|
messageBuilder.AppendFormat(@"{0:x2}", b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
messageBuilder.Append(text);
|
|
|
|
|
string message = messageBuilder.ToString();
|
|
|
|
|
byte[] messageBytes = new byte[Encoding.UTF8.GetByteCount(message) + 2];
|
|
|
|
|
messageBytes[0] = messageBytes[messageBytes.Length - 1] = 0x0F;
|
|
|
|
|
Encoding.UTF8.GetBytes(message).CopyTo(messageBytes, 1);
|
|
|
|
|
|
|
|
|
|
using (Socket sock = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
|
|
|
|
|
{
|
|
|
|
|
sock.NoDelay = sock.Blocking = true;
|
|
|
|
|
sock.Connect(endPoint);
|
|
|
|
|
sock.Send(messageBytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-13 23:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|