-
Notifications
You must be signed in to change notification settings - Fork 1k
[Fix] UPnP Discover
#4235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cschuchardt88
wants to merge
15
commits into
neo-project:dev
Choose a base branch
from
cschuchardt88:dev/fix/upnp
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Fix] UPnP Discover
#4235
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
80cade5
[`Fix`] UPnP Discover
cschuchardt88 bf7daf3
Merge branch 'dev' into dev/fix/upnp
cschuchardt88 35b987c
dotnet format
cschuchardt88 594bd12
Apply suggestions from code review
shargon 64adaa4
Added unit tests
cschuchardt88 be036f5
Added try/catch
cschuchardt88 60b2b1c
Update src/Neo/Network/Messages/DiscoveryResponseMessage.cs
cschuchardt88 6b211aa
Fix test because of timeout
cschuchardt88 a5d0e3a
Add some error logging
cschuchardt88 3de9cb5
Add logging and more fixes for tests
cschuchardt88 3322076
fix macos test
cschuchardt88 7ba0476
Added AppleTalk
cschuchardt88 f16f300
Added LoopBack!!!
cschuchardt88 8a16ecb
Merge branch 'dev' into dev/fix/upnp
ajara87 552eb1f
Merge branch 'dev' into dev/fix/upnp
ajara87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // XmlNodeExtensions.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System.Xml; | ||
|
|
||
| namespace Neo.Extensions.Xml | ||
| { | ||
| public static class XmlNodeExtensions | ||
| { | ||
| public static string GetXmlElementText(this XmlNode node, string elementName) | ||
| { | ||
| var element = node[elementName]; | ||
| return element != null ? element.InnerText : string.Empty; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // DiscoveryResponseMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace Neo.Network.Messages | ||
| { | ||
| internal class DiscoveryResponseMessage | ||
| { | ||
| private readonly IDictionary<string, string> _headers; | ||
|
|
||
| public DiscoveryResponseMessage(string message) | ||
| { | ||
| var lines = message.Split(["\r\n"], StringSplitOptions.RemoveEmptyEntries); | ||
cschuchardt88 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var headers = from h in lines.Skip(1) | ||
| let c = h.Split(':') | ||
| let key = c[0] | ||
| let value = c.Length > 1 | ||
| ? string.Join(":", c.Skip(1).ToArray()) | ||
| : string.Empty | ||
| select new { Key = key, Value = value.Trim() }; | ||
| _headers = headers.ToDictionary(x => x.Key.ToUpperInvariant(), x => x.Value); | ||
| } | ||
|
|
||
| public string this[string key] | ||
| { | ||
| get { return _headers[key.ToUpperInvariant()]; } | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
src/Neo/Network/Messages/Requests/CreatePortMappingRequestMessage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // CreatePortMappingRequestMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Neo.Network.Messages.Requests | ||
| { | ||
| internal class CreatePortMappingRequestMessage : RequestMessage | ||
| { | ||
| private readonly string _remoteHost; | ||
| private readonly string _publicPort; | ||
| private readonly string _privatePort; | ||
| private readonly string _privateIp; | ||
| private readonly string _description; | ||
|
|
||
| public CreatePortMappingRequestMessage( | ||
| string remoteHost, | ||
| string publicPort, | ||
| string privatePort, | ||
| string privateIp, | ||
| string description) | ||
| { | ||
| _remoteHost = remoteHost; | ||
| _publicPort = publicPort; | ||
| _privatePort = privatePort; | ||
| _privateIp = privateIp; | ||
| _description = description; | ||
| } | ||
|
|
||
| public override Dictionary<string, object> ToXml() => | ||
| new() | ||
| { | ||
| {"NewRemoteHost", _remoteHost}, | ||
| {"NewExternalPort", _publicPort}, | ||
| {"NewProtocol", "TCP"}, | ||
| {"NewInternalPort", _privatePort}, | ||
| {"NewInternalClient", _privateIp}, | ||
| {"NewEnabled", 1}, | ||
| {"NewPortMappingDescription", _description}, | ||
| {"NewLeaseDuration", 0}, | ||
| }; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Neo/Network/Messages/Requests/DeletePortMappingRequestMessage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // DeletePortMappingRequestMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Neo.Network.Messages.Requests | ||
| { | ||
| internal class DeletePortMappingRequestMessage : RequestMessage | ||
| { | ||
| private readonly string _publicPort; | ||
|
|
||
| public DeletePortMappingRequestMessage( | ||
| string publicPort) | ||
| { | ||
| _publicPort = publicPort; | ||
| } | ||
|
|
||
| public override Dictionary<string, object> ToXml() => | ||
| new() | ||
| { | ||
| {"NewRemoteHost", string.Empty}, | ||
| {"NewExternalPort", _publicPort}, | ||
| {"NewProtocol", "TCP"}, | ||
| }; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/Neo/Network/Messages/Requests/GetExternalIPAddressRequestMessage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // GetExternalIPAddressRequestMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Neo.Network.Messages.Requests | ||
| { | ||
| internal class GetExternalIPAddressRequestMessage : RequestMessage | ||
| { | ||
| public override Dictionary<string, object> ToXml() => | ||
| []; | ||
shargon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // RequestMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Neo.Network.Messages.Requests | ||
| { | ||
| internal abstract class RequestMessage | ||
| { | ||
| public const string GetExternalIpAddressActionName = "GetExternalIpAddress"; | ||
| public const string AddPortMappingActionName = "AddPortMapping"; | ||
|
|
||
| public abstract IDictionary<string, object> ToXml(); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/Neo/Network/Messages/Responses/GetExternalIPAddressResponseMessage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // GetExternalIPAddressResponseMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Extensions.Xml; | ||
| using System.Net; | ||
| using System.Xml; | ||
|
|
||
| namespace Neo.Network.Messages.Responses | ||
| { | ||
| internal class GetExternalIPAddressResponseMessage : ResponseMessage | ||
| { | ||
| public IPAddress ExternalIPAddress { get; private set; } | ||
|
|
||
| public GetExternalIPAddressResponseMessage(XmlDocument response, string serviceType) | ||
| : base(response, serviceType, nameof(GetExternalIPAddressResponseMessage)) | ||
| { | ||
| var ip = GetNode().GetXmlElementText("NewExternalIPAddress"); | ||
|
|
||
| if (IPAddress.TryParse(ip, out var ipAddress)) | ||
| ExternalIPAddress = ipAddress; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // ResponseMessage.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System; | ||
| using System.Xml; | ||
|
|
||
| namespace Neo.Network.Messages.Responses | ||
| { | ||
| internal abstract class ResponseMessage | ||
| { | ||
| private readonly XmlDocument _document; | ||
| protected string _serviceType; | ||
| private readonly string _typeName; | ||
|
|
||
| protected ResponseMessage(XmlDocument response, string serviceType, string typeName) | ||
| { | ||
| _document = response; | ||
| _serviceType = serviceType; | ||
| _typeName = typeName; | ||
| } | ||
|
|
||
| protected XmlNode GetNode() | ||
| { | ||
| var nsm = new XmlNamespaceManager(_document.NameTable); | ||
| nsm.AddNamespace("responseNs", _serviceType); | ||
|
|
||
| var typeName = _typeName; | ||
| var messageName = typeName.Substring(0, typeName.Length - "Message".Length); | ||
shargon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var node = _document.SelectSingleNode("//responseNs:" + messageName, nsm) ?? | ||
| throw new InvalidOperationException("The response is invalid: " + messageName); | ||
|
|
||
| return node; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.