Skip to content

Commit

Permalink
only allow valid IPv4 addresses for NT server host
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansen4857 committed Jan 18, 2024
1 parent 05a0d99 commit d8f3851
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
11 changes: 11 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ void main() async {

SharedPreferences prefs = await SharedPreferences.getInstance();

// Ensure saved host is an IP
try {
String? hostIP = prefs.getString(PrefsKeys.ntServerAddress);
if (hostIP != null) {
Uri.parseIPv4Address(hostIP);
}
} catch (_) {
// Not a valid IP, reset to default
prefs.setString(PrefsKeys.ntServerAddress, Defaults.ntServerAddress);
}

PPLibTelemetry telemetry = PPLibTelemetry(
serverBaseAddress: prefs.getString(PrefsKeys.ntServerAddress) ??
Defaults.ntServerAddress);
Expand Down
2 changes: 1 addition & 1 deletion lib/util/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Defaults {
static const bool holonomicMode = true;
static const double projectLeftWeight = 0.5;
static const double editorTreeWeight = 0.5;
static const String ntServerAddress = 'localhost';
static const String ntServerAddress = '127.0.0.1';
static const bool treeOnRight = true;
static const String pathSortOption = 'recent';
static const String autoSortOption = 'recent';
Expand Down
21 changes: 14 additions & 7 deletions lib/widgets/dialogs/settings_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,21 @@ class _SettingsDialogState extends State<SettingsDialog> {
Expanded(
child: _buildTextField(
context,
'Host',
'Host IP (localhost = 127.0.0.1)',
(value) {
widget.prefs.setString(
PrefsKeys.ntServerAddress, value);
setState(() {
_pplibClientHost = value;
});
widget.onSettingsChanged();
// Check if valid IP
try {
Uri.parseIPv4Address(value);

widget.prefs.setString(
PrefsKeys.ntServerAddress, value);
setState(() {
_pplibClientHost = value;
});
widget.onSettingsChanged();
} catch (_) {
setState(() {});
}
},
_pplibClientHost,
null,
Expand Down

0 comments on commit d8f3851

Please sign in to comment.