.NET读取QQWry.Dat 纯真版ip数据库(根据ip判断国家)

  1    外部调用:
2 //测试地址搜索#region 测试地址搜索
3 IPScaner objScan = new IPScaner();
4 string ip = Request.UserHostAddress.ToString();
5 objScan.DataPath = @"D:\\webwz\\zyue.cn\\App_data\\QQWry.Dat";
6 objScan.IP = ip;
7 string addre = objScan.IPLocation();
8 string err = objScan.ErrMsg;
9 Response.Write(addre);//国家
10 Response.Write(err);//地区
11 类:
12 /******************************************************************
13 ** File Name:IPScaner.cs
14 ** Copyright (c) 2004-2005 PPTech Studio(PPTech.Net)
15 ** Creater:Rexsp(MSN:[email protected])
16 ** Create Date:2004-12-27 20:10:28
17 ** Modifier:
18 ** Modify Date:
19 ** Description:to scan the ip location from qqwry.dat
20 ** Version: IPScaner 1.0.0
21 ******************************************************************/
22 using System;
23 using System.IO;
24 using System.Collections;
25 using System.Text;
26 using System.Text.RegularExpressions;
27 namespace PPTech.WebSite.BusinessRules
28 {
29 /**////<summary>
30 /// to scan the ip location from qqwry.dat
31 ///</summary>
32 public class IPScaner
33 {
34 //私有成员#region 私有成员
35 private string dataPath;
36 private string ip;
37 private string country;
38 private string local;
39
40 private long firstStartIp=0;
41 private long lastStartIp=0;
42 private FileStream objfs = null;
43 private long startIp=0;
44 private long endIp=0;
45 private int countryFlag=0;
46 private long endIpOff=0;
47 private string errMsg=null;
48
49
50 //构造函数#region 构造函数
51 public IPScaner()
52 {
53 //
54 // TODO: 在此处添加构造函数逻辑
55 //
56 }
57
58
59 //公共属性#region 公共属性
60 public string DataPath
61 {
62 set{dataPath=value;}
63 }
64 public string IP
65 {
66 set{ip=value;}
67 }
68 public string Country
69 {
70 get{return country;}
71 }
72 public string Local
73 {
74 get{return local;}
75 }
76 public string ErrMsg
77 {
78 get{return errMsg;}
79 }
80
81
82 //搜索匹配数据#region 搜索匹配数据
83 private int QQwry()
84 {
85 string pattern = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))";
86 Regex objRe = new Regex(pattern);
87 Match objMa = objRe.Match(ip);
88 if(!objMa.Success)
89 {
90 this.errMsg="IP格式错误";
91 return 4;
92 }
93
94 long ip_Int = this.IpToInt(ip);
95 int nRet=0;
96 if(ip_Int>=IpToInt("127.0.0.0")&&ip_Int<=IpToInt("127.255.255.255"))
97 {
98 this.country="本机内部环回地址";
99 this.local="";
100 nRet=1;
101 }
102 else if((ip_Int>=IpToInt("0.0.0.0")&&ip_Int<=IpToInt("2.255.255.255"))||(ip_Int>=IpToInt("64.0.0.0")&&ip_Int<=IpToInt("126.255.255.255"))||(ip_Int>=IpToInt("58.0.0.0")&&ip_Int<=IpToInt("60.255.255.255")))
103 {
104 this.country="网络保留地址";
105 this.local="";
106 nRet=1;
107 }
108 objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);
109 try
110 {
111 //objfs.Seek(0,SeekOrigin.Begin);
112 objfs.Position=0;
113 byte[] buff = new Byte[8] ;
114 objfs.Read(buff,0,8);
115 firstStartIp=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256;
116 lastStartIp=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256;
117 long recordCount=Convert.ToInt64((lastStartIp-firstStartIp)/7.0);
118 if(recordCount<=1)
119 {
120 country="FileDataError";
121 objfs.Close();
122 return 2;
123 }
124 long rangE=recordCount;
125 long rangB=0;
126 long recNO=0;
127 while(rangB<rangE-1)
128 {
129 recNO=(rangE+rangB)/2;
130 this.GetStartIp(recNO);
131 if(ip_Int==this.startIp)
132 {
133 rangB = recNO;
134 break;
135 }
136 if(ip_Int>this.startIp)
137 rangB=recNO;
138 else
139 rangE=recNO;
140 }
141 this.GetStartIp(rangB);
142 this.GetEndIp();
143 if(this.startIp<=ip_Int&&this.endIp>=ip_Int)
144 {
145 this.GetCountry();
146 this.local=this.local.Replace("(我们一定要解放台湾!!!)","");
147 }
148 else
149 {
150 nRet=3;
151 this.country="未知";
152 this.local="";
153 }
154 objfs.Close();
155 return nRet;
156 }
157 catch
158 {
159 return 1;
160 }
161
162 }
163
164 // IP地址转换成Int数据#region IP地址转换成Int数据
165 private long IpToInt(string ip)
166 {
167 char[] dot = new char[]{'.'};
168 string [] ipArr = ip.Split(dot);
169 if(ipArr.Length==3)
170 ip=ip+".0";
171 ipArr=ip.Split(dot);
172
173 long ip_Int=0;
174 long p1=long.Parse(ipArr[0])*256*256*256;
175 long p2=long.Parse(ipArr[1])*256*256;
176 long p3=long.Parse(ipArr[2])*256;
177 long p4=long.Parse(ipArr[3]);
178 ip_Int=p1+p2+p3+p4;
179 return ip_Int;
180 }
181
182 //int转换成IP#region int转换成IP
183 private string IntToIP(long ip_Int)
184 {
185 long seg1=(ip_Int&0xff000000)>>24;
186 if(seg1<0)
187 seg1+=0x100;
188 long seg2=(ip_Int&0x00ff0000)>>16;
189 if(seg2<0)
190 seg2+=0x100;
191 long seg3=(ip_Int&0x0000ff00)>>8;
192 if(seg3<0)
193 seg3+=0x100;
194 long seg4=(ip_Int&0x000000ff);
195 if(seg4<0)
196 seg4+=0x100;
197 string ip=seg1.ToString()+"."+seg2.ToString()+"."+seg3.ToString()+"."+seg4.ToString();
198
199 return ip;
200 }
201 //获取起始IP范围#region 获取起始IP范围
202 private long GetStartIp(long recNO)
203 {
204 long offSet = firstStartIp+recNO*7;
205 //objfs.Seek(offSet,SeekOrigin.Begin);
206 objfs.Position=offSet;
207 byte [] buff = new Byte[7];
208 objfs.Read(buff,0,7);
209
210 endIpOff=Convert.ToInt64(buff[4].ToString())+Convert.ToInt64(buff[5].ToString())*256+Convert.ToInt64(buff[6].ToString())*256*256;
211 startIp=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256+Convert.ToInt64(buff[3].ToString())*256*256*256;
212 return startIp;
213 }
214 // 获取结束IP#region 获取结束IP
215 private long GetEndIp()
216 {
217 //objfs.Seek(endIpOff,SeekOrigin.Begin);
218 objfs.Position=endIpOff;
219 byte [] buff = new Byte[5];
220 objfs.Read(buff,0,5);
221 this.endIp=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256+Convert.ToInt64(buff[3].ToString())*256*256*256;
222 this.countryFlag=buff[4];
223 return this.endIp;
224 }
225 //获取国家/区域偏移量#region 获取国家/区域偏移量
226 private string GetCountry()
227 {
228 switch(this.countryFlag)
229 {
230 case 1:
231 case 2:
232 this.country=GetFlagStr(this.endIpOff+4);
233 this.local=( 1 == this.countryFlag )?"":this.GetFlagStr(this.endIpOff+8);
234 break;
235 default:
236 this.country=this.GetFlagStr(this.endIpOff+4);
237 this.local=this.GetFlagStr(objfs.Position);
238 break;
239 }
240 return "";
241 }
242 //获取国家/区域字符串#region 获取国家/区域字符串
243 private string GetFlagStr(long offSet)
244 {
245 int flag=0;
246 byte [] buff = new Byte[3];
247 while(1==1)
248 {
249 //objfs.Seek(offSet,SeekOrigin.Begin);
250 objfs.Position=offSet;
251 flag = objfs.ReadByte();
252 if(flag==1||flag==2)
253 {
254 objfs.Read(buff,0,3);
255 if(flag==2)
256 {
257 this.countryFlag=2;
258 this.endIpOff=offSet-4;
259 }
260 offSet=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256;
261 }
262 else
263 {
264 break;
265 }
266 }
267 if(offSet<12)
268 return "";
269 objfs.Position=offSet;
270 return GetStr();
271 }
272 //GetStr#region GetStr
273 private string GetStr()
274 {
275 byte lowC=0;
276 byte upC=0;
277 string str="";
278 byte[] buff = new byte[2];
279 while(1==1)
280 {
281 lowC= (Byte)objfs.ReadByte();
282 if(lowC==0)
283 break;
284 if(lowC>127)
285 {
286 upC=(byte)objfs.ReadByte();
287 buff[0]=lowC;
288 buff[1]=upC;
289 System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
290 str+=enc.GetString(buff);
291 }
292 else
293 {
294 str+=(char)lowC;
295 }
296 }
297 return str;
298 }
299 //获取IP地址#region 获取IP地址
300 public string IPLocation()
301 {
302 this.QQwry();
303 return this.country+this.local;
304 }
305 public string IPLocation(string dataPath,string ip)
306 {
307 this.dataPath=dataPath;
308 this.ip=ip;
309 this.QQwry();
310 return this.country+this.local;
311 }
312
313 }
314 }
315
316 经过自己多次测试还请几个朋友都进行测试了,感觉可以。


编辑器加载中...

你可能感兴趣的:(.net)