PDA

View Full Version : ايجاد كانكشن VPN



debugger
دوشنبه 11 خرداد 1388, 11:40 صبح
با عرض سلام خدمت دوستان عزيز

من ميخوام با سي شارپ يه وي پي ان كانكشن ايجاد كنم . يعني يه يرنامه بنويسم كه توي برنامه ip و user و pass را بدم

و كاربر فقط با يك كليك و بدون هيچ تنظيماتي كانكت بشه

به اين تاپيك هم يه نگاهي بندازيد فقط اينو پيدا كردم

http://barnamenevis.org/forum/showthread.php?p=359495

لطفا راهنمايي كنيد .

با تشكر فراوان

debugger
دوشنبه 11 خرداد 1388, 17:58 عصر
دوستان اين كلاس آماده را پيدا كردم ولي نتونستم استفاده كنم




using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics;

namespace VPNManager
{
public class VPN

{

#region --Const--
private const string VPNPROCESS = "C:\\WINDOWS\\system32\\rasphone.exe";

#endregion
#region --Fields--
private string _VPNConnectionName = "";
private string _IPToPing = "";
private bool _isConnected = false;
private System.Timers.Timer MonitorTimer;
private bool _isChecking = false;
private bool _isManaging = false;

#endregion
#region --Events--
public delegate void PingingHandler();
public delegate void ConnectingHandler();
public delegate void DisconnectingHandler();
public delegate void IdleHandler();
public delegate void ConnectionStatusChangedHandler(bool Connected);
public event PingingHandler Pinging;
public event ConnectingHandler Connecting;
public event DisconnectingHandler Disconnecting;
public event IdleHandler Idle;
public event ConnectionStatusChangedHandler ConnectionStatusChanged;

protected void OnPinging()
{
if (Pinging != null)
{
Pinging();
}
}
protected void OnConnecting()
{
if (Connecting != null)
{
Connecting();
}
}
protected void OnDisconnecting()
{
if (Disconnecting != null)
{
Disconnecting();
}
}
protected void OnIdle()
{
if (Idle != null)
{
Idle();
}
}
protected void OnConnectionStatusChanged(bool Connected)
{
if (ConnectionStatusChanged != null)
{
ConnectionStatusChanged(Connected);
}
}

#endregion
#region --Properties--
public bool IsConnected
{
get { return _isConnected; }
}
public string IPToPing
{
get { return _IPToPing; }
set { _IPToPing = value; }
}
public string VPNConnectionName
{
get { return _VPNConnectionName; }
set { _VPNConnectionName = value; }
}

#endregion
#region --Private Methods--
public bool TestConnection()
{
bool RV = false;
_isChecking = true;
try

{
OnPinging();
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
if (ping.Send(_IPToPing).Status == System.Net.NetworkInformation.IPStatus.Success)
{
RV = true;
}
else

{
RV = false;
}
ping = null;
if (RV != _isConnected)
{
_isConnected = RV;
OnConnectionStatusChanged(_isConnected);
}
OnIdle();
}
catch (Exception Ex)
{
Debug.Assert(false, Ex.ToString());
RV = false;
OnIdle();
}
_isChecking = false;
return RV;
}
private bool ConnectToVPN()
{
bool RV = false;
try

{
OnConnecting();
Process.Start(VPNPROCESS, " -d " + _VPNConnectionName);
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(5000);
System.Windows.Forms.Application.DoEvents();
RV = true;
OnIdle();
}
catch (Exception Ex)
{
Debug.Assert(false, Ex.ToString());
RV = false;
OnIdle();
}
return RV;
}
private bool DisconnectFromVPN()
{
bool RV = false;
try

{
OnDisconnecting();
System.Diagnostics.Process.Start(VPNPROCESS, " -h " + _VPNConnectionName);
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(8000);
System.Windows.Forms.Application.DoEvents();
RV = true;
OnIdle();
}
catch (Exception Ex)
{
Debug.Assert(false, Ex.ToString());
RV = false;
OnIdle();
}
return RV;
}
private void Manage()
{
try

{
if (!_isManaging)
{
_isManaging = true;
if (!_isChecking)
{
if (!TestConnection())
{
ConnectToVPN();
if (!TestConnection())
{
DisconnectFromVPN();
ConnectToVPN();
if (!TestConnection())
{
DisconnectFromVPN();
ConnectToVPN();
}
}
}
}
_isManaging = false;
}
}
catch (Exception)
{
_isManaging = false;
}
}

#endregion
#region --Public Methods--
public void StartManaging(string VPNName, string IPtoPing)
{
_VPNConnectionName = VPNName;
_IPToPing = IPtoPing;
StartManaging();
}
public void StartManaging()
{
if (!string.IsNullOrEmpty(_VPNConnectionName) & !string.IsNullOrEmpty(_IPToPing))
{
MonitorTimer = new System.Timers.Timer(15000);
MonitorTimer.Enabled = true;
MonitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(MonitorTimer_Ela psed);
System.Net.NetworkInformation.NetworkChange.Networ kAvailabilityChanged += new System.Net.NetworkInformation.NetworkAvailabilityC hangedEventHandler(NetworkChange_NetworkAvailabili tyChanged);
Microsoft.Win32.SystemEvents.PowerModeChanged += new Microsoft.Win32.PowerModeChangedEventHandler(Syste mEvents_PowerModeChanged);
Manage();
}
}

#endregion
#region --Constructors--
public VPN(string VPNName, string IPtoPing)
{
StartManaging(VPNName, IPtoPing);
}
public VPN()
{
}

#endregion
#region --Event Handlers--
void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
if (e.Mode == Microsoft.Win32.PowerModes.Resume)
{
MonitorTimer.Stop();
System.Threading.Thread.Sleep(15000);
MonitorTimer.Start();
}
if (e.Mode == Microsoft.Win32.PowerModes.Suspend)
{
MonitorTimer.Stop();
}
}
void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityE ventArgs e)
{
if (e.IsAvailable)
{
if (!MonitorTimer.Enabled)
{
MonitorTimer.Start();
}
}
else

{
MonitorTimer.Stop();
}
}

void MonitorTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Manage();
}

#endregion

}
}

nassimit
چهارشنبه 25 آذر 1388, 22:05 عصر
3مقاله در مورد هوش مصنوعی

azygole
پنج شنبه 07 بهمن 1389, 15:33 عصر
سلام . من هم می خوام vpn conection ایجاد کنم ولی نمی تونم . میشه کمک کنید ؟