File tree 8 files changed +415
-1
lines changed
8 files changed +415
-1
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "parser": "babel-eslint",
3
+ "env": {
4
+ "node": true,
5
+ "mocha": true
6
+ },
7
+ "rules": {
8
+ "strict": [0],
9
+ "eqeqeq": 2,
10
+ "quotes": [2, "single"],
11
+ "no-underscore-dangle": 0,
12
+ "eol-last": 0,
13
+ "camelcase": 0,
14
+ "no-loop-func": 0,
15
+ "no-trailing-spaces": 0,
16
+ "consistent-return": 0,
17
+ "new-cap": 0,
18
+ "no-shadow": 0,
19
+ //"semi": 0,
20
+ "no-process-exit": 0,
21
+ "no-empty": 0,
22
+ "yoda": 0,
23
+ "no-new-func": 0
24
+ }
25
+ }
Original file line number Diff line number Diff line change @@ -25,3 +25,5 @@ build/Release
25
25
# Dependency directory
26
26
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27
27
node_modules
28
+
29
+ lib /
Original file line number Diff line number Diff line change
1
+ node_modules /
2
+ coverage /
Original file line number Diff line number Diff line change
1
+ language : node_js
2
+ node_js :
3
+ - ' 4'
4
+ sudo : false
5
+ script :
6
+ - " npm run test-cov"
7
+ after_script : " npm install coveralls && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
Original file line number Diff line number Diff line change 1
1
# think-ip-filter
2
2
3
- ip-filter for ThinkJS 2.0
3
+ ip-filter middleware for ThinkJS 2.0 support blackList & whiteList.
4
+
5
+ ## Install
6
+
7
+ ``` sh
8
+ npm install think-ip-filter --save
9
+ ```
10
+
11
+ ## How to use
12
+
13
+ ### register middleware
14
+
15
+ create file if not exist, ` src/common/bootstrap/middleware.js ` .
16
+
17
+ ``` js
18
+ import ipFilter from ' think-ip-filter' ;
19
+ think .middleware (' ip_filter' , ipFilter);
20
+ ```
21
+
22
+ ### config hook
23
+
24
+ create file if not exist, ` src/common/config/hook.js ` .
25
+
26
+ ``` js
27
+ export default {
28
+ request_begin: [' prepend' , ' ip_filter' ]
29
+ }
30
+ ```
31
+
32
+ ### config ip whiteList or blackList
33
+
34
+ add ` ip_filter ` config in file ` src/common/config/config.js ` .
35
+
36
+ ** black list**
37
+
38
+ ``` js
39
+ export default {
40
+ ip_filter: [' 111.222.333.444' , ' 120.*' ]
41
+ }
42
+ ```
43
+
44
+ ** white list**
45
+
46
+ ``` js
47
+ export default {
48
+ ip_filter: {
49
+ whiteList: [' 123.222.122.*' ]
50
+ }
51
+ }
52
+ ```
53
+
54
+ ** dynamic load**
55
+
56
+ dynamic load by function, must be return Promise.
57
+
58
+ ``` js
59
+ export default {
60
+ ip_filter : function (){
61
+ // such as: get from db
62
+ return Promise .resolve ([' 111.*' ]);
63
+ }
64
+ }
65
+ ```
66
+
67
+ ## LICENSE
68
+
69
+ MIT
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " think-ip-filter" ,
3
+ "description" : " ip filter middleware for ThinkJS 2.0, support whitelist & blacklist" ,
4
+ "version" : " 1.0.0" ,
5
+ "author" : {
6
+ "name" : " welefen" ,
7
+
8
+ },
9
+ "scripts" : {
10
+ "test" : " mocha --reporter spec --timeout 5000 --recursive test/" ,
11
+ "test-cov" : " istanbul cover --report html ./node_modules/mocha/bin/_mocha -- -t 5000 --recursive -R spec test/" ,
12
+ "compile" : " babel --loose all --optional runtime --stage 0 --modules common src/ --out-dir lib/" ,
13
+ "watch-compile" : " npm run compile -- --watch" ,
14
+ "prepublish" : " npm run compile" ,
15
+ "eslint" : " eslint src/"
16
+ },
17
+ "contributors" : [{
18
+ "name" : " welefen" ,
19
+
20
+ }],
21
+ "main" : " lib/index.js" ,
22
+ "dependencies" : {},
23
+ "devDependencies" : {
24
+ "mocha" : " 1.20.1" ,
25
+ "istanbul" : " 0.4.0" ,
26
+ "babel" : " 5.8.23" ,
27
+ "thinkjs" : " 2.x.x" ,
28
+ "eslint" : " 1.8.0" ,
29
+ "babel-eslint" : " 4.1.3"
30
+ },
31
+ "keywords" : [
32
+ " thinkjs" ,
33
+ " ip-filter" ,
34
+ " ipfilter"
35
+ ],
36
+ "repository" : {
37
+ "type" : " git" ,
38
+ "url" : " https://github.com/welefen/think-ip-filter"
39
+ },
40
+ "engines" : {
41
+ "node" : " >=0.12.0"
42
+ },
43
+ "license" : " MIT" ,
44
+ "readmeFilename" : " README.md" ,
45
+ "bugs" : {
46
+ "url" : " https://github.com/welefen/think-ip-filter/issues"
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ /**
3
+ * ip filter, support blacklist & whitelist
4
+ */
5
+ export default class extends think . middleware . base {
6
+ /**
7
+ * check ip is white
8
+ * @return {Boolean } []
9
+ */
10
+ checkIp ( ip , ipList ) {
11
+ if ( ! think . isArray ( ipList ) ) {
12
+ ipList = [ ipList ] ;
13
+ }
14
+ let ips = ip . split ( '.' ) ;
15
+ let flag = ipList . some ( item => {
16
+ if ( think . isRegExp ( item ) ) {
17
+ return item . test ( ip ) ;
18
+ }
19
+ return item . split ( '.' ) . every ( ( num , i ) => {
20
+ if ( num === '*' || num === ips [ i ] ) {
21
+ return true ;
22
+ }
23
+ } ) ;
24
+ } ) ;
25
+ return flag ;
26
+ }
27
+ /**
28
+ * show error
29
+ * @return {Promise } []
30
+ */
31
+ showError ( ) {
32
+ this . http . error = new Error ( 'ip is not allowed' ) ;
33
+ return think . statusAction ( 403 , this . http ) ;
34
+ }
35
+ /**
36
+ * run
37
+ * @return {Promise } []
38
+ */
39
+ async run ( ) {
40
+
41
+ let config = this . config ( 'ip_filter' ) ;
42
+ if ( ! config ) {
43
+ return ;
44
+ }
45
+
46
+ //get config by dynamic
47
+ if ( think . isFunction ( config ) ) {
48
+ config = await think . co . wrap ( config ) ( this . http ) || [ ] ;
49
+ }
50
+
51
+ //only blackList
52
+ if ( think . isArray ( config ) ) {
53
+ config = { blackList : config } ;
54
+ }
55
+
56
+ let ip = this . http . ip ( ) ;
57
+ if ( ! think . isEmpty ( config . whiteList ) ) {
58
+ let flag = this . checkIp ( ip , config . whiteList ) ;
59
+ if ( ! flag ) {
60
+ return this . showError ( ) ;
61
+ }
62
+ return ;
63
+ }
64
+
65
+ if ( ! think . isEmpty ( config . blackList ) ) {
66
+ let flag = this . checkIp ( ip , config . blackList ) ;
67
+ if ( flag ) {
68
+ return this . showError ( ) ;
69
+ }
70
+ }
71
+ }
72
+ }
You can’t perform that action at this time.
0 commit comments