/* Made by Lonami Exo
* (C) LonamiWebs */
// NOTE That this needs Mono.Nat
using System;
using System.Net;
using Mono.Nat;
public class PortForwarder {
#region Public events and delegates
public delegate void ReadyDelegate();
public event ReadyDelegate OnReady;
#endregion
#region Properties and variables
/// <summary>
/// True if PortForwarder is ready. It must be ready for the methods to work
/// </summary>
public bool Ready { get; private set; }
/// <summary>
/// THe maximum port number
/// </summary>
public const int MaxPort = 65535;
/// <summary>
/// The minimum port number
/// </summary>
public const int MinPort = 1;
#endregion
#region Private variables
INatDevice device;
#endregion
#region Constructors
public PortForwarder() {
NatUtility.DeviceFound += DeviceFound;
NatUtility.StartDiscovery();
}
#endregion
#region Public methods
/// <summary>
/// Retrieves the public IP of the current machine
/// </summary>
/// <returns>The public IP</returns>
public IPAddress GetPublicIP() {
CheckReady();
return device.GetExternalIP();
}
/// <summary>
/// Forwards a desired port
/// </summary>
/// <param name="port">The port</param>
/// <param name="protocol">The protocol type</param>
public void Forward(int port, Protocol protocol = Protocol.Tcp) {
CheckReady();
CheckValidPort(port);
device.CreatePortMap(new Mapping(protocol, port, port));
}
/// <summary>
/// Forwards a desired port for both TCP and UDP
/// </summary>
/// <param name="port">The port</param>
public void ForwardBothProtocols(int port) {
CheckReady();
CheckValidPort(port);
device.CreatePortMap(new Mapping(Protocol.Tcp, port, port));
device.CreatePortMap(new Mapping(Protocol.Udp, port, port));
}
/// <summary>
/// Stops forwarding the given port
/// </summary>
/// <param name="port">The port</param>
/// <param name="protocol">The protocol type</param>
public void StopForwarding(int port, Protocol protocol = Protocol.Tcp) {
CheckReady();
CheckValidPort(port);
device.DeletePortMap(new Mapping(protocol, port, port));
}
/// <summary>
/// Stops forwarding the given port for both TCP and UDP
/// </summary>
/// <param name="port">The port</param>
public void StopForwardingBothProtocols(int port) {
CheckReady();
CheckValidPort(port);
device.DeletePortMap(new Mapping(Protocol.Tcp, port, port));
device.DeletePortMap(new Mapping(Protocol.Udp, port, port));
}
/// <summary>
/// Gets the forwarded potrs in a string array
/// </summary>
/// <returns>The forwarded ports</returns>
public string[] GetForwardedPorts() {
var mappings = device.GetAllMappings();
var ports = new string[mappings.Length];
for (int i = 0; i < mappings.Length; i++)
ports[i] = mappings[i].ToString();
return ports;
}
/// <summary>
/// Gets all the mappings (forwarded ports) with more information than
/// <see cref="GetForwardedPorts">GetForwardedPorts</see>
/// </summary>
/// <returns>The mappings</returns>
public Mapping[] GetAllMappings() {
return device.GetAllMappings();
}
#endregion
#region Private methods
void DeviceFound(object sender, DeviceEventArgs args)
{
device = args.Device;
StopDiscovery();
}
void CheckReady() {
if (!Ready)
throw new Exception("PortForwarder is not ready yet!");
}
void CheckValidPort(int port) {
if (port < MinPort || port > MaxPort)
throw new ArgumentOutOfRangeException("The port must be between " + MinPort + " and " + MaxPort);
}
void StopDiscovery() {
NatUtility.DeviceFound -= DeviceFound;
Ready = true;
OnReady();
}
#endregion
}
[NOTE: Mono.Nat library is required]
This class makes easy the port forwarding.
This class makes easy the port forwarding.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.