PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/MyIPForm.cs

https://bitbucket.org/rag3rac3r/myip
C# | 169 lines | 139 code | 20 blank | 10 comment | 25 complexity | 3e2635d80f89a2963e8dff62f47b3be0 MD5 | raw file
  1. using System;
  2. using System.Net.NetworkInformation;
  3. using System.Reflection;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Windows.Forms;
  7. using MyIP.Localization;
  8. namespace MyIP
  9. {
  10. public partial class MyIP : Form
  11. {
  12. public MyIP()
  13. {
  14. //Thread.CurrentThread.CurrentUICulture = new CultureInfo("sv-SE");
  15. InitializeComponent();
  16. StartPosition = FormStartPosition.CenterScreen;
  17. Version version = Assembly.GetExecutingAssembly().GetName().Version;
  18. int major = version.Major;
  19. int minor = version.Minor;
  20. int build = version.Build;
  21. Text = String.Format("{0} {1}.{2}", Language.MyIPForm.Title, major, minor);
  22. btnAbout.Text = Language.MyIPForm.btnAbout;
  23. btnRefresh.Text = Language.MyIPForm.btnRefresh;
  24. btnExit.Text = Language.MyIPForm.btnExit;
  25. boxExternalIP.Text = Language.MyIPForm.boxExternalIP;
  26. boxInternalIP.Text = Language.MyIPForm.boxInternalIP;
  27. btnExtIPCopy.Text = Language.MyIPForm.btnCopy;
  28. btnIntIPCopy.Text = Language.MyIPForm.btnCopy;
  29. }
  30. private void RefreshBtn_Click(object sender, EventArgs e)
  31. {
  32. string wan = CheckExtIp();
  33. string lan = CheckIntIp();
  34. lblExtIP.Text = wan;
  35. lblIntIP.Text = lan;
  36. // Enable copy buttons first AFTER some content have been entered
  37. btnIntIPCopy.Enabled = true;
  38. btnExtIPCopy.Enabled = true;
  39. }
  40. private void ExitBtn_Click(object sender, EventArgs e)
  41. {
  42. this.Close();
  43. }
  44. private static string CheckExtIp()
  45. {
  46. // IPv4 & IPv6: myip.rrnet.info
  47. // IPv4 only: myip4.rrnet.info
  48. // IPv6 only: myip6.rrnet.info
  49. // TODO: Error handling if the webserver don't respond or any other error occurs
  50. string content = Encoding.ASCII.GetString(Http.Get("http://myip4.rrnet.info"));
  51. try
  52. {
  53. content = Regex.Replace(content, @"[^\w\.@-]", "");
  54. }
  55. catch (Exception ex)
  56. {
  57. Program.ShowException(ex);
  58. }
  59. return content.ToString();
  60. }
  61. private static bool IsPrivIp(string address)
  62. {
  63. bool isPrivate = false;
  64. if (new IPSubnet("192.168.0.0/16").Contains(address))
  65. {
  66. isPrivate = true;
  67. }
  68. else if (new IPSubnet("172.16.0.0/12").Contains(address))
  69. {
  70. isPrivate = true;
  71. }
  72. else if (new IPSubnet("10.0.0.0/8").Contains(address))
  73. {
  74. isPrivate = true;
  75. }
  76. return isPrivate;
  77. }
  78. private static string CheckIntIp()
  79. {
  80. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  81. if (nics == null || nics.Length < 1)
  82. {
  83. MessageBox.Show("No Nics");
  84. return "";
  85. }
  86. foreach (NetworkInterface adapter in nics)
  87. {
  88. // Ignore Non-IPv4 interfaces
  89. // Ignore Loopback interfaces
  90. // Ignore interfaces that are not up
  91. if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211
  92. || adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
  93. && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback
  94. && adapter.OperationalStatus == OperationalStatus.Up
  95. && adapter.Supports(NetworkInterfaceComponent.IPv4) == true)
  96. {
  97. foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
  98. {
  99. if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  100. {
  101. if (IsPrivIp(ip.Address.ToString()))
  102. {
  103. return ip.Address.ToString();
  104. //MessageBox.Show(adapter.Name + "\r\n" + ip.Address.ToString());
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return "";
  111. }
  112. private void MyIP_Load(object sender, EventArgs e)
  113. {
  114. }
  115. private void AboutBtn_Click(object sender, EventArgs e)
  116. {
  117. Cursor.Current = Cursors.WaitCursor;
  118. using (AboutForm form = new AboutForm())
  119. {
  120. Cursor.Current = Cursors.Default;
  121. form.ShowDialog();
  122. }
  123. }
  124. private void btnExtIPCopy_Click(object sender, EventArgs e)
  125. {
  126. Clipboard.SetText(lblExtIP.Text.ToString());
  127. if (Clipboard.GetText() == lblExtIP.Text.ToString())
  128. {
  129. MessageBox.Show(String.Format(Language.MyIPForm.CopySuccess, lblExtIP.Text.ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
  130. }
  131. else
  132. {
  133. MessageBox.Show(String.Format(Language.MyIPForm.CopyFailed, lblExtIP.Text.ToString()), Language.General.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
  134. }
  135. }
  136. private void btIntIPCopy_Click(object sender, EventArgs e)
  137. {
  138. Clipboard.SetText(lblIntIP.Text.ToString());
  139. if (Clipboard.GetText() == lblIntIP.Text.ToString())
  140. {
  141. MessageBox.Show(String.Format(Language.MyIPForm.CopySuccess, lblIntIP.Text.ToString()), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
  142. }
  143. else
  144. {
  145. MessageBox.Show(String.Format(Language.MyIPForm.CopyFailed, lblIntIP.Text.ToString()), Language.General.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
  146. }
  147. }
  148. }
  149. }