-
Notifications
You must be signed in to change notification settings - Fork 21
UPnP support #7
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
base: master
Are you sure you want to change the base?
UPnP support #7
Changes from 1 commit
9dfc8ea
c580956
53dfa50
6ade419
492300c
983f3dc
623ed10
30817e1
5ade5c5
aa50d36
2ed27c2
a365d03
58d8215
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | ||
| <PackageReference Include="Open.NAT.Core" Version="2.1.0" /> | ||
|
||
| <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" /> | ||
| <PackageReference Include="System.Collections.Immutable" Version="1.5.0" /> | ||
| <PackageReference Include="System.Net.Sockets" Version="4.3.0" /> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is part of TorrentCore. | ||
| // https://torrentcore.org | ||
| // Copyright (c) Samuel Fisher. | ||
| // | ||
| // Licensed under the GNU Lesser General Public License, version 3. See the | ||
| // LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Threading.Tasks; | ||
|
|
||
| namespace TorrentCore.Transport | ||
| { | ||
| public interface IUPnPClient | ||
| { | ||
| Task TryAddPortMappingAsync(int port); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ class TcpTransportProtocol : ITcpTransportProtocol | |
| { | ||
| private readonly ILogger<TcpTransportProtocol> _logger; | ||
| private readonly ConcurrentBag<TcpTransportStream> _streams; | ||
| private readonly IUPnPClient _uPnPClient; | ||
|
|
||
| private TcpListener _listener; | ||
|
|
||
|
|
@@ -40,6 +41,7 @@ public TcpTransportProtocol(ILogger<TcpTransportProtocol> logger, IOptions<Local | |
| _logger = logger; | ||
| _streams = new ConcurrentBag<TcpTransportStream>(); | ||
| Port = options.Value.Port; | ||
| _uPnPClient = new TcpTransportUPnPClient(options.Value.UseUPnP); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you register this in the DI container and inject it instead of creating it here? This will allow users more flexibility to change how UPnP is handled if required. |
||
| LocalBindAddress = options.Value.BindAddress ?? IPAddress.Any; | ||
| RateLimiter = new RateLimiter(); | ||
| } | ||
|
|
@@ -108,6 +110,8 @@ public void Start() | |
| // If port=0 was supplied, set the actual port we are listening on. | ||
| Port = ((IPEndPoint)_listener.LocalEndpoint).Port; | ||
| ListenForIncomingConnections(); | ||
|
|
||
| _uPnPClient.TryAddPortMappingAsync(Port); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // This file is part of TorrentCore. | ||
| // https://torrentcore.org | ||
| // Copyright (c) Samuel Fisher. | ||
| // | ||
| // Licensed under the GNU Lesser General Public License, version 3. See the | ||
| // LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Open.Nat; | ||
|
|
||
| namespace TorrentCore.Transport.Tcp | ||
| { | ||
| class TcpTransportUPnPClient : IUPnPClient | ||
| { | ||
| private bool _useUPnP; | ||
|
|
||
| public TcpTransportUPnPClient(bool useUPnP) | ||
| { | ||
| _useUPnP = useUPnP; | ||
| } | ||
|
|
||
| public async Task TryAddPortMappingAsync(int port) | ||
| { | ||
| if (!_useUPnP) | ||
| return; | ||
|
|
||
| try | ||
| { | ||
| var discoverer = new NatDiscoverer(); | ||
| var cts = new CancellationTokenSource(10000); | ||
|
|
||
| var device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts); | ||
| await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "TorrentCore")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't take into consideration which IP the application is listening on ( For context, |
||
| } | ||
| catch | ||
| { | ||
| // Do nothing | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What type of exceptions does this throw? It could do with some logging to indicate something has gone wrong. |
||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks unnecessary, can you remove it?