1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . ComponentModel ;
4
- using System . Data ;
5
2
using System . Drawing ;
6
- using System . Linq ;
7
- using System . Text ;
8
- using System . Threading . Tasks ;
9
3
using System . Windows . Forms ;
10
- using System . Runtime . InteropServices ;
11
4
using Microsoft . Win32 ;
12
5
using Renci . SshNet ;
13
6
@@ -36,7 +29,7 @@ private void btnConnect_Click(object sender, EventArgs e)
36
29
37
30
if ( ! int . TryParse ( registery_get_data ( "port" ) , out port ) ) port = 22 ;
38
31
39
- if ( password == null || password == null || ip == null )
32
+ if ( password == "" || password == "" || username == "" || ip == "" )
40
33
{
41
34
MessageBox . Show ( "Error : You should set SSH server settings..." , "Error!" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
42
35
btnConnect . Text = "Connect" ;
@@ -55,15 +48,15 @@ private void btnConnect_Click(object sender, EventArgs e)
55
48
56
49
Cursor . Current = Cursors . Default ;
57
50
58
- button3 . Enabled = true ;
51
+ btnDisconnect . Enabled = true ;
59
52
btnConnect . Enabled = false ;
60
53
61
- panel1 . BackColor = Color . Green ;
62
- label1 . Text = " Connected" ;
54
+ pnlStatus . BackColor = Color . Green ;
55
+ lblStatus . Text = " Connected" ;
63
56
}
64
57
catch ( Exception ex )
65
58
{
66
- MessageBox . Show ( "Error : " + ex ) ;
59
+ MessageBox . Show ( "Error : " + ex . Message , "Connecton Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
67
60
btnConnect . Text = "Connect" ;
68
61
}
69
62
}
@@ -80,10 +73,10 @@ private void btnDisconnect_Click(object sender, EventArgs e)
80
73
unset_windows_proxy ( ) ;
81
74
82
75
btnConnect . Enabled = true ;
83
- button3 . Enabled = false ;
76
+ btnDisconnect . Enabled = false ;
84
77
85
- panel1 . BackColor = Color . Red ;
86
- label1 . Text = "Not connected" ;
78
+ pnlStatus . BackColor = Color . Red ;
79
+ lblStatus . Text = "Not connected" ;
87
80
88
81
Cursor . Current = Cursors . Default ;
89
82
}
@@ -94,13 +87,9 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
94
87
{
95
88
var result = MessageBox . Show ( "Do you want to exit? If you click on yes, the VPN will be disconnected..." , "Exit Program?" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
96
89
if ( result == DialogResult . No )
97
- {
98
90
e . Cancel = true ;
99
- }
100
91
else
101
- {
102
92
btnDisconnect_Click ( null , null ) ;
103
- }
104
93
}
105
94
}
106
95
@@ -115,29 +104,25 @@ private void githubLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
115
104
githubLink . LinkVisited = true ;
116
105
githubLink . LinkBehavior = LinkBehavior . HoverUnderline ;
117
106
githubLink . Links [ 0 ] . LinkData = "https://github.com/omidmousavi/csharp-ssh-vpn" ;
118
-
119
- // Open the URL in the default web browser
120
107
System . Diagnostics . Process . Start ( githubLink . Links [ 0 ] . LinkData . ToString ( ) ) ;
121
108
}
122
109
123
110
bool back_status = false ;
124
111
int seconds = 0 ;
125
112
private void timer_check_status_Tick ( object sender , EventArgs e )
126
113
{
127
-
128
114
if ( ! sshClient . IsConnected && sshClient . IsConnected != back_status )
129
115
{
130
-
131
116
portForwarded . Stop ( ) ;
132
117
sshClient . Disconnect ( ) ;
133
118
134
119
unset_windows_proxy ( ) ;
135
120
136
121
btnConnect . Enabled = true ;
137
- button3 . Enabled = false ;
122
+ btnDisconnect . Enabled = false ;
138
123
139
- panel1 . BackColor = Color . Red ;
140
- label1 . Text = "Not connected" ;
124
+ pnlStatus . BackColor = Color . Red ;
125
+ lblStatus . Text = "Not connected" ;
141
126
142
127
btnConnect . Text = "Connect" ;
143
128
@@ -170,8 +155,8 @@ private void set_windows_proxy()
170
155
registry . SetValue ( "ProxyEnable" , 1 ) ;
171
156
registry . SetValue ( "ProxyServer" , "socks5://127.0.0.1:9000" ) ;
172
157
173
- // WinINetInterop.InternetSetOption(IntPtr.Zero, WinINetInterop.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
174
- // WinINetInterop.InternetSetOption(IntPtr.Zero, WinINetInterop.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
158
+ WinINetInterop . InternetSetOption ( IntPtr . Zero , WinINetInterop . INTERNET_OPTION_SETTINGS_CHANGED , IntPtr . Zero , 0 ) ;
159
+ WinINetInterop . InternetSetOption ( IntPtr . Zero , WinINetInterop . INTERNET_OPTION_REFRESH , IntPtr . Zero , 0 ) ;
175
160
}
176
161
177
162
private void unset_windows_proxy ( )
@@ -187,13 +172,9 @@ private string registery_get_data(string name)
187
172
using ( RegistryKey key = Registry . CurrentUser . OpenSubKey ( keyName ) )
188
173
{
189
174
if ( key == null )
190
- {
191
175
return "" ;
192
- }
193
176
else
194
- {
195
177
return key . GetValue ( name ) as string ;
196
- }
197
178
}
198
179
}
199
180
}
0 commit comments