bklLiudl
2024-07-23 675b8bcc4a3630d95e3d0b97d933e63442075ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
 
namespace Common
{
    public class IPScanerHelper
    {
        private string dataPath;
        private string ip;
        private string country;
        private string local;
        private long firstStartIp = 0L;
        private long lastStartIp = 0L;
        private FileStream objfs = null;
        private long startIp = 0L;
        private long endIp = 0L;
        private int countryFlag = 0;
        private long endIpOff = 0L;
        private string errMsg = null;
 
        public string DataPath
        {
            set
            {
                this.dataPath = value;
            }
        }
 
        public string IP
        {
            set
            {
                this.ip = value;
            }
        }
 
        public string Country
        {
            get
            {
                return this.country;
            }
        }
 
        public string Local
        {
            get
            {
                return this.local;
            }
        }
 
        public string ErrMsg
        {
            get
            {
                return this.errMsg;
            }
        }
 
        private int QQwry()
        {
            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]))";
            Regex objRe = new Regex(pattern);
            Match objMa = objRe.Match(this.ip);
            int result;
            if (!objMa.Success)
            {
                this.errMsg = "IP格式错误";
                result = 4;
            }
            else
            {
                long ip_Int = this.IpToInt(this.ip);
                int nRet = 0;
                if (ip_Int >= this.IpToInt("127.0.0.0") && ip_Int <= this.IpToInt("127.255.255.255"))
                {
                    this.country = "本机内部环回地址";
                    this.local = "";
                    nRet = 1;
                }
                else
                {
                    if ((ip_Int >= this.IpToInt("0.0.0.0") && ip_Int <= this.IpToInt("2.255.255.255")) || (ip_Int >= this.IpToInt("64.0.0.0") && ip_Int <= this.IpToInt("126.255.255.255")) || (ip_Int >= this.IpToInt("58.0.0.0") && ip_Int <= this.IpToInt("60.255.255.255")))
                    {
                        this.country = "网络保留地址";
                        this.local = "";
                        nRet = 1;
                    }
                }
                this.objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);
                try
                {
                    this.objfs.Position = 0L;
                    byte[] buff = new byte[8];
                    this.objfs.Read(buff, 0, 8);
                    this.firstStartIp = (long)((int)buff[0] + (int)buff[1] * 256 + (int)buff[2] * 256 * 256 + (int)buff[3] * 256 * 256 * 256);
                    this.lastStartIp = (long)((int)buff[4] + (int)buff[5] * 256 + (int)buff[6] * 256 * 256 + (int)buff[7] * 256 * 256 * 256);
                    long recordCount = Convert.ToInt64((double)(this.lastStartIp - this.firstStartIp) / 7.0);
                    if (recordCount <= 1L)
                    {
                        this.country = "FileDataError";
                        this.objfs.Close();
                        result = 2;
                    }
                    else
                    {
                        long rangE = recordCount;
                        long rangB = 0L;
                        while (rangB < rangE - 1L)
                        {
                            long recNO = (rangE + rangB) / 2L;
                            this.GetStartIp(recNO);
                            if (ip_Int == this.startIp)
                            {
                                rangB = recNO;
                                break;
                            }
                            if (ip_Int > this.startIp)
                            {
                                rangB = recNO;
                            }
                            else
                            {
                                rangE = recNO;
                            }
                        }
                        this.GetStartIp(rangB);
                        this.GetEndIp();
                        if (this.startIp <= ip_Int && this.endIp >= ip_Int)
                        {
                            this.GetCountry();
                            this.local = this.local.Replace("(我们一定要解放台湾!!!)", "");
                        }
                        else
                        {
                            nRet = 3;
                            this.country = "未知";
                            this.local = "";
                        }
                        this.objfs.Close();
                        result = nRet;
                    }
                }
                catch
                {
                    result = 1;
                }
            }
            return result;
        }
 
        private long IpToInt(string ip)
        {
            char[] dot = new char[]
            {
                '.'
            };
            string[] ipArr = ip.Split(dot);
            if (ipArr.Length == 3)
            {
                ip += ".0";
            }
            ipArr = ip.Split(dot);
            long p = long.Parse(ipArr[0]) * 256L * 256L * 256L;
            long p2 = long.Parse(ipArr[1]) * 256L * 256L;
            long p3 = long.Parse(ipArr[2]) * 256L;
            long p4 = long.Parse(ipArr[3]);
            return p + p2 + p3 + p4;
        }
 
        private string IntToIP(long ip_Int)
        {
            long seg = (ip_Int & 0xff000000) >> 24;
            if (seg < 0L)
            {
                seg += 0x100;
            }
            long seg2 = (ip_Int & 0x00ff0000) >> 16;
            if (seg2 < 0L)
            {
                seg2 += 0x100;
            }
            long seg3 = (ip_Int & 0x0000ff00) >> 8;
            if (seg3 < 0L)
            {
                seg3 += 0x100;
            }
            long seg4 = ip_Int & 0x000000ff;
            if (seg4 < 0L)
            {
                seg4 += 0x100;
            }
            return string.Concat(new string[]
            {
                seg.ToString(),
                ".",
                seg2.ToString(),
                ".",
                seg3.ToString(),
                ".",
                seg4.ToString()
            });
        }
 
        private long GetStartIp(long recNO)
        {
            long offSet = this.firstStartIp + recNO * 7L;
            this.objfs.Position = offSet;
            byte[] buff = new byte[7];
            this.objfs.Read(buff, 0, 7);
            this.endIpOff = Convert.ToInt64(buff[4].ToString()) + Convert.ToInt64(buff[5].ToString()) * 256L + Convert.ToInt64(buff[6].ToString()) * 256L * 256L;
            this.startIp = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256L + Convert.ToInt64(buff[2].ToString()) * 256L * 256L + Convert.ToInt64(buff[3].ToString()) * 256L * 256L * 256L;
            return this.startIp;
        }
 
        private long GetEndIp()
        {
            this.objfs.Position = this.endIpOff;
            byte[] buff = new byte[5];
            this.objfs.Read(buff, 0, 5);
            this.endIp = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256L + Convert.ToInt64(buff[2].ToString()) * 256L * 256L + Convert.ToInt64(buff[3].ToString()) * 256L * 256L * 256L;
            this.countryFlag = (int)buff[4];
            return this.endIp;
        }
 
        private string GetCountry()
        {
            switch (this.countryFlag)
            {
                case 1:
                case 2:
                    this.country = this.GetFlagStr(this.endIpOff + 4L);
                    this.local = ((1 == this.countryFlag) ? " " : this.GetFlagStr(this.endIpOff + 8L));
                    break;
 
                default:
                    this.country = this.GetFlagStr(this.endIpOff + 4L);
                    this.local = this.GetFlagStr(this.objfs.Position);
                    break;
            }
            return " ";
        }
 
        private string GetFlagStr(long offSet)
        {
            byte[] buff = new byte[3];
            while (true)
            {
                this.objfs.Position = offSet;
                int flag = this.objfs.ReadByte();
                if (flag != 1 && flag != 2)
                {
                    break;
                }
                this.objfs.Read(buff, 0, 3);
                if (flag == 2)
                {
                    this.countryFlag = 2;
                    this.endIpOff = offSet - 4L;
                }
                offSet = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256L + Convert.ToInt64(buff[2].ToString()) * 256L * 256L;
            }
            string result;
            if (offSet < 12L)
            {
                result = " ";
            }
            else
            {
                this.objfs.Position = offSet;
                result = this.GetStr();
            }
            return result;
        }
 
        private string GetStr()
        {
            string str = "";
            byte[] buff = new byte[2];
            while (true)
            {
                byte lowC = (byte)this.objfs.ReadByte();
                if (lowC == 0)
                {
                    break;
                }
                if (lowC > 127)
                {
                    byte upC = (byte)this.objfs.ReadByte();
                    buff[0] = lowC;
                    buff[1] = upC;
                    Encoding enc = Encoding.GetEncoding("GB2312");
                    str += enc.GetString(buff);
                }
                else
                {
                    str += (char)lowC;
                }
            }
            return str;
        }
 
        public string IPLocation()
        {
            this.QQwry();
            return this.country + this.local;
        }
 
        public string IPLocation(string dataPath, string ip)
        {
            this.dataPath = dataPath;
            this.ip = ip;
            this.QQwry();
            return this.country + this.local;
        }
    }
}