Port forward fixes
This commit is contained in:
@@ -1365,12 +1365,3 @@ class Program
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PortForward
|
||||
{
|
||||
public string VmName { get; set; } = string.Empty;
|
||||
public int HostPort { get; set; }
|
||||
public int VmPort { get; set; }
|
||||
public string Protocol { get; set; } = "TCP";
|
||||
public DateTime Created { get; set; }
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
using QemuVmManager.Models;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.Json;
|
||||
using System.Linq;
|
||||
|
||||
namespace QemuVmManager.Core;
|
||||
|
||||
@@ -74,6 +76,39 @@ public class QemuCommandBuilder
|
||||
return string.Join(" ", _arguments);
|
||||
}
|
||||
|
||||
private string GetCustomPortForwards()
|
||||
{
|
||||
try
|
||||
{
|
||||
var configFile = "port-forwards.json";
|
||||
if (!File.Exists(configFile))
|
||||
return string.Empty;
|
||||
|
||||
var json = File.ReadAllText(configFile);
|
||||
var portForwards = System.Text.Json.JsonSerializer.Deserialize<List<PortForward>>(json) ?? new List<PortForward>();
|
||||
|
||||
// Filter port forwards for this VM
|
||||
var vmForwards = portForwards.Where(pf => pf.VmName == _config.Name).ToList();
|
||||
|
||||
if (!vmForwards.Any())
|
||||
return string.Empty;
|
||||
|
||||
// Build hostfwd arguments for custom port forwards
|
||||
var forwardArgs = new List<string>();
|
||||
foreach (var pf in vmForwards)
|
||||
{
|
||||
forwardArgs.Add($"hostfwd=tcp::{pf.HostPort}-:{pf.VmPort}");
|
||||
}
|
||||
|
||||
return string.Join(",", forwardArgs);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Warning: Could not load custom port forwards: {ex.Message}");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddMachineConfiguration()
|
||||
{
|
||||
_arguments.Add("-machine");
|
||||
@@ -210,7 +245,24 @@ public class QemuCommandBuilder
|
||||
// On macOS, use host networking with automatic DHCP (built into QEMU)
|
||||
// The 'net=' parameter automatically enables DHCP server
|
||||
var hostPort = 10000 + i; // Use different ports for multiple interfaces
|
||||
netdevArgs = $"user,id=net{i},net=192.168.100.0/24,host=192.168.100.1,hostfwd=tcp::{hostPort}-:22,hostfwd=tcp::{hostPort + 1}-:80,hostfwd=tcp::{hostPort + 2}-:443";
|
||||
|
||||
// Build base netdev arguments
|
||||
var baseArgs = $"user,id=net{i},net=192.168.100.0/24,host=192.168.100.1";
|
||||
|
||||
// Add built-in port forwards
|
||||
var builtInForwards = $"hostfwd=tcp::{hostPort}-:22,hostfwd=tcp::{hostPort + 1}-:80,hostfwd=tcp::{hostPort + 2}-:443";
|
||||
|
||||
// Add custom port forwards from configuration
|
||||
var customForwards = GetCustomPortForwards();
|
||||
if (!string.IsNullOrEmpty(customForwards))
|
||||
{
|
||||
netdevArgs = $"{baseArgs},{builtInForwards},{customForwards}";
|
||||
}
|
||||
else
|
||||
{
|
||||
netdevArgs = $"{baseArgs},{builtInForwards}";
|
||||
}
|
||||
|
||||
_arguments.Add(netdevArgs);
|
||||
|
||||
Console.WriteLine($"Info: Using host-based networking with automatic DHCP on macOS");
|
||||
@@ -326,7 +378,11 @@ public class QemuCommandBuilder
|
||||
if (displayType == "vnc")
|
||||
{
|
||||
var vncPort = display.SpicePort > 0 ? display.SpicePort : 5900;
|
||||
displayType = $"vnc=0.0.0.0:{vncPort}";
|
||||
// QEMU VNC display numbering: vnc=0.0.0.0:0 means port 5900, vnc=0.0.0.0:1 means port 5901, etc.
|
||||
// So for port 5900, we use display 0, for port 5901, we use display 1, etc.
|
||||
var displayNumber = vncPort - 5900;
|
||||
if (displayNumber < 0) displayNumber = 0; // Default to display 0 if port < 5900
|
||||
displayType = $"vnc=0.0.0.0:{displayNumber}";
|
||||
}
|
||||
|
||||
_arguments.Add(displayType);
|
||||
|
@@ -135,3 +135,12 @@ public class PortForwardingResponse
|
||||
public string? ErrorMessage { get; set; }
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public class PortForward
|
||||
{
|
||||
public string VmName { get; set; } = string.Empty;
|
||||
public int HostPort { get; set; }
|
||||
public int VmPort { get; set; }
|
||||
public string Protocol { get; set; } = "TCP";
|
||||
public DateTime Created { get; set; }
|
||||
}
|
||||
|
Reference in New Issue
Block a user