├── Dockerfile ├── README.md ├── app.js └── package.json /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:latest 2 | MAINTAINER malaohu 3 | RUN apt-get clean all 4 | RUN apt-get update 5 | RUN apt-get -y install git 6 | RUN git clone https://github.com/malaohu/Merge-Public-Hosts.git /app 7 | WORKDIR /app 8 | RUN npm install 9 | EXPOSE 3000 10 | CMD npm start 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Merge-Public-Hosts 2 | 合并公共的HOSTS 3 | 4 | # 部署指南 5 | http://51.ruyo.net/p/4252.html 6 | 7 | # 详细说明 8 | * AD(vokins):https://raw.githubusercontent.com/vokins/yhosts/master/hosts 9 | * play : https://raw.githubusercontent.com/sy618/hosts/master/p 10 | * YouTube : https://raw.githubusercontent.com/sy618/hosts/master/y 11 | * fq(racaljk):https://raw.githubusercontent.com/racaljk/hosts/master/hosts 12 | 公共项目搭配,订阅以上四个就能得到最新最全的hosts!适合大众使用。 13 | 14 | 15 | 参考自:https://github.com/sy618/hosts/blob/master/md/hosts%E8%AE%A2%E9%98%85%E6%96%B9%E6%A1%88.md 16 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const request = require('request'); 3 | const async = require('async'); 4 | const cache = require('memory-cache'); 5 | 6 | const cache_key = 'RUYO'; 7 | const cache_timeout = 1000 * 60 * 10; 8 | const hostname = '0.0.0.0'; 9 | const port = 3000; 10 | const source = [ 11 | 'https://raw.githubusercontent.com/vokins/yhosts/master/hosts', 12 | 'https://raw.githubusercontent.com/sy618/hosts/master/p', 13 | 'https://raw.githubusercontent.com/sy618/hosts/master/y', 14 | 'https://raw.githubusercontent.com/racaljk/hosts/master/hosts' 15 | ]; 16 | const server = http.createServer((req, res) => { 17 | build(source,function(err,data){ 18 | res.statusCode = 200; 19 | res.setHeader('Content-Type', 'application/json'); 20 | let ccc = '#total : ' + data.length 21 | + '\n' 22 | + '#create time : ' + new Date() 23 | + '\n' 24 | + '#how to use : http://51.ruyo.net/p/988.html' 25 | + '\n' 26 | + '########################### HOSTS ###########################' 27 | + '\n' 28 | + '#' + source.join('\n#') 29 | + '\n' 30 | + '\n'; 31 | res.end(ccc + data.join('\n')); 32 | }); 33 | }); 34 | 35 | server.listen(port, hostname, () => { 36 | console.log(`服务器运行在 http://${hostname}:${port}/`); 37 | }); 38 | 39 | 40 | function requestit(url,callback) 41 | { 42 | request(url, function (error, response, body) { 43 | if (!error && response.statusCode == 200) { 44 | callback(null,body); 45 | } 46 | else 47 | callback('none thing') 48 | }) 49 | } 50 | 51 | function build(source,callback) 52 | { 53 | let arr = cache.get(cache_key); 54 | if(arr && arr.length > 0){ 55 | console.log('get data from cache'); 56 | return callback(null,arr); 57 | } 58 | 59 | let result = {}; 60 | async.eachSeries(source,function(url,cb){ 61 | requestit(url,function(err,data){ 62 | //剔除注释内容,空行 63 | data = data.replace(/#[^\n]+$/mg,'').replace(/\n[\s]+/mg,'').split('\n'); 64 | console.log(data.length) 65 | for(let i = 0; i < data.length; i++) 66 | { 67 | _data = data[i].split(/\s+/); 68 | if(_data.length > 1){ 69 | //console.log(_data[1] + ' -> ' + _data[0]) 70 | result[_data[1]] = _data[0]; 71 | } 72 | } 73 | cb(); 74 | }) 75 | },function(err){ 76 | //将JSON转成数组 77 | let arr = []; 78 | for(let o in result) 79 | arr.push(result[o] + ' ' + o); 80 | cache.put(cache_key, arr, cache_timeout); 81 | console.log('get data from api'); 82 | callback(err,arr); 83 | }); 84 | } 85 | 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Merge-Public-Hosts", 3 | "version": "0.0.1", 4 | "author": "malaohu", 5 | "license": "GPL-2.0", 6 | "description": "merge hosts", 7 | "main": "app.js", 8 | "scripts": { 9 | "start": "node ./app.js", 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "engines": { 13 | "node": ">=4.0.0" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/malaohu/Merge-Public-Hosts.git" 18 | }, 19 | "dependencies": { 20 | "request":"*", 21 | "async":"*", 22 | "memory-cache":"*" 23 | } 24 | } 25 | --------------------------------------------------------------------------------