ONE NOTE

Rage, rage against the dying of the light.

垃圾回收

为什么要垃圾回收

程序运行中,有些不再使用的对象持续占有内存,称之为内存泄漏memory leak.

内存泄漏导致计算机崩溃.

例如,服务程序(长期运行)中发生内存泄漏会导致内存被占满,会导致计算机崩溃.

好在python作为一种易用的语言,我们不用亲自进行内存管理(反面的是c语言,程序员必须非常注意内存管理).

python的垃圾回收机制

引用计数

对每一个对象进行计数,ob_refcnt记录了一个对象被引用的次数.

当引用计数归零时,回收对象(被动触发)

sys.getrefcount()可以获取一个对象的ob_refcnt 引用增加的情况:

  • 对象被创建,例如 a=23

  • 对象被引用,例如 b=a

  • 对象被作为参数,传入到一个函数中,例如get_refcnt(a)

  • 对象作为一个元素,存储在容器中,例如list1=[a,a]\

引用减少的情况:

  • 对象的别名被显式销毁,例如del a

  • 对象的别名被赋予新的对象,例如a=24

  • 一个对象离开它的作用域,例如 f 函数执行完毕时,func函数中的局部变量(全局变量不会)

  • 对象所在的容器被销毁,或从容器中删除对象

优缺点

  • 优点:被动进行,消耗资源少

  • 缺点:无法解决循环引用问题

标记清除

一个程序的所有引用关系构成一个有向图,标记清除法就是从顶层对象Roots递归所有引用关系,不可达的对象将会被删除.(主动进行)

分代回收

每当对象在某次的的垃圾回收中存活下来时,则将该对象移动至下一代.

代数越多,说明这个对象越可能重要,于是减少垃圾回收频率.

内存模型

Python内存管理机制——内存模型

两工具侧重点有区别,eslint注重代码检查,prettier更注重代码格式化。

prettier

官方文档:prettier configuration

默认忽略文件配置路径:./.prettierignore,格式同.gitignore

配置样例如下:

.prettierrc.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 使用tab换行
useTabs: true
# 每个缩进空格数
tabWidth: 2
# 一行最大长度
printWidth: 100
# xml末尾">"不单独一行
bracketSameLine: true
# markdown一个段落一行
proseWrap: never
# overrides:
# - files: "*.md"
# options:
# printWidth: 9999

命令行

prettier --write .

eslint

配置样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"eslintConfig": {
"ignorePatterns": [
"example.js"
],
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "@babel/eslint-parser"
},
"rules": {
"no-use-before-define": "off",
"no-unused-vars": "off",
"no-redeclare": "off",
"indent": [
"error",
"tab"
],
"no-undef": "off",
"no-empty": "off",
"vue/multi-word-component-names": "off",
"vue/require-v-for-key": "off",
"vue/html-indent": [
"error",
"tab",
{
"attribute": 1,
"baseIndent": 1,
"closeBracket": 0,
"alignAttributesVertically": true,
"ignores": []
}
]
}

命令行:eslint --fix --ext .vue,.js ./

安装系统

环境配置

sudo vim /etc/profile\

1
2
3
4
5
6
7
sudo vim /etc/environment

EDITOR="vim"

http_proxy=""
https_proxy=""
no_proxy="192.168.1.1/24,10.92.128.1/18,.lan"

时区

sudo tzselect

sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

配置apt镜像源[可选]

sudo vim /etc/apt/sources.list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# https://mirrors.tuna.tsinghua.edu.cn/
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

ubuntu配置静态ip

  • vim /etc/netplan/00-netcfg.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: false
dhcp6: false
accept-ra: false
addresses:
- 192.168.1.2/24
link-local: []
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 127.0.0.1
search:
- ".lan"
enp4s0:
optional: true
dhcp4: true
dhcp6: true
  • sudo netplan apply

docker配置

  • sudo apt-get update
  • sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  • sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  • sudo apt-get install docker-ce docker-ce-cli containerd.io

docker部署openwrt

macvlan模式

1.打开网卡混杂模式

sudo ip link set enp3s0 promisc on

sudo ip link set enp4s0 promisc on

2.创建网络

docker network create -d macvlan -o parent=enp3s0 vlan

docker network create -d macvlan -o parent=enp4s0 vwan

3.拉取镜像

docker pull registry.cn-shanghai.aliyuncs.com/suling/openwrt:x86_64

4.创建并启动容器

docker run --name openwrt -d --network none --privileged registry.cn-shanghai.aliyuncs.com/suling/openwrt:x86_64 /sbin/init

5.连接网络

docker network connect vlan openwrt

docker network connect vwan openwrt

6.进入容器并修改相关参数(修改固定ip)

docker exec -it openwrt bash

vim /etc/config/network

7.重启

/etc/init.d/network restart

host模式(网络性能更好)

1.拉取镜像

docker pull registry.cn-shanghai.aliyuncs.com/suling/openwrt:x86_64

OpenWrt-Docker 项目地址

2.创建并启动容器\

1
2
3
4
5
docker run --name openwrt \
-v /mnt/share:/mnt/share \
-d --network host --privileged \
registry.cn-shanghai.aliyuncs.com/suling/openwrt:x86_64 \
/sbin/init

3.进入容器并修改相关参数

docker exec -it openwrt bash

vim /etc/config/network

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'

config globals 'globals'
option packet_steering '1'

config interface 'lan'
option proto 'static'
option ifname 'enp3s0'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'

config interface 'wan'
option proto 'dhcp'
option ifname 'enp4s0'
option hostname 'openwrt

4.重启

/etc/init.d/network restart

注意

1.修改配置前先备份原本的配置文件

2.docker的openwrt尽量注意避免与主系统冲突,比如不要让主系统和openwrt同时自动挂载设备,可能产生冲突。

参考

菜鸟教程

美丽应用

什么是旁路由

将传统的 终端-路由模式 改为 终端-网关-路由模式 ,数据经过网关的代理,再发送给路由器。

我为什么使用旁路由

我的软路由无法连接到宿舍的校园网,并非无法获取ip,而是爱快系统的某些配置导致即使网卡的mac已经经过了校园网的认证(通过其他设备更改mac认证实现),仍然无法链接上网络。而虚拟机的openwrt系统就能正常上网,推测是爱快的线路检测机制(设置静态路由表也不好使)。

也许可以让爱快使用openwrt的网络?但是这实在算不上优雅。

现在宿舍网络采用的是小米路由器连接校园网作为主路由,软路由当作旁路网关的一个结构。

我的旁路由如何配置

首先在openwrt上只配置一个lan口,网关配置为主路由,然后ssr允许lan口连接配置。

然后在爱快上配置dhcp服务,爱快的dhcp管理真的很好用,可以为每个设备配置单独的网关和dns。记得给openwrt分配一个固定的ip,然后给需要留学的设备设置网关为openwrt.

最后最好关掉小米路由器的dhcp服务。

为什么要配置防火墙

在配置完成后,我发现一个问题,电脑网络一切正常,安卓机却只能留学不能访问国内网站(很慢),遂百度之,发现可能是路由器或者安卓的防劫持机制。鉴于电脑正常,我估计是安卓的机制。简单来说就是安卓发送数据的网关是openwrt,却收到了主路由返回的包,认为被劫持了,就不要这个数据包。

这个帖子讲的比较清楚:关于N1做旁路由添加 iptables 自定义防火墙规则的见解

也就是让旁路网关把数据包源ip改成自己的,再发送出去,这样回复包也会先给网关,再由网关返回终端设备,就不会触发防劫持,也能使去广告等插件正常工作。

不过这样带来一次ip转换,性能会有微小损失,但是这不是我30m校园网该考虑的事。

增加了一个跳数

下一步打算给软路由刷个linux当服务器使,爱快没有root权限实在很限制手脚。

windows注册表不是一个太好的设计,个人更喜欢unix系统里的配置文件。

win8/win10无线网络名称后面的数字怎么删掉或修改?

windows所有连接过的网络都保存在这两个位置:

1
2
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged

并没有提供删除方式,连接到ssid相同的网络时会在后面加上数字,分别不同网络。

删掉就行。

题目

统计子岛屿 问题,如图

解法

对表格2,采用递归的方法遍历岛屿,在递归时检查一下表格一对应位置是不是陆地。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @param {number[][]} grid1
* @param {number[][]} grid2
* @return {number}
*/
var countSubIslands = function (grid1, grid2) {
m = grid1.length;
n = grid1[0].length;
count = 0;

const dfs = (row, col) => {
if (row < 0 || row >= m || col < 0 || col >= n || grid2[row][col] != 1) return true;
grid2[row][col] = 2;
let temp = grid1[row][col] === 1;
temp = dfs(row + 1, col) && temp;
temp = dfs(row - 1, col) && temp;
temp = dfs(row, col + 1) && temp;
temp = dfs(row, col - 1) && temp;
return temp;
};

for (let row = 0; row < m; row++) {
for (let col = 0; col < n; col++) {
if (grid2[row][col] === 1 && dfs(row, col)) {
count++;
}
}
}
return count;
};

低级错误

问题来了,在递归时需要一个变量保存该点所在岛屿是否是子岛。在上面的代码中,temp这一变量就是这个作用。但是在错误版本中,我把let temp = grid1[row][col] === 1错误的丢掉了let关键字——这使得所有栈中共享一个temp,赋值会改变其他层中temp被覆盖,导致错误。

反思

js的函数中,变量可以不声明而直接赋值,这一点导致我写着写着就以为自己在写python,排查时完全没想到是语法错误。警钟长鸣!

3天前,知乎短暂上架了一个实验性功能——在网页版和app添加肉眼不可见的盲水印。

但是当我查看网页版时,这个功能已经下架了,使用图片盲水印在线工具也没办法看到水印了。

原理

据可以查到的资料,使用了一个透明度为千分之五的svg显示了用户的id,属于是比较粗暴的做法。

如果使用频域水印,不仅肉眼无法识别,上面的工具也会无可奈何了。

更多图片隐写术:https://www.bilibili.com/video/BV1Su411X7vY ,防不胜防啊。

以后打算用这个来拍屏幕替代截图:

对抗

网页版是最简单的,直接移除元素即可。

客户端涉及反编译,就比较麻烦。

一点感想

技术无罪,图片隐写术用来保护版权,就是干好事,用来驭民,就是坏技术。

以及,这也是我喜欢使用网页应用而不是app的原因,网页相当于用户是完全透明的,但我至今不知道qqProtect.exe到底保护了什么。