路由器插入廣告實現

如今有不少免費的wifi 訪問網頁的時候底部會出現一些廣告,如下爲的實現過程:javascript

1.在路由器上抓http的TCP/IP包,解壓,對比,並從新算校驗碼,打包,並轉發html

若是沒有很深刻對的TCP/IP的瞭解,確定作不了java

2.使用現場的一些帶過濾的代理軟件(其實也是實現以上過程,但已封裝好接口,不須要你在去了解TCP/IP的高深知識)小程序

 在IPTABLE上轉作好端口轉發,好比把80轉發到此代理的上,如8080緩存

如下爲第2種實現,基於openwrt(目前國內絕大部分廣告路由的實現方式,沒高大上的代碼,但方式消耗更多的資源,不建議使用)服務器

安裝網絡

http://www.privoxy.org/user-manual/actions-file.html#ADD-HEADERapp

修改配置文件dom

confdir /etc/privoxy
logdir /var/log
filterfile default.filter
filterfile user.filter
#logfile privoxy
actionsfile match-all.action # Actions that are applied to all sites and maybe overruled later on.
actionsfile default.action   # Main actions file
actionsfile user.action      # User customizations
listen-address  10.1.1.1:8118 #你的路由地址,代理服務器地址
toggle  1
enable-remote-toggle  1
enable-remote-http-toggle  0
enable-edit-actions 1
enforce-blocks 0
buffer-limit 4096
forwarded-connect-retries  0
accept-intercepted-requests 1
allow-cgi-request-crunching 0
split-large-forms 0
keep-alive-timeout 300
socket-timeout 300
permit-access  10.1.1.0/24
debug   1    # show each GET/POST/CONNECT request
debug   4096 # Startup banner and warnings
debug   8192 # Errors - *we highly recommended enabling this*
#admin-address privoxy-admin@example.com
#proxy-info-url http://www.example.com/proxy-service.html

在user.filter 中添加規則socket

FILTER: block-weeds  
s|</head>|<script type="text/javascript" src="http://www.yourdomainname.com/ystest/js/hupu.js"></script>$0|

把用戶規則加入到服務 user.action

{+filter{block-weeds}}
.*

把訪問80的轉向到你的本地代理服務器,若是你弄有wifidog時候,將要一些判斷

iptables -t nat -A PREROUTING -s 0.0.0.0/0.0.0.0 -p tcp --dport 80 -j REDIRECT --to-ports 8118 

若是一些普通的商家路由,此方法沒任何問題,本身寫個C小程序管理下配置等.

 

增長百度推廣 user.action (哎,有錢你們賺....) 其餘首頁推廣相似實現.

{+redirect{?tn=baiduerr}}
www.baidu.com/$
{+redirect{s@tn=\w+@tn=baiduerr@}}
www.baidu.com/s\?

 

配置註釋:

##config
#配置在線手冊
user-manual ./doc/user-manual/
#不信任的網頁
trust-info-url  http://www.example.com/why_we_block.html
#出錯時候管理員的郵箱
admin-address privoxy-admin@example.com
#不可訪問時候的地址
proxy-info-url http://www.example.com/proxy-service.html
#配置目錄
confdir .
#日誌目錄
logdir .
#模板目錄
templdir .
#全部動做
actionsfile match-all.action
#基於網址過濾(系統帶,過濾廣告地址)
actionsfile default.action
#基於網址過濾(自定義)
actionsfile user.action
#基於內容過濾(系統帶,去除一些網站廣告內容)
filterfile default.filter
#基於內容過濾(自定義) 
filterfile user.filter
#日誌文件
logfile privoxy.log
#日誌等級
debug 1
#監聽的地址
listen-address 127.0.0.1:8118
#啓用內容切換
toggle 1
#是否能夠網頁上開啓內容切換
enable-remote-toggle 0
#內容切換後是否使用特殊的HTTP頭
enable-remote-http-toggle 0
#是否能夠在網頁上編輯網絡行爲
enable-edit-actions 0
#被禁止的頁面是否能夠被用戶解封
enforce-blocks 0
#緩存大小
buffer-limit 4096
#代理驗證
enable-proxy-authentication-forwarding 0
forwarded-connect-retries 0
#
accept-intercepted-requests 1
allow-cgi-request-crunching 0
split-large-forms 0
#活動連接超時
keep-alive-timeout 5
#流水線送達,訪問頁面不正常禁用
tolerate-pipelining 1
#SOCKET 超時
socket-timeout 300
相關文章
相關標籤/搜索