Labor in Section 5: ASP. NET access restriction

Source: Internet
Author: User

May Day holiday for three days, I did work for three days, the main job is to complete the website (http://www.step1.cn) access control, this function I think is quite necessary, however, I searched the internet for a long time and did not find any related items. I had to write one on my own. Although the function was not very good, it would be enough to achieve the goal.

The cause is that on the last day of June April, I downloaded the server logs of the website (because it is a virtual host, it is difficult to obtain logs, so this is not a frequent task ), so I analyzed it carefully from various aspects. Other aspects are not very problematic. The most important thing is that I found several IP addresses frequently access the website, I found several of them were Baidu's spider (to be honest, I didn't expect Baidu Spider's access to the website so frequently), and some were not Spider's, so I was very skeptical.

First, I checked a website that can reverse query domain names based on IP addresses. The results showed some very suspicious websites, one of which is extremely typical and published, let me despise it: (ditu.123175.com/place/cn ), the website has multiple columns that completely copy the content of my website, and the server keeps crawling content on my website, eldest brother. Do you have some technical skills? Such behavior is really ...... If you don't forget it, this article is still a technical topic and won't be able to solve this problem.

So I will take measures. I started to write a simpleProgramTo directly shield the IP address, and then improve the program in three days of the Fifth Festival. Currently, this program can meet my requirements, because the program is still being debugged online, I will not announce it for the momentCodeLet's just talk about the specific implementation process and principle:

1. the basic architecture of my website is still from the URL rewriting mechanism (users who have visited my website a long time ago may know that my website was actually a blog, the source code of the blog garden is used. Later, because I want to publish some map-related programs, I gradually migrate my blog to the blog garden, but I removed the URL rewriting code and kept it.) This mechanism is implemented through the Web. httphandlers settings in config to control all ASP. net response, all ASP.. Net requests are intercepted and processed.

2. I call a static method in the originally intercepted class. In this method, the request statistics and restriction process are executed. Once the request is determined to be invalid, 400 (Bad request) is returned immediately without further execution. Therefore, once the request is restricted, normal content will not be returned.

3. because I am in ASP. as a result, static content, such as htm, is not affected, and the content does not contain data, therefore, no restrictions are required.

4. my basic implementation logic is as follows: each IP Address can access up to 30 ASP entries per minute. NET page. A maximum of 600 pages can be accessed every hour, and a maximum of 1800 pages can be accessed every day. This is the deployment after I make statistics on the current access status of the website, I think this will not affect normal use. Of course, these numbers can be stored on the web. config.

5. whenever an IP address is accessed, I add this IP address to the minute list MList, which records the number of times each IP Address has accessed in the current minute range. Once this minute ends, I will sort the list of visits in this minute and add the top 10 IP addresses to the hlist in the hour list (why do I only need the top 10 IP addresses? The list is too large, performance impact). Similarly, after the end of the hour, add the top 10 of the hour list to the daily list Dlist.

6. after the IP address is added to the List, check whether the access times of the IP address exceed the limit in the three lists. If yes, error 400 is returned, that is, IP addresses that exceed the limit per minute cannot be accessed before the end of this minute. IP addresses that exceed the limit per hour are not allowed to be accessed within this hour.

7. every time an access is prohibited, the access time, IP address, access path, and useragent must be output to the log to check whether there are unreasonable access restrictions, the changes to the daily list Dlist (24 times a day) are output to the log file to check whether exceptions occur.

8. in addition to the above access limit, I also added the IP blacklist restriction. Once the access is blacklisted, all access requests will be restricted. In this case, you can only access static content on this site. Of course, this blacklist is also on the web. which can be configured in config.

9. the biggest problem is record persistence. You know, I don't want to use a database, because one of the purposes of my access restriction is to reduce the pressure on the database. If I use a database, it is estimated that the database will be suspended, so I started to think about using ASP. net static variables, after all, are three small lists. As a result, after online detection, the data will be cleared after a certain period of time, so far, I do not know where the problem is. I began to suspect that it was my own program. After studying for a long time, I began to suspect that it was a problem with the lifecycle of static variables, I checked it on the Internet. It seems that some heroes do not recommend static variables, but they do not explicitly say what will happen, on the Internet, everyone thinks that the statici variable should be the same as the overall lifecycle of the website. However, the three lists of my program seem to be cleared every 20 minutes, therefore, only the number of minutes can be used.

Later, I thought it could be a problem with the virtual host. I was so angry that I decided to write the data in three lists to the file every minute using serialization, then, the data is read from the file every time the data is cleared. In this case, the loss record will certainly not exceed 1 minute, and the normal restart of the service industry will not affect the statistical data, for example, if I want to update something, I will definitely restart the application. According to the current method, although the performance may be poor, the implementation effect is still good.

The above is my labor on May Day, that is, the webmaster colleagues who do not love to work and do not like to get the work have ruined my three-day labor day. I think: There is a technology and time to steal other people's websites, why not write something unique by yourself? I admit that my website has also borrowed content from other websites, but I never want to copy it because I want to give my website a reason for its existence.

PS: because there are too many advertisements for my website commented on by others, it is a bit like a waste station. I am reflecting on it and are ready to adjust the website layout. After the adjustment, the website advertisement will become fewer and will not exist again, and it may be more conspicuous. In the future, the width of the left sidebar may be expanded to 350 (now it is 200), so there will be a larger advertisement on the left, however, the main reason for the increase in the left column is that I canceled the right column, and some features really need a larger left space to display. There may be only ads in the left navigation bar on the page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.