private void CreateFirewall_Click(object sender, EventArgs e) { // Prompt the user... DialogResult dialogresult = MessageBox.Show ( this, "Clicking 'Yes' creates an inbound firewall rule for the " + "TwainDirect.Scanner program, so that TWAIN Local applications " + "on other PCs can use your scanner." + Environment.NewLine + Environment.NewLine + "Would you like to continue?", "Create Firewall Rule", MessageBoxButtons.YesNo ); // If yes, then do the refresh... if (dialogresult == DialogResult.Yes) { // Issue the command... string szFirewallBat = Path.Combine(Path.GetTempPath(), "twaindirectfirewall.bat"); File.WriteAllText ( szFirewallBat, "@echo off" + Environment.NewLine + "netsh advfirewall firewall add rule name=TwainDirect.Scanner dir=in action=allow program=system enable=yes profile=any interfacetype=any protocol=tcp localport=" + m_szPort + " remoteport=any security=notrequired localip=any remoteip=any edge=yes" ); ManageCertificates.RunBatchFile(szFirewallBat); File.Delete(szFirewallBat); // Update the form... SuspendLayout(); UpdateFirewallInfoOnForm(); ResumeLayout(); } }
/// <summary> /// Constructor for our main form... /// </summary> public FormMain() { // Init the form... InitializeComponent(); this.MinimizeBox = false; this.MaximizeBox = false; // Are we running in reduced mode? string szTwainDirectApp = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); szTwainDirectApp = Path.Combine(szTwainDirectApp, "TwainDirect.App.exe"); if (File.Exists(szTwainDirectApp)) { m_groupboxSelfSignedCertificates.Enabled = false; m_labelRoot.Enabled = false; m_labelExchange.Enabled = false; m_labelUrlAcl.Enabled = false; m_richtextboxRoot.Enabled = false; m_richtextboxExchange.Enabled = false; m_richtextboxUrlAcl.Enabled = false; m_buttonDeleteCertificates.Enabled = false; m_buttonRefreshCertificates.Enabled = false; m_groupboxFirewall.Enabled = false; m_labelFirewall.Enabled = false; m_richtextboxFirewall.Enabled = false; m_buttonDeleteFirewall.Enabled = false; m_buttonCreateFirewall.Enabled = false; } // Init other stuff... m_szRootCertificateName = "TWAIN Direct Self-Signed Root Authority for " + Environment.MachineName; // Our root certificate m_szExchangeCertificateName = Environment.MachineName + ".local"; // The exchange name for this PC m_szTwainDirectScannerApp = "{aadc29dd-1d81-42f5-873d-5d89cf6e58ee}"; // TwainDirect.Scanner's GUID m_szPort = "34034"; // The port we'll be using // Init the certificate manager... m_managecertificates = new ManageCertificates(m_szRootCertificateName, m_szExchangeCertificateName, m_szTwainDirectScannerApp, m_szPort); // Update the form... UpdateBonjourInfoOnForm(); UpdateCertificateInfoOnForm(); UpdateFirewallInfoOnForm(); }
/// <summary> /// Update the firewall info displayed on the form... /// </summary> private void UpdateFirewallInfoOnForm() { string szNoAccess = "Other PCs on this local area network cannot access this PC's scanner. If you'd like to change that, click on the 'Create Firewall Rule' button."; // Update the firewall info... try { // Issue the command... string szFirewallBat = Path.Combine(Path.GetTempPath(), "twaindirectfirewall.bat"); File.WriteAllText ( szFirewallBat, "@echo off" + Environment.NewLine + "netsh advfirewall firewall show rule TwainDirect.Scanner" ); string szOutput = ManageCertificates.RunBatchFile(szFirewallBat); File.Delete(szFirewallBat); // Tokenize... List <string> listSzTwainDirect = new List <string>(); string[] aszLines = szOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); // We have no rules... if (aszLines.Length <= 2) { m_richtextboxFirewall.Text = szNoAccess; } // Show what we have... else { m_richtextboxFirewall.Text = "Other PCs on this local area network can access this PC's scanner."; foreach (string szLine in aszLines) { m_richtextboxFirewall.Text += Environment.NewLine + szLine; } } } catch { m_richtextboxUrlAcl.Text = szNoAccess; } }
private void DeleteFirewall_Click(object sender, EventArgs e) { // Prompt the user... DialogResult dialogresult = MessageBox.Show ( this, "Clicking 'Yes' deletes the inbound firewall rule for the " + "TwainDirect.Scanner program, preventing TWAIN Local applications " + "on other PCs from using your scanner. You will still be able " + "to run TWAIN Local applications on this PC." + Environment.NewLine + Environment.NewLine + "Would you like to continue?", "Delete Firewall Rule", MessageBoxButtons.YesNo ); // If yes, then do the refresh... if (dialogresult == DialogResult.Yes) { // Issue the command... string szFirewallBat = Path.Combine(Path.GetTempPath(), "twaindirectfirewall.bat"); File.WriteAllText ( szFirewallBat, "@echo off" + Environment.NewLine + "netsh advfirewall firewall delete rule \"name=TwainDirect.Scanner\"" ); ManageCertificates.RunBatchFile(szFirewallBat); File.Delete(szFirewallBat); // Update the form... SuspendLayout(); UpdateFirewallInfoOnForm(); ResumeLayout(); } }
/// <summary> /// Update the certificate info displayed on the form... /// </summary> private void UpdateCertificateInfoOnForm() { bool blSuccess; DateTime datetimeNotBefore; DateTime datetimeNotAfter; // Show the root certificate, if any... blSuccess = m_managecertificates.IsCertificateInstalled ( StoreLocation.LocalMachine, StoreName.Root, m_szRootCertificateName, m_szRootCertificateName, out datetimeNotBefore, out datetimeNotAfter ); if (!blSuccess) { m_richtextboxRoot.Text = "(no data)"; } else { m_richtextboxRoot.Text = m_szRootCertificateName + ", " + datetimeNotBefore.ToShortDateString() + " to " + datetimeNotAfter.ToShortDateString(); } // Show the exchange certificate, if any... blSuccess = m_managecertificates.IsCertificateInstalled ( StoreLocation.LocalMachine, StoreName.My, m_szExchangeCertificateName, m_szRootCertificateName, out datetimeNotBefore, out datetimeNotAfter ); if (!blSuccess) { m_richtextboxExchange.Text = "(no data)"; } else { m_richtextboxExchange.Text = m_szExchangeCertificateName + ", " + datetimeNotBefore.ToShortDateString() + " to " + datetimeNotAfter.ToShortDateString(); } // Update the URCACL info... try { m_richtextboxUrlAcl.Text = "(no data)"; // Issue the command... string szUrlAclBat = Path.Combine(Path.GetTempPath(), "twaindirecturlacl.bat"); File.WriteAllText(szUrlAclBat, "netsh http show urlacl"); string szOutput = ManageCertificates.RunBatchFile(szUrlAclBat); File.Delete(szUrlAclBat); // Tokenize... List <string> listSzTwainDirect = new List <string>(); string[] aszLines = szOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); // Find the TWAIN Direct entry, if any, we shouldn't find more // than one, but let's be prepared for that possibility... foreach (string szLine in aszLines) { if (szLine.ToLowerInvariant().Contains("/twaindirect/session/") && szLine.ToLowerInvariant().Contains("https")) { listSzTwainDirect.Add(szLine.Substring(szLine.IndexOf("https"))); } } // Show what we found... m_richtextboxUrlAcl.Text = ""; foreach (string szTwainDirect in listSzTwainDirect) { int iIndex = szTwainDirect.IndexOf('/', 8); if ((iIndex < 0) || ((iIndex + 1) >= szTwainDirect.Length)) { continue; } string szPort = szTwainDirect.Remove(iIndex + 1).ToLowerInvariant(); foreach (string szLine in aszLines) { if (szLine.ToLowerInvariant().Contains(szPort)) { if (!string.IsNullOrEmpty(m_richtextboxUrlAcl.Text)) { m_richtextboxUrlAcl.Text += Environment.NewLine; } m_richtextboxUrlAcl.Text += szLine.ToLowerInvariant().Substring(szLine.IndexOf("https")); } } } // Ruh-roh... if (string.IsNullOrEmpty(m_richtextboxUrlAcl.Text)) { m_richtextboxUrlAcl.Text = "(no data)"; } } catch { m_richtextboxUrlAcl.Text = "(no data)"; } }