Skip to content

Commit

Permalink
2022.11.02
Browse files Browse the repository at this point in the history
  • Loading branch information
he3210 committed Nov 2, 2022
1 parent f3b2b64 commit e70932d
Show file tree
Hide file tree
Showing 24 changed files with 842 additions and 15 deletions.
2 changes: 1 addition & 1 deletion algorithm notebook/二叉树的遍历.org
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
stack S;
init_stack(S);
BiTree t;
push(S, root);
push(S, T);
while(!is_empty(S))
{
pop(S, t);
Expand Down
8 changes: 5 additions & 3 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ h2 {

h3 {
font-size: 1.3em;
font-style: italic;
/* font-style: italic; */
color: #FF8A65;
padding: 0.5em 0 10px;
border-bottom: 1px solid #eee;
margin-left: 2em;
/* margin-left: 2em; */
}

h4 {
Expand All @@ -122,7 +122,7 @@ h4 {
color: #FF8A65;
padding: 0.5em 0 10px;
border-bottom: 1px solid #eee;
margin-left: 4em;
/* margin-left: 4em; */
}

h1, h2, h3, h4 {
Expand Down Expand Up @@ -223,9 +223,11 @@ h4 .done {
content: "\01F4C5 ";
}

/* 避免正文都高亮,所以注释
.content {
background-color: #fff;
}
*/

.target {
background-color: lavender;
Expand Down
12 changes: 12 additions & 0 deletions docker notebook/docker 网络笔记.org
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ $ docker network connect app db2 # 把容器 db2 添加到网络 app
$ docker network disconnect app db2 # 从指定网络断开一个容器
$ docker run -it --rm --name db --net=my-net he3210/redis sh # 从镜像创建一个容器并把该容器连接到指定网络
#+END_SRC

* mac 下网络问题测试
| src | dst | 结论 |
|-----------------------------------+-------------------------------------------+------------------------------------------------------|
| container1: =nc 192.168.3.8 1234= | mac: =nc -l 192.168.3.8 1234= | 容器可以连 mac。且容器的 1234 端口没做映射,也没暴露 |
| container2: =nc 172.17.0.4 1234= | container1: =nc -l -s 172.17.0.4 -p 1234= | 容器可以连容器。且容器的 1234 端口没做映射,也没暴露 |
| mac: =nc 172.17.0.4 1234= | container1: =nc -l -s 172.17.0.4 -p 1234= | mac 连不上容器。mac 都 ping 不通容器,肯定连不上 |


docker desktop for mac 没有提供从宿主机通过容器 IP 访问容器的方式,解决方案:
- [[https://github.com/wenjunxiao/mac-docker-connector/blob/master/README-ZH.md][desktop-docker-connector]]
- [[https://github.com/chipmk/docker-mac-net-connect][docker-mac-net-connect]]
Binary file added images/clion_remote_debug_ros_edit_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/clion_remote_debug_ros_target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/vmware_NAT_static_ip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions notebooks.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
- [[./docker notebook/docker.org][docker 笔记]]
- [[./redis notebook/redis.org][redis 笔记]]
- [[./golang notebook/golang.org][golang 笔记]]
- [[./ros notebook/ros.org][ros 笔记]]
- [[./others notebook/others.org][未分类笔记]]
2 changes: 2 additions & 0 deletions others notebook/others.org
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
- [[./评论系统架构设计.org][评论系统架构设计]]
- [[./MySQL 存储引擎.org][MySQL 存储引擎]]
- [[./tcp RST 攻击实验.org][tcp RST 攻击实验]]
- [[./浏览器 url 回车会发生什么.org][浏览器 url 回车会发生什么]]
- [[./quic 协议实现调研.org][quic 协议实现调研]]
224 changes: 224 additions & 0 deletions others notebook/quic 协议实现调研.org
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
16 changes: 16 additions & 0 deletions others notebook/浏览器 url 回车会发生什么.org
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 再次发送请求
2 changes: 2 additions & 0 deletions qu/qu.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
- [[./charles.org][charles 注册码]]
- [[./排行榜系统设计.org][排行榜系统设计]]
- [[./c++面试题.org][c++ 面试题]]
- [[./session 认证过程.org][session 认证过程]]
- [[https://www.zhihu.com/question/29270034/answer/1679116463][短 url 系统设计]]
Loading

0 comments on commit e70932d

Please sign in to comment.