Skip to content
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

Make port reusable between sessions #38

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions p2pconn/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using UdtSharp;
using System.IO;
using p2p.StunServer;
using p2p.p2p;

namespace p2pconn
{
Expand Down Expand Up @@ -78,10 +79,7 @@ private void CheckDataGridView()

private void GetEndPoint()
{
int newPort = r.Next(49152, 65535);
socket.Bind(new IPEndPoint(IPAddress.Any, newPort));

P2pEndPoint p2pEndPoint = GetExternalEndPoint(socket);
P2pEndPoint p2pEndPoint = GetP2pEndPoint();

if (p2pEndPoint == null)
return;
Expand All @@ -93,6 +91,19 @@ private void GetEndPoint()
string[] words = localendpoint.Split(':');
// txtLocalHost.Text = Functions.Base64Encode(GetPhysicalIPAdress() + ":" + words[1]);
txtLocalHost.Text = GetPhysicalIPAdress() + ":" + words[1];

P2pEndPoint GetP2pEndPoint()
{
string storedPort = RegistryFuncs.GetFromRegistry(P2PKeys.LastPort);
int newPort = r.Next(49152, 65535);
if (!string.IsNullOrEmpty(storedPort))
newPort = Convert.ToInt32(storedPort);
socket.Bind(new IPEndPoint(IPAddress.Any, newPort));
P2pEndPoint tmpEndPoint = GetExternalEndPoint(socket);
if (tmpEndPoint != null)
RegistryFuncs.SetToRegistry(P2PKeys.LastPort, newPort.ToString());
return tmpEndPoint;
}
}

private string GetPhysicalIPAdress()
Expand Down
40 changes: 40 additions & 0 deletions p2pconn/RegistryFuncs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace p2p
{
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace p2p
{
public enum P2PKeys
{
LastIp,
LastPort,
LastExternalIP,
LastExternalPort,
}

public static class RegistryFuncs
{
public const string P2pPath = @"HKEY_CURRENT_USER\Software\P2pRemoteDesktop\";
public static string GetFromRegistry(P2PKeys key)
{
string keyValue = (string)Registry.GetValue(P2pPath, key.ToString(), null);
return keyValue;
}
public static void SetToRegistry(P2PKeys key, string value)
{
Registry.SetValue(P2pPath, key.ToString(), value);
}
}
}
}
1 change: 1 addition & 0 deletions p2pconn/p2pconn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Functions.cs" />
<Compile Include="RegistryFuncs.cs" />
<Compile Include="StreamingDesktop\InputControl.cs" />
<Compile Include="StreamingDesktop\pDesktop.cs">
<SubType>Form</SubType>
Expand Down