PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/net/geoip/geoip/Form1.cs

https://github.com/berak/cs
C# | 286 lines | 255 code | 21 blank | 10 comment | 73 complexity | 53d43d4934fb794a719817bfe7dfc6d3 MD5 | raw file
  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.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Net.Sockets;
  12. namespace geoip
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
  20. this.ipCombo.KeyDown += new KeyEventHandler( ipCombo_KeyDown );
  21. unpickle();
  22. }
  23. void Form1_KeyPress(object sender, KeyPressEventArgs e)
  24. {
  25. // break on escape, i can't live without it!
  26. if (e.KeyChar == 27)
  27. this.Close();
  28. }
  29. public void ipCombo_KeyDown(object sender, KeyEventArgs e)
  30. {
  31. if (((KeyEventArgs)e).KeyValue == 13)
  32. {
  33. if (this.ipCombo.Text != "")
  34. {
  35. findButton.PerformClick(); // press it visibly
  36. }
  37. }
  38. }
  39. private void osmSearchCompleted(object sender, EventArgs e)
  40. {
  41. textBox.Enabled = true;
  42. MapControl.SearchProvider search = (MapControl.SearchProvider)sender;
  43. foreach (MapControl.SearchResult r in search.Results)
  44. {
  45. textBox.Text += r.DisplayName + "\r\n " + r.Latitude + " " + r.Longitude + "\r\n";
  46. }
  47. }
  48. private string findIp(string Hostname)
  49. {
  50. try
  51. {
  52. if (Uri.CheckHostName(Hostname) == UriHostNameType.Unknown)
  53. {
  54. return null;
  55. }
  56. IPHostEntry host = Dns.GetHostEntry(Hostname);
  57. return host.AddressList[0].ToString();
  58. }
  59. catch (Exception) { }
  60. return null;
  61. }
  62. // this is the free lookup, sometimes does not find stuff.
  63. private string getlatlon(string ip)
  64. {
  65. string ll = null;
  66. WebClient wc = new WebClient();
  67. Stream str = wc.OpenRead("http://api.hostip.info/get_html.php?ip="+ip+"&position=true");
  68. StreamReader sr = new StreamReader(str);
  69. while ( true )
  70. {
  71. string line = sr.ReadLine();
  72. if ( line == null )
  73. break;
  74. if ( line.StartsWith("Latitude: " ) )
  75. ll = line.Substring( 10 );
  76. else
  77. if ( line.StartsWith("Longitude: " ) )
  78. ll += " " + line.Substring( 11 );
  79. else
  80. textBox.Text += line + "\r\n";
  81. }
  82. textBox.Text += "latlon: " + ll + "\r\n";
  83. textBox.Text += "(fetched from http://api.hostip.info)\r\n";
  84. return ll;
  85. }
  86. private string whois(string whoisServer, string url)
  87. {
  88. StringBuilder stringBuilderResult = new StringBuilder();
  89. TcpClient tcpClinetWhois = new TcpClient(whoisServer, 43);
  90. NetworkStream networkStreamWhois = tcpClinetWhois.GetStream();
  91. BufferedStream bufferedStreamWhois = new BufferedStream(networkStreamWhois);
  92. StreamWriter streamWriter = new StreamWriter(bufferedStreamWhois);
  93. streamWriter.WriteLine(url);
  94. streamWriter.Flush();
  95. StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois);
  96. while (!streamReaderReceive.EndOfStream)
  97. stringBuilderResult.AppendLine(streamReaderReceive.ReadLine());
  98. return stringBuilderResult.ToString();
  99. }
  100. private void getMap(string lat, string lon)
  101. {
  102. try
  103. { // find map for latlon:
  104. string query = "http://tah.openstreetmap.org/MapOf/index.php?lat=" + lat + "&long=" + lon + "&z=" + zoomBox.Text + "&w=400&h=400&format=png";
  105. query = query.Replace(',', '.'); // arghhh, i HATE localization!!!!!(german, in this case)
  106. pictureBox1.Load(query);
  107. }
  108. catch (Exception ex) { textBox.Text += "\r\n\r\n" + ex.Message; return; }
  109. }
  110. private void findButton_Click(object sender, EventArgs e)
  111. {
  112. if (textBox.SelectedText != "")
  113. { // check if a lat-lon was selected:
  114. string[] ss = textBox.SelectedText.Split(" ".ToCharArray());
  115. if (ss.Length == 2)
  116. {
  117. if ((Double.Parse(ss[0]) != 0) && (Double.Parse(ss[1]) != 0))
  118. {
  119. ipCombo.Text = textBox.SelectedText;
  120. }
  121. }
  122. }
  123. string lat = "", lon = "";
  124. textBox.Text = "";
  125. textBox.Enabled = false;
  126. // chop copy&paste blanks:
  127. while (ipCombo.Text.StartsWith(" "))
  128. ipCombo.Text = ipCombo.Text.Remove(0, 1);
  129. //check for ip or hostname
  130. string ip = findIp(ipCombo.Text);
  131. if (ip != null)
  132. {
  133. // check free lookup first
  134. string ll = getlatlon(ip);
  135. if (ll != null && ll != " ")
  136. {
  137. string[] ss = ll.Split(" ".ToCharArray());
  138. if (ss.Length == 2)
  139. {
  140. if ((Double.Parse(ss[0]) != 0) && (Double.Parse(ss[1]) != 0))
  141. {
  142. lat = ss[0];
  143. lon = ss[1];
  144. }
  145. textBox.Enabled = true;
  146. }
  147. }
  148. if ( lat=="" && lon=="" )
  149. {
  150. // restricted to 40 shots a day.
  151. IP2Geo geo = new IP2Geo();
  152. IPInformation res = null;
  153. try
  154. { // find latlon from ip or hostname:
  155. res = geo.ResolveIP(ip, "0"); // 0 is the limited testkey.
  156. if (res == null) return;
  157. if (res.Longitude == 0 && res.Latitude == 0)
  158. {
  159. textBox.Text = res.Organization; // please wait or get a key ...
  160. return;
  161. }
  162. textBox.Enabled = true;
  163. textBox.Text = res.City + " (" + res.Country + ")\r\n" +
  164. "latlon:" + res.Latitude + " " + res.Longitude + "\r\n" +
  165. "ip:" + ip;
  166. lat = "" + res.Latitude;
  167. lon = "" + res.Longitude;
  168. textBox.Text += "\r\n(fetched from http://ws.cdyne.com)\r\n";
  169. }
  170. catch (Exception ex) { textBox.Text += "\r\n\r\n" + ex.Message; return; }
  171. }
  172. //textBox.Text += "\r\n--- whois " + ip + "\r\n";
  173. //textBox.Text += whois("whois.internic.com", ip); ;
  174. }
  175. // check for latlon in input:
  176. if (lat == "" && lon == "")
  177. {
  178. string[] ss = ipCombo.Text.Split(" ".ToCharArray());
  179. if (ss.Length == 2)
  180. {
  181. if ((Double.Parse(ss[0]) != 0) && (Double.Parse(ss[1]) != 0))
  182. {
  183. lat = ss[0];
  184. lon = ss[1];
  185. }
  186. }
  187. }
  188. // still not found?! check osm for place names :
  189. if (lat == "" && lon == "")
  190. {
  191. MapControl.SearchProvider search = new MapControl.SearchProvider();
  192. search.SearchCompleted += new EventHandler(osmSearchCompleted);
  193. if (search.Search(ipCombo.Text))
  194. {
  195. textBox.Enabled = false;
  196. }
  197. }
  198. if (lat != "" && lon != "")
  199. {
  200. getMap(lat, lon);
  201. }
  202. if (!this.ipCombo.Items.Contains(this.ipCombo.Text))
  203. this.ipCombo.Items.Add(this.ipCombo.Text);
  204. }
  205. private void label_click(int id, double off)
  206. {
  207. string[] ss = ipCombo.Text.Split(" ".ToCharArray());
  208. if (ss.Length == 2)
  209. {
  210. double [] latlon = new double[2] {Double.Parse(ss[0]),Double.Parse(ss[1])};
  211. if ((latlon[0] != 0) && (latlon[1] != 0))
  212. {
  213. double f = off / (18-Double.Parse(zoomBox.Text));
  214. ipCombo.Text = ipCombo.Text.Replace(ss[id], ("" + (latlon[id] + f)));
  215. getMap(ss[0], ss[1]);
  216. }
  217. }
  218. }
  219. private void labelL_Click(object sender, EventArgs e)
  220. {
  221. label_click(1, -0.02);
  222. }
  223. private void labelU_Click(object sender, EventArgs e)
  224. {
  225. label_click(0, 0.02);
  226. }
  227. private void labelD_Click(object sender, EventArgs e)
  228. {
  229. label_click(0, -0.02);
  230. }
  231. private void labelR_Click(object sender, EventArgs e)
  232. {
  233. label_click(1, 0.02);
  234. }
  235. private void pickle()
  236. {
  237. try
  238. {
  239. System.IO.StreamWriter file = new System.IO.StreamWriter("ip.txt");
  240. for (int i = 0; i < ipCombo.Items.Count; i++)
  241. {
  242. file.WriteLine(ipCombo.Items[i].ToString());
  243. }
  244. file.WriteLine("#");
  245. file.Close();
  246. }
  247. catch (Exception) { }
  248. }
  249. private void unpickle()
  250. {
  251. try
  252. {
  253. System.IO.StreamReader file = new System.IO.StreamReader("ip.txt");
  254. while (true)
  255. {
  256. string s = file.ReadLine();
  257. if (s == "#") break;
  258. ipCombo.Items.Add(s);
  259. }
  260. file.Close();
  261. }
  262. catch (Exception) { }
  263. }
  264. }
  265. }