Skip to content

Commit

Permalink
Reuse port
Browse files Browse the repository at this point in the history
Try to reuse last port
  • Loading branch information
jupagose committed Oct 26, 2022
1 parent cfbe510 commit 511bc39
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 17 deletions.
9 changes: 9 additions & 0 deletions P2pConnWpf/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="P2pConnWpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:P2pConnWpf"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions P2pConnWpf/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace P2pConnWpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
10 changes: 10 additions & 0 deletions P2pConnWpf/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
12 changes: 12 additions & 0 deletions P2pConnWpf/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Window x:Class="P2pConnWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:P2pConnWpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>

</Grid>
</Window>
28 changes: 28 additions & 0 deletions P2pConnWpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace P2pConnWpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
14 changes: 14 additions & 0 deletions P2pConnWpf/P2pConnWpf.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\p2pconn\p2pconn.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions P2pConnWpf/P2pConnWpf.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<ItemGroup>
<ApplicationDefinition Update="App.xaml">
<SubType>Designer</SubType>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>
14 changes: 12 additions & 2 deletions p2pconn.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2009
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "p2pconn", "p2pconn\p2pconn.csproj", "{33456E72-F8E8-4384-88C4-700867DF12E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P2pConnWpf", "P2pConnWpf\P2pConnWpf.csproj", "{DEBE4A7E-C49B-435E-A182-77AC08F72A53}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,14 @@ Global
{33456E72-F8E8-4384-88C4-700867DF12E2}.Release|Any CPU.Build.0 = Release|Any CPU
{33456E72-F8E8-4384-88C4-700867DF12E2}.Release|x86.ActiveCfg = Release|x86
{33456E72-F8E8-4384-88C4-700867DF12E2}.Release|x86.Build.0 = Release|x86
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Debug|x86.ActiveCfg = Debug|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Debug|x86.Build.0 = Debug|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Release|Any CPU.Build.0 = Release|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Release|x86.ActiveCfg = Release|Any CPU
{DEBE4A7E-C49B-435E-A182-77AC08F72A53}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
43 changes: 28 additions & 15 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;

namespace p2pconn
{
Expand Down Expand Up @@ -63,7 +64,8 @@ private void CheckDataGridView()
this.dataGridView1.Rows.Insert(i, _stun.Server, _stun.Port);
i++;
}
} else
}
else
{
this.dataGridView1.Rows.Insert(0, "stun.l.google.com", 19302);
}
Expand All @@ -78,13 +80,8 @@ private void CheckDataGridView()

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

P2pEndPoint p2pEndPoint = GetExternalEndPoint(socket);

if (p2pEndPoint == null)
return;
P2pEndPoint p2pEndPoint = GetEndPoint();
if (p2pEndPoint == null) return;

// txtmyHost.Text = Functions.Base64Encode(p2pEndPoint.External.ToString());
txtmyHost.Text = p2pEndPoint.External.ToString();
Expand All @@ -93,6 +90,21 @@ private void GetEndPoint()
string[] words = localendpoint.Split(':');
// txtLocalHost.Text = Functions.Base64Encode(GetPhysicalIPAdress() + ":" + words[1]);
txtLocalHost.Text = GetPhysicalIPAdress() + ":" + words[1];

P2pEndPoint GetEndPoint()
{
string newPort = RegistryFuncs.GetFromRegistry(P2PKeys.LastExternalPort);
int sugestedPort;
if (newPort == null)
sugestedPort = r.Next(49152, 65535);
else
sugestedPort = Convert.ToInt32(newPort);
socket.Bind(new IPEndPoint(IPAddress.Any, sugestedPort));
P2pEndPoint tmpp2pEndPoint = GetExternalEndPoint(socket);
if (tmpp2pEndPoint != null)
RegistryFuncs.SetToRegistry(P2PKeys.LastExternalPort, sugestedPort.ToString());
return tmpp2pEndPoint;
}
}

private string GetPhysicalIPAdress()
Expand Down Expand Up @@ -147,7 +159,7 @@ private void connect()
if (connection == null)
{
label4.Invoke((MethodInvoker)(() => label4.ForeColor = Color.Red));
label4.Invoke((MethodInvoker)(() => label4.Text = "Failed to establish P2P conn to " + remoteIp));
label4.Invoke((MethodInvoker)(() => label4.Text = "Failed to establish P2P conn to " + remoteIp));
return;
}
try
Expand Down Expand Up @@ -279,7 +291,7 @@ public UdtSocket PeerConnect(Socket socket, string remoteAddr, int remotePort)
private void CloseAll()
{
if (bConnected == true)
{
{
SenderReceiver.SendMessage("end|");
thread.Abort();
SenderReceiver.netStream.Close();
Expand Down Expand Up @@ -341,7 +353,7 @@ private void button4_Click(object sender, EventArgs e)
{
if (txtnsg.Text != "")
{
if(bConnected == true)
if (bConnected == true)
{
Writetxtchatrom("Blue", txtnsg.Text);
SenderReceiver.SendMessage("c|" + txtnsg.Text);
Expand All @@ -362,7 +374,7 @@ private void btn_paste_Click(object sender, EventArgs e)
{
txtRemoteIP.Text = Clipboard.GetText();
Clipboard.SetText(txtmyHost.Text);
if(txtmyHost.Text == txtRemoteIP.Text || txtLocalHost.Text == txtRemoteIP.Text)
if (txtmyHost.Text == txtRemoteIP.Text || txtLocalHost.Text == txtRemoteIP.Text)
{
txtRemoteIP.Text = "";
MessageBox.Show("Please paste peer remote host:port not your!");
Expand Down Expand Up @@ -511,7 +523,7 @@ private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(RemoteDesktop.DesktopRunning == true)
if (RemoteDesktop.DesktopRunning == true)
{
if (checkBox1.Checked == true)
{
Expand Down Expand Up @@ -553,7 +565,8 @@ private void button6_Click(object sender, EventArgs e)
{
if (row.IsNewRow) continue;
this.dataGridView1.Rows.RemoveAt(row.Index);
CheckJsonFile(); }
CheckJsonFile();
}
}
}

Expand Down Expand Up @@ -589,7 +602,7 @@ private void SaveDataToJsoin()
}

StunServer.WriteStunServersToFile(ListJsonStunServers, StunServersJson);

if (this.dataGridView1.Rows.Count > 1)
{
MessageBox.Show("Stun Servers List saved to: " + StunServersJson);
Expand Down
31 changes: 31 additions & 0 deletions p2pconn/RegistryFuncs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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

0 comments on commit 511bc39

Please sign in to comment.