Skip to content

Commit

Permalink
#50 Port list from txt to xml changed
Browse files Browse the repository at this point in the history
  • Loading branch information
BornToBeRoot committed Sep 3, 2017
1 parent 007b728 commit 77dd54b
Show file tree
Hide file tree
Showing 5 changed files with 74,834 additions and 31 deletions.
83 changes: 69 additions & 14 deletions Scripts/Create-PortListFromWeb.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,87 @@
#[xml]$LatestPorts = Get-Content -Path "$PSScriptRoot\Service Name and Transport Protocol Port Number Registry.xml"
[xml]$LatestPorts = (Invoke-WebRequest -Uri "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml").Content
# Filepath in the resources
[string]$OutFilePath = Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath "Source\NETworkManager\Resources\Ports.xml"

$Output = ""
# Download from IANA...
[xml]$ServiceNamePortNumbers = (Invoke-WebRequest -Uri "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml").Content

foreach($record in $LatestPorts.ChildNodes.record)
# Create xml document
[xml]$Document = New-Object System.Xml.XmlDocument
$Declaration = $Document.CreateXmlDeclaration("1.0", "UTF-8", $null)

[void]$Document.AppendChild($Declaration)

# Description
$Description = @"
Service Name and Transport Protocol Port Number Registry
Generated $(Get-Date)
"@

[void]$Document.AppendChild($Document.CreateComment($Description))

# Root node
$RootNode = $Document.CreateNode("element", "Ports", $null)

# Create node for each port
foreach($Record in $ServiceNamePortNumbers.ChildNodes.record)
{
if([string]::IsNullOrEmpty($record.number) -or ([string]::IsNullOrEmpty($record.protocol)))
if([string]::IsNullOrEmpty($Record.number) -or ([string]::IsNullOrEmpty($Record.protocol)))
{
continue
}

$Description = ($record.description -replace '`n','') -replace '\s+',' '
}

$Number = $record.number
$Description = ($Record.description -replace '`n','') -replace '\s+',' '
$Number = $Record.number

if($Number -like "*-*")
{
$NumberArr = $Number.Split('-')

foreach($Number1 in $NumberArr[0]..$NumberArr[1])
{
$Output += "$Number1|$($record.protocol)|$($record.name)|$Description`n"
{
$PortNode = $Document.CreateNode("element", "Port", $null)

$NumberElement = $Document.CreateElement("Number")
$NumberElement.InnerText = $Number1
[void]$PortNode.AppendChild($NumberElement)

$ProtocolElement = $Document.CreateElement("Protocol")
$ProtocolElement.InnerText = $record.protocol
[void]$PortNode.AppendChild($ProtocolElement)

$NumberName = $Document.CreateElement("Name")
$NumberName.InnerText = $Record.name
[void]$PortNode.AppendChild($NumberName)

$NumberDescription = $Document.CreateElement("Description")
$NumberDescription.InnerText = $Description
[void]$PortNode.AppendChild($NumberDescription)

[void]$RootNode.AppendChild($PortNode)
}
}
else
{
$Output += "$Number|$($record.protocol)|$($record.name)|$Description`n"
$PortNode = $Document.CreateNode("element", "Port", $null)

$NumberElement = $Document.CreateElement("Number")
$NumberElement.InnerText = $Number
[void]$PortNode.AppendChild($NumberElement)

$ProtocolElement = $Document.CreateElement("Protocol")
$ProtocolElement.InnerText = $record.protocol
[void]$PortNode.AppendChild($ProtocolElement)

$NumberName = $Document.CreateElement("Name")
$NumberName.InnerText = $Record.name
[void]$PortNode.AppendChild($NumberName)

$NumberDescription = $Document.CreateElement("Description")
$NumberDescription.InnerText = $Description
[void]$PortNode.AppendChild($NumberDescription)

[void]$RootNode.AppendChild($PortNode)
}
}
}

Out-File -InputObject $Output -FilePath (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath "Source\NETworkManager\Resources\ports.txt")
[void]$Document.AppendChild($RootNode)
$Document.Save($OutFilePath)
21 changes: 9 additions & 12 deletions Source/NETworkManager/Models/Network/PortLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using System.IO;
using NETworkManager.Models.Settings;
using System;
using System.Globalization;
using System.Xml;

namespace NETworkManager.Models.Network
{
public static class PortLookup
{
#region Variables
private static string PortsFilePath = Path.Combine(ConfigurationManager.Current.ExecutionPath, "Resources", "ports.txt");
private static string PortsFilePath = Path.Combine(ConfigurationManager.Current.ExecutionPath, "Resources", "Ports.xml");

private static List<PortLookupInfo> PortList;
private static Lookup<int, PortLookupInfo> Ports;
Expand All @@ -22,18 +22,15 @@ static PortLookup()
{
PortList = new List<PortLookupInfo>();

// Load list from resource folder (.txt-file)
foreach (string line in File.ReadAllLines(PortsFilePath))
{
if (string.IsNullOrEmpty(line))
continue;

string[] portData = line.Split('|');
XmlDocument document = new XmlDocument();
document.Load(PortsFilePath);

int port = int.Parse(portData[0]);
Protocol protocol = (Protocol)Enum.Parse(typeof(Protocol), portData[1]);
foreach (XmlNode node in document.SelectNodes("/Ports/Port"))
{
int port = int.Parse(node.SelectSingleNode("Number").InnerText);
Protocol protocol = (Protocol)Enum.Parse(typeof(Protocol), node.SelectSingleNode("Protocol").InnerText);

PortList.Add(new PortLookupInfo(port, protocol, portData[2], portData[3]));
PortList.Add(new PortLookupInfo(port, protocol, node.SelectSingleNode("Name").InnerText, node.SelectSingleNode("Description").InnerText));
}

Ports = (Lookup<int, PortLookupInfo>)PortList.ToLookup(x => x.Number);
Expand Down
10 changes: 5 additions & 5 deletions Source/NETworkManager/NETworkManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="Resources\ports.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Resource Include="3rdParty\Heijden.DNS\Records\totla.txt" />
<Resource Include="3rdParty\Heijden.DNS\Root\named_root.txt" />
Expand All @@ -667,6 +662,11 @@
<ItemGroup>
<Resource Include="Resources\Entypo-license.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Ports.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading

0 comments on commit 77dd54b

Please sign in to comment.