iptables

IPTABLES

参考

Tables

表按类型组织规则,规则进一步组成链(hooks 触发 chains)

Built-in tables
  • nat 网络地址转换 chains-> OUTPUT,POSTROUTING,PREROUTING
  • filter 过滤数据包,链的默认表 chains-> FORWARD,INPUT,OUTPUT
  • mangle 修改数据包 chains-> FORWARD,INPUT,OUTPUT,POSTROUTING,PREROUTING

Chains

flow
链与网络层的hooks关联,每个流经网络层的包都将触发hooks.

  • PREROUTING: Triggered by the NF_IP_PRE_ROUTING hook.
  • INPUT: Triggered by the NF_IP_LOCAL_IN hook.
  • FORWARD: Triggered by the NF_IP_FORWARD hook.
  • OUTPUT: Triggered by the NF_IP_LOCAL_OUT hook.
  • POSTROUTING: Triggered by the NF_IP_POST_ROUTING hook.

每个Hook上的chains有执行的优先顺序,参考下图
tables_chains

Examples (Matches ,Targets)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.80
# wireguard example
iptables -I INPUT -p udp --dport 51824 -j ACCEPT
iptables -N wg0-input
iptables -A INPUT -i wg0 -j wg0-input
iptables -A wg0-input -m state --state ESTABLISHED,RELATED -j ACCEPT
# wireguard example 2
iptables -t mangle -A PREROUTING -i wg0 -j MARK --set-mark 0x200
iptables -t nat -A POSTROUTING ! -o wg0 -m mark --mark 0x200 -j MASQUERADE

# drop them if the rate exceeds 10 per second
iptables -A INPUT -p icmp --icmp-type ping -m limit !--limit 10/s -j DROP
iptables -A PREROUTING -i eth1 -m mac --mac-source ! 0d:bc:97:02:18:21 -j DROP

MARKDOWN

# Heading level 1

### Heading level 3

##### Heading level 5

**boldtext**
*italic text*
~~strikethrought text~~

  1. 1. Fist item
  2. 2. Second item
    1. 1. Indented item
    2. 2. Indented item
  3. 3. Third item
  • - Fist item
  • - Second item
    • - Indented item
    • - Indented item
  • - Third item

[Link](https://blog.lizhen.pub)
<https://blog.lizhen.pub>

`inline code`

1
2
3
//```js  
const message = 'li'
//```
Horizontal rules: -– ___ ***



Table:

| ID | Title |
|—|——|
|#1 | Hello |
|#2 | Markdown |

ID Title
#1 Hello
#2 Markdown

PROMISE ERROR

JS PROMISE ERROR

一般规则:
子函数返回Promise,不必使用catch处理,错误处理由最顶层的Promise处理.

  • promise reject 后,没有被catch的话,目前不会结束代码运行。会继续执行后续代码,不过会在最后报个警告: UnhandledPromiseRejectionWarning。并提示在后续的 node 版本中会导致进程非正常结束,就是崩掉。

  • Async的作用有二个

    1. 函数返回 Promise
    2. 函数中使用 await
  • await 对错误的处理方式:

    • await 执行时如返回REJECT,则扔出错误,可以使用try/catch语句捕获.
  • Then , Catch , Finally

    • catch 捕获错误,
    • 使用reject 而不是throw
    • finally 不影响 then/catch的数据流.不接受任何参数,可做一些清理工作.
      1
      2
      3
      4
      # Output: Good ABC Promise{ <state>: "fulfilled" ,<value>: undefined }
      Promise.resolve("ABC").finally(() => console.log("Good")).then((dat) => console.log(dat));
      # Output: Good Promise{ <state>: "fulfilled", <value>:"ABC" }
      Promise.reject("ABC").finally(() => console.log("Good")).catch((dat) => dat);

SSH EXAMPLE

SSH 举例

tldr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- Connect to a remote server:
ssh username@remote_host

- Connect to a remote server with a specific identity (private key):
ssh -i path/to/key_file username@remote_host

- Connect to a remote server using a specific port:
ssh username@remote_host -p 2222

- Run a command on a remote server with a [t]ty allocation allowing interaction with the remote command:
ssh username@remote_host -t command command_arguments

- SSH tunneling: Dynamic port forwarding (SOCKS proxy on `localhost:1080`):
ssh -D 1080 username@remote_host

- SSH tunneling: Forward a specific port (`localhost:9999` to `example.org:80`) along with disabling pseudo-[T]ty allocation and executio[N] of remote commands:
ssh -L 9999:example.org:80 -N -T username@remote_host

- SSH jumping: Connect through a jumphost to a remote server (Multiple jump hops may be specified separated by comma characters):
ssh -J username@jump_host username@remote_host

- Agent forwarding: Forward the authentication information to the remote machine (see `man ssh_config` for available options):
ssh -A username@remote_host

CSS FLEX

CSS Flex 弹性布局
  • 采用 Flex 布局的元素,称为 Flex 容器(flex container),简称“容器”。它的所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称“项目”。
  • 容器存在2根轴, 主轴,从轴. 容器不存在行的概念.
容器属性(控制所属子元素)
  • flex-direction: 主轴方向,项目(子元素)的排列方向.
  • flex-wrap: 子元素(项目)超出父容器时是否换行.
  • flex-flow: flex-direction flex-wrap 的缩写.
  • justify-content: 子元素(项目)在主轴方向上的对齐方式
  • align-items: 子元素(项目)在次轴方向上的对齐方式
  • align-content: 子元素(项目)在侧轴还有多余空间时调整容器内项目的对齐方式
项目属性(子元素)
  • order: 设置项目在容器中出现的顺序.
  • align-self: 覆盖 align-items 属性,为某个项目设置不同的对齐方式
  • flex: flex-grow flex-shrink flex-basis 的缩写.分别对应项目的增长量,收缩量,长度. ( 默认值: flex: 0 1 auto; )
  • flex-grow,flow-shrink: 计算方式
Flex 一维布局
  • Flex是一维布局的含义之一:假设主轴尺度足够,所有flex items都将布局在主轴上.

Python3 开发准备

虚拟环境
1
2
3
python -m venv myvenv # 创建
source myvenv/bin/activate #激活,source means that is is executed in the current shell.
deactivate
安装
1
2
3
pip freeze > requirements.txt
pip install -r requirements.txt
pip install ... -i https://mirrors.aliyun.com/pypi/simple/ #阿里源

REACT 我的部件

开发REACT时,我的常用包和工具

查看公网IP地址

GET Public IP

家里联通猫得到的是100.64.x.x的运营商私有区域地址.为获得使用的公网地址可采用如下方法:

1
2
3
4
5
6
7
8
dig +short myip.opendns.com @resolver1.opendns.com
dig +short txt ch whoami.cloudflare @1.0.0.1
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
host myip.opendns.com resolver1.opendns.com
# 使用第三方网站
curl ifconfig.me
curl icanhazip.com
curl ipecho.net/plain

postgresql 事务隔离级别

事务隔离级别

  • 事务隔离级别简单的说,就是当激活事务时,控制事务内因SQL语句产生的锁定需要保留多入,影响范围多大,以防止多人访问时,在事务内发生数据查询的错误。
  • postgresql 使用MVVC 读快照 , 读永远不妨碍写.
  • postgresql 默认使用READ COMMITTED事务隔离级别. 就是说事务中每个语句得到一个新的数据快照.为了使一个事务使用同一快照,请使用REPEATABLE READ事务隔离级别. postgresql中最后一个隔离级别是SSI串行读.

For Update 类似update一样锁定行, 必须等待commit后才释放锁. For Update NOWait ,nowait 就是不等待,当已被其他事务锁定时,直接报错.

1
2
3
4
5
BEGIN;
Select * From invoice Where processed = false For Update;
** application magic will happen here **
Update invoice Set processed = true ...
COMMIT;

其他: postgresql 的 advisory locks.

React Hook Internal

REACT HOOK

React Fiber with Render and Commit phrases

  • fiber 是react创建的内部对象代表每个元素(element),可以是一个DOM,也可以是一个函数部件.
  • hook出现之前的函数部件,只接受属性(那时的react引擎不使用fiber). 因为函数不能保存状态,而使用fiber后,状态保存在fiber中.类似与将函数使用的变量保存函数体外.(例如: 全局变量可供所有函数使用).
  • React 为每个元素创建2个fiber.一个为”current“ ,一个为”workInProgress“.
    • “current” 在屏幕上展示给客户. 同时 “workInProgress”在后台处理更新,一旦完成,后者转变为”current”.
    • 初始化时,我们称为Mount, 每个部件都需创建, 比较费时. “workInProgress”创建完成后变为”current”(就是一个指针分配).
    • 当发生更新时, 我们称为Update ,”workInProgress”再次创建,但他从”current”克隆未变的部分.速度大大加快, 完成后成为”current”.

React useState

1
const [ data ,setData ] = useState();
  • 当代码使用setData时 ,意思是 Dispatch action ,引擎将action安排至对应fiber中hook对应的更新队列.下次更新时执行.
  • 执行的结果决定了Update时, data获得的最新状态.

React useEffect

What is a side effect?
  • 相对于纯函数, 函数有隐含的依赖.
  • 解决方式有二种:
    1. 将依赖都放入函数参数中. 完美解决方式,但有时很难实现.
    2. 将依赖打包并推迟到最后, 直到真正需要时才执行.在下面例子中,c and log 作为输入参数.所以addFunc是纯的, add 也是纯的.
      1
      2
      3
      4
      5
      6
      7
      function addFunc(c ,log) {
      function add(a ,b) {
      log(a,b)
      return a + b + c
      }
      return add
      }

      useEffect

      1
      useEffect( () => { ...; return ... }, [DependArray] )
  • useEffect 位于上图的绿色部分.再更新完DOM后执行.
  • useEffect 将使用fiber的updateQueue属性,保存effect函数(分为 create 和destory两类), 取决于场景Mount/Upudate 和 依赖数组.
  • 每次更新完屏幕后, react引擎会flush effect 队列.(先 destory ,后 create)