Form1.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. namespace WebUIWrapper
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. String ztDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One";
  24. String authToken = "";
  25. Int32 port = 9993;
  26. try
  27. {
  28. byte[] tmp = File.ReadAllBytes(ztDir + "\\authtoken.secret");
  29. authToken = System.Text.Encoding.ASCII.GetString(tmp).Trim();
  30. } catch {
  31. MessageBox.Show("Unable to read ZeroTier One authtoken.secret from:\r\n" + ztDir,"ZeroTier One");
  32. this.Close();
  33. }
  34. if ((authToken == null)||(authToken.Length <= 0))
  35. {
  36. MessageBox.Show("Unable to read ZeroTier One authtoken.secret from:\r\n" + ztDir, "ZeroTier One");
  37. this.Close();
  38. }
  39. try
  40. {
  41. byte[] tmp = File.ReadAllBytes(ztDir + "\\zerotier-one.port");
  42. port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim());
  43. if ((port <= 0) || (port > 65535))
  44. port = 9993;
  45. }
  46. catch
  47. {
  48. }
  49. try
  50. {
  51. TcpClient tc = new TcpClient();
  52. try
  53. {
  54. tc.Connect("127.0.0.1", port);
  55. tc.Close();
  56. }
  57. catch
  58. {
  59. MessageBox.Show("ZeroTier One service does not appear to be running at local port " + port.ToString(),"ZeroTier One");
  60. this.Close();
  61. return;
  62. }
  63. webContainer.Navigate("http://127.0.0.1:" + port.ToString() + "/index.html?authToken=" + authToken);
  64. }
  65. catch
  66. {
  67. MessageBox.Show("Unable to open service control panel.", "ZeroTier One");
  68. this.Close();
  69. }
  70. }
  71. }
  72. }