Example #1
0
        private void trayContextMenuServersClick(Object sender, EventArgs e)
        {
            ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
            int    index  = trayContextMenuServers.DropDownItems.IndexOf(toolStripMenuItem);
            Tunnel tunnel = tunnels[index];

            tunnel.enable = !tunnel.enable;
            serversCheckedListBox.SetItemChecked(index, tunnel.enable);
            toolStripMenuItem.Checked = tunnel.enable;
            if (tunnel.enable)
            {
                tunnel.connect();
            }
            else
            {
                tunnel.disconnect();
            }
        }
Example #2
0
        private void serverAddButtonClick(object sender, EventArgs e)
        {
            if (serversCheckedListBox.SelectedItem == null)
            {
                bool isNull = (remoteAddressTextBox.Text.Equals("")) ||
                              (remotePortTextBox.Text.Equals("")) ||
                              (localPortTextBox.Text.Equals("")) ||
                              (modeComboBox.Text.Equals(""));
                if (!isNull)
                {
                    Tunnel tunnel = new Tunnel();
                    tunnel.remoteAddress = remoteAddressTextBox.Text;
                    try
                    {
                        tunnel.remotePort = Int32.Parse(remotePortTextBox.Text);
                        tunnel.localPort  = Int32.Parse(localPortTextBox.Text);
                    }
                    catch
                    {
                        MessageBox.Show("请输入正确的端口号", "错误");
                        return;
                    }

                    tunnel.mode   = modeComboBox.Text.ToLower();
                    tunnel.enable = true;


                    foreach (Tunnel t in tunnels)
                    {
                        if (t.localPort == tunnel.localPort)
                        {
                            string message = string.Format("本地端口{0}已被占用", tunnel.localPort);
                            MessageBox.Show(message, "错误");
                            return;
                        }
                    }

                    tunnels.Add(tunnel);
                    Utils.writeTunnels(tunnels);

                    string tip = String.Format("{0}->{1}:{2}", tunnel.localPort, tunnel.remoteAddress, tunnel.remotePort);
                    serversCheckedListBox.Items.Insert(tunnels.Count - 1, tip);
                    serversCheckedListBox.SelectedIndex = 0;

                    serversCheckedListBox.SetItemChecked(0, true);
                    if (config.enable && tunnel.enable)
                    {
                        tunnel.connect();
                    }

                    ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
                    toolStripMenuItem.Name         = string.Format("trayContextMenuServers{0}", tunnels.IndexOf(tunnel));
                    toolStripMenuItem.Text         = tip;
                    toolStripMenuItem.CheckOnClick = true;
                    toolStripMenuItem.Checked      = tunnel.enable;
                    toolStripMenuItem.Click       += new EventHandler(trayContextMenuServersClick);
                    trayContextMenuServers.DropDownItems.Insert(tunnels.IndexOf(tunnel), toolStripMenuItem);
                }
                else
                {
                    // 对应全部为空的情况
                    serversCheckedListBox.Items.Add("未配置的服务器");
                    serversCheckedListBox.SelectedIndex = 0;
                }
            }
            else
            {
                string item   = serversCheckedListBox.SelectedItem.ToString();
                bool   isNull = (remoteAddressTextBox.Text.Equals("")) ||
                                (remotePortTextBox.Text.Equals("")) ||
                                (localPortTextBox.Text.Equals("")) ||
                                (modeComboBox.Text.Equals(""));
                if ("未配置的服务器".Equals(item))
                {
                    if (!isNull)
                    {
                        Tunnel tunnel = new Tunnel();
                        tunnel.remoteAddress = remoteAddressTextBox.Text;
                        try
                        {
                            tunnel.remotePort = Int32.Parse(remotePortTextBox.Text);
                            tunnel.localPort  = Int32.Parse(localPortTextBox.Text);
                        }
                        catch
                        {
                            MessageBox.Show("请输入正确的端口号", "错误");
                            return;
                        }
                        tunnel.mode   = modeComboBox.Text.ToLower();
                        tunnel.enable = true;

                        foreach (Tunnel t in tunnels)
                        {
                            if (t.localPort == tunnel.localPort)
                            {
                                string message = string.Format("本地端口{0}已被占用", tunnel.localPort);
                                MessageBox.Show(message, "错误");
                                return;
                            }
                        }

                        tunnels.Add(tunnel);
                        Utils.writeTunnels(tunnels);

                        string tip = String.Format("{0}->{1}:{2}", tunnel.localPort, tunnel.remoteAddress, tunnel.remotePort);
                        //serversCheckedListBox.SelectedIndex = tunnels.Count - 1;
                        //serversCheckedListBox.Items[tunnels.Count - 1] = tip;
                        serversCheckedListBox.Items.Insert(tunnels.Count - 1, tip);
                        serversCheckedListBox.SelectedIndex = tunnels.Count - 1;
                        serversCheckedListBox.Items.RemoveAt(tunnels.Count);
                        serversCheckedListBox.SetItemChecked(tunnels.Count - 1, true);
                        if (config.enable && tunnel.enable)
                        {
                            tunnel.connect();
                        }

                        ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
                        toolStripMenuItem.Name         = string.Format("trayContextMenuServers{0}", tunnels.IndexOf(tunnel));
                        toolStripMenuItem.Text         = tip;
                        toolStripMenuItem.CheckOnClick = true;
                        toolStripMenuItem.Checked      = tunnel.enable;
                        toolStripMenuItem.Click       += new EventHandler(trayContextMenuServersClick);
                        trayContextMenuServers.DropDownItems.Insert(tunnels.IndexOf(tunnel), toolStripMenuItem);
                    }
                }
                else
                {
                    serversCheckedListBox.Items.Add("未配置的服务器");
                    serversCheckedListBox.SelectedIndex = serversCheckedListBox.Items.Count - 1;
                }
            }
        }