Girlfriend's birthday, I spent 20 minutes to write her a proxy server

Girlfriend said: "You look very hard recently, I send you a gift and see what you want, I will serve.."

I thought for a long time, from books to shoes to electronics and finally to daily necessities, felt nothing, and then she said:. "You save time and money, and I do not need anything."

She insisted on seeing:. "No, you have to say a gift, I would like to send you something."

So, I seriously up, pick up the phone, Taobao shopping for a few minutes, but still could not think what comes out shortcomings, and finally there is no way: "Well, if you really want to send something, then you write a proxy server bar "

She did not say anything, laughed together, then write this blog.

definition

It allows a network terminal (typically a client) by the service with another network terminal (typically a server) non-direct connection. As shown, communication system for general Web application communication method employed compared with the proxy server.
Here Insert Picture Description

principle

Proxy server at the specified port (eg 8080) to listen browser access request (needs to be set accordingly in the client browser), upon receiving the browser request from the browser to the remote site, the proxy server starts to retrieve the caching proxy server URL corresponding objects (web pages, images and other objects), after finding object files, extract the most recently modified the object file; proxy server program request message in the client's header insertion <If-modified-Since: date the object file modified>, and original Web server forwards the request message after modification. If the proxy server does not cache of the object, the server will forward the request directly to the original packet and forwards the response to the original server returns directly to the client, while the cache object to the proxy server. Proxy server program will clean up the cache based on time, size, and extraction of records to the cache. In this study, to be achieved by a simple HTTP proxy server can be divided into two steps :( First of all, please set your browser to open a local agent, pay attention to set the proxy port and proxy server listening port is consistent).

content

  1. Design and implement a basic HTTP proxy server. Required to receive a specified port (e.g. 8080) from the client's HTTP request and the HTTP server to access the address pointed to (origin server) according to one of the URL address, the HTTP server receives the response packet, and forwards the response packet to the corresponding customers browse.
  2. Cache design and implement a support function of the HTTP proxy server. Requirements can cache object original server to respond, and can modify the request message (add if-modified-since header row), the original cache server to confirm whether the object is the latest version. (Selected as the content, extra points, when the Church can be completed or completed after class)
  3. Extension to the HTTP proxy server that supports the following functions:
    A) Web filtering: allow / disallow access to certain sites;
    b) the user to filter: support / do not support certain users access external Web sites;
    c) Guide: a user of a site the visit led to a mock website (fishing).

process

Browser proxy settings

IE browser to set an example: open the browser tool browser options - Connections - LAN Settings - Proxy Server. Setting address is 127.0.0.1, port number is 10240.
Here Insert Picture Description

Achieve a basic HTTP proxy server

HTTP proxy server for a network terminal (typically a client) non-proxy service through a direct connection with another network terminal (usually the server). Design flow chart follows:
Here Insert Picture Description
(. 1) InitSocket () function
features: initialize the socket
(2) ProxyThread () function is
the function: function execution threads
(3) ParseHttpHead () function is
the function: parsing the HTTP TCP packet header
( 4) ConnectToServer () function
function: create a target host server socket and connect

Cache function

  1. When the client data server first request, the proxy server returns the response to the cached request, save it in the local file.
  2. When the client access to the second data, the proxy server checks whether a local response to the request , if not, continue the cache; if so, sending a request to the server by comparing the last modified time to determine if the cache has expired, If the server returns a status code 304, it is not expired; if the server returns a status code 200, the cache expires, a local cache is updated .
  3. The corresponding function
    (1) getfileDate () function
    functions: access the local file, acquisition date in the local cache
    (2) sendnewHTTP () function is
    the function: modification request message, adding if-modified-since header row
    to view the requested packet format:
    (3) checkfileCache () function
    : test the status code returned by the host, if it is acquired from the local cache 304 forwards, or to update the cache
    (4) storefileCache () function
    : test the status code returned by the host, if 200 Gets the local cache

Expansion Capabilities

Web filtering

First set the url is not allowed to access the site
Here Insert Picture Description
when processing client requests, checks whether the request message is allowed to access url, if access is not allowed, is denied
Here Insert Picture Description

Users filter

The communication network IP address of the proxy server for a particular change from INADDR_ANY an IP address, so that only the IP address through the
proxy server to access external Web sites, can not be other IP
Here Insert Picture Description

Guide

First, set the target sites and corresponding host name and phishing sites
Here Insert Picture Description
when a client request, the request message the host and replace url url set in advance and a good simulation host site
Here Insert Picture Description

Source

Code link github

Published 109 original articles · won praise 128 · views 40000 +

Guess you like

Origin blog.csdn.net/JAck_chen0309/article/details/104781600