Improved check for illegal chars in path

This commit is contained in:
MaxXor 2015-06-01 18:50:20 +02:00
parent 90f30caafb
commit 836b77d13e
4 changed files with 21 additions and 20 deletions

View File

@ -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);

View File

@ -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)
{

View File

@ -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));
}
}
}

View File

@ -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)