public bool ProcessCommandHeader(INatDevice Router, dynamic Response, string Key) { IEnumerable <string> Commands; IEnumerable <string> Parameters; if (Response.Headers.TryGetValues("Command", out Commands)) { foreach (string c in Commands) { Command = c; } } if (Response.Headers.TryGetValues("Parameter", out Parameters)) { foreach (string p in Parameters) { Parameter = p; } } // If no command headers were found in the response, return false if (Command == null || Parameter == null) { return(false); } int rdpPortExternal = Int32.Parse(Registry.Get("Port")); // Decrypt the headers passed back by server string commandDecrypted = Harpocrates.Engine.Decrypt(Command, Key); string parameterDecrypted = Harpocrates.Engine.Decrypt(Parameter, Key); if (commandDecrypted == "open") { int lifetime = Convert.ToInt32(Registry.Get("PortLifetime")); string desc = "Drawbridge [" + Dns.GetHostName() + "]"; Mapping mapping = new Mapping(Protocol.Tcp, 3389, rdpPortExternal, lifetime * 60); Router.CreatePortMap(mapping); } else if (commandDecrypted == "close") { try { Router.DeletePortMap(new Mapping(Protocol.Tcp, 3389, rdpPortExternal)); } catch { } } else if (commandDecrypted == "lifetime") { Registry.Set("PortLifetime", parameterDecrypted); } else if (commandDecrypted == "interval") { Registry.Set("Interval", parameterDecrypted); } else if (commandDecrypted == "test") { PingRequest.SendCommandToTargetAsync(parameterDecrypted, "test-reply", Dns.GetHostName()); } else if (commandDecrypted == "test-reply") { MessageBox.Show(String.Format("Test command reply processed from: {0}", parameterDecrypted)); } else if (commandDecrypted == "port") { // Delete old port mapping on router (if any exists) try { Router.DeletePortMap(new Mapping(Protocol.Tcp, 3389, rdpPortExternal)); } catch { } Registry.Set("Port", parameterDecrypted); } else if (commandDecrypted == "randomize") { // Delete old port mapping on router (if any exists) try { Router.DeletePortMap(new Mapping(Protocol.Tcp, 3389, rdpPortExternal)); } catch { } StaticHelpers.RandomizePort(); } return(true); }
private void InitializeComponent() { TrayIcon = new NotifyIcon(); // If this is the first run, generate a random port if (Registry.Has("Port") == false) { StaticHelpers.RandomizePort(); } // If this is the first run, make sure key field is available if (Registry.Has("PortLifetime") == false) { Registry.Set("PortLifetime", 30.ToString()); } // If this is the first run, set default connection interval if (Registry.Has("Interval") == false) { Registry.Set("Interval", 20.ToString()); } // If this is the first run, prompt user with authentication settings page if (Registry.Has("ApiKey") == false || Registry.Has("Key") == false) { SettingsMenuItem_Click(null, null); } // If user aborted before entering either key, we need to exit here. if (Registry.Has("ApiKey") == false || Registry.Has("Key") == false) { MessageBox.Show("Drawbridge will now exit."); Environment.Exit(1); } // Start the main timer loop this.loopTimer = new System.Timers.Timer(); this.loopTimer.Interval = Int32.Parse(Registry.Get("Interval")) * 1000; this.loopTimer.Elapsed += timerTickAsync; this.loopTimer.AutoReset = true; this.loopTimer.Enabled = true; this.loopTimer.Start(); timerTickAsync(null, null); // Check if this is the first run by seeing if there is a saved API and Protocol key // If keys are not present, then prompt user to enter them //The icon is added to the project resources. //Here I assume that the name of the file is 'TrayIcon.ico' TrayIcon.Icon = Properties.Resources.BlackRook; //Optional - Add a context menu to the TrayIcon: TrayIconContextMenu = new ContextMenuStrip(); TrayIconContextMenu.SuspendLayout(); this.TitleMenuItem.Text = String.Format("Drawbridge [{0}]", StaticHelpers.GetVersion()); this.TrayIconContextMenu.Items.Add(this.TitleMenuItem); this.TitleMenuItem.Image = Properties.Resources.bullet_grey; // Add some HRs this.TrayIconContextMenu.Items.Add("-"); this.TrayIconContextMenu.Items.Add("-"); ToolStripMenuItem SettingsMenuItem = new ToolStripMenuItem(); SettingsMenuItem.Text = "Settings"; this.TrayIconContextMenu.Items.Add(SettingsMenuItem); ToolStripMenuItem Authentication = new ToolStripMenuItem(); Authentication.Text = "Authentication"; Authentication.Click += new EventHandler(this.SettingsMenuItem_Click); SettingsMenuItem.DropDownItems.Add(Authentication); this.IntervalPicker.Text = "Ping Interval"; SettingsMenuItem.DropDownItems.Add(this.IntervalPicker); foreach (KeyValuePair <int, string> Ping in this.PingsIntervals) { ToolStripMenuItem x = new ToolStripMenuItem(); x.Text = Ping.Value; x.Tag = Ping.Key; if (Ping.Key == Int32.Parse(Registry.Get("Interval"))) { x.Checked = true; } x.Click += delegate(Object sender, EventArgs e) { ToolStripMenuItem s = sender as ToolStripMenuItem; this.SelectIntervalSetting(Int32.Parse(s.Tag.ToString())); Registry.Set("Interval", Ping.Key.ToString()); }; this.IntervalPicker.DropDownItems.Add(x); } foreach (KeyValuePair <int, string> Lifetime in this.Lifetimes) { ToolStripMenuItem x = new ToolStripMenuItem(); x.Text = Lifetime.Value; x.Tag = Lifetime.Key; if (Lifetime.Key == Int32.Parse(Registry.Get("PortLifetime"))) { x.Checked = true; } x.Click += delegate(Object sender, EventArgs e) { // Uncheck existing option... foreach (ToolStripMenuItem a in this.LifetimePickerMenuItem.DropDownItems) { a.Checked = false; } ToolStripMenuItem s = sender as ToolStripMenuItem; Registry.Set("PortLifetime", Lifetime.Key.ToString()); s.Checked = true; }; this.LifetimePickerMenuItem.DropDownItems.Add(x); } ToolStripMenuItem PortSettingsMenuItem = new ToolStripMenuItem(); PortSettingsMenuItem.Text = "Mapped Port"; SettingsMenuItem.DropDownItems.Add(PortSettingsMenuItem); this.PortDisplayLabelItem.Text = "Port: " + Registry.Get("Port"); PortSettingsMenuItem.DropDownItems.Add(this.PortDisplayLabelItem); PortSettingsMenuItem.DropDownItems.Add("-"); ToolStripMenuItem PortRandomizeMenuItem = new ToolStripMenuItem(); PortRandomizeMenuItem.Text = "Randomize"; PortRandomizeMenuItem.Click += delegate(Object sender, EventArgs e) { int port = StaticHelpers.RandomizePort(); this.PortDisplayLabelItem.Text = "Port: " + port; timerTickAsync(null, null); }; PortSettingsMenuItem.DropDownItems.Add(PortRandomizeMenuItem); ToolStripMenuItem PortSetManualMenuItem = new ToolStripMenuItem(); PortSetManualMenuItem.Text = "Specify"; PortSetManualMenuItem.Click += delegate(Object sender, EventArgs e) { int port = PortNumberInput(Int32.Parse(Registry.Get("Port"))); Registry.Set("Port", port.ToString()); this.PortDisplayLabelItem.Text = "Port: " + port; timerTickAsync(null, null); }; PortSettingsMenuItem.DropDownItems.Add(PortSetManualMenuItem); this.LifetimePickerMenuItem.Text = "Time To Live"; PortSettingsMenuItem.DropDownItems.Add(this.LifetimePickerMenuItem); WindowsServiceMenuItem.Text = "Windows Service"; SettingsMenuItem.DropDownItems.Add(WindowsServiceMenuItem); this.RefreshWindowsServiceMenu(); this.UpdateAvailableMenuItem.Visible = false; this.UpdateAvailableMenuItem.Text = "Update Available!"; this.UpdateAvailableMenuItem.Click += new EventHandler(UpdateAvailableMenuItem_Click); this.TrayIconContextMenu.Items.Add(this.UpdateAvailableMenuItem); ToolStripMenuItem RefreshMenuItem = new ToolStripMenuItem(); RefreshMenuItem.Text = "Refresh"; RefreshMenuItem.Click += new EventHandler(RefreshMenuItem_Click); this.TrayIconContextMenu.Items.Add(RefreshMenuItem); ToolStripMenuItem CloseMenuItem = new ToolStripMenuItem(); CloseMenuItem.Text = "Close"; CloseMenuItem.Click += new EventHandler(CloseMenuItem_Click); this.TrayIconContextMenu.Items.Add(CloseMenuItem); TrayIconContextMenu.ResumeLayout(false); TrayIcon.ContextMenuStrip = TrayIconContextMenu; }