mirror of
https://github.com/freedbygrace/PSOPNSenseAPI.git
synced 2026-07-27 04:09:29 +00:00
Clean up repository: remove GitHub workflow, update .gitignore, remove unused files, update module structure
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
|
||||
<AssemblyName>PSOPNSenseAPI</AssemblyName>
|
||||
<RootNamespace>PSOPNSenseAPI</RootNamespace>
|
||||
<Version>2025.04.14.2253</Version>
|
||||
<Version>2025.04.14.2306</Version>
|
||||
<Authors>PSOPNSenseAPI Contributors</Authors>
|
||||
<Company>PSOPNSenseAPI</Company>
|
||||
<Description>PowerShell module for interacting with the OPNSense API to configure firewalls</Description>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
@{
|
||||
# Script module or binary module file associated with this manifest.
|
||||
RootModule = 'PSOPNSenseAPI.dll'
|
||||
RootModule = 'PSOPNSenseAPI.psm1'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '0.5.0'
|
||||
ModuleVersion = '2025.04.14.2306'
|
||||
|
||||
# Supported PSEditions
|
||||
CompatiblePSEditions = @('Desktop', 'Core')
|
||||
@@ -45,7 +45,7 @@
|
||||
# RequiredModules = @()
|
||||
|
||||
# Assemblies that must be loaded prior to importing this module
|
||||
RequiredAssemblies = @('System.Net.Http', 'Newtonsoft.Json', 'System.Net.IPNetwork')
|
||||
# RequiredAssemblies = @()
|
||||
|
||||
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
|
||||
# ScriptsToProcess = @()
|
||||
|
||||
@@ -1,202 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using PSOPNSenseAPI.Models;
|
||||
|
||||
namespace PSOPNSenseAPI.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Service for managing port forwarding rules in OPNSense
|
||||
/// </summary>
|
||||
public class PortForwardingService : ServiceBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PortForwardingService"/> class
|
||||
/// </summary>
|
||||
/// <param name="apiClient">The API client to use for requests</param>
|
||||
public PortForwardingService(ApiClient apiClient) : base(apiClient)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all port forwarding rules
|
||||
/// </summary>
|
||||
/// <returns>A list of port forwarding rules</returns>
|
||||
public async Task<List<PortForwardingRule>> GetPortForwardingRulesAsync()
|
||||
{
|
||||
var response = await ApiClient.GetAsync("/firewall/nat/getRule");
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var jsonResponse = JObject.Parse(responseContent);
|
||||
|
||||
if (jsonResponse["status"]?.ToString() != "ok")
|
||||
{
|
||||
throw new Exception($"Failed to get port forwarding rules: {jsonResponse["status_msg"]}");
|
||||
}
|
||||
|
||||
var rules = new List<PortForwardingRule>();
|
||||
var rulesData = jsonResponse["rows"];
|
||||
|
||||
foreach (var rule in rulesData)
|
||||
{
|
||||
var portForwardingRule = new PortForwardingRule
|
||||
{
|
||||
Uuid = rule["uuid"]?.ToString(),
|
||||
Enabled = rule["enabled"]?.ToString() == "1",
|
||||
Description = rule["descr"]?.ToString(),
|
||||
Interface = rule["interface"]?.ToString(),
|
||||
Protocol = rule["protocol"]?.ToString(),
|
||||
Source = rule["source"]?.ToString(),
|
||||
SourcePort = rule["source_port"]?.ToString(),
|
||||
Destination = rule["destination"]?.ToString(),
|
||||
DestinationPort = rule["destination_port"]?.ToString(),
|
||||
TargetIP = rule["target"]?.ToString(),
|
||||
TargetPort = rule["target_port"]?.ToString(),
|
||||
NatReflection = rule["natreflection"]?.ToString(),
|
||||
FilterRuleAssociation = rule["associated-rule-id"]?.ToString() != "none",
|
||||
Log = rule["log"]?.ToString() == "1"
|
||||
};
|
||||
|
||||
rules.Add(portForwardingRule);
|
||||
}
|
||||
|
||||
return rules;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a port forwarding rule by UUID
|
||||
/// </summary>
|
||||
/// <param name="uuid">The UUID of the rule to get</param>
|
||||
/// <returns>The port forwarding rule</returns>
|
||||
public async Task<PortForwardingRule> GetPortForwardingRuleAsync(string uuid)
|
||||
{
|
||||
var rules = await GetPortForwardingRulesAsync();
|
||||
return rules.Find(r => r.Uuid == uuid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new port forwarding rule
|
||||
/// </summary>
|
||||
/// <param name="rule">The rule to create</param>
|
||||
/// <returns>The UUID of the created rule</returns>
|
||||
public async Task<string> CreatePortForwardingRuleAsync(PortForwardingRule rule)
|
||||
{
|
||||
var ruleData = new
|
||||
{
|
||||
rule = new
|
||||
{
|
||||
enabled = rule.Enabled ? "1" : "0",
|
||||
descr = rule.Description,
|
||||
interface = rule.Interface,
|
||||
protocol = rule.Protocol,
|
||||
source = rule.Source,
|
||||
source_port = rule.SourcePort,
|
||||
destination = rule.Destination,
|
||||
destination_port = rule.DestinationPort,
|
||||
target = rule.TargetIP,
|
||||
target_port = rule.TargetPort,
|
||||
natreflection = rule.NatReflection,
|
||||
associated_rule_id = rule.FilterRuleAssociation ? "pass" : "none",
|
||||
log = rule.Log ? "1" : "0"
|
||||
}
|
||||
};
|
||||
|
||||
var response = await ApiClient.PostAsync("/firewall/nat/addRule", ruleData);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var jsonResponse = JObject.Parse(responseContent);
|
||||
|
||||
if (jsonResponse["status"]?.ToString() != "ok")
|
||||
{
|
||||
throw new Exception($"Failed to create port forwarding rule: {jsonResponse["status_msg"]}");
|
||||
}
|
||||
|
||||
// Apply changes
|
||||
await ApplyChangesAsync();
|
||||
|
||||
return jsonResponse["uuid"]?.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing port forwarding rule
|
||||
/// </summary>
|
||||
/// <param name="rule">The rule to update</param>
|
||||
/// <returns>True if successful</returns>
|
||||
public async Task<bool> UpdatePortForwardingRuleAsync(PortForwardingRule rule)
|
||||
{
|
||||
if (string.IsNullOrEmpty(rule.Uuid))
|
||||
{
|
||||
throw new ArgumentException("Rule UUID cannot be null or empty", nameof(rule));
|
||||
}
|
||||
|
||||
var ruleData = new
|
||||
{
|
||||
rule = new
|
||||
{
|
||||
enabled = rule.Enabled ? "1" : "0",
|
||||
descr = rule.Description,
|
||||
interface = rule.Interface,
|
||||
protocol = rule.Protocol,
|
||||
source = rule.Source,
|
||||
source_port = rule.SourcePort,
|
||||
destination = rule.Destination,
|
||||
destination_port = rule.DestinationPort,
|
||||
target = rule.TargetIP,
|
||||
target_port = rule.TargetPort,
|
||||
natreflection = rule.NatReflection,
|
||||
associated_rule_id = rule.FilterRuleAssociation ? "pass" : "none",
|
||||
log = rule.Log ? "1" : "0"
|
||||
}
|
||||
};
|
||||
|
||||
var response = await ApiClient.PostAsync($"/firewall/nat/setRule/{rule.Uuid}", ruleData);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var jsonResponse = JObject.Parse(responseContent);
|
||||
|
||||
if (jsonResponse["status"]?.ToString() != "ok")
|
||||
{
|
||||
throw new Exception($"Failed to update port forwarding rule: {jsonResponse["status_msg"]}");
|
||||
}
|
||||
|
||||
// Apply changes
|
||||
await ApplyChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a port forwarding rule
|
||||
/// </summary>
|
||||
/// <param name="uuid">The UUID of the rule to delete</param>
|
||||
/// <returns>True if successful</returns>
|
||||
public async Task<bool> DeletePortForwardingRuleAsync(string uuid)
|
||||
{
|
||||
var response = await ApiClient.PostAsync($"/firewall/nat/delRule/{uuid}", null);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var jsonResponse = JObject.Parse(responseContent);
|
||||
|
||||
if (jsonResponse["status"]?.ToString() != "ok")
|
||||
{
|
||||
throw new Exception($"Failed to delete port forwarding rule: {jsonResponse["status_msg"]}");
|
||||
}
|
||||
|
||||
// Apply changes
|
||||
await ApplyChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the changes to the firewall
|
||||
/// </summary>
|
||||
/// <returns>True if successful</returns>
|
||||
private async Task<bool> ApplyChangesAsync()
|
||||
{
|
||||
var response = await ApiClient.PostAsync("/firewall/nat/apply", null);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var jsonResponse = JObject.Parse(responseContent);
|
||||
|
||||
return jsonResponse["status"]?.ToString() == "ok";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user