lyan

  • 首页

  • 标签

  • 分类

  • 归档

  • 一些栗子

https密钥交换过程

发表于 2021-04-22

关键点

  • 使用 非对称加密 处理用于加密数据的密钥

  • 使用 对称加密 处理具体的数据

图解

流程图地址,按需自取

Client
Client
Server
Server
请求publicKey
请求publicKey
发起请求
发起请求
Time Line
Time Line
处理请求返回公钥:publicKey
处理请求返回公钥:publicKey
返回publicKey
返回publicKey
验证公钥合法性
验证公钥合法性
使用CA验证公钥
使用CA验证公钥
Y
Y
N
N
是否合法
是否合法
结束
结束
生成随机密钥:randomKey
生成随机密钥:randomKey
使用publicKey加密randomKey得到:
encryptRandomKey
使用publicKey加密randomKey…
发送encryptRandomKey
发送encryptRan…
获取randomKey
获取randomKey
使用私钥解密
encryptRandomKey
得到:
randomKey
使用私钥解密encryptRandomKey…
使用randomKey加密要传输的业务数据data
使用randomKey加密要传输的业务数据data
解密数据
解密数据
使用randomKey解密密文,得到明文
使用randomKey解密密文,得到明文
发送data
发送data
后续请求使用randomKey加密/解密数据
后续请求使用randomKey加密/解密数据
Request
Request
后续请求使用randomKey加密/解密数据
后续请求使用randomKey加密/解密数据
Response
Response
Viewer does not support full SVG 1.1

流程说明

1.客户端获取公钥

  • 客户端发送请求,服务端返回公钥

  • 客户端用证书验证公钥,如果合法,继续下一步

2.客户端生成用于加密数据的密钥

  • 客户端生成随机密钥
  • 然后用服务器返回的公钥对这个密钥加密,得到加密后的密钥

3.服务端验证密钥

  • 客户端将加密后的密钥发给服务端
  • 服务端使用私钥解密得到密钥

4.使用密钥加密数据进行传输,使用密钥解密数据得到明文

  • 服务端使用获得的密钥加密数据,发送给客户端
  • 客户端使用密钥解密得到明文。

根据末节点找到指定路径

发表于 2020-12-02

在业务开发中,经常会遇到级联的数据结构,比如省市区、公司组织架构等; 如下图中的树形结构,现在要找出

数据结构定义如下

1
2
3
4
interface Node {
value: any;
children?: Node[];
}

递归实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

function findPath(node, v) {
function walk(node, path = []) {
if (!node) {
return path;
}
let { children, value } = node;
if (children && children.length) {
let len = children.length;
for (let i = 0; i < len; i++) {
let n = children[i];
walk(n, path.slice(0));
}
} else {
path.push(value);
if (value === v) {
return path;
}
}
}

}

使用@route装饰器简化koa-router写法

发表于 2019-03-22

前言

本文所有代码见github

koa-router

koa-router是koa项目中被广泛使用到的一个路由中间件。
koa-router的基本使用方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
var Koa = require('koa');
var Router = require('koa-router');

var app = new Koa();
var router = new Router();

router.get('/', (ctx, next) => {
// ctx.router available
});
// 在app中使用routes中间件
app
.use(router.routes())
.use(router.allowedMethods());

可以看到,如果项目中有很多路由,那么我们需要写很多的类似router.get('xxx', () => {})这样的代码,重复劳动(多写了几个单词?)。我们需要一种方式来简化router的书写,decorator(装饰器/注解)登场了。

阅读全文 »

nginx配置https

发表于 2019-03-20

我的服务器使用的系统是CentOS 6,因此直接使用yum安装nginx,其它系统以及版本可能有所差异。

使用yum安装nginx

step1, 先要安装nginx的yum源

设置源

1
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

执行完上面的命令后,我们使用命令yum info nginx查看一下Nginx 软件包信息.

阅读全文 »

微信小程序IDE上传/预览代码分析

发表于 2019-01-13

接口分析见:小程序IDE http接口分析

打包源代码

主要步骤就是,遍历小程序项目目录,读取每一个文件,最终合并成一个文件
内容如下:packFile.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const a = require("glob"),
b = require("fs"),
c = require("path"),
e = require("crypto");
module.exports = async (f, g, h = {}) => {
var i = [
new Buffer(1),
new Buffer(4),
new Buffer(4),
new Buffer(4),
new Buffer(1)
];
i[0].writeIntLE(190), i[1].writeInt32BE(1), i[4].writeIntLE(237);
let j = 0,
k = [],
l = [],
m = [],
n = [],
o = [];
return new Promise((p, q) => {
let r = Object.assign({ nodir: !0 }, h),
s = {};
a(`${f}/**`, r, (a, r) => {
if (!a) {
r.forEach(a => {
let d = b.readFileSync(a),
g = c.relative(f, a);
if (h.needMd5) {
let a = e.createHash("md5");
a.update(d);
let b = a.digest("hex");
if (((s[g] = b), h.ignoreFileMd5 && h.ignoreFileMd5[g] == b))
return;
}
let i = new Buffer(`/${g.replace(/\\/g, "/")}`);
j++, m.push(i), n.push(d);
});
let a = 18 + 12 * j + Buffer.concat(m).length;
l = m.map((b, c) => {
let d = new Buffer(4);
d.writeInt32BE(b.length);
let e = new Buffer(4),
f = n[c].length,
g = a;
e.writeInt32BE(g), (a += f);
let h = new Buffer(4);
return h.writeInt32BE(f), Buffer.concat([d, b, e, h]);
});
let q = new Buffer(4);
q.writeInt32BE(j),
l.unshift(q),
(k = Buffer.concat(l)),
(o = Buffer.concat(n)),
i[2].writeInt32BE(k.length),
i[3].writeInt32BE(o.length),
(i = Buffer.concat(i));
let t = Buffer.concat([i, k, o]);
b.writeFileSync(g, t),
p({ destPath: g, data: t, fileMd5Info: s });
} else q(a);
});
});
};

上传预览

使用packFile.js打包源代码,将结果拿到,然后进行gz压缩,调用request模块进行上传.
upload.js 如下

1
2
3
4
5
6
7
8
9
10
11
const zlib = require("zlib");
const request = require("request");
const readFile = require('./packFile');

var url = 'https://servicewechat.com/wxa-dev-new/testsource?_r=0.8285819896175328&appid=xxx&platform=0&ext_appid=&os=darwin&clientversion=1011711020&gzip=1&path=pages%2Fhome%3F&newticket=_GWuaDA19IaLERB2pjnjJGaI61JHOfXnA8N6dDoJT20&os=darwin&clientversion=1.01.1711020';
var f = readFile('dist/', './1.wx').then((res) => {
var data = zlib.gzipSync(res.data);
request({ url, body: data, method: 'post',"proxy":"http://127.0.0.1:8888","tunnel":false }, (a, b, c) => {
console.log(JSON.stringify(c));
});
});

运行 sudo node ./upload.js;
则会生成1.wx文件,拿到这个文件上传就行了~

微信小程序IDE之http接口分析

发表于 2019-01-10

在2017年的时候就做了这个分析,一直没发出来。⚠️注意文中使用的ide版本是Mac 版的,ide version: 1.01.1711020,不推荐在生产环境中使用。

准备工作

下载IDE版本:1.01.1711020

  • 美化开发工具js代码:在js目录执行命令 find . -type f -name '*.js' -exec js-beautify -r -s 2 -p -f '{}' \;

  • 打开针对IDE的调试工具:在./app.nw/js/core/index.js 第81行 init() 过后 加入以下代码:nw.Window.get().showDevTools();

  • 在IDE源码中使用 global.contentWindow.console.log 来打印变量

    阅读全文 »

使用shell命令从请求日志中分析pv、uv

发表于 2019-01-01

在日常工作中,我们经常要统计用户的访问量。
对于pv、uv的统计方式有很多,一般就是前端主动埋点,然后去相关的系统里面查看。
但有时候,前端可能忘记埋点,或者一些其它原因,导致拿不到pv、uv。这个时候,我们可以通过用户的请求日志中来分析出pv、uv。

#准备
假如我们有如下日志,其中user_id为用户唯一id,那么就可以通过这个字段来统计UV。其实我们的需求就是匹配出每行日志中的user_id值,然后进行排序,去重,就可以拿到数量了。最简单的办法是通过正则匹配,在这篇文章里我们另辟蹊径。

阅读全文 »

mac命令行更改文件权限

发表于 2018-08-23

在开发一些系统应用的时候,有时候需要让用户输入电脑密码,然后我们拿到密码对系统进行操作。

1
echo  userpassword | sudo -S chmod 777 path-or-dir

我们先知道了密码,然后在进行系统操作。是个什么场景呢?比如开发了一款更改hosts文件的小工具,但是更改hosts文件需要用户电脑密码授权。我们可以给用户弹个输入框接收密码,然后拿到密码就可以做各种操作了。

平常更改hosts文件的流程是:先进行了修改,然后系统提示你输入密码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const child_process = require("child_process");
const prompt = require("prompt");

prompt.start();

var schema = {
properties: {
password: {
description: "Enter your password",
replace: "*",
hidden: true
}
}
};

prompt.get(schema, (err, result) => {
if (err) {
throw err;
}

child_process.exec(`echo ${result.password} | sudo -S chmod 777 ./src/test.js`, (error, res) => {
if (error) {
throw error;
}

})
});

更改过的文件权限信息如下

image.png

123
lyan

lyan

22 日志
1 分类
22 标签
RSS
GitHub Weibo E-Mail 简书
© 2023 lyan