-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
842 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,7 @@ | |
stack S; | ||
init_stack(S); | ||
BiTree t; | ||
push(S, root); | ||
push(S, T); | ||
while(!is_empty(S)) | ||
{ | ||
pop(S, t); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
#+TITLE: quic 协议实现调研 | ||
|
||
** QUIC 协议 | ||
QUIC 的全称是 Quick UDP Internet Connections。由 Google 设计提出,目前由 IETF 工作组推动进展,它是一种基于 UDP 的低时延的传输层协议 | ||
[[https://datatracker.ietf.org/doc/rfc9000/][IETF QUIC]] | ||
|
||
|
||
** 调研目标 | ||
- 实现是否符合 IETF 协议标准。如果不符合标准,这样会导致和其它 QUIC 应用互通时有问题 | ||
- 代码是否持续维护和迭代 | ||
|
||
|
||
** c/c++ 开源实现 | ||
|
||
*** [[https://github.com/microsoft/msquic][microsoft/msquic]] | ||
- 是用 c 实现 IETF QUIC 协议的跨平台通用库 | ||
- 依赖:CMake3.16 及以上(xaiver 113 CMake 版本是 3.10) | ||
#+begin_src bash | ||
$ cd /usr/local/src/ | ||
$ wget 'https://github.com/Kitware/CMake/releases/download/v3.23.2/cmake-3.23.2-linux-x86_64.tar.gz' # aarch64: https://cmake.org/files/v3.23.2/cmake-3.23.2-linux-aarch64.tar.gz | ||
$ tar zxvf cmake-3.20.0-linux-x86_64.tar.gz && rm cmake-3.20.0-linux-x86_64.tar.gz | ||
$ mv cmake-3.23.2-linux-x86_64 cmake-3.23.2 | ||
$ /usr/local/src/cmake-3.23.2/bin/cmake --version | ||
cmake version 3.23.2 | ||
|
||
CMake suite maintained and supported by Kitware (kitware.com/cmake). | ||
#+end_src | ||
- 下载: =git clone --recursive https://github.com/microsoft/msquic.git= | ||
|
||
*** [[https://github.com/alibaba/xquic][alibaba/xquic]] | ||
|
||
|
||
*** Google's Chromium | ||
|
||
参考:https://www.chromium.org/quic/playing-with-quic/ | ||
|
||
系统要求 | ||
- 64 位 Intel 机器,8GB 内存 | ||
- 100GB 磁盘 | ||
- git 和 Python3.6+ | ||
|
||
使用 Ubuntu18.04 实验 | ||
|
||
**** 下载源码 && 安装依赖 | ||
|
||
#+begin_src bash | ||
$ cd /home/he/ | ||
$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git | ||
$ export PATH="$PATH:/home/he/depot_tools" # 使用绝对路径或者 ${HOME},不能使用 ~ | ||
$ mkdir ~/chromium && cd ~/chromium | ||
$ fetch --no-history --nohooks chromium # 获取不带有历史版本的 chromium 源码 | ||
$ cd src | ||
$ ./build/install-build-deps.sh --no-chromeos-fonts # 如果 chromeos-fonts 下载不了的话,使用 --no-chromeos-fonts 标签 | ||
$ gclient runhooks | ||
#+end_src | ||
|
||
在 arch linux 上,不用 =install-build-deps.sh= 脚本安装 =build dependencies= ,使用如下命令安装 | ||
#+begin_src bash | ||
$ sudo pacman -S --needed python perl gcc gcc-libs bison flex gperf pkgconfig \ | ||
nss alsa-lib glib2 gtk3 nspr freetype2 cairo dbus libgnome-keyring \ | ||
xorg-server-xvfb xorg-xdpyinfo | ||
#+end_src | ||
|
||
**** build 源码 | ||
- chromium 使用 Ninja 作为它的 build 工具,gn 命令用来生成 =.ninja= 编译脚本,我们可以创建多个 =build directories= | ||
- gn 默认生成 Debug 版本配置,如果想要 release 版本配置,请参考 [[https://www.chromium.org/developers/gn-build-configuration/][GN build configuration]] | ||
|
||
#+begin_src bash | ||
$ gn gen out/Debug # 创建一个 build directories(必须在 out 子目录下) | ||
$ ninja -C out/Debug quic_server quic_client epoll_quic_server # 编译 quic_server quic_client epoll_quic_server | ||
$ ninja -C out/Debug -t clean # clean | ||
$ mkdir /tmp/quic-data # 开始准备测试数据 | ||
$ cd /tmp/quic-data | ||
$ wget -p --save-headers https://www.example.org | ||
$ cat www.example.org/index.html | ||
HTTP/1.1 200 OK | ||
Age: 577466 | ||
Cache-Control: max-age=604800 | ||
Content-Type: text/html; charset=UTF-8 | ||
Date: Fri, 03 Jun 2022 15:46:11 GMT | ||
Etag: "3147526947+ident" | ||
Expires: Fri, 10 Jun 2022 15:46:11 GMT | ||
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT | ||
Server: ECS (oxr/837A) | ||
Vary: Accept-Encoding | ||
X-Cache: HIT | ||
Content-Length: 1256 | ||
X-Original-Url: https://www.example.org/ # 设置资源的 url | ||
|
||
{"code": 200, "name": "quic"} # 网页内容改为自己要测的内容 | ||
#+end_src | ||
|
||
**** 生成证书 | ||
- [[https://chromium.googlesource.com/chromium/src/+/HEAD/docs/linux/cert_management.md][Linux Cert Management]] | ||
|
||
#+begin_src bash | ||
$ cd /home/he/chromium/src/net/tools/quic/certs | ||
$ ./generate-certs.sh # 生成证书。一定要进到该目录,再执行这个脚本 | ||
$ cd /home/he/chromium/src | ||
$ ls net/tools/quic/certs/out/ | ||
01.pem 2048-sha256-root.key 2048-sha256-root-serial.old leaf_cert.req | ||
2048-sha256-root-index.txt 2048-sha256-root.pem leaf_cert.key | ||
2048-sha256-root-index.txt.attr 2048-sha256-root.req leaf_cert.pem | ||
2048-sha256-root-index.txt.old 2048-sha256-root-serial leaf_cert.pkcs8 | ||
$ sudo apt install libnss3-tools # 安装 NSS 证书管理工具 | ||
$ mkdir -p $HOME/.pki/nssdb | ||
$ certutil -d $HOME/.pki/nssdb -N # 为 NSS Certificate DB 设置密码时,直接按回车了 | ||
$ certutil -d sql:$HOME/.pki/nssdb -A -t "C,," -n quic -i /home/he/chromium/src/net/tools/quic/certs/out/2048-sha256-root.pem # 添加信任 root CA 证书 | ||
$ certutil -d sql:$HOME/.pki/nssdb -L # 列出所有证书。删除指定证书可以使用命令:certutil -d sql:$HOME/.pki/nssdb -D -n quic | ||
|
||
Certificate Nickname Trust Attributes | ||
SSL,S/MIME,JAR/XPI | ||
|
||
quic C,, | ||
#+end_src | ||
|
||
**** 运行 QUIC server 和 client | ||
#+begin_src bash | ||
$ ./out/Debug/quic_server \ | ||
--quic_response_cache_dir=/tmp/quic-data/www.example.org \ | ||
--certificate_file=net/tools/quic/certs/out/leaf_cert.pem \ | ||
--key_file=net/tools/quic/certs/out/leaf_cert.pkcs8 \ | ||
--host=127.0.0.1 \ | ||
--port=6121 \ | ||
--v=1 | ||
#+end_src | ||
|
||
#+begin_src bash | ||
$ ./out/Debug/quic_client --host=127.0.0.1 --port=6121 --disable_certificate_verification=true "https://www.example.org/" | ||
#+end_src | ||
|
||
- =--disable_certificate_verification= 如果要连接的服务端没有受信任的证书,客户端可以使用该标志禁用证书验证 | ||
- =--allow_unknown_root_cert= 如果服务器的证书是可信的,但链接到用户安装的 CA(例如,由上面提到的脚本生成的 CA),客户端可使用该标志来允许证书链接到用户安装的 CA | ||
- ~--v=1~ 显示更多内容 | ||
|
||
|
||
*** quiche | ||
|
||
**** 安装依赖库 tcmalloc_minimal | ||
安装库 tcmalloc_minimal,下载页面 [[https://github.com/gperftools/gperftools/releases][ gperftools]] | ||
#+begin_src bash | ||
$ wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.10/gperftools-2.10.tar.gz | ||
$ tar -zcvf gperftools-2.10.tar.gz | ||
$ cd gperftools-2.10/ | ||
$ ./configure --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimal # 只生成 tcmalloc_minimal 库 | ||
$ make | ||
$ make install | ||
#+end_src | ||
|
||
**** 下载编译 bilibili/quiche | ||
|
||
#+begin_src bash | ||
$ apt install cmake build-essential protobuf-compiler libprotobuf-dev golang-go libunwind-dev libicu-dev | ||
$ git submodule update --init --recursive | ||
$ mkdir build && cd build | ||
$ cmake .. | ||
$ make | ||
$ cd - | ||
#+end_src | ||
|
||
**** example | ||
编译 simple_quic_server simple_quic_client | ||
|
||
#+begin_src bash | ||
$ cd build | ||
$ make simple_quic_server simple_quic_client | ||
$ cd - | ||
#+end_src | ||
|
||
|
||
下载测试页面 && 生成证书 | ||
#+begin_src bash | ||
$ mkdir -p /data/quic-root | ||
$ wget -p --save-headers https://www.example.org -P /data/quic-root | ||
$ cd utils | ||
$ chmod +x generate-certs.sh google_quiche_rewrite.sh | ||
$ ./generate-certs.sh | ||
Generating RSA private key, 2048 bit long modulus (2 primes) | ||
...................................+++++ | ||
................+++++ | ||
e is 65537 (0x010001) | ||
Signature ok | ||
subject=CN = QUIC Server Root CA | ||
Getting Private key | ||
Generating a RSA private key | ||
.............+++++ | ||
.............+++++ | ||
writing new private key to 'out/leaf_cert.key' | ||
----- | ||
Using configuration from ca.cnf | ||
Can't open out/2048-sha256-root-index.txt.attr for reading, No such file or directory | ||
140510139908544:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('out/2048-sha256-root-index.txt.attr','r') | ||
140510139908544:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79: | ||
Check that the request matches the signature | ||
Signature ok | ||
The Subject's Distinguished Name is as follows | ||
countryName :PRINTABLE:'US' | ||
stateOrProvinceName :ASN.1 12:'California' | ||
localityName :ASN.1 12:'Mountain View' | ||
organizationName :ASN.1 12:'QUIC Server' | ||
commonName :ASN.1 12:'127.0.0.1' | ||
Certificate is to be certified until Jun 12 15:44:38 2022 GMT (3 days) | ||
|
||
Write out database with 1 new entries | ||
Data Base Updated | ||
$ mkdir -p /data/quic-cert | ||
$ mv ./out/* /data/quic-cert/ | ||
$ cd - | ||
#+end_src | ||
|
||
服务端 quic | ||
#+begin_src bash | ||
$ ./build/simple_quic_server \ | ||
--quic_response_cache_dir=/data/quic-root/ \ | ||
--certificate_file=/data/quic-cert/leaf_cert.pem \ | ||
--key_file=/data/quic-cert/leaf_cert.pkcs8 \ | ||
--host=172.16.123.128 \ | ||
--port=6121 | ||
#+end_src | ||
|
||
客户端 quic | ||
#+begin_src bash | ||
$ ./build/simple_quic_client --host=172.16.123.128 --port=6121 --disable_certificate_verification=true "https://www.example.org/index.html" | ||
#+end_src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#+TITLE: 浏览器 url 回车会发生什么 | ||
|
||
url 解析 -> 检查是否有缓存 -> 查询域名对应 IP -> 发起 http 请求 -> 服务器处理请求,返回页面源码 -> 浏览器接收页面源码,渲染 html -> 浏览器发送嵌入到 html 中的对象请求 -> 浏览器关闭 tcp 连接 | ||
|
||
** DNS 解析 | ||
浏览器缓存 -> 操作系统缓存 -> hosts 文件 -> 路由器缓存 -> ISP DNS 缓存(在计算机上设置的首选 DNS 服务器 ip)-> 根域名服务器 | ||
|
||
** http 请求 | ||
应用层:发送 http GET 请求 | ||
传输层:tcp 三次握手、在这条 tcp 连接上进行 tls 握手(客户端验证服务端证书,确定通信的对称密钥)、传输数据。应用层数据 > MSS 时会进行 tcp 切片 | ||
网络层:判断目标地址是否与当前地址处于同一网络中,是的话直接根据 Mac 地址发送,否则使用路由表查找下一跳地址,以及使用 ARP 协议查询下一跳的 Mac 地址。ip 层结点如果 MTU 值过小,也会进行切片操作。如果客户端在局域网里,需要 NAT 转换私网源 ip 到公网源 ip | ||
数据链路层:打包成帧,发送到物理层 | ||
服务端收到完整的数据包后,按照以上步骤逆转一层一层拆包。发现 MAC 地址符合、IP 地址符合,则会把数据发给相应的进程 | ||
|
||
** 页面重定向 | ||
客户端向服务端发送 HTTP 请求,如果服务器返回以 301 之类的重定向,浏览器根据相应头中的 location 再次发送请求 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.