diff --git a/Server/Core/Commands/ConnectionHandler.cs b/Server/Core/Commands/ConnectionHandler.cs index fa903686..a1800c82 100644 --- a/Server/Core/Commands/ConnectionHandler.cs +++ b/Server/Core/Commands/ConnectionHandler.cs @@ -30,13 +30,10 @@ namespace xServer.Core.Commands client.Value.PCName = packet.PCName; string userAtPc = string.Format("{0}@{1}", client.Value.Username, client.Value.PCName); - char[] illegal = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray(); - bool containsIllegalChar = userAtPc.Count(c => illegal.Contains(c)) > 0; - if (!containsIllegalChar) - client.Value.DownloadDirectory = Path.Combine(Application.StartupPath, "Clients\\" + userAtPc + "\\"); - else - client.Value.DownloadDirectory = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\"); + client.Value.DownloadDirectory = (!Helper.Helper.CheckPathForIllegalChars(userAtPc)) ? + Path.Combine(Application.StartupPath, "Clients\\" + userAtPc + "\\") : + Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\"); if (!FrmMain.Instance.ListenServer.AllTimeConnectedClients.ContainsKey(client.Value.Id)) FrmMain.Instance.ListenServer.AllTimeConnectedClients.Add(client.Value.Id, DateTime.Now); diff --git a/Server/Core/Helper/Helper.cs b/Server/Core/Helper/Helper.cs index 9edaec8e..1d6b4e8f 100644 --- a/Server/Core/Helper/Helper.cs +++ b/Server/Core/Helper/Helper.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Linq; using System.Text; namespace xServer.Core.Helper @@ -8,6 +10,12 @@ namespace xServer.Core.Helper private const string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; private static readonly Random _rnd = new Random(Environment.TickCount); private static readonly string[] _sizes = {"B", "KB", "MB", "GB"}; + private static readonly char[] _illegalChars = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray(); + + public static bool CheckPathForIllegalChars(string path) + { + return path.Any(c => _illegalChars.Contains(c)); + } public static string GetRandomFilename(int length, string extension) { diff --git a/Server/Forms/FrmAddToAutostart.cs b/Server/Forms/FrmAddToAutostart.cs index ce7eebbc..f8947866 100644 --- a/Server/Forms/FrmAddToAutostart.cs +++ b/Server/Forms/FrmAddToAutostart.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Windows.Forms; +using xServer.Core.Helper; using xServer.Core.Misc; namespace xServer.Forms @@ -52,16 +53,14 @@ namespace xServer.Forms private void txtName_KeyPress(object sender, KeyPressEventArgs e) { - string illegal = new string(Path.GetInvalidPathChars()) + new string(Path.GetInvalidFileNameChars()); - if ((e.KeyChar == '\\' || illegal.Contains(e.KeyChar.ToString())) && !char.IsControl(e.KeyChar)) - e.Handled = true; + e.Handled = ((e.KeyChar == '\\' || Helper.CheckPathForIllegalChars(e.KeyChar.ToString())) && + !char.IsControl(e.KeyChar)); } private void txtPath_KeyPress(object sender, KeyPressEventArgs e) { - string illegal = new string(Path.GetInvalidPathChars()) + new string(Path.GetInvalidFileNameChars()); - if ((e.KeyChar == '\\' || illegal.Contains(e.KeyChar.ToString())) && !char.IsControl(e.KeyChar)) - e.Handled = true; + e.Handled = ((e.KeyChar == '\\' || Helper.CheckPathForIllegalChars(e.KeyChar.ToString())) && + !char.IsControl(e.KeyChar)); } } } \ No newline at end of file diff --git a/Server/Forms/FrmBuilder.cs b/Server/Forms/FrmBuilder.cs index 29f095ef..ba35e40c 100644 --- a/Server/Forms/FrmBuilder.cs +++ b/Server/Forms/FrmBuilder.cs @@ -130,8 +130,7 @@ namespace xServer.Forms private void txtPort_KeyPress(object sender, KeyPressEventArgs e) { - if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) - e.Handled = true; + e.Handled = (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)); } private void txtDelay_KeyPress(object sender, KeyPressEventArgs e) @@ -141,16 +140,14 @@ namespace xServer.Forms private void txtInstallname_KeyPress(object sender, KeyPressEventArgs e) { - char[] illegal = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray(); - - e.Handled = ((e.KeyChar == '\\' || illegal.Any(illegalChar => (illegalChar == e.KeyChar))) && !char.IsControl(e.KeyChar)); + e.Handled = ((e.KeyChar == '\\' || Helper.CheckPathForIllegalChars(e.KeyChar.ToString())) && + !char.IsControl(e.KeyChar)); } private void txtInstallsub_KeyPress(object sender, KeyPressEventArgs e) { - char[] illegal = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray(); - - e.Handled = ((e.KeyChar == '\\' || illegal.Any(illegalChar => (illegalChar == e.KeyChar))) && !char.IsControl(e.KeyChar)); + e.Handled = ((e.KeyChar == '\\' || Helper.CheckPathForIllegalChars(e.KeyChar.ToString())) && + !char.IsControl(e.KeyChar)); } private void btnMutex_Click(object sender, EventArgs e)