Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash on invalid ip and or port change #233

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ThatGuyThimo
Copy link

@ThatGuyThimo ThatGuyThimo commented Oct 7, 2024

When changing the ip address or port number to an invalid value on the next restart the app would crash do to the loaded invalid value'(s),

This pull requests checks if: the ip address is not blank or white space, the ip is an valid ip address, it is a resolvable hostname.
It also checks if the ports are not smaller than 1.

Let me know if the code needs any modifications.

@benaclejames benaclejames self-requested a review October 7, 2024 20:45
Comment on lines 92 to 101
{
var hostEntry = Dns.GetHostEntry(address);
return true;
}
catch
{
// DNS resolution failed
return false;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be simplified as

return Uri.CheckHostName(address) != UriHostNameType.Unknown

That way you could avoid the exception handling, as well as resolve for hostnames and IP addresses all in a single line.

Comment on lines 78 to 102
{
if (string.IsNullOrWhiteSpace(address))
{
return false;
}

// Check if it's a valid IP address
if (IPAddress.TryParse(address, out _))
{
return true;
}

// Check if it's a resolvable hostname
try
{
var hostEntry = Dns.GetHostEntry(address);
return true;
}
catch
{
// DNS resolution failed
return false;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do elect to use Uri.CheckHostName, this whole function could probably be simplified to

private static bool IsValidAddress(string address) =>
    !string.IsNullOrWhiteSpace(address) && 
    (IPAddress.TryParse(address, out _) || 
    Uri.CheckHostName(address) != UriHostNameType.Unknown);

Comment on lines 59 to 75
{
if (newValue < 1)
{
InPort = DefaultInPort;
}
}

/// Called after the OutPort property has changed.
/// It checks if the OutPort is valid and reverts to the default if it's invalid (less than 1).
partial void OnOutPortChanged(int oldValue, int newValue)
{
if (newValue < 1)
{
OutPort = DefaultOutPort;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry this will cause an infinite loop. Also, such functionality could be achieved by editing values in the ui xaml here


and here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, not sure you want to be using these operators. They both make it so that the port can only be set if the port is less than 1..

Copy link
Author

@ThatGuyThimo ThatGuyThimo Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i will use that instead than the port check does not have to exist,

As for the IP verification i might change the way i approach that since host names are disallowed as it will throw an exception in

UpdateTarget(new IPEndPoint(IPAddress.Parse(_oscTarget.DestinationAddress), _oscTarget.InPort));

It might also be smart to do this in

{
_logger = logger;
_cts = new CancellationTokenSource();
_oscTarget = oscTarget;
_settingsService = settingsService;
_oscTarget.PropertyChanged += (_, args) =>
{
if (args.PropertyName is not nameof(IOscTarget.InPort))
{
return;
}
if (string.IsNullOrEmpty(_oscTarget.DestinationAddress) || _oscTarget.InPort == default)
{
return;
}
UpdateTarget(new IPEndPoint(IPAddress.Parse(_oscTarget.DestinationAddress), _oscTarget.InPort));
};
}

As there is already some handling in there but it seems to actually not handle empty strings but does handle NULL ref

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this would still cause the application to crash on restart since it doesn't reset the value.

…ettingsPage.xaml, updated the IsValidAddress to be more comppressed and removed the hostname check since its not used.
@ThatGuyThimo
Copy link
Author

Changed the files as requested. I ended up not using host name since its not supported as listed in the comment above,
Also used the settingsPage.xaml for the minimum as that also effectively prevents setting invalid value's.

/// Called after the DestinationAddress property has changed.
/// It validates the new address and reverts it to the default if invalid.
partial void OnDestinationAddressChanged(string oldValue, string newValue)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be an idea to use NotifyDataErrorInfo for this instead of just reverting the val
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#requesting-property-validation

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might even be able to create a custom validationattribute to validate it's a valid IP and be rid of that whole method

Copy link
Author

@ThatGuyThimo ThatGuyThimo Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look into it. Seems promising, would also like to not have an validating function in the class if its not necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be an idea to use NotifyDataErrorInfo for this instead of just reverting the val https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#requesting-property-validation

this is not directly possible since NotifyDataErrorInfo is an inheritance from ObservableProperty while the OSCTarget class is an child of ObservableObject which does not support it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wat? The parent of OscTarget has no relevance. You should just be able to append the attribute below the existing ObservableProperty attribute of DestinationAddress

Copy link
Author

@ThatGuyThimo ThatGuyThimo Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk man it didn't allow me to do it this way, i just ended up doing your second suggestion which is a more elegant solution anyway, in my opinion.

…ogging when given IP is invalid and revert back to default.
@ThatGuyThimo
Copy link
Author

ThatGuyThimo commented Oct 9, 2024

Maybe Remove the IsNullOrEmpty check in OscRecvService and OscSendService since that is now being handled by the validator.
https://github.com/ThatGuyThimo/VRCFaceTracking/blob/37b39c8e1ffcb444e91e8e04bd68107cca93d85a/VRCFaceTracking.Core/Services/OscRecvService.cs#L45-L49

…nd OscSendService, updated ValidIpAddressAttribute to handel IsNullOrWhiteSpace correctly
Comment on lines +53 to +58
if (!Validator.TryValidateObject(oscTarget, context, validationResults, true))
{
var errorMessages = string.Join(Environment.NewLine, validationResults.Select(vr => vr.ErrorMessage));
_logger.LogWarning($"{errorMessages} Reverting to default.");
oscTarget.DestinationAddress = "127.0.0.1";
}
Copy link
Author

@ThatGuyThimo ThatGuyThimo Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On startup this gets run before the DestinationAddress is loaded from the SavedSetting which cause the warning to be printed in the Output, but this has no effect on the final DestinationAddress that gets loaded afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants