Browse Source

iOS: fix port forwarding input issues

osy 4 years ago
parent
commit
d5752882b3

+ 4 - 4
Configuration/UTMConfiguration+Networking.m

@@ -196,9 +196,9 @@ static const NSString *const kUTMConfigNetworkPortForwardGuestPortKey = @"GuestP
         portForward = [[UTMConfigurationPortForward alloc] init];
         portForward.protocol = dict[kUTMConfigNetworkPortForwardProtocolKey];
         portForward.hostAddress = dict[kUTMConfigNetworkPortForwardHostAddressKey];
-        portForward.hostPort = [dict[kUTMConfigNetworkPortForwardHostPortKey] integerValue];
+        portForward.hostPort = dict[kUTMConfigNetworkPortForwardHostPortKey];
         portForward.guestAddress = dict[kUTMConfigNetworkPortForwardGuestAddressKey];
-        portForward.guestPort = [dict[kUTMConfigNetworkPortForwardGuestPortKey] integerValue];
+        portForward.guestPort = dict[kUTMConfigNetworkPortForwardGuestPortKey];
     }
     return portForward;
 }
@@ -210,9 +210,9 @@ static const NSString *const kUTMConfigNetworkPortForwardGuestPortKey = @"GuestP
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
     dict[kUTMConfigNetworkPortForwardProtocolKey] = argument.protocol;
     dict[kUTMConfigNetworkPortForwardHostAddressKey] = argument.hostAddress;
-    dict[kUTMConfigNetworkPortForwardHostPortKey] = @(argument.hostPort);
+    dict[kUTMConfigNetworkPortForwardHostPortKey] = argument.hostPort;
     dict[kUTMConfigNetworkPortForwardGuestAddressKey] = argument.guestAddress;
-    dict[kUTMConfigNetworkPortForwardGuestPortKey] = @(argument.guestPort);
+    dict[kUTMConfigNetworkPortForwardGuestPortKey] = argument.guestPort;
     [self propertyWillChange];
     self.rootDict[kUTMConfigNetworkingKey][kUTMConfigNetworkPortForwardKey][index] = dict;
 }

+ 2 - 2
Configuration/UTMConfigurationPortForward.h

@@ -22,9 +22,9 @@ NS_ASSUME_NONNULL_BEGIN
 
 @property (nonatomic, nullable) NSString *protocol;
 @property (nonatomic, nullable) NSString *hostAddress;
-@property (nonatomic) NSInteger hostPort;
+@property (nonatomic, nullable) NSNumber *hostPort;
 @property (nonatomic, nullable) NSString *guestAddress;
-@property (nonatomic) NSInteger guestPort;
+@property (nonatomic, nullable) NSNumber *guestPort;
 
 @end
 

+ 2 - 2
Configuration/UTMConfigurationPortForward.m

@@ -29,7 +29,7 @@
     _hostAddress = hostAddress;
 }
 
-- (void)setHostPort:(NSInteger)hostPort {
+- (void)setHostPort:(NSNumber *)hostPort {
     [self propertyWillChange];
     _hostPort = hostPort;
 }
@@ -39,7 +39,7 @@
     _guestAddress = guestAddress;
 }
 
-- (void)setGuestPort:(NSInteger)guestPort {
+- (void)setGuestPort:(NSNumber *)guestPort {
     [self propertyWillChange];
     _guestPort = guestPort;
 }

+ 2 - 4
Platform/Shared/VMConfigPortForwardForm.swift

@@ -34,8 +34,7 @@ struct VMConfigPortForwardForm: View {
             HStack {
                 Text("Guest Port")
                 Spacer()
-                TextField("1234", value: $configPort.guestPort, formatter: NumberFormatter())
-                    .keyboardType(.numberPad)
+                NumberTextField("1234", number: $configPort.guestPort)
             }
             HStack {
                 Text("Host Address")
@@ -46,8 +45,7 @@ struct VMConfigPortForwardForm: View {
             HStack {
                 Text("Host Port")
                 Spacer()
-                TextField("1234", value: $configPort.hostPort, formatter: NumberFormatter())
-                    .keyboardType(.numberPad)
+                NumberTextField("1234", number: $configPort.hostPort)
             }
         }
     }

+ 5 - 5
Platform/iOS/Legacy/VMConfigPortForwardingViewController.m

@@ -55,7 +55,7 @@
     NSAssert(indexPath.section == 0, @"Invalid section");
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"portForwardCell" forIndexPath:indexPath];
     UTMConfigurationPortForward *portForward = [self.configuration portForwardForIndex:indexPath.row];
-    cell.textLabel.text = [NSString stringWithFormat:@"%@:%ld ➡️ %@:%ld", portForward.hostAddress, portForward.hostPort, portForward.guestAddress, portForward.guestPort];
+    cell.textLabel.text = [NSString stringWithFormat:@"%@:%@ ➡️ %@:%@", portForward.hostAddress, portForward.hostPort, portForward.guestAddress, portForward.guestPort];
     cell.detailTextLabel.text = portForward.protocol;
     return cell;
 }
@@ -98,7 +98,7 @@
     [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
         textField.placeholder = NSLocalizedString(@"Host port (required)", @"VMConfigPortForwardingViewController");
         textField.keyboardType = UIKeyboardTypeNumberPad;
-        textField.text = existing ? [@(existing.hostPort) stringValue] : @"";
+        textField.text = existing ? [existing.hostPort stringValue] : @"";
     }];
     [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
         textField.placeholder = NSLocalizedString(@"Guest address (optional)", @"VMConfigPortForwardingViewController");
@@ -108,7 +108,7 @@
     [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
         textField.placeholder = NSLocalizedString(@"Guest port (required)", @"VMConfigPortForwardingViewController");
         textField.keyboardType = UIKeyboardTypeNumberPad;
-        textField.text = existing ? [@(existing.guestPort) stringValue] : @"";
+        textField.text = existing ? [existing.guestPort stringValue] : @"";
     }];
     [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"VMConfigPortForwardingViewController")
                                                         style:UIAlertActionStyleCancel
@@ -119,9 +119,9 @@
         UTMConfigurationPortForward *portForward = [[UTMConfigurationPortForward alloc] init];
         portForward.protocol = tcp ? @"tcp" : @"udp";
         portForward.hostAddress = alertController.textFields[0].text;
-        portForward.hostPort = [alertController.textFields[1].text integerValue];
+        portForward.hostPort = @([alertController.textFields[1].text integerValue]);
         portForward.guestAddress = alertController.textFields[2].text;
-        portForward.guestPort = [alertController.textFields[3].text integerValue];
+        portForward.guestPort = @([alertController.textFields[3].text integerValue]);
         //TODO: validate input
         if (existing) {
             [self.configuration updatePortForwardAtIndex:index withValue:portForward];

+ 6 - 6
Platform/iOS/VMConfigNetworkPortForwardView.swift

@@ -29,7 +29,7 @@ struct VMConfigNetworkPortForwardView: View {
                         destination: PortForwardEdit(config: config, index: index),
                         label: {
                             VStack(alignment: .leading) {
-                                Text("\(configPort.guestAddress ?? ""):\(String(configPort.guestPort)) ➡️ \(configPort.hostAddress ?? ""):\(String(configPort.hostPort))")
+                                Text(verbatim: "\(configPort.guestAddress ?? ""):\(configPort.guestPort ?? 0) ➡️ \(configPort.hostAddress ?? ""):\(configPort.hostPort ?? 0)")
                                 Text(configPort.protocol ?? "").font(.subheadline)
                             }
                         })
@@ -77,7 +77,7 @@ struct PortForwardEdit: View {
         }.navigationBarItems(trailing:
             Button(action: savePortForward, label: {
                 Text("Save")
-            }).disabled(configPort.guestPort == 0 || configPort.hostPort == 0)
+            }).disabled(configPort.guestPort?.intValue ?? 0 == 0 || configPort.hostPort?.intValue ?? 0 == 0)
         )
     }
     
@@ -99,15 +99,15 @@ struct VMConfigNetworkPortForwardView_Previews: PreviewProvider {
                     let newConfigPort = UTMConfigurationPortForward()
                     newConfigPort.protocol = "tcp"
                     newConfigPort.guestAddress = "1.2.3.4"
-                    newConfigPort.guestPort = 1234
+                    newConfigPort.guestPort = NSNumber(value: 1234)
                     newConfigPort.hostAddress = "4.3.2.1"
-                    newConfigPort.hostPort = 4321
+                    newConfigPort.hostPort = NSNumber(value: 4321)
                     config.newPortForward(newConfigPort)
                     newConfigPort.protocol = "udp"
                     newConfigPort.guestAddress = nil
-                    newConfigPort.guestPort = 2222
+                    newConfigPort.guestPort = NSNumber(value: 2222)
                     newConfigPort.hostAddress = nil
-                    newConfigPort.hostPort = 3333
+                    newConfigPort.hostPort = NSNumber(value: 3333)
                     config.newPortForward(newConfigPort)
                 }
             }

+ 6 - 6
Platform/macOS/VMConfigNetworkPortForwardView.swift

@@ -48,7 +48,7 @@ struct VMConfigNetworkPortForwardView: View {
                     }
 
                     Button(action: { editingNewPortBinding.wrappedValue = true }, label: {
-                        Text("\(configPort.guestAddress ?? ""):\(String(configPort.guestPort)) ➡️ \(configPort.hostAddress ?? ""):\(String(configPort.hostPort)) (\(configPort.protocol ?? ""))")
+                        Text(verbatim: "\(configPort.guestAddress ?? ""):\(configPort.guestPort ?? 0) ➡️ \(configPort.hostAddress ?? ""):\(configPort.hostPort ?? 0)")
                     }).buttonStyle(PlainButtonStyle())
                     .popover(isPresented: editingNewPortBinding, arrowEdge: .bottom) {
                         PortForwardEdit(config: config, index: index).padding()
@@ -86,7 +86,7 @@ struct PortForwardEdit: View {
                 Spacer()
                 Button(action: savePortForward, label: {
                     Text("Save")
-                }).disabled(configPort.guestPort == 0 || configPort.hostPort == 0)
+                }).disabled(configPort.guestPort?.intValue ?? 0 == 0 || configPort.hostPort?.intValue ?? 0 == 0)
             }
         }
     }
@@ -111,15 +111,15 @@ struct VMConfigNetworkPortForwardView_Previews: PreviewProvider {
                     let newConfigPort = UTMConfigurationPortForward()
                     newConfigPort.protocol = "tcp"
                     newConfigPort.guestAddress = "1.2.3.4"
-                    newConfigPort.guestPort = 1234
+                    newConfigPort.guestPort = NSNumber(value: 1234)
                     newConfigPort.hostAddress = "4.3.2.1"
-                    newConfigPort.hostPort = 4321
+                    newConfigPort.hostPort = NSNumber(value: 4321)
                     config.newPortForward(newConfigPort)
                     newConfigPort.protocol = "udp"
                     newConfigPort.guestAddress = nil
-                    newConfigPort.guestPort = 2222
+                    newConfigPort.guestPort = NSNumber(value: 2222)
                     newConfigPort.hostAddress = nil
-                    newConfigPort.hostPort = 3333
+                    newConfigPort.hostPort = NSNumber(value: 3333)
                     config.newPortForward(newConfigPort)
                 }
             }