Validate ip address and port

This commit is contained in:
Stef
2021-11-17 21:54:34 +01:00
parent dc8b43574f
commit 78b3557635
@@ -9,7 +9,9 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Acr.UserDialogs; using Acr.UserDialogs;
using Xamarin.Essentials; using Xamarin.Essentials;
using Xamarin.Forms; using Xamarin.Forms;
@@ -41,7 +43,7 @@ namespace Xamarin.Neo4j.ViewModels
Commands.Add("Test", new Command(async () => Commands.Add("Test", new Command(async () =>
{ {
// TODO: Validate input. if (!ValidateInput()) return;
var connectionString = BuildConnectionString(); var connectionString = BuildConnectionString();
@@ -52,7 +54,7 @@ namespace Xamarin.Neo4j.ViewModels
Commands.Add("Save", new Command(async () => Commands.Add("Save", new Command(async () =>
{ {
// TODO: Validate input. if (!ValidateInput()) return;
var connectionString = BuildConnectionString(); var connectionString = BuildConnectionString();
@@ -70,7 +72,7 @@ namespace Xamarin.Neo4j.ViewModels
Commands.Add("Connect", new Command(async () => Commands.Add("Connect", new Command(async () =>
{ {
// TODO: Validate input. if (!ValidateInput()) return;
var connectionString = BuildConnectionString(); var connectionString = BuildConnectionString();
@@ -103,6 +105,28 @@ namespace Xamarin.Neo4j.ViewModels
}; };
} }
private bool ValidateInput()
{
// Username and Password not checked because username- and passwordless connections are allowed
var ipRegex = new Regex(@"\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b");
var portRegex = new Regex(@"^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$");
if (!ipRegex.IsMatch(Host))
{
UserDialogs.Instance.AlertAsync("Incorrect host format");
return false;
}
if (!portRegex.IsMatch(Port.ToString()))
{
UserDialogs.Instance.AlertAsync("Incorrect port format");
return false;
}
return true;
}
#region Bindable Properties #region Bindable Properties
public bool Encrypted public bool Encrypted