mm_os 2.8.8 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/base/index.js +28 -28
- package/core/base/mqtt/index.js +314 -314
- package/core/base/mqtt/lib.js +39 -39
- package/core/base/web/index.js +109 -109
- package/core/com/mqtt/index.js +2 -6
- package/demo/index.js +28 -0
- package/middleware/cors/index.js +84 -84
- package/middleware/cors/middleware.json +6 -6
- package/middleware/log/index.js +20 -20
- package/middleware/log/middleware.json +8 -8
- package/middleware/mqtt_base/index.js +10 -10
- package/middleware/mqtt_base/middleware.json +7 -7
- package/middleware/waf/index.js +64 -64
- package/middleware/waf/middleware.json +8 -8
- package/middleware/waf_ip/index.js +115 -115
- package/middleware/waf_ip/middleware.json +9 -9
- package/middleware/web_base/index.js +65 -65
- package/middleware/web_base/middleware.json +8 -8
- package/middleware/web_event/index.js +412 -412
- package/middleware/web_event/middleware.json +9 -9
- package/middleware/web_proxy/index.js +23 -23
- package/middleware/web_proxy/middleware.json +8 -8
- package/middleware/web_router/index.js +33 -33
- package/middleware/web_router/middleware.json +9 -9
- package/middleware/web_socket/index.js +21 -21
- package/middleware/web_socket/middleware.json +8 -8
- package/middleware/web_static/index.js +25 -25
- package/middleware/web_static/middleware.json +8 -8
- package/package.json +4 -4
- package/static/file/image/user/42/2025_01_02_10_58_15.png +0 -0
package/core/base/mqtt/lib.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
var urilib = require('url');
|
|
2
|
-
var scanSchema = require('jsonschema/lib/scan').scan;
|
|
3
|
-
var Validator = require("jsonschema/lib/validator.js");
|
|
4
|
-
var helpers = require('jsonschema/lib/helpers');
|
|
5
|
-
var SchemaContext = helpers.SchemaContext;
|
|
6
|
-
var anonymousBase = '/';
|
|
7
|
-
|
|
8
|
-
// 重构校验函数,解决error问题
|
|
9
|
-
Validator.prototype.validate = function validate(instance, schema, options, ctx) {
|
|
10
|
-
if (!options) {
|
|
11
|
-
options = {};
|
|
12
|
-
}
|
|
13
|
-
// This section indexes subschemas in the provided schema, so they don't need to be added with Validator#addSchema
|
|
14
|
-
// This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI
|
|
15
|
-
var id = schema.$id || schema.id;
|
|
16
|
-
var base = urilib.resolve(options.base || anonymousBase, id || '');
|
|
17
|
-
if (!ctx) {
|
|
18
|
-
ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas));
|
|
19
|
-
if (!ctx.schemas[base]) {
|
|
20
|
-
ctx.schemas[base] = schema;
|
|
21
|
-
}
|
|
22
|
-
var found = scanSchema(base, schema);
|
|
23
|
-
for (var n in found.id) {
|
|
24
|
-
var sch = found.id[n];
|
|
25
|
-
ctx.schemas[n] = sch;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
if (options.required && instance === undefined) {
|
|
29
|
-
var result = new ValidatorResult(instance, schema, options, ctx);
|
|
30
|
-
result.addError('is required, but is undefined');
|
|
31
|
-
return result;
|
|
32
|
-
}
|
|
33
|
-
var result = this.validateSchema(instance, schema, options, ctx);
|
|
34
|
-
if (!result) {
|
|
35
|
-
throw new Error('Result undefined');
|
|
36
|
-
} else if (options.throwAll && result.errors.length) {
|
|
37
|
-
throw new ValidatorResultError(result);
|
|
38
|
-
}
|
|
39
|
-
return result;
|
|
1
|
+
var urilib = require('url');
|
|
2
|
+
var scanSchema = require('jsonschema/lib/scan').scan;
|
|
3
|
+
var Validator = require("jsonschema/lib/validator.js");
|
|
4
|
+
var helpers = require('jsonschema/lib/helpers');
|
|
5
|
+
var SchemaContext = helpers.SchemaContext;
|
|
6
|
+
var anonymousBase = '/';
|
|
7
|
+
|
|
8
|
+
// 重构校验函数,解决error问题
|
|
9
|
+
Validator.prototype.validate = function validate(instance, schema, options, ctx) {
|
|
10
|
+
if (!options) {
|
|
11
|
+
options = {};
|
|
12
|
+
}
|
|
13
|
+
// This section indexes subschemas in the provided schema, so they don't need to be added with Validator#addSchema
|
|
14
|
+
// This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI
|
|
15
|
+
var id = schema.$id || schema.id;
|
|
16
|
+
var base = urilib.resolve(options.base || anonymousBase, id || '');
|
|
17
|
+
if (!ctx) {
|
|
18
|
+
ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas));
|
|
19
|
+
if (!ctx.schemas[base]) {
|
|
20
|
+
ctx.schemas[base] = schema;
|
|
21
|
+
}
|
|
22
|
+
var found = scanSchema(base, schema);
|
|
23
|
+
for (var n in found.id) {
|
|
24
|
+
var sch = found.id[n];
|
|
25
|
+
ctx.schemas[n] = sch;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (options.required && instance === undefined) {
|
|
29
|
+
var result = new ValidatorResult(instance, schema, options, ctx);
|
|
30
|
+
result.addError('is required, but is undefined');
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
var result = this.validateSchema(instance, schema, options, ctx);
|
|
34
|
+
if (!result) {
|
|
35
|
+
throw new Error('Result undefined');
|
|
36
|
+
} else if (options.throwAll && result.errors.length) {
|
|
37
|
+
throw new ValidatorResultError(result);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
40
|
};
|
package/core/base/web/index.js
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
const Koa = require('koa');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Web http 通讯类
|
|
5
|
-
*/
|
|
6
|
-
class WEB {
|
|
7
|
-
/**
|
|
8
|
-
* 构造函数
|
|
9
|
-
* @param {Object} config 配置参数
|
|
10
|
-
*/
|
|
11
|
-
constructor(config) {
|
|
12
|
-
/**
|
|
13
|
-
* 配置参数
|
|
14
|
-
*/
|
|
15
|
-
this.config = Object.assign({
|
|
16
|
-
// 访问地址
|
|
17
|
-
"host": "0.0.0.0",
|
|
18
|
-
// 访问端口号
|
|
19
|
-
"port": 5000,
|
|
20
|
-
// 是否启用websocket
|
|
21
|
-
"socket": true,
|
|
22
|
-
// 是否启用压缩
|
|
23
|
-
"compress": true,
|
|
24
|
-
// 是否启用事件
|
|
25
|
-
"event": true,
|
|
26
|
-
// 是否启用事件
|
|
27
|
-
"log": true,
|
|
28
|
-
// 是否启用静态文件
|
|
29
|
-
"static": true,
|
|
30
|
-
// 静态文件路径
|
|
31
|
-
"static_path": "./static".fullname($.runPath),
|
|
32
|
-
// 代理转发
|
|
33
|
-
"proxy": {}
|
|
34
|
-
}, config);
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* web服务器
|
|
38
|
-
*/
|
|
39
|
-
this.server = null;
|
|
40
|
-
this.list = [];
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 初始化
|
|
46
|
-
* @param {Object} config
|
|
47
|
-
*/
|
|
48
|
-
WEB.prototype.init = function(config) {
|
|
49
|
-
if (config) {
|
|
50
|
-
this.config = Object.assign(this.config, config);
|
|
51
|
-
}
|
|
52
|
-
this.server = new Koa();
|
|
53
|
-
return this;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 引用
|
|
58
|
-
* @param {Function} 函数
|
|
59
|
-
*/
|
|
60
|
-
WEB.prototype.use = function(func) {
|
|
61
|
-
this.server.use(func);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 运行主程序
|
|
66
|
-
* @param {String} state 状态
|
|
67
|
-
*/
|
|
68
|
-
WEB.prototype.main = function(state) {
|
|
69
|
-
var cg = this.config;
|
|
70
|
-
var host = cg.host;
|
|
71
|
-
if (host == '0.0.0.0') {
|
|
72
|
-
host = '127.0.0.1'
|
|
73
|
-
}
|
|
74
|
-
this.server.listen(cg.port, cg.host, () => {
|
|
75
|
-
console.info(`HTTP访问 http://${host}:${cg.port}`);
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 运行主程序前
|
|
81
|
-
* @param {String} state 状态
|
|
82
|
-
*/
|
|
83
|
-
WEB.prototype.before = async function(state) {
|
|
84
|
-
var list = this.list;
|
|
85
|
-
for (var i = 0; i < list.length; i++) {
|
|
86
|
-
var o = list[i];
|
|
87
|
-
o.func = require(o.func_file);
|
|
88
|
-
if (o.func) {
|
|
89
|
-
o.func(this.server, this.config);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 运行主程序后
|
|
96
|
-
* @param {String} state 状态
|
|
97
|
-
*/
|
|
98
|
-
WEB.prototype.after = async function(state) {};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 运行
|
|
102
|
-
* @param {String} state 状态
|
|
103
|
-
*/
|
|
104
|
-
WEB.prototype.run = async function(state = 'start') {
|
|
105
|
-
await this.before(state);
|
|
106
|
-
await this.main(state);
|
|
107
|
-
await this.after(state);
|
|
108
|
-
};
|
|
109
|
-
|
|
1
|
+
const Koa = require('koa');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Web http 通讯类
|
|
5
|
+
*/
|
|
6
|
+
class WEB {
|
|
7
|
+
/**
|
|
8
|
+
* 构造函数
|
|
9
|
+
* @param {Object} config 配置参数
|
|
10
|
+
*/
|
|
11
|
+
constructor(config) {
|
|
12
|
+
/**
|
|
13
|
+
* 配置参数
|
|
14
|
+
*/
|
|
15
|
+
this.config = Object.assign({
|
|
16
|
+
// 访问地址
|
|
17
|
+
"host": "0.0.0.0",
|
|
18
|
+
// 访问端口号
|
|
19
|
+
"port": 5000,
|
|
20
|
+
// 是否启用websocket
|
|
21
|
+
"socket": true,
|
|
22
|
+
// 是否启用压缩
|
|
23
|
+
"compress": true,
|
|
24
|
+
// 是否启用事件
|
|
25
|
+
"event": true,
|
|
26
|
+
// 是否启用事件
|
|
27
|
+
"log": true,
|
|
28
|
+
// 是否启用静态文件
|
|
29
|
+
"static": true,
|
|
30
|
+
// 静态文件路径
|
|
31
|
+
"static_path": "./static".fullname($.runPath),
|
|
32
|
+
// 代理转发
|
|
33
|
+
"proxy": {}
|
|
34
|
+
}, config);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* web服务器
|
|
38
|
+
*/
|
|
39
|
+
this.server = null;
|
|
40
|
+
this.list = [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 初始化
|
|
46
|
+
* @param {Object} config
|
|
47
|
+
*/
|
|
48
|
+
WEB.prototype.init = function(config) {
|
|
49
|
+
if (config) {
|
|
50
|
+
this.config = Object.assign(this.config, config);
|
|
51
|
+
}
|
|
52
|
+
this.server = new Koa();
|
|
53
|
+
return this;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 引用
|
|
58
|
+
* @param {Function} 函数
|
|
59
|
+
*/
|
|
60
|
+
WEB.prototype.use = function(func) {
|
|
61
|
+
this.server.use(func);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 运行主程序
|
|
66
|
+
* @param {String} state 状态
|
|
67
|
+
*/
|
|
68
|
+
WEB.prototype.main = function(state) {
|
|
69
|
+
var cg = this.config;
|
|
70
|
+
var host = cg.host;
|
|
71
|
+
if (host == '0.0.0.0') {
|
|
72
|
+
host = '127.0.0.1'
|
|
73
|
+
}
|
|
74
|
+
this.server.listen(cg.port, cg.host, () => {
|
|
75
|
+
console.info(`HTTP访问 http://${host}:${cg.port}`);
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 运行主程序前
|
|
81
|
+
* @param {String} state 状态
|
|
82
|
+
*/
|
|
83
|
+
WEB.prototype.before = async function(state) {
|
|
84
|
+
var list = this.list;
|
|
85
|
+
for (var i = 0; i < list.length; i++) {
|
|
86
|
+
var o = list[i];
|
|
87
|
+
o.func = require(o.func_file);
|
|
88
|
+
if (o.func) {
|
|
89
|
+
o.func(this.server, this.config);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 运行主程序后
|
|
96
|
+
* @param {String} state 状态
|
|
97
|
+
*/
|
|
98
|
+
WEB.prototype.after = async function(state) {};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 运行
|
|
102
|
+
* @param {String} state 状态
|
|
103
|
+
*/
|
|
104
|
+
WEB.prototype.run = async function(state = 'start') {
|
|
105
|
+
await this.before(state);
|
|
106
|
+
await this.main(state);
|
|
107
|
+
await this.after(state);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
110
|
module.exports = WEB;
|
package/core/com/mqtt/index.js
CHANGED
|
@@ -426,9 +426,7 @@ MQTT.prototype.save_online = async function(arr, online = 1) {
|
|
|
426
426
|
var body = {
|
|
427
427
|
online
|
|
428
428
|
}
|
|
429
|
-
var
|
|
430
|
-
sql.open();
|
|
431
|
-
var db = sql.db();
|
|
429
|
+
var db = $.sql.db();
|
|
432
430
|
db.table = this.config.table || "iot_device";
|
|
433
431
|
db.size = 0;
|
|
434
432
|
var query = {};
|
|
@@ -442,9 +440,7 @@ MQTT.prototype.save_online = async function(arr, online = 1) {
|
|
|
442
440
|
* 获取所有设备
|
|
443
441
|
*/
|
|
444
442
|
MQTT.prototype.get_drives = async function() {
|
|
445
|
-
var
|
|
446
|
-
sql.open();
|
|
447
|
-
var db = sql.db();
|
|
443
|
+
var db = $.sql.db();
|
|
448
444
|
db.table = this.config.table || "iot_device";
|
|
449
445
|
db.size = 0;
|
|
450
446
|
return await db.get({}, "", "clientid, online");
|
package/demo/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require("mm_expand");
|
|
2
|
+
$.runPath = __dirname + $.slash;
|
|
3
|
+
const OS = require("../index.js");
|
|
4
|
+
|
|
5
|
+
var NODE_ENV = process.env.NODE_ENV || 'local';
|
|
6
|
+
// 避免掉线
|
|
7
|
+
if (NODE_ENV == 'local') {
|
|
8
|
+
process.on('uncaughtException', (err) => {
|
|
9
|
+
console.error('捕获到未处理的异常:', err);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
var config = `/config/${NODE_ENV}.json`.loadJson();
|
|
14
|
+
if (config) {
|
|
15
|
+
var package = "/package.json".loadJson();
|
|
16
|
+
config.name = "mm_os";
|
|
17
|
+
config.version = package.dependencies["mm_os"];
|
|
18
|
+
}
|
|
19
|
+
var os = new OS(config);
|
|
20
|
+
|
|
21
|
+
if ("/config/lock.cache".hasFile()) {
|
|
22
|
+
$.isReady = true;
|
|
23
|
+
} else {
|
|
24
|
+
$.isReady = false;
|
|
25
|
+
console.log("初次访问");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
os.run();
|
package/middleware/cors/index.js
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 应用
|
|
3
|
-
* @param {Object} server 服务
|
|
4
|
-
* @param {Object} config 配置参数
|
|
5
|
-
*/
|
|
6
|
-
module.exports = function(server, config) {
|
|
7
|
-
var cos = config.cos;
|
|
8
|
-
if (cos && cos.status) {
|
|
9
|
-
if (cos.headers && cos.origin !== "*") {
|
|
10
|
-
/* 跨域限制(web防火墙) */
|
|
11
|
-
server.use(async (ctx, next) => {
|
|
12
|
-
var req = ctx.request;
|
|
13
|
-
|
|
14
|
-
// 允许来自所有域名请求
|
|
15
|
-
ctx.set('Access-Control-Allow-Origin', cos.origin);
|
|
16
|
-
// 这样就能只允许 http://localhost:8080 这个域名的请求了
|
|
17
|
-
// ctx.set("Access-Control-Allow-Origin", "http://localhost:8080");
|
|
18
|
-
|
|
19
|
-
// 字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段.
|
|
20
|
-
if (req.method == "OPTIONS") {
|
|
21
|
-
ctx.set('Access-Control-Allow-Headers', cos.headers);
|
|
22
|
-
|
|
23
|
-
// 服务器收到请求以后,检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后,确认允许跨源请求,就可以做出回应。
|
|
24
|
-
// Content-Type表示具体请求中的媒体类型信息
|
|
25
|
-
// ctx.set("Content-Type", "application/json;charset=utf-8");
|
|
26
|
-
// 设置所允许的HTTP请求方法PUT,POST,GET,DELETE,HEAD,OPTIONS
|
|
27
|
-
// ctx.set('Access-Control-Allow-Methods', 'GET,POST');
|
|
28
|
-
|
|
29
|
-
// 该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。
|
|
30
|
-
// 当设置成允许请求携带cookie时,需要保证"Access-Control-Allow-Origin"是服务器有的域名,而不能是"*";
|
|
31
|
-
// ctx.set("Access-Control-Allow-Credentials", 'false');
|
|
32
|
-
|
|
33
|
-
// 该字段可选,用来指定本次预检请求的有效期,单位为秒。
|
|
34
|
-
// 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时,服务器会提前发送一次请求进行验证
|
|
35
|
-
// 下面的的设置只本次验证的有效时间,即在该时间段内服务端可以不用进行验证
|
|
36
|
-
// ctx.set("Access-Control-Max-Age", '3600');
|
|
37
|
-
/*
|
|
38
|
-
CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:
|
|
39
|
-
Cache-Control、
|
|
40
|
-
Content-Language、
|
|
41
|
-
Content-Type、
|
|
42
|
-
Expires、
|
|
43
|
-
Last-Modified、
|
|
44
|
-
Pragma
|
|
45
|
-
*/
|
|
46
|
-
// 需要获取其他字段时,使用Access-Control-Expose-Headers,
|
|
47
|
-
// getResponseHeader('myData')可以返回我们所需的值
|
|
48
|
-
// ctx.set("Access-Control-Expose-Headers", "myData");
|
|
49
|
-
ctx.status = 204;
|
|
50
|
-
} else {
|
|
51
|
-
await next();
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
} else if (cos.headers && cos.headers !== "*") {
|
|
55
|
-
/* 跨域限制(web防火墙) */
|
|
56
|
-
server.use(async (ctx, next) => {
|
|
57
|
-
var req = ctx.request;
|
|
58
|
-
// 允许来自所有域名请求
|
|
59
|
-
ctx.set('Access-Control-Allow-Origin', '*');
|
|
60
|
-
// ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
|
|
61
|
-
if (req.method === 'OPTIONS') {
|
|
62
|
-
ctx.set('Access-Control-Allow-Headers', cos.headers);
|
|
63
|
-
ctx.status = 204;
|
|
64
|
-
} else {
|
|
65
|
-
await next();
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
} else {
|
|
69
|
-
/* 跨域限制(web防火墙) */
|
|
70
|
-
server.use(async (ctx, next) => {
|
|
71
|
-
var req = ctx.request;
|
|
72
|
-
// 允许来自所有域名请求
|
|
73
|
-
ctx.set('Access-Control-Allow-Origin', '*');
|
|
74
|
-
// ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
|
|
75
|
-
if (req.method === 'OPTIONS') {
|
|
76
|
-
ctx.set('Access-Control-Allow-Headers', '*');
|
|
77
|
-
ctx.status = 204;
|
|
78
|
-
} else {
|
|
79
|
-
await next();
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return server;
|
|
1
|
+
/**
|
|
2
|
+
* 应用
|
|
3
|
+
* @param {Object} server 服务
|
|
4
|
+
* @param {Object} config 配置参数
|
|
5
|
+
*/
|
|
6
|
+
module.exports = function(server, config) {
|
|
7
|
+
var cos = config.cos;
|
|
8
|
+
if (cos && cos.status) {
|
|
9
|
+
if (cos.headers && cos.origin !== "*") {
|
|
10
|
+
/* 跨域限制(web防火墙) */
|
|
11
|
+
server.use(async (ctx, next) => {
|
|
12
|
+
var req = ctx.request;
|
|
13
|
+
|
|
14
|
+
// 允许来自所有域名请求
|
|
15
|
+
ctx.set('Access-Control-Allow-Origin', cos.origin);
|
|
16
|
+
// 这样就能只允许 http://localhost:8080 这个域名的请求了
|
|
17
|
+
// ctx.set("Access-Control-Allow-Origin", "http://localhost:8080");
|
|
18
|
+
|
|
19
|
+
// 字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段.
|
|
20
|
+
if (req.method == "OPTIONS") {
|
|
21
|
+
ctx.set('Access-Control-Allow-Headers', cos.headers);
|
|
22
|
+
|
|
23
|
+
// 服务器收到请求以后,检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后,确认允许跨源请求,就可以做出回应。
|
|
24
|
+
// Content-Type表示具体请求中的媒体类型信息
|
|
25
|
+
// ctx.set("Content-Type", "application/json;charset=utf-8");
|
|
26
|
+
// 设置所允许的HTTP请求方法PUT,POST,GET,DELETE,HEAD,OPTIONS
|
|
27
|
+
// ctx.set('Access-Control-Allow-Methods', 'GET,POST');
|
|
28
|
+
|
|
29
|
+
// 该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。
|
|
30
|
+
// 当设置成允许请求携带cookie时,需要保证"Access-Control-Allow-Origin"是服务器有的域名,而不能是"*";
|
|
31
|
+
// ctx.set("Access-Control-Allow-Credentials", 'false');
|
|
32
|
+
|
|
33
|
+
// 该字段可选,用来指定本次预检请求的有效期,单位为秒。
|
|
34
|
+
// 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时,服务器会提前发送一次请求进行验证
|
|
35
|
+
// 下面的的设置只本次验证的有效时间,即在该时间段内服务端可以不用进行验证
|
|
36
|
+
// ctx.set("Access-Control-Max-Age", '3600');
|
|
37
|
+
/*
|
|
38
|
+
CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:
|
|
39
|
+
Cache-Control、
|
|
40
|
+
Content-Language、
|
|
41
|
+
Content-Type、
|
|
42
|
+
Expires、
|
|
43
|
+
Last-Modified、
|
|
44
|
+
Pragma
|
|
45
|
+
*/
|
|
46
|
+
// 需要获取其他字段时,使用Access-Control-Expose-Headers,
|
|
47
|
+
// getResponseHeader('myData')可以返回我们所需的值
|
|
48
|
+
// ctx.set("Access-Control-Expose-Headers", "myData");
|
|
49
|
+
ctx.status = 204;
|
|
50
|
+
} else {
|
|
51
|
+
await next();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
} else if (cos.headers && cos.headers !== "*") {
|
|
55
|
+
/* 跨域限制(web防火墙) */
|
|
56
|
+
server.use(async (ctx, next) => {
|
|
57
|
+
var req = ctx.request;
|
|
58
|
+
// 允许来自所有域名请求
|
|
59
|
+
ctx.set('Access-Control-Allow-Origin', '*');
|
|
60
|
+
// ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
|
|
61
|
+
if (req.method === 'OPTIONS') {
|
|
62
|
+
ctx.set('Access-Control-Allow-Headers', cos.headers);
|
|
63
|
+
ctx.status = 204;
|
|
64
|
+
} else {
|
|
65
|
+
await next();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
} else {
|
|
69
|
+
/* 跨域限制(web防火墙) */
|
|
70
|
+
server.use(async (ctx, next) => {
|
|
71
|
+
var req = ctx.request;
|
|
72
|
+
// 允许来自所有域名请求
|
|
73
|
+
ctx.set('Access-Control-Allow-Origin', '*');
|
|
74
|
+
// ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
|
|
75
|
+
if (req.method === 'OPTIONS') {
|
|
76
|
+
ctx.set('Access-Control-Allow-Headers', '*');
|
|
77
|
+
ctx.status = 204;
|
|
78
|
+
} else {
|
|
79
|
+
await next();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return server;
|
|
85
85
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "web_cors",
|
|
3
|
-
"title": "web跨域请求",
|
|
4
|
-
"description": "用于跨域请求限制",
|
|
5
|
-
"version": "1.0",
|
|
6
|
-
"sort": 4
|
|
1
|
+
{
|
|
2
|
+
"name": "web_cors",
|
|
3
|
+
"title": "web跨域请求",
|
|
4
|
+
"description": "用于跨域请求限制",
|
|
5
|
+
"version": "1.0",
|
|
6
|
+
"sort": 4
|
|
7
7
|
}
|
package/middleware/log/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 应用
|
|
3
|
-
* @param {Object} server 服务
|
|
4
|
-
* @param {Object} config 配置参数
|
|
5
|
-
*/
|
|
6
|
-
module.exports = function(server, config) {
|
|
7
|
-
/* 跨域限制(web防火墙) */
|
|
8
|
-
server.use(async (ctx, next) => {
|
|
9
|
-
var url = ctx.path + ctx.querystring;
|
|
10
|
-
var body = "";
|
|
11
|
-
if (ctx.request.body) {
|
|
12
|
-
body = JSON.stringify(ctx.request.body);
|
|
13
|
-
if (body.length > 1024) {
|
|
14
|
-
body = body.substring(0, 1024) + "..."
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
$.log.http(`${ctx.method} ${url} ${body}`);
|
|
18
|
-
await next();
|
|
19
|
-
});
|
|
20
|
-
return server;
|
|
1
|
+
/**
|
|
2
|
+
* 应用
|
|
3
|
+
* @param {Object} server 服务
|
|
4
|
+
* @param {Object} config 配置参数
|
|
5
|
+
*/
|
|
6
|
+
module.exports = function(server, config) {
|
|
7
|
+
/* 跨域限制(web防火墙) */
|
|
8
|
+
server.use(async (ctx, next) => {
|
|
9
|
+
var url = ctx.path + ctx.querystring;
|
|
10
|
+
var body = "";
|
|
11
|
+
if (ctx.request.body) {
|
|
12
|
+
body = JSON.stringify(ctx.request.body);
|
|
13
|
+
if (body.length > 1024) {
|
|
14
|
+
body = body.substring(0, 1024) + "..."
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
$.log.http(`${ctx.method} ${url} ${body}`);
|
|
18
|
+
await next();
|
|
19
|
+
});
|
|
20
|
+
return server;
|
|
21
21
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "web_log",
|
|
3
|
-
"title": "web请求日志",
|
|
4
|
-
"description": "用于记录http请求",
|
|
5
|
-
"version": "1.0",
|
|
6
|
-
"type": "web",
|
|
7
|
-
"process_type": "common_before",
|
|
8
|
-
"sort": 13
|
|
1
|
+
{
|
|
2
|
+
"name": "web_log",
|
|
3
|
+
"title": "web请求日志",
|
|
4
|
+
"description": "用于记录http请求",
|
|
5
|
+
"version": "1.0",
|
|
6
|
+
"type": "web",
|
|
7
|
+
"process_type": "common_before",
|
|
8
|
+
"sort": 13
|
|
9
9
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const MQTT = require('mm_mqtt');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 中间件
|
|
5
|
-
* @param {Object} server 服务
|
|
6
|
-
* @param {Object} config 配置参数
|
|
7
|
-
*/
|
|
8
|
-
module.exports = function(server, config) {
|
|
9
|
-
return server;
|
|
10
|
-
};
|
|
1
|
+
const MQTT = require('mm_mqtt');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 中间件
|
|
5
|
+
* @param {Object} server 服务
|
|
6
|
+
* @param {Object} config 配置参数
|
|
7
|
+
*/
|
|
8
|
+
module.exports = function(server, config) {
|
|
9
|
+
return server;
|
|
10
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
{
|
|
2
|
-
"mode": "mqtt",
|
|
3
|
-
"name": "mqtt_base",
|
|
4
|
-
"title": "MQTT通讯器",
|
|
5
|
-
"description": "用于MQTT通讯",
|
|
6
|
-
"version": "1.0",
|
|
7
|
-
"sort": 3
|
|
1
|
+
{
|
|
2
|
+
"mode": "mqtt",
|
|
3
|
+
"name": "mqtt_base",
|
|
4
|
+
"title": "MQTT通讯器",
|
|
5
|
+
"description": "用于MQTT通讯",
|
|
6
|
+
"version": "1.0",
|
|
7
|
+
"sort": 3
|
|
8
8
|
}
|