コード例 #1
0
ファイル: VPNClient.cs プロジェクト: eitguide/VPNClient
        private void DoAfterDisconnected()
        {
            if (bConnect.InvokeRequired)
            {
                bConnect.Invoke(new Action(() =>
                {
                    bConnect.Enabled = true;
                }));
            }
            else
            {
                bConnect.Enabled = true;
            }

            if (bDisconnect.InvokeRequired)
            {
                bDisconnect.Invoke(new Action(() =>
                {
                    bDisconnect.Enabled = false;
                }));
            }
            else
            {
                bDisconnect.Enabled = false;
            }

            if (bRemoveVpn.InvokeRequired)
            {
                bRemoveVpn.Invoke(new Action(() =>
                {
                    bRemoveVpn.Enabled = true;
                }));
            }
            else
            {
                bRemoveVpn.Enabled = true;
            }

            string cmdResult = string.Empty;

            if (addRouteArray.Count > 0)
            {
                foreach (var addRoute in addRouteArray)
                {
                    var addRouteInfo = addRoute.Split(' ');
                    if (addRouteInfo.Length > 1)
                    {
                        CMDHelper.RunCmd("route delete" + addRouteInfo[1], out cmdResult);
                    }
                }
            }

            isStopping = false;
            conn       = null;
            DoMessage(string.Format("{0} 连接已断开", DateTime.Now.ToString()));
        }
コード例 #2
0
ファイル: VPNClient.cs プロジェクト: eitguide/VPNClient
        private void DoAfterConnected()
        {
            if (bConnect.InvokeRequired)
            {
                bConnect.Invoke(new Action(() =>
                {
                    bConnect.Enabled = false;
                }));
            }
            else
            {
                bConnect.Enabled = false;
            }

            if (bDisconnect.InvokeRequired)
            {
                bDisconnect.Invoke(new Action(() =>
                {
                    bDisconnect.Enabled = true;
                }));
            }
            else
            {
                bDisconnect.Enabled = true;
            }

            if (bRemoveVpn.InvokeRequired)
            {
                bRemoveVpn.Invoke(new Action(() =>
                {
                    bRemoveVpn.Enabled = false;
                }));
            }
            else
            {
                bRemoveVpn.Enabled = false;
            }

            var ipInfo = GetVpnIP();

            // 当前VPN的IP地址
            string vpnClientIP = string.Empty;
            string vpnServerIP = string.Empty;

            if (ipInfo != null)
            {
                vpnClientIP = ipInfo.IPAddress.ToString();
                vpnServerIP = ipInfo.ServerIPAddress.ToString();
            }

            var ifIndex = IPHelper.GetAdapterIndex(sEntryId);

            // 添加路由
            string cmdResult = string.Empty;

            if (addRouteArray.Count > 0)
            {
                foreach (var addRoute in addRouteArray)
                {
                    CMDHelper.RunCmd("route " + addRoute + " " + vpnServerIP + " if " + ifIndex, out cmdResult);
                }
            }

            if (deleteRouteArray.Count > 0)
            {
                foreach (var deleteRoute in deleteRouteArray)
                {
                    CMDHelper.RunCmd("route " + deleteRoute, out cmdResult);
                }
            }

            DoMessage(string.Format("{0} 绑定IP:{1}", DateTime.Now.ToString(), vpnClientIP));

            isStopping  = false;
            haveStopped = false;
        }