Skip to content

Commit 040cc79

Browse files
firewall port opening facility
1 parent d2fec60 commit 040cc79

File tree

10 files changed

+277
-46
lines changed

10 files changed

+277
-46
lines changed

WindowsFormApplication/ClipSyncWindowsFormApplication/ClipSync.csproj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<TargetCulture>en-IN</TargetCulture>
2929
<ProductName>ClipSync</ProductName>
3030
<PublisherName>Pishang Ujeniya</PublisherName>
31-
<ApplicationRevision>5</ApplicationRevision>
31+
<ApplicationRevision>6</ApplicationRevision>
3232
<ApplicationVersion>3.0.0.%2a</ApplicationVersion>
3333
<UseApplicationTrust>true</UseApplicationTrust>
3434
<CreateDesktopShortcut>true</CreateDesktopShortcut>
@@ -44,6 +44,7 @@
4444
<DefineConstants>DEBUG;TRACE</DefineConstants>
4545
<ErrorReport>prompt</ErrorReport>
4646
<WarningLevel>4</WarningLevel>
47+
<DocumentationFile>bin\Debug\ClipSync.xml</DocumentationFile>
4748
</PropertyGroup>
4849
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
4950
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -53,6 +54,7 @@
5354
<DefineConstants>TRACE</DefineConstants>
5455
<ErrorReport>prompt</ErrorReport>
5556
<WarningLevel>4</WarningLevel>
57+
<DocumentationFile>bin\Release\ClipSync.xml</DocumentationFile>
5658
</PropertyGroup>
5759
<PropertyGroup>
5860
<StartupObject>ClipSync.Program</StartupObject>
@@ -130,6 +132,9 @@
130132
<Private>True</Private>
131133
<Private>True</Private>
132134
</Reference>
135+
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
136+
<HintPath>..\packages\Microsoft.PowerShell.5.ReferenceAssemblies.1.1.0\lib\net4\System.Management.Automation.dll</HintPath>
137+
</Reference>
133138
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
134139
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
135140
</Reference>
@@ -267,9 +272,9 @@
267272
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
268273
</ItemGroup>
269274
<ItemGroup>
270-
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1">
275+
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
271276
<Visible>False</Visible>
272-
<ProductName>Microsoft .NET Framework 4.6.1 %28x86 and x64%29</ProductName>
277+
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 and x64%29</ProductName>
273278
<Install>true</Install>
274279
</BootstrapperPackage>
275280
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">

WindowsFormApplication/ClipSyncWindowsFormApplication/ClipSyncController.Designer.cs

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WindowsFormApplication/ClipSyncWindowsFormApplication/ClipSyncController.cs

Lines changed: 116 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,47 @@
99

1010
namespace ClipSync {
1111

12+
/// <summary>
13+
/// Mail Home Form of ClipSync
14+
/// </summary>
1215
public partial class ClipSyncControlForm : Form {
1316

17+
/// <summary>
18+
/// time interval for checking copying data
19+
/// </summary>
1420
public string mTime = DateTime.Now.ToLongTimeString();
1521

22+
/// <summary>
23+
/// Global Helper Instance
24+
/// </summary>
1625
private readonly GlobalHelper globalHelper;
1726

27+
/// <summary>
28+
/// Signal R Hub Proxy
29+
/// </summary>
1830
public IHubProxy _hub;
1931

32+
/// <summary>
33+
/// signal R Disposable Hub object
34+
/// </summary>
2035
private IDisposable signalRDisposable { get; set; }
2136

2237
bool isSignalRConnected = false;
2338

39+
/// <summary>
40+
/// User Id
41+
/// </summary>
2442
public string uid = "";
2543

44+
/// <summary>
45+
/// Default Constructor
46+
/// </summary>
2647
internal ClipSyncControlForm() {
2748
InitializeComponent();
2849
this.globalHelper = new GlobalHelper();
2950
}
3051

31-
private void LoginSignUpForm_Load(object sender, EventArgs e) {
52+
private void ClipSyncControlForm_Load(object sender, EventArgs e) {
3253
try {
3354

3455
this.LogWriter("-------------------");
@@ -42,13 +63,48 @@ private void LoginSignUpForm_Load(object sender, EventArgs e) {
4263
this.connectUidTextBox.Text = new Random().Next(1000, 9999).ToString();
4364

4465

45-
}
46-
catch (Exception ex) {
66+
} catch (Exception ex) {
4767
this.LogWriter(ex.ToString());
4868
}
4969
}
5070

51-
private void LoginButtonClick(object sender, EventArgs e) {
71+
/// <summary>
72+
/// Server Port text box key press event
73+
/// </summary>
74+
/// <param name="sender"></param>
75+
/// <param name="e"></param>
76+
private void serverPortTextBox_KeyPress(object sender, KeyPressEventArgs e) {
77+
if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
78+
e.Handled = true;
79+
}
80+
81+
/// <summary>
82+
/// Connect To Server Port Key Press Event
83+
/// </summary>
84+
/// <param name="sender"></param>
85+
/// <param name="e"></param>
86+
private void connectServerPortTextBox_KeyPress(object sender, KeyPressEventArgs e) {
87+
if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
88+
e.Handled = true;
89+
}
90+
91+
/// <summary>
92+
/// Connect Uid Key Press Event
93+
/// </summary>
94+
/// <param name="sender"></param>
95+
/// <param name="e"></param>
96+
private void connectUidTextBox_KeyPress(object sender, KeyPressEventArgs e) {
97+
98+
if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
99+
e.Handled = true;
100+
}
101+
102+
/// <summary>
103+
/// Login Button Click
104+
/// </summary>
105+
/// <param name="sender"></param>
106+
/// <param name="e"></param>
107+
private void LoginButton_Click(object sender, EventArgs e) {
52108

53109
Login_Button.Enabled = false;
54110

@@ -68,17 +124,15 @@ private void LoginButtonClick(object sender, EventArgs e) {
68124
this._hub = connection.CreateHubProxy(ConfigurationManager.AppSettings["hub_name"]);
69125
connection.Start().Wait();
70126
isSignalRConnected = true;
71-
}
72-
catch (Exception ex) {
127+
} catch (Exception ex) {
73128
isSignalRConnected = false;
74129
Login_Button.Enabled = true;
75130
this.LogWriter("Exception in connecting to SignalR Hub : " + ex.ToString());
76131
}
77132

78133
if (!isSignalRConnected) {
79134
MessageBox.Show("ClipSync Server is not running, so Please close the whole app and try again after sometime.", "Warning");
80-
}
81-
else {
135+
} else {
82136

83137
MessageBox.Show("ClipSync Server is now connected, You need to connect to this uid: " + uid + " from all your devices. Now you can minimise this window", "Success");
84138
this.LogWriter("ClipSync Server is now connected, You need to connect to this uid: " + uid + " from all your devices. Now you can minimise this window");
@@ -101,6 +155,11 @@ private void LoginButtonClick(object sender, EventArgs e) {
101155

102156
}
103157

158+
/// <summary>
159+
/// Start Server Button Click
160+
/// </summary>
161+
/// <param name="sender"></param>
162+
/// <param name="e"></param>
104163
private void StartServerButton_Click(object sender, EventArgs e) {
105164
this.startServerButton.Enabled = false;
106165
this.LogWriter("Starting server on ");
@@ -117,20 +176,55 @@ private void StartServerButton_Click(object sender, EventArgs e) {
117176
this.connectServerAddressTextBox.Text = globalHelper.GetMachineIpAddress();
118177
this.connectServerPortTextBox.Text = this.serverPortTextBox.Text;
119178
this.startServerButton.Enabled = false;
120-
}
121-
catch (System.Reflection.TargetInvocationException ex) {
179+
180+
this.OpenPortButton.PerformClick();
181+
} catch (System.Reflection.TargetInvocationException ex) {
122182
this.LogWriter("You need to run as administrator");
123183
this.LogWriter("Only server address localhost is allowed without administrator permissions");
124184
MessageBox.Show("You need to run as administrator", "Error");
125185
Console.WriteLine(ex.ToString());
126186
this.startServerButton.Enabled = true;
127-
}
128-
catch (Exception ex) {
187+
} catch (Exception ex) {
129188
this.LogWriter(ex.ToString());
130189
this.startServerButton.Enabled = true;
131190
}
132191
}
133192

193+
/// <summary>
194+
/// Open Port Button Click
195+
/// </summary>
196+
/// <param name="sender"></param>
197+
/// <param name="e"></param>
198+
private void OpenPortButton_Click(object sender, EventArgs e) {
199+
try {
200+
201+
this.OpenPortButton.Enabled = false;
202+
int serverPort = Convert.ToInt32(this.serverPortTextBox.Text);
203+
204+
this.LogWriter("Checking the status of inbound port: " + serverPort + " Please wait...");
205+
if (this.globalHelper.IsPortOpened(serverPort, "ClipSync")) {
206+
this.LogWriter("Port " + serverPort + " is already open");
207+
} else {
208+
this.LogWriter("Opening the inbound port: " + serverPort + " Please wait...");
209+
if (this.globalHelper.OpenInboundFirewallPort(serverPort, "ClipSync", serverPort.ToString())) {
210+
this.LogWriter("Successfully opened the inbound port : " + serverPort);
211+
} else {
212+
this.LogWriter("Failed to open the inbound port : " + serverPort);
213+
this.LogWriter("Try running as administrator");
214+
MessageBox.Show("Try running as administrator", "Error");
215+
}
216+
}
217+
this.OpenPortButton.Enabled = true;
218+
} catch (System.Reflection.TargetInvocationException ex) {
219+
this.LogWriter("You need to run as administrator");
220+
MessageBox.Show("You need to run as administrator", "Error");
221+
Console.WriteLine(ex.ToString());
222+
} catch (Exception ex) {
223+
this.OpenPortButton.Enabled = true;
224+
this.LogWriter(ex.ToString());
225+
}
226+
}
227+
134228
/// <summary>
135229
/// Log Writer on Form
136230
/// </summary>
@@ -143,26 +237,24 @@ internal void LogWriter(string t) {
143237
));
144238
return;
145239
}
146-
consoleTextBox.AppendText(Environment.NewLine + t);
240+
consoleTextBox.AppendText(Environment.NewLine + Environment.NewLine + t);
147241
consoleTextBox.SelectionStart = consoleTextBox.Text.Length;
148242
consoleTextBox.ScrollToCaret();
149243

150244
}
151245

246+
/// <summary>
247+
/// Adds ClipBoard Listener to the Window
248+
/// </summary>
152249
private void AddClipBoardListener() {
153250
//NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
154251
NativeMethods.AddClipboardFormatListener(Handle);
155252
}
156253

157-
private void websocket_MessageReceived(object sender, EventArgs e) {
158-
this.LogWriter("websocket_MessageReceived");
159-
}
160-
161-
private void websocket_Closed(object sender, EventArgs e) {
162-
this.LogWriter("websocket_Closed");
163-
}
164-
165-
254+
/// <summary>
255+
/// WindProc for getting ClipBoard Data
256+
/// </summary>
257+
/// <param name="m"></param>
166258
protected override void WndProc(ref Message m) {
167259
if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE) {
168260

@@ -179,8 +271,7 @@ protected override void WndProc(ref Message m) {
179271
_hub.Invoke(ConfigurationManager.AppSettings["send_copied_text_signalr_method_name"], copied_content);
180272
}
181273
}
182-
}
183-
else if (iData.GetDataPresent(DataFormats.Bitmap)) {
274+
} else if (iData.GetDataPresent(DataFormats.Bitmap)) {
184275
//Bitmap image = (Bitmap)iData.GetData(DataFormats.Bitmap); // Clipboard image
185276
//do something with it
186277
}
@@ -189,6 +280,7 @@ protected override void WndProc(ref Message m) {
189280
base.WndProc(ref m);
190281
}
191282

283+
192284
}
193285

194286
internal static class NativeMethods {

0 commit comments

Comments
 (0)