iidear / blog

blog
0 stars 0 forks source link

网络 #11

Open iidear opened 5 years ago

iidear commented 5 years ago

没有设置 Cache-Control 请求头,chrome 返回了 200 (from memory cache)

原因: 没有设置 Cache-Control 请求头,但设置了 Last-Modified 请求头,浏览器会启发式的计算一个强缓存时间.

If there is no Cache-Control header and no Expires header, but there is a Last-Modified header (which most web servers send by default for static assets), most browsers will use heuristic freshness to determine how long to cache that asset for.

The typical calculation for this (which is the one suggested by RFC 7234) is: (current time - last modified time) / 10

在 Chrome 75.0.3770.100 中测试,符合上面的解释。

iidear commented 3 years ago

常见网络协议和代理

常见互联网应用:

浏览器、音视频、邮箱、IM、App、游戏、curl

协议

应用层:

HTTP(网页) HTTPS(安全网页) SMTP(邮件发送) POP3(邮件接收) DNS(域名解析) DHCP(动态活动IPDNS服务器等参数) CIFS(Windows 文件共享协议) NFS(Unix/Linux 文件共享) NTP(时钟同步) RTP(IP多媒体电话的语音、文字、视频等流体的传输) SSH、FTP、RTSP、Gopher、IRC、Telnet、SNMP、IMAP、LDAP、…

传输层:TCP、UDP、TLS/SSL、DCCP、…

网络层:IP、ICMP、…

链路层:ARP、NDP、PPP、MAC(Ethernet、DSL、ISDN、FDDI)、…

常用代理:

VPN(链路层) SOCKS(会话层) Charles(应用层) …

参考

互联网协议套件 活跃的应用层协议 抓包

iidear commented 3 years ago

前端与网络

在互联网应用开发中,前端常常需要与网络打交道。前端日常面临的网络相关的问题主要涉及以下几个方面:

  1. 网络问题的排查
    • http 与 https
    • 常用 http 状态码、请求头与响应头
    • 缓存
    • 同源策略与跨域访问
    • cookie
  2. 网络性能优化
  3. 网络安全
  4. 常用 web 服务器及功能
    • 负载均衡
    • 反向代理
iidear commented 3 years ago

http 与 https

https 指在 http 协议之下增加一层安全协议(TLS)。

TLS 协商关键词:

https 服务器:生成证书 --> CA 认证证书 --> 服务器配置证书 https 客户端:预置根证书 --> 请求服务器 --> 服务器返回证书(公钥) --> 验证服务器证书 --> 使用公钥加密对称密钥发送给服务端。

操作系统根证书库可添加自签证书。

本地搭建 https 服务器

  1. 生成非对称加密密钥(key)
    openssl genpkey -out fd.key -algorithm RSA -pkeyopt rsa_keygen_bits:2048
  2. 生成 CSR(证书签名请求文件)
    openssl req -new -key fd.key -out fd.csr
  3. 生成 CRT(自签证书)
    1. chrome 会校验证书的(SAN)主题备用名称,添加 SAN 扩展
      echo "subjectAltName = DNS:*.domain.com, DNS: domain.com" > fd.ext
    2. 生成证书
      openssl x509 -req -days 365 -in fd.csr -signkey fd.key -out fd.crt -extfile fd.ext
  4. 服务器配置密钥和自签证书
    
    const https = require('https');
    const fs = require('fs');

https.createServer({ key: fs.readFileSync('./fd.key'), cert: fs.readFileSync('./fd.crt'), }, (req, res) => { res.writeHead(200); res.end('hello world\n'); }).listen(8000);


5. 系统配置信任自签证书
    + Mac 启动台 --> 钥匙串访问
    + Chrome 浏览器 --> chrome://settings/security --> 管理证书

### 参考
+ [openssl cookbook](https://www.feistyduck.com/library/openssl-cookbook/online/ch-openssl.html)
iidear commented 3 years ago

Http 响应码

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status

常见状态码:

iidear commented 3 years ago

浏览器缓存

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Caching_FAQ

Origin

https://developer.mozilla.org/zh-CN/docs/Glossary/%E6%BA%90

跨域资源共享

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS