UNPKG

6.71 kBJavaScriptView Raw
1var isUtf8 = require('is-utf8');
2var debug = require('debug')('hosts');
3var fs = require('fs');
4var util = require('util');
5var _ = require('underscore');
6
7/*
8 * host文件中clam管理的区块
9 */
10var beginTag = '################ clam config start ################';
11var endTag = '################ clam config end ################';
12/*
13 * 读写 HOSTS 文件,获取换行符格式
14 */
15var hostFile = (function () {
16 var isWin = !!process.platform.match(/^win/);
17 var split_char = isWin ? '\r\n' : '\n';
18 var charset ;
19 var get_file = function () {
20 var file_path;
21 if (isWin) {
22 file_path = 'c:\\windows\\system32\\drivers\\etc\\hosts';
23 } else {
24 file_path = '/etc/hosts';
25 }
26 if (fs.existsSync(file_path)) {
27 return file_path;
28 } else {
29 return false;
30 }
31 };
32 charset = isUtf8(fs.readFileSync(get_file())) ? 'utf-8' : 'gbk';
33 return {
34 get : function () {
35 return fs.readFileSync(get_file(), charset);
36 },
37 set : function (str) {
38 if (isWin) {
39 str = str.replace(/([^\r])\n/g, "$1\r\n");
40 }
41 debug(get_file());
42 fs.writeFileSync(get_file(), str, charset);
43 },
44 split_char: split_char
45 }
46})();
47
48var NodeHostManager = (function () {
49 var split_char = hostFile.split_char;
50 return (function () {
51 try{
52 var ip_regx = /^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/;
53 // copy from http://forums.intermapper.com/viewtopic.php?t=452
54 var ip6_regx = /^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?$/;
55
56 var lines = [];
57 var hosts = {};
58
59 var cur_host_content = "";
60
61 var parse_host = function () {
62
63 lines = [];
64 hosts = {};
65 //read
66 var host = hostFile.get();
67 var areaReg = new RegExp(beginTag+'[\\s\\S]*?'+endTag+'\\n\\r?', 'g');
68 host = host.replace(areaReg, '');
69 cur_host_content = host;
70
71 if (host && host.charAt(host.length - 1) != "\n") { //fix no lf
72 host += split_char;
73 }
74
75 var l_p = 0; //pointer to line
76 var regx = /(.*?)\r?\n/mg;
77
78 while (true) {
79 var l = regx.exec(host);
80 if (!l) {
81 break;
82 }
83 l = l[0];
84
85 lines[l_p++] = l;
86
87 l = l.replace(/^(\s*#)+/, "#");
88 l = l.replace(/#/g, " # ");
89 l = l.replace(/^\s+|\s+$/g, "");
90 l = l.replace(/\s+/g, " ");
91 var tks = l.split(" ");
92 var ip;
93 if (ip_regx.test(tks[0]) || ip6_regx.test(tks[0])) {
94 ip = tks[0];
95 tks.splice(0, 1);
96 tks.forEach(function (item,i){
97 if (!hosts[item]) {
98 hosts[item] = {
99 ip : ip,
100 line_no: l_p
101 }
102 }
103 });
104 }
105 }
106 };
107 parse_host();
108 var line_disable = function (host_name) {
109 if (hosts[host_name]) {
110 lines[hosts[host_name].line_no - 1] = '# ' + lines[hosts[host_name].line_no-1]
111 }
112 };
113 /**
114 * 设置项目HOST
115 * @param pro_hosts {array} example: ['127.0.0.1 farm1.staticflickr.com a.tbcdn.cn', '127.0.0.1 kezhan.trip.taobao.com bb.aa.com']
116 */
117 var set_project_host = function (pro_hosts) {
118 if(_.isString(pro_hosts)){
119 pro_hosts = pro_hosts.split(/\n\r?/);
120 }
121 parse_host();
122 lines.push(beginTag + split_char);
123 pro_hosts.forEach(function (item, i) {
124 var ip ;
125 item = item.replace(/^\s+|\s+$/g, '');
126 item = item.replace(/\s+/g, ' ');
127 item = item.split(' ');
128 if (ip_regx.test(item[0]) || ip6_regx.test[item[0]]) {
129 ip = item[0];
130 item.splice(0,1);
131 item.forEach(function (_name, _i) {
132 line_disable(_name);
133 });
134 lines.push(ip + ' ' + item.join(' ') + split_char);
135 }
136 });
137 lines.push(endTag + split_char);
138
139 hostFile.set(lines.join(''));
140 };
141
142 /**
143 * 恢复HOST并存储
144 */
145 var restore = function () {
146 var host = hostFile.get();
147 var areaReg = new RegExp(beginTag+'[\\s\\S]*?'+endTag+'\\r?\\n?', 'g');
148 host = host.replace(areaReg, '');
149 hostFile.set(host);
150 };
151 return {
152 setHosts : set_project_host,//设置项目HOST
153 restore : restore //恢复原始HOST配置
154 };
155 }catch(e){
156 return {
157 setHosts : function(){},//设置项目HOST
158 restore : function(){} //恢复原始HOST配置
159 };
160 }
161
162 })();
163})();
164
165//test
166//NodeHostManager.restore();
167exports = module.exports = NodeHostManager;
\No newline at end of file