Use shadowsocks libev

This post is under the environment of Ubuntu 18.04, this instruction will guide how to install shadowsocks libev both server side and client settings manually. And show the step to configure the simple-obfs.

The source code is in following two repositories:

You can manually build and install yourself. But under Ubuntu 18.04, you can just install like this:

sudo apt install shadowsocks-libev simple-obfs

and the configuration is under /etc/shadowsocks-libev/. You can manullay modify it and make it work.

If your system is before Ubuntu 18.04, you have to build it from source.

Install shadowsocks-libev via Ubuntu PPA

sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:max-c-lv/shadowsocks-libev -y
sudo apt-get update
sudo apt install shadowsocks-libev

Install simple-obfs

sudo apt-get install --no-install-recommends build-essential autoconf libtool libssl-dev libpcre3-dev libev-dev asciidoc xmlto automake
git clone https://github.com/shadowsocks/simple-obfs.git
cd simple-obfs
git submodule update --init --recursive
./autogen.sh
./configure && make
sudo make install

Make ~obfs-server~ able to listen on port 443

setcap cap_net_bind_service+ep /usr/local/bin/obfs-server

Server configuration

Add below to ~/etc/shadowsocks-libev/config.json~

{
    "server":"0.0.0.0",
    "server_port":443,
    "local_port":1080,
    "password":"password",
    "timeout":300,
    "method":"chacha20-ietf-poly1305",
    "plugin":"obfs-server",
    "plugin_opts": "obfs=tls;obfs-host=www.douban.com",
}

Start ~shadowsocks-libev~ server

systemctl enable shadowsocks-libev.service
systemctl start shadowsocks-libev.service
systemctl status shadowsocks-libev.service

Optimizations

Install & enable BBR TCP congestion control

 apt install --install-recommends linux-generic-hwe-16.04
 apt autoremove
 modprobe tcp_bbr
 echo "tcp_bbr" >> /etc/modules-load.d/modules.conf
 echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
 echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
 sysctl -p

MISC

Add below to ~/etc/sysctl.d/local.conf~

 fs.file-max = 51200

 net.core.rmem_max = 67108864
 net.core.wmem_max = 67108864
 net.core.netdev_max_backlog = 250000
 net.core.somaxconn = 4096

 net.ipv4.tcp_syncookies = 1
 net.ipv4.tcp_tw_reuse = 1
 net.ipv4.tcp_fin_timeout = 30
 net.ipv4.tcp_keepalive_time = 1200
 net.ipv4.ip_local_port_range = 10000 65000
 net.ipv4.tcp_max_syn_backlog = 8192
 net.ipv4.tcp_max_tw_buckets = 5000
 net.ipv4.tcp_fastopen = 3
 net.ipv4.tcp_mem = 25600 51200 102400
 net.ipv4.tcp_rmem = 4096 87380 67108864
 net.ipv4.tcp_wmem = 4096 65536 67108864
 net.ipv4.tcp_mtu_probing = 1

Reboot

reboot

Client configuration

Add below to /usr/local/etc/shadowsocks-libev.json

Note that the ~plugin~ has to be absolute path in order to be able to use ~brew services start shadowsocks-libev~.

{
    "server":"SERVER",
    "server_port":443,
    "local_address": "127.0.0.1",
    "local_port":1080,
    "password":"PASSWORD",
    "timeout":300,
    "method":"chacha20-ietf-poly1305",
    "workers":8,
    "plugin":"/usr/local/bin/obfs-local",
    "plugin_opts": "obfs=tls;obfs-host=www.bing.com",
    "fast_open":true,
    "reuse_port":true
}

reference


2019-09-26 socks , shadowsocks , shadowsocks-libev , proxy , ubuntu

排查导致 CPU load 过高的 Java 线程

When you encouter the high cpu load caused by java application, you may follow these steps to troubleshooting.

identify the PID

identify the PID, use top or htop then press P, sorted by CPU

identify the thread

identify the thread using top -n 1 -H -p PID, this command will give an output listing all the threads in the selected process

like this:

KiB Mem : 16342620 total,   270500 free, 12773428 used,  3298692 buff/cache
KiB Swap:  7812092 total,  2654116 free,  5157976 used.  1999284 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
28958 einverne  20   0 8824828 3.021g  23784 S  30.0 19.4   0:00.00 java
28959 einverne  20   0 8824828 3.021g  23784 S  0.0 19.4   0:00.56 java
28960 einverne  20   0 8824828 3.021g  23784 S  0.0 19.4   0:10.35 GC Thread#0
28961 einverne  20   0 8824828 3.021g  23784 S  0.0 19.4   0:00.06 G1 Main Marker
28962 einverne  20   0 8824828 3.021g  23784 S  0.0 19.4   0:07.24 G1 Conc#0
28963 einverne  20   0 8824828 3.021g  23784 S  0.0 19.4   0:00.42 G1 Refine#0

Here for example, PID 28958 use 30% of CPU.

jstack

Then use jstack to show the stack trace for the PID

jstack PID > jstack.out

Convert thread ID to Hexadecimal value

Use the above result as example, thread id 28958 is the problem thread. Then convert it to hex value. 28958 -> 711e

Here I provide an easy way to convert decimal to hexadecimal under bash:

printf '%x\n' 28958
# or
echo "obase=16; 28958" | bc

Search the stack trace output for the hex value using the tools you’re familiar with, grep, less etc.

less jstack.out

Then you will found the thread details.

reference


2019-09-26 java , linux , top , cpu , load

Vim 插件之:vim-abolish

vim-abolish 又一款 Tim Pope 大神所制作的插件,这款插件扩展了一条名为 :Subvert 的自定义命令,作用类似于 Vim 内置命令 :substitute 的扩展。

比如说想要将整个文档中的 man 和 dog 两个单词交换,如果用 Vim 原生的替换比较麻烦,而使用该插件则只需要 :%S/{man,dog}/{dog,man}/g

在 GitHub 页面上也有大量的使用方式介绍,这里再提一个官方页面上的用例,比如想要把所有的 facility 替换成 building,那么 facility 有复数, building 也有复数,怎么办

:%S/facilit{y, ies}/building{,s}/g

这个比较好理解,但是 Abolish 还有一个非常贴心的转换,在编程中有驼峰命名,小写字母加下划线命令,假如要将一些变量从小写下划线变成驼峰命名,这个插件提供了一个方法 crc

compute_vm_current_status

将光标移动到该变量名,然后按下 crc 就可以快速将变量命名修改成 camelCase (crc).

同样的

  • crs 变成 snake_case , 小写下划线
  • crm MixedCase
  • crc camelCase
  • crs snake_case
  • cru UPPER_CASE
  • cr- dash-case
  • cr. dot.case
  • cr space case
  • crt Title Case

更加详细内容 :help abolish


2019-09-22 vim , vim-plugin , tim-pope

Vim global 命令

global 命令结合了 Ex 命令和 Vim 模式匹配的能力,借助该命令,可以在指定模式的所有匹配行上运行 Ex 命令。global 命令是除了点操作和宏命令之外,最强大的 Vim 工具之一。

Basic

global 命令基本形式,可以读作在 range 上,如果行匹配 pattern,那么执行全局命令 cmd

:[range] global[!]/{pattern}/[cmd]

说明:

  • 缺省情况下,global 命令作用范围是整个文件 (%).
  • 除了 global 还有 :vglobal 表示反转,在没有匹配 pattern 的行上执行 cmd
  • pattern 与查找历史相互关联,如果留空,Vim 会自动使用当前的查找模式
  • [cmd] 可以是 :global 之外的任何 Ex 命令,如果不指定 [cmd], 那么缺省是 :print

Use case

删除所有包含模式的行

比如删除所有空行

:g/^$/d

拷贝包含 TODO 的行到文件末尾

代码中经常会写一些 TODO , 如果想要统一处理这些 TODO,可以使用

:g/TODO/t$

将 TODO 行拷贝到文件末尾,来处理。


2019-09-22 vim , vim-global , ex-command , vim-mode , editor , linux

Oracle 提供免费的云服务

前两天在 Twitter 上看到有人分享新闻说,Oracle 发布了新的云服务政策,提供 Always Free 的主机和对象存储还是数据库,所以就看了一下,搜了一下新闻,还真的有 Oracle Offers Always Free Autonomous Database and Cloud Infrastructure,所以就注册一下。但是当天晚上创建 VM 的时候提示我 “out of host capacity”,Google 了一下才发现,原来这条消息早就在羊毛党炸锅了,不亚于 Google Cloud Platform 当年的新闻。不过我个人作为学习使用,并没有违背 Oracle 的政策。所以我就等着 Oracle 解决 “out of host capacity” 的问题。于是等到周末,突然想起这件事情,就登录账号是了一下,确实可以创建了。

这里再整理一下 Oracle 提供的服务内容,根据它官方的博文,Oracle 提供的服务没有像其他云服务提供商一样提供 12 个月的免费体验,而是对于基础服务,比如 Compute VMs, Database, Block and Object Storage, and Load Balancer, 等等只要在用,不超过限额,那么就在账号有效期内免费使用。

甲骨文的永久免费 VPS,可以永久免费使用 2 台 AMD 服务器,外加 4 台 Arm-based 云服务器,还有数据库,对象存储等等服务。

这里引用 Oracle 官方的文章:

The new program enables developers to build applications using any language and framework on top of Oracle Cloud Infrastructure and Autonomous Database. They can get started quickly without waiting for IT to provision and learn new technologies such as artificial intelligence and machine learning. Enterprises can use Free Tier to prototype, prove out new technologies, and do testing before moving production workloads to the cloud. They can sample robust enterprise infrastructure capabilities like load balancing and storage cloning. Additionally, students can learn how to use the latest technologies and become better prepared for their careers.

Oracle 提供的免费服务包括两个部分:

  • Always Free services, which provide access to Oracle Cloud services for an unlimited time
  • Free Trial, which provides $300 in credits for 30 days to try additional services and larger shapes 而 Always Free 项目涵盖了开发,测试应用必要的各种服务,包括 Oracle Autonomous Database, Compute VMs, Block Volumes, Object and Archive Storage, and Load Balancer 等等。具体来说:

  • 2 Autonomous Databases (Autonomous Data Warehouse or Autonomous Transaction Processing), each with 1 OCPU and 20 GB storage
  • 2 Compute VMs, each with 1/8 OCPU and 1 GB memory
  • 2 Block Volumes, 100 GB total, with up to 5 free backups
  • 10 GB Object Storage, 10 GB Archive Storage, and 50,000/month API requests
  • 1 Load Balancer, 10 Mbps bandwidth
  • 10 TB/month Outbound Data Transfer
  • 500 million ingestion Datapoints and 1 billion Datapoints for Monitoring Service
  • 1 million Notification delivery options per month and 1000 emails per month

这里可以看到提供的主机是 1/8 OCPU,什么是 OCPU ? 官方的解释是:OCPU 定义为等同于启用了超线程的 Intel Xeon 处理器一个物理核心的 CPU 容量或者等同于 Orcale SPARC 的一个物理核心。1 对于 Intel Xeon 处理器,每个 OCPU 对应于两个硬件执行线程 ( vCPU )。2

AWS, Microsoft 和 Google 提供的云服务都是以 vCPU 作为计算单元,每一个 vCPU 都表示 Intel Xeon 核心的的一个超线程。一个标准的 Intel 核心如果开启了超线程,有两个线程。

Compute

计算实例配置:

Shape: VM.Standard.E2.1.Micro
Processor: 1/8th of an OCPU with the ability to use additional CPU resources
Memory: 1 GB
Networking: Includes one VNIC with one public IP address and up to 480 Mbps network bandwidth
Operating System: Your choice of one of the following Always Free-eligible operating systems:
Oracle Linux
Canonical Ubuntu Linux
CentOS Linux

Block Volume

存储

100 GB total of combined boot volume and block volume Always Free Block Volume storage.

Five total volume backups (boot volume and block volume combined).

Object Storage

免费账户:

20 GiB of combined Object Storage and Archive Storage
50,000 Object Storage API requests per month

付费账户:

10 GiB of Object Storage
10 GiB of Archive Storage
50,000 Object Storage API requests per month

Server

网站登录地址:

韩国区域后台管理地址:

Server Config

Check this post

ubuntu@instance: $ sudo su - root
root@instance: # passwd # set password of root user
adduser yourname
apt install vim
update-alternatives --config editor   # choose vim
visudo # add  yourname ALL=(ALL:ALL) NOPASSWD:ALL
vi /etc/ssh/sshd_config # AllowUsers yourname
/etc/init.d/ssh reload
# copy ssh pub to ~/.ssh/authorized_keys

then

ssh -p 22 yourname@ip

另外我个人建议可以登陆后修改一下默认的 SSH 端口。

Ping 设置

甲骨文的主机创建之后无法 ping 通,是因为在网络安全组里面禁用了 ICMP ping。

点击子网,在安全列表中,找到入站规则,允许 ICMP (IP 协议选项中)。

UuQp

防火墙设置

实例管理页面,Virtual Cloud Network Details,Security Lists, 在防火墙安全策略里面将需要的端口配上。

另外需要特别注意需要登录机器配置实例上的防火墙。

sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -F

甲骨文的 Ubuntu 镜像内置了防火墙规则,即使在面板上开通了端口白名单,重启之后端口依然会被防火墙拦住,所以每次都得手动执行上面的命令。主要原因是 Oracle 自带的 Ubuntu 镜像默认设置了 iptable 规则,可以手动关闭:

sudo apt purge netfilter-persistent
sudo reboot

或者可以直接删除 iptabls 的配置文件

rm -rf /etc/iptables && reboot

卸载停止不需要的服务

sudo systemctl stop rpcbind.socket
sudo systemctl disable rpcbind.socket

卸载 Oracle 后台监控

sudo systemctl stop snap.oracle-cloud-agent.oracle-cloud-agent.service
sudo systemctl disable snap.oracle-cloud-agent.oracle-cloud-agent.service

QA

新开的实例,我一般都会直接修改其 ssh 登录端口,而这台机器白天开了之后设置了 sshd 端口,并且 reload 了 sshd 设置,晚上回到家后发现

connect to host port xxx:xxx No route to host

这个时候我想着要不就是端口没有生效,要不就是防火墙问题,所以通过 Oracle 后台远程登录的方式进入到机器上,先关了防火墙,然后去控制后台配置了端口,然后再连接即可。

reference


2019-09-22 linux , oracle , gcp , cloud , vps , server

ModuleNotFoundError: No module named 'marshmallow.compat'

今天重新部署一个服务时发现了一个奇怪的错误,之前用很久都没有错,重新部署一个新环境就发生了这样的问题,问题应该就出现在新依赖的包中。

  File "/usr/local/lib/python3.6/dist-packages/flask_marshmallow/fields.py", line 15, in <module>
	from marshmallow.compat import iteritems
ModuleNotFoundError: No module named 'marshmallow.compat'

所以搜了一圈在原来的老环境中发现依赖的 marshmallow 是 2.15.4 重新安装这个版本之后解决了这个问题。那就可能是 marshmallow 这个包升级到 3.2.0 之后的问题了。

解决办法:

pip install marshmallow==2.15.4

后来查看 marshmallow 的升级说明,和一些 issue 发现 marshmallow 这个包 2.x 和 3.x 没有完全兼容,看来又要改代码了。


2019-09-22 flask , marshmallow , flask-marshmallow

又一 Google 服务停止:Google Translator Toolkit 关闭

前两天收到一份邮件,标题写着 Google Translator Toolkit to be shut down on December 4, 20191,感叹又一款良心服务要终结其生命了。虽然不常用 Google 翻译工具包,甚至很长时间这个翻译工具都偷偷的被隐藏在翻译页面的角落里面,但是不得不说这个工具曾经帮助我翻译过不少文档内容,也非常适合学习。Google 翻译工具包提供上传文档自动翻译,术语翻译等等功能,结合 Google Translate 的帮助能非常快速的辅助完成一篇文档的翻译。在自动翻译完成的基础上可以人工的进行修改润色。更甚至可以添加好友一同翻译,将文档分享给他人。至今为止我也只有在 Google 翻译工具包中体验过如此完整的翻译体验。

很多其他的翻译工具大多只能够提供字符串的辅助翻译,比如之前帮别人翻译过 Android 应用内文本,这些工具都非常的简陋,甚至有些术语都不能自动帮忙翻译,还需要一个字一个字的输入。虽然 Google 在邮件中给出了一些 alternative 但我只想说这些工具要么就是限制平台的,要么就看起来不像是一个完整的产品。体验没有一个能比得上 Google Translator Toolkit.

理想中的翻译工具应该有的功能

  • 导入术语库,自动翻译
  • 自动总结翻译习惯,提取常用翻译
  • 多人协作翻译,提供校对审阅确认等机制

几个社区常用的翻译网站

这些网站多多少少我都有用过,目前 crowdin 做的还不错,有机器翻译自动提示,格式化处理的也比较好,快捷键也很合适。

另外开源版本的 Pootle,也有不少人推荐,用 Python + Django 写的。不过还没有尝试。

另一个开源的本地化工具 Weblate


2019-09-22 google-translator , translator

gpg: keyserver receive failed: Server indicated a failure 解决

sudo add-apt-repository 添加 PPA 时突然遇到 gpg 添加 key 失败,大概知道可能是因为网络问题,但是这个问题在我家里的网络一直存在,非常恼人。

gpg: keyserver receive failed: Server indicated a failure

所以我想从根本上解决这个问题,这个问题的根源可能是因为网络问题导致 gpg key 没有被导入到本地。所以如果能够手动下载 gpg public key 然后手动导入不就可以了?

所以随意打开一个 PPA,比如

在页面中 Technical details about this PPA 下方有 Signing key 点击该链接会跳转到一个签名的 key 列表,在该列表中找到报错内容中的 KEY

W: GPG error: http://ppa.launchpad.net/eosrei/fonts/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ADA83EDC62D7EDF8

复制该链接,然后使用下面的命令:

curl -sL https://keyserver.ubuntu.com/pks/lookup\?op\=get\&search\=0xada83edc62d7edf8  | sudo apt-key add -

等出现 OK 即可。注意这个 URL 中的 key 需要在前面加上 0x,否则会找不到该 key.

reference


2019-09-20 gpg , ppa , ubuntu , apt-repository

Linux 下使用 emoji

Ubuntu 或者其他一些 Linux 发行版 (Debian/Ubuntu/Linux Mint) 会内置 Google Noto Color emoji font,如果没有也可以直接通过一个命令直接安装 Noto Color emoji.

安装字体

首先要安装支持 Emoji 的字体,个人比较喜欢 Google Noto Color Emoji,这是 Google 开源的用于 Android 的字体。并且支持力度一直都非常大

Noto color Emoji

直接安装

sudo apt install fonts-noto-color-emoji

或者从这里下载字体文件:

将字体文件放到 ~/.fonts 目录中。

然后运行 sudo fc-cache -f -v

ttf-ancient-fonts

Symbola font 可以将绝大部分 emoji 显示为单色的图案。

Ubuntu 系安装:

sudo apt-get install ttf-ancient-fonts

twemoji

Twitter 的 emoji 方案:

sudo apt-add-repository ppa:eosrei/fonts
sudo apt-get update
sudo apt-get install fonts-twemoji-svginot

Emoji One Font

另一种可选方案:

使用 EmojiOne Picker 来输入 Emoji

Ubuntu 上可以使用 EmojiOne 来输入 Emoji

PPA:

sudo add-apt-repository ppa:ys/emojione-picker
sudo apt-get update
sudo apt-get install emojione-picker

如果在 Ubuntu 18.04 下安装 PPA 有问题,参考这里 解决。

先将 apt source 下的内容改成

sudo vi /etc/apt/sources.list.d/ys-ubuntu-emojione-picker-bionic.list

修改为

deb http://ppa.launchpad.net/ys/emojione-picker/ubuntu xenial main

然后再安装

sudo apt install emojione-picker -t xenial

在 Chrome 中使用 Emoji

在安装了 Noto Color Emoji 之后,记得需要在 Chrome 的设置中将 Chrome 的字体设置成 Noto 字体。

  • rebuild font cache
  • restart Chrome

reference


2019-09-12 linux , ubuntu , linux-mint , mint , emoji

Java 查漏补缺:Java 8 中接口 default 方法

Java 8 新特性:

  • lambda expressions
  • functional interfaces
  • method references
  • streams
  • Optional

还有 interface 中的 staticdefault 方法。

基本使用

Java 8 允许在接口中定义默认方法。

interface Collection {

	void add();

	default void debug(){
		System.out.println("put the key in");
	}
}

为什么要引入 default 方法

和接口中定义的其他方法一样,default 方法默认是 public

接口是用来定义类的行为的,如果要在接口中新添加方法,那么所有实现此接口的类都需要强制的实现新添加的方法。而 default 方法就可以规避该问题。

当多个接口定义了相同 default 方法

当一个类实现的多个接口定义了相同的 default 方法,那么编译时会失败。需要子类 Override 该方法实现。在子类中可以通过 Interface.super.xxx() 方法来调用接口的 default 方法。

@Override
public void turnOnAlarm() {
	Vehicle.super.turnOnAlarm();
	Alarm.super.turnOnAlarm();
}

接口中的 static 方法

除了 default 方法,Java 8 也允许在接口中定义 static 方法。

接口中的静态方法属于类,在接口中定义 static 方法和在类中定义一样。

因为 Java 不支持多继承,所以在遇到一些代码共享的时候,就不能通过多继承来实现,通常的做法是定义一个静态类,包含可能被多个类使用到的共同的方法,比如 Java 中的 Collections 类。

而通过接口中的 static 方法可以提高代码的 cohesion,将相关的逻辑集中到一起,而不用另外定义一个 Object。

同样 Abstract 类也能做到,但是和抽象类的区别在于,抽象类是有 Constructors, state, behavior 的。

总结

理想状态下,接口不应该封装具体的行为,只应该用来定义某类型的公开接口。

但是为了弥补 Java 不能多继承而带来的一些缺点,Java 8 中引入接口的 default 和 static 方法,也肯定是 JDK 工程师仔细考量后的一个权衡。

reference


2019-09-10 java , java8 , interface

电子书

本站提供服务

最近文章

  • 从 Buffer 消费图学习 CCPM 项目管理方法 CCPM(Critical Chain Project Management)中文叫做关键链项目管理方法,是 Eliyahu M. Goldratt 在其著作 Critical Chain 中踢出来的项目管理方法,它侧重于项目执行所需要的资源,通过识别和管理项目关键链的方法来有效的监控项目工期,以及提高项目交付率。
  • AI Shell 让 AI 在命令行下提供 Shell 命令 AI Shell 是一款在命令行下的 AI 自动补全工具,当你想要实现一个功能,敲一大段命令又记不住的时候,使用自然语言让 AI 给你生成一个可执行的命令,然后确认之后执行。
  • 最棒的 Navidrome 音乐客户端 Sonixd(Feishin) Sonixd 是一款跨平台的音乐播放器,可以使用 [[Subsonic API]],兼容 Jellyfin,[[Navidrome]],Airsonic,Airsonic-Advanced,Gonic,Astiga 等等服务端。 Sonixd 是一款跨平台的音乐播放器,可以使用 [[Subsonic API]],兼容 Jellyfin,[[Navidrome]],Airsonic,Airsonic-Advanced,Gonic,Astiga 等等服务端。
  • 中心化加密货币交易所 Gate 注册以及认证 Gate.io 是一个中心化的加密货币交易所。Gate 中文通常被称为「芝麻开门」,Gate 创立于 2013 年,前身是比特儿,是一家致力于安全、稳定的数字货币交易所,支持超过 1600 种数字货币的交易,提供超过 2700 个交易对。
  • 不重启的情况下重新加载 rTorrent 配置文件 因为我在 Screen 下使用 rTorrent,最近经常调试修改 rtorrent.rc 配置文件,所以想要找一个方法可以在不重启 rTorrent 的情况重新加载配置文件,网上调查了一下之后发现原来挺简单的。