Skip to content

Commit

Permalink
(#9) Add network basics post
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 committed May 6, 2024
1 parent cee47e1 commit f926ffc
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 40 deletions.
3 changes: 3 additions & 0 deletions _posts/container/2024-02-12-container-docker-to-k8s.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ docker 有自己內建的 dns server, 這也解釋了為什麼我們可以單純
而你每一次呼叫 "web" service 的時候,dns server 都要決定要給你哪一個 ip 對吧\
所以負載平衡這段其實是由 docker 內建的 dns server 幫你做掉了

> 有關 DNS load balancing\
> 可參考 [重新認識網路 - 從基礎開始 \| Shawn Hsu](../../network/networking-basics)
你說可是上面我們還是使用了 nginx 阿\
那是因為每一個 replica service 都使用了相同的 port number(這個會造成 error), 我們需要一個統一的進入點存取\
所以這裡才使用了 nginx
Expand Down
4 changes: 2 additions & 2 deletions _posts/network/2022-04-28-network-osi.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ OSI 七層模型是由 [國際電信聯盟電信標準化部門 - ITU-T](https:/
<hr>

常見的協議如 [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) 以及 [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) 都是跑在傳輸層之上的協議
<!-- > 詳細的討論可以參考 [重新認識網路 - TCP/IP \| Shawn Hsu](../networking-tcp) 以及 [重新認識網路 - UDP \| Shawn Hsu](../networking-udp) -->
> 詳細的討論可以參考 [重新認識網路 - 從基礎開始 \| Shawn Hsu](../../network/networking-basics)
## Network Layer - 3
網路層主要是作 routing 的功能\
Expand Down Expand Up @@ -117,7 +117,7 @@ hop 這個東西簡單講就是,`經過的中繼站點個數`, 它也可以用
layer 3 switch 以及 IP 分享器 都是網路層的設備

除此之外,[Internet Protocol IP](https://en.wikipedia.org/wiki/Internet_Protocol) 也是跑在網路層的上的協議
<!-- > 詳細關於 TCP/IP 的討論可以參考 [重新認識網路 - TCP/IP \| Shawn Hsu](../networking-tcp) -->
> 詳細的討論可以參考 [重新認識網路 - 從基礎開始 \| Shawn Hsu](../../network/networking-basics)
## Data Link Layer - 2
資料連結層為了提供 [connectionless-mode](https://en.wikipedia.org/wiki/Connectionless_communication) 以及 connection-mode,所以它必須提供了一系列的連線建立、維護\
Expand Down
462 changes: 462 additions & 0 deletions _posts/network/2024-05-07-networking-basics.md

Large diffs are not rendered by default.

37 changes: 1 addition & 36 deletions _posts/random/2024-01-18-real-time-communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,42 +115,7 @@ timeout 設定成一分鐘的情況下\
在這一方面可謂完美的解決了 [Polling](#polling) 以及 [Long Polling](#long-polling) 建立連線的巨大成本帶來的 overhead\
看似完美的解決方案,但是它會遇到另一個問題,file descriptor 數量限制

<hr>

> In UNIX, Everything is a File
事情還得要從作業系統說起\
UNIX 的設計哲學是,Everything is a File\
每個東西都是檔案,什麼意思?\
檔案不就是檔案嗎? 那麼網路,印表機這些也算檔案嗎?\
依照作業系統的邏輯,如果要詳細區分這些東西為各種不同的類別,那麼實作起來將會無比困難\
因此,就算是印表機這種東西,底層實作也會將它視為是 **檔案**

建立網路連線,它分成大約兩步驟
1. 建立 socket(i.e. file descriptor),建立好一個對接的端口
2. 建立連線(並且透過 socket 進行資料的傳輸)

而 socket 就是那個 **檔案**\
每個應用程式都擁有自己的 socket 接口\
你可以使用 [lsof](https://linux.die.net/man/8/lsof) 這個指令查看當前 internet file descriptor 的狀況\
執行 `$ sudo lsof -i` 你會看到類似這張圖的輸出
![](/assets/img/posts/lsof.png)
可以很清楚的看到,chrome 瀏覽器開了許多的 socket

> 你應該還會看到其他應用程式,像我的還有 telegram, docker ... etc.
<hr>

socket 有沒有數量限制呢? 或者說比較 general 一點\
file descriptor 有沒有上限?\
我想答案很明顯是有的

```shell
$ ulimit -n
1024
```

> 可參考 [man ulimit](https://linuxcommand.org/lc3_man_pages/ulimith.html)
> 有關 socket 的介紹可參考 [重新認識網路 - 從基礎開始 \| Shawn Hsu](../../network/networking-basics#socket)
我的電腦,預設 file descriptor 上限數量為 `1024`\
也就是說一個 process 的 file descriptor 上限就那麼多\
Expand Down
9 changes: 7 additions & 2 deletions _posts/website/2023-03-08-website-jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,11 @@ client 要驗證的唯一方式 是不是用金鑰下去算算看?\
就代表這個 token 來源被驗證了! 嗎

記不記得在前面 [JWS(JSON Web Signature)](#jwsjson-web-signature) 有提到\
簽章有兩種方式, digital signature 跟 Message Authentication Codes(MACs)\
其中 MACs 是沒辦法驗證來源的
簽章有兩種方式, digital signature 跟 Message Authentication Codes(MACs)

> 數位簽章(digital signature) 可以驗證 1. 資料完整性 2. 來源合法性\
> Message Authentication Codes(MACs) 可以驗證 1. 資料完整性\
> 其中 MACs 是沒辦法驗證來源的
道理倒也淺顯易懂\
如果第三方取得你的 signing key, 我是不是也能偽造簽章了?
Expand Down Expand Up @@ -386,6 +389,8 @@ signature 的計算是使用存在 server 上的 signing key 加上前面 header
如果真的有傳送機密訊息的需求,使用 [JWE](#jwejson-web-encryption) 是最好的選擇\
不過普遍來說,我們認為 http 的 TLS 已經足夠應付大多數的場景了

> 有關 TLS 的介紹可以參考 [重新認識網路 - 從基礎開始 \| Shawn Hsu](../../network/networking-basics#ssl---secure-sockets-layertls)
既然 JWS 沒有進行加密\
一般來說是不建議在上面塞入任何敏感資訊\
以我自己來說,我通常只會帶個 user id 之類洩漏也不會怎麼樣的資料\
Expand Down
Binary file added assets/img/posts/tcpdump1.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 assets/img/posts/tcpdump2.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 assets/img/posts/tcpdump3.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 assets/img/posts/udpdump.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 assets/img/posts/udpdump1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f926ffc

Please sign in to comment.