一款新的vpn程序-tinyFecVPN

by FISHERMAN

YM’S BLOG.人生如戏,全靠演技.既要看得透,还要看得远. / 2017-11-14 17:43

A Lightweight High-Performance VPN with Build-in Forward Error Correction Support, Improves your Network Quality on a High-latency Lossy Link, uses same lib as UDPspeeder.

A Lightweight High-Performance VPN with Build-in Forward Error Correction Support(or A Network Improving Tool which works at VPN mode). Improves your Network Quality on a High-latency Lossy Link.
image
TinyFecVPN uses Forward Error Correction(Reed-Solomon code) to reduce packet loss rate, at the cost of additional bandwidth usage.
Assume your local network to your server is lossy. Just establish a VPN connection to your server with tinyFecVPN, access your server via this VPN connection, then your connection quality will be significantly improved. With well-tuned parameters , you can easily reduce IP or UDP/ICMP packet-loss-rate to less than 0.01% . Besides reducing packet-loss-rate, tinyFecVPN can also significantly improve your TCP latency and TCP single-thread download speed.
TinyFecVPN uses same lib as UDPspeeder, supports all FEC features of UDPspeeder. TinyFecVPN works at VPN mode,while UDPspeeder works at UDP tunnel mode.
简体中文(内容更丰富)

Note

UDPspeeder’s repo:
https://github.com/wangyu-/UDPspeeder

Note2

You can use udp2raw with tinyFecVPN together to get better speed on some ISP with UDP QoS(UDP throttling).
udp2raw’s repo:
https://github.com/wangyu-/udp2raw-tunnel

Efficacy

Tested on a link with 100ms roundtrip and 10% packet loss at both direction. You can easily reproduce the test result by yourself.

Ping Packet Loss

SCP Copy Speed

Supported Platforms

Linux host (including desktop Linux,Android phone/tablet, OpenWRT router, or Raspberry PI).Binaries for amd64 x86mips_be mips_le arm are provided.
For Windows and MacOS, You can run TinyFecVPN inside this 7.5mb virtual machine image.
Need root or at least CAP_NET_ADMIN permission to run, for creating tun device.

How doest it work

TinyFecVPN uses FEC(Forward Error Correction) to reduce packet loss rate, at the cost of additional bandwidth usage. The algorithm for FEC is called Reed-Solomon.

Reed-Solomon

In coding theory, the Reed–Solomon code belongs to the class of non-binary cyclic error-correcting codes. The Reed–Solomon code is based on univariate polynomials over finite fields.
It is able to detect and correct multiple symbol errors. By adding t check symbols to the data, a Reed–Solomon code can detect any combination of up to t erroneous symbols, or correct up to ⌊t/2⌋ symbols. As an erasure code, it can correct up to t known erasures, or it can detect and correct combinations of errors and erasures. Reed–Solomon codes are also suitable as multiple-burst bit-error correcting codes, since a sequence of b + 1 consecutive bit errors can affect at most two symbols of size b. The choice of t is up to the designer of the code, and may be selected within wide limits.

Check wikipedia for more info, https://en.wikipedia.org/wiki/Reed–Solomon_error_correction

Getting Started

Installing

Download binary release from https://github.com/wangyu-/tinyFecVPN/releases

Running

Assume your server ip is 44.55.66.77, you have a service listening on udp/tcp port 0.0.0.0:7777.

# Run at server side:
./tinyvpn -s -l0.0.0.0:4096 -f20:10 -k "passwd" --sub-net 10.22.22.0

# Run at client side
./tinyvpn -c -r44.55.66.77:4096 -f20:10 -k "passwd" --sub-net 10.22.22.0

Now, use 10.22.22.1:7777 to connect to your service,all traffic will be improved by FEC. If you ping 10.22.22.1, you will get ping reply.

Note

-f20:10 means sending 10 redundant packets for every 20 original packets.
-k enables simple XOR encryption

Advanced Topic

Usage

tinyFecVPN
git version: b03df1b586 build date: Oct 31 2017 19:46:50
repository: https://github.com/wangyu-/tinyFecVPN/

usage:
run as client: ./this_program -c -r server_ip:server_port [options]
run as server: ./this_program -s -l server_listen_ip:server_port [options]

common options, must be same on both sides:
-k,--key <string> key for simple xor encryption. if not set, xor is disabled
main options:
--sub-net <number> specify sub-net, for example: 192.168.1.0 , default: 10.22.22.0
--tun-dev <number> sepcify tun device name, for example: tun10, default: a random name such as tun987
-f,--fec x:y forward error correction, send y redundant packets for every x packets
--timeout <number> how long could a packet be held in queue before doing fec, unit: ms, default: 8ms
--mode <number> fec-mode,available values: 0, 1; 0 cost less bandwidth, 1 cost less latency;default: 0)
--report <number> turn on send/recv report, and set a period for reporting, unit: s
--keep-reconnect re-connect after lost connection,only for client.
advanced options:
--mtu <number> mtu. for mode 0, the program will split packet to segment smaller than mtu_value.
for mode 1, no packet will be split, the program just check if the mtu is exceed.
default value: 1250
-j,--jitter <number> simulated jitter. randomly delay first packet for 0~<number> ms, default value: 0.
do not use if you dont know what it means.
-i,--interval <number> scatter each fec group to a interval of <number> ms, to protect burst packet loss.
default value: 0. do not use if you dont know what it means.
--random-drop <number> simulate packet loss, unit: 0.01%. default value: 0
--disable-obscure <number> disable obscure, to save a bit bandwidth and cpu
developer options:
--tun-mtu <number > mtu of the tun interface,most time you shouldnt change this
--disable-mssfix <number > disable mssfix for tcp connection
-i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax
--fifo <string> use a fifo(named pipe) for sending commands to the running program, so that you
can change fec encode parameters dynamically, check readme.md in repository for
supported commands.
-j ,--jitter jmin:jmax similiar to -j above, but create jitter randomly between jmin and jmax
-i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax
-q,--queue-len <number> max fec queue len, only for mode 0
--decode-buf <number> size of buffer of fec decoder,u nit: packet, default: 2000
--fix-latency <number> try to stabilize latency, only for mode 0
--delay-capacity <number> max number of delayed packets
--disable-fec <number> completely disable fec, turn the program into a normal udp tunnel
--sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024
log and help options:
--log-level <number> 0: never 1: fatal 2: error 3: warn
4: info (default) 5: debug 6: trace
--log-position enable file name, function name, line number in log
--disable-color disable log color
-h,--help print this help message

FEC Options

The program supports all options of UDPspeeder,check UDPspeeder repo for details:
https://github.com/wangyu-/UDPspeeder

Addtional Options

--tun-dev

Specify a tun device name to use. Example: --tun-dev tun100.
If not set,tinyFecVPN will randomly chose a name,such as tun987.

--sub-net

Specify the sub-net of VPN. Example: --sub-net 10.10.10.0, in this way,server IP will be 10.10.10.1,client IP will be 10.10.10.2.
The last number of option should be zero, for exmaple 10.10.10.123 is invalild, and will be corrected automatically to 10.10.10.0.

--keep-reconnect

Only works at client side.
TinyFecVPN server only handles one client at same time,the connection of a new client will kick old client,after being kicked,old client will just exit by default.
If --keep-reconnect is enabled , the client will try to get connection back after being kicked.

Performance Test(throughput)

Server is a Vulr VPS in japan,CPU: single core 2.4GHz,ram: 512mb. Client is a Bandwagonhost VPS in USA,CPU: single core 2.0GHZ,ram: 96mb. To put pressure on the FEC algorithm, an additional 10% packet-loss rate was introduced at both direction.

Test command

Server side:
./tinyvpn_amd64 -s -l 0.0.0.0:5533 --mode 0 -f20:10
iperf3 -s

Client side:
./tinyvpn_amd64 -c -r 44.55.66.77:5533 --mode 0 -f20:10
iperf3 -c 10.22.22.1 -P10

Test result

image
Note: the performance is mainly limited by the RS code lib.

Other

For regulations consideration, there is currently an intended restriction at server side in the pre-released binaries. You cant use tinyFecVPN to access a third host directly. So, as a connection speed-up tool, when used alone, it only allows you to speed-up your connection to your server. You cant use it for bypassing firewalls by default.
You can easily get rid of this restriction by compiling the source code by yourself.

FROM https://github.com/wangyu-/tinyFecVPN

一款新的vpn程序-tinyFecVPN

Bash 脚本 set 命令教程

by 阮一峰

IT牛人博客聚合网站 / 2017-11-15 10:49

服务器的开发和管理离不开 Bash 脚本,掌握它需要学习大量的细节。

bg2017111501.png

set命令是 Bash 脚本的重要环节,却常常被忽视,导致脚本的安全性和可维护性出问题。本文介绍它的基本用法,让你可以更安心地使用 Bash 脚本。

一、简介

我们知道,Bash 执行脚本的时候,会创建一个新的 Shell。

 $ bash script.sh 

上面代码中,script.sh是在一个新的 Shell 里面执行。这个 Shell 就是脚本的执行环境,Bash 默认给定了这个环境的各种参数。

set命令用来修改 Shell 环境的运行参数,也就是可以定制环境。一共有十几个参数可以定制,官方手册有完整清单,本文介绍其中最常用的四个。

顺便提一下,如果命令行下不带任何参数,直接运行set,会显示所有的环境变量和 Shell 函数。

 $ set 

二、set -u

执行脚本的时候,如果遇到不存在的变量,Bash 默认忽略它。

 #!/usr/bin/env bash echo $a echo bar 

上面代码中,$a是一个不存在的变量。执行结果如下。

 $ bash script.sh bar 

可以看到,echo $a输出了一个空行,Bash 忽略了不存在的$a,然后继续执行echo bar。大多数情况下,这不是开发者想要的行为,遇到变量不存在,脚本应该报错,而不是一声不响地往下执行。

set -u就用来改变这种行为。脚本在头部加上它,遇到不存在的变量就会报错,并停止执行。

 #!/usr/bin/env bash set -u echo $a echo bar 

运行结果如下。

 $ bash script.sh bash: script.sh:行4: a: 未绑定的变量 

可以看到,脚本报错了,并且不再执行后面的语句。

-u还有另一种写法-o nounset,两者是等价的。

 set -o nounset 

三、set -x

默认情况下,脚本执行后,屏幕只显示运行结果,没有其他内容。如果多个命令连续执行,它们的运行结果就会连续输出。有时会分不清,某一段内容是什么命令产生的。

set -x用来在运行结果之前,先输出执行的那一行命令。

 #!/usr/bin/env bash set -x echo bar 

执行上面的脚本,结果如下。

 $ bash script.sh + echo bar bar 

可以看到,执行echo bar之前,该命令会先打印出来,行首以+表示。这对于调试复杂的脚本是很有用的。

-x还有另一种写法-o xtrace。

 set -o xtrace 

四、Bash 的错误处理

如果脚本里面有运行失败的命令(返回值非0),Bash 默认会继续执行后面的命令。

 #!/usr/bin/env bash foo echo bar 

上面脚本中,foo是一个不存在的命令,执行时会报错。但是,Bash 会忽略这个错误,继续往下执行。

 $ bash script.sh script.sh:行3: foo: 未找到命令 bar 

可以看到,Bash 只是显示有错误,并没有终止执行。

这种行为很不利于脚本安全和除错。实际开发中,如果某个命令失败,往往需要脚本停止执行,防止错误累积。这时,一般采用下面的写法。

 command || exit 1 

上面的写法表示只要command有非零返回值,脚本就会停止执行。

如果停止执行之前需要完成多个操作,就要采用下面三种写法。

 # 写法一 command || { echo "command failed"; exit 1; } # 写法二 if ! command; then echo "command failed"; exit 1; fi # 写法三 command if [ "$?" -ne 0 ]; then echo "command failed"; exit 1; fi 

另外,除了停止执行,还有一种情况。如果两个命令有继承关系,只有第一个命令成功了,才能继续执行第二个命令,那么就要采用下面的写法。

 command1 && command2 

五、 set -e

上面这些写法多少有些麻烦,容易疏忽。set -e从根本上解决了这个问题,它使得脚本只要发生错误,就终止执行。

 #!/usr/bin/env bash set -e foo echo bar 

执行结果如下。

 $ bash script.sh script.sh:行4: foo: 未找到命令 

可以看到,第4行执行失败以后,脚本就终止执行了。

set -e根据返回值来判断,一个命令是否运行失败。但是,某些命令的非零返回值可能不表示失败,或者开发者希望在命令失败的情况下,脚本继续执行下去。这时可以暂时关闭set -e,该命令执行结束后,再重新打开set -e。

 set +e command1 command2 set -e 

上面代码中,set +e表示关闭-e选项,set -e表示重新打开-e选项。

还有一种方法是使用command || true,使得该命令即使执行失败,脚本也不会终止执行。

 #!/bin/bash set -e foo || true echo bar 

上面代码中,true使得这一行语句总是会执行成功,后面的echo bar会执行。

-e还有另一种写法-o errexit。

 set -o errexit 

六、set -o pipefail

set -e有一个例外情况,就是不适用于管道命令。

所谓管道命令,就是多个子命令通过管道运算符(|)组合成为一个大的命令。Bash 会把最后一个子命令的返回值,作为整个命令的返回值。也就是说,只要最后一个子命令不失败,管道命令总是会执行成功,因此它后面命令依然会执行,set -e就失效了。

请看下面这个例子。

 #!/usr/bin/env bash set -e foo | echo a echo bar 

执行结果如下。

 $ bash script.sh a script.sh:行4: foo: 未找到命令 bar 

上面代码中,foo是一个不存在的命令,但是foo | echo a这个管道命令会执行成功,导致后面的echo bar会继续执行。

set -o pipefail用来解决这种情况,只要一个子命令失败,整个管道命令就失败,脚本就会终止执行。

 #!/usr/bin/env bash set -eo pipefail foo | echo a echo bar 

运行后,结果如下。

 $ bash script.sh a script.sh:行4: foo: 未找到命令 

可以看到,echo bar没有执行。

七、总结

set命令的上面这四个参数,一般都放在一起使用。

 # 写法一 set -euxo pipefail # 写法二 set -eux set -o pipefail 

这两种写法建议放在所有 Bash 脚本的头部。

另一种办法是在执行 Bash 脚本的时候,从命令行传入这些参数。

 $ bash -euxo pipefail script.sh 

八、参考链接

(完)

文档信息

0 0

udpwork.com 聚合 | 评论: 0 | 要! 要! 即刻! Now!

Bash 脚本 set 命令教程

构建基于nginx-php-yaf的docker镜像

by sa

Linux服务器运维日志 / 2017-11-17 13:26

  • 需求 : php7.1 + nginx
  • php扩展: yaf、redis、ldap、pdo、mbstring、 mcrypt

阅读完本文后,你能解决以下常见问题:

  • 如何写Dockerfile,并通过Dockerfile构建镜像。
  • 如何通过supervisord管理进程,并将进程日志通过docker logs {container}输出

实现步骤

我们的镜像基于centos:7系统, Dockerfile代码如下:

FROM centos:7 MAINTAINER zhaowei RUN rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \ && rpm -Uvh https://sp.repo.webtatic.com/yum/el7/webtatic-release.rpm \ && yum update -y \ && yum install -y wget \ sed \ gcc \ gcc-c++ \ gd \ gd-devel \ gmp-devel \ epel-release \ net-tools \ ntpdate \ ntp \ openssh-clients \ curl \ crontabs \ openssl \ openssl-devel \ nginx \ squid \ php71w \ php71w-fpm \ php71w-gd \ php71w-soap \ php71w-pdo_mysql \ php71w-pear \ php71w-devel \ php71w-mbstring \ php71w-mcrypt \ php71w-ldap \ && yum update -y ntp ntpdate kernel-headers \ && yum clean all \ && sed -i 's/http_access deny all/#http_access deny all/g' /etc/squid/squid.conf \ && cd /tmp \ && wget -O get-pip.py https://bootstrap.pypa.io/get-pip.py \ && python get-pip.py \ && pip install supervisor \ && mkdir -p /etc/supervisor.d/*.conf \ && pecl install mongodb \ && pecl install yaf \ && wget -c https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz \ && tar zxvf 3.1.4.tar.gz \ && cd phpredis-3.1.4 \ && phpize \ && ./configure \ && make \ && make install \ && cd .. \ && rm -rf phpredis* \ && rm 3.1.4.tar.gz \ && echo "" >> /etc/php.ini \ && echo "extension=redis.so" >> /etc/php.ini \ && echo "extension=yaf.so" >> /etc/php.ini \ && echo "extension=mongodb.so" >> /etc/php.ini \ && echo "[yaf]" >> /etc/php.ini \ && echo "yaf.use_namespace = 1" >> /etc/php.ini COPY ./supervisord.conf /etc/ COPY ./default.conf /etc/nginx/conf.d/ COPY ./nginx.conf /etc/supervisor.d/ COPY ./php.conf /etc/supervisor.d/ COPY ./ntpdate.conf /etc/supervisor.d/ COPY ./nginx.conf.def /etc/nginx/nginx.conf COPY ./index.php /data/work/code/ EXPOSE 80 443 3128 WORKDIR /data/work/code CMD ["supervisord","-c","/etc/supervisord.conf"] 

问题

子进程stdout如何重定向到supervisord

Supervisor的监督进程supervisord可以捕获所管理子进程的stdout及stderr,并可以配置为写入日志文件
镜像初步构建成功时,运行容器,并通过 docker logs -f {container} 查看容器日志,总是提示错误, 如下:

2017-11-09 07:41:41,256 CRIT uncaptured python exception, closing channel <POutputDispatcher at 36463248 for <Subprocess at 35644984 with name nginx in state RUNNING> (stderr)> (<type 'exceptions.IOError'>:[Errno 29] Illegal seek [/usr/lib/python2.7/site-packages/supervisor/supervisord.py|runforever|227] [/usr/lib/python2.7/site-packages/supervisor/dispatchers.py|handle_read_event|232] [/usr/lib/python2.7/site-packages/supervisor/dispatchers.py|record_output|166] [/usr/lib/python2.7/site-packages/supervisor/dispatchers.py|_log|142] [/usr/lib/python2.7/site-packages/supervisor/loggers.py|info|275] [/usr/lib/python2.7/site-packages/supervisor/loggers.py|log|293] [/usr/lib/python2.7/site-packages/supervisor/loggers.py|emit|186] [/usr/lib/python2.7/site-packages/supervisor/loggers.py|doRollover|211]) 

经过了N多次查询,N多次尝试,终于找到原因,原来是在配置supervisord管理的子进程文件时,如果配置了stdout_logfile=/dev/stdout, 则必须一块配置stdout_logfile_maxbytes=0

查找的资料说明如下:

If anyone stumbles upon this error like me, be aware that the mentioned options apply to the [supervisord] section, within [program:x] sections, you need to use stdout_logfile_maxbytes = 0and/or stderr_logfile_maxbytes = 0 respectively. using stdout_logfile=/dev/stdout along with stdout_logfile_maxbytes=0 

对于需要将supervisord管理的子进程的日志输出到stdout的program, 配置如下:

[program:php7-fpm] command=/usr/local/sbin/php-fpm autostart = true stderr_logfile = /dev/stdout stdout_logfile = /dev/stdout stdout_logfile_maxbytes = 0 stderr_logfile_maxbytes = 0 

参见: http://veithen.github.io/2015/01/08/supervisord-redirecting-stdout.html

php-fpm 的错误日志如何定向到docker logs

看到 https://github.com/docker-library/php/issues/63 解释,将error_log及access.log的配置改成如下error_log=/proc/self/fd/2 和 access.log=/proc/self/fd2即可,一直不明白,/proc/self/fd/2和/dev/stdout /dev/stderr有什么联系,经过ls -al /dev 才明白:

lrwxrwxrwx 1 root root 15 Nov 9 07:44 stderr -> /proc/self/fd/2 lrwxrwxrwx 1 root root 15 Nov 9 07:44 stdin -> /proc/self/fd/0 lrwxrwxrwx 1 root root 15 Nov 9 07:44 stdout -> /proc/self/fd/1 

对于我们的需求,是将错误及access信息都在docker logs 中显示,所以,我们配置php-fpm.conf如下:

[global] error_log=/proc/self/fd/1 

文件/etc/php-fpm.d/www.conf配置如下:

php_admin_value[error_log] = /proc/self/fd/1 slowlog = /proc/self/fd/1 catch_workers_output = yes clear_env=no 
构建基于nginx-php-yaf的docker镜像

denyhostsでSSHの不正アタック対策

by tamohiko

server-memo.net / 2017-11-03 02:03

  2017/11/02  

denyhostsとは

「denyhosts」は不正ログインの形跡をチェックして、該当する接続元のIPアドレスを「/etc/hosts.deny」に自動的に登録して、接続を自動的に遮断してくれます。

今回はCentOS7の環境に「denyhosts」を使ってSSHへの不正アタック対策を行う手順を説明していきます。

インストール

「yum」を使ってインストールすることが出来るのですが、その際EPELリポジトリを使用するのでEPELリポジトリを追加して使用できるように設定しておく必要があります。

EPELリポジトリ追加

「EPEL」リポジトリの追加はとても簡単です。

# yum -y install epel-release  

「EPEL」リポジトリの追加直後は自動的にリポジトリを使用するように設定されているため、明示的に指定しなければ使用できないよう設定をしておきます。

# cp -p /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo_yyyymmdd  # vi /etc/yum.repos.d/epel.repo  

変更箇所は下記通りとなります。

変更前 enabled=1  変更後 enabled=0  

※詳しい手順はこちらのページで解説してあります。

denyhostsインストール

準備が出来たら「yum」で「denyhosts」のインストールを行います。

# yum --enablerepo=epel install denyhosts  

設定

「denyhosts」の設定ファイルは「/etc/denyhosts.conf」となりますので、このファイルを編集して設定を行っていきます。

# cp -p /etc/denyhosts.conf /etc/denyhosts.conf_yyyymmdd  # vi /etc/denyhosts.conf  

色々な設定があるのですが、今回はとりあえず基本的な設定として下記項目を編集していきます。

項目 設定内容
PURGE_DENY 接続拒否時間の設定
PURGE_THRESHOLD 接続拒否自動解除回数
BLOCK_SERVICE 接続拒否をするサービス
DENY_THRESHOLD_INVALID サーバに存在しないユーザでのログイン失敗の監視
DENY_THRESHOLD_VALID サーバに存在するユーザでのログイン失敗の監視
DENY_THRESHOLD_ROOT root ユーザでのログイン失敗の監視
ADMIN_EMAIL メール通知設定 
SMTP_HOST メールサーバホスト設定
SMTP_PORT メールサーバポート番号設定
SMTP_FROM メールの差出人設定
SMTP_SUBJECT メールタイトル設定
DAEMON_SLEEP denyhostsデーモン接続遮断を判断する間隔
DAEMON_PURGE denyhostsデーモンが遮断解除のチェックを行う間隔

※メール通知を行わないのであればメール関連の設定は必要ありません。

PURGE_DENY

接続を遮断する期間の設定を行います。期間は「数字」+「単位」で設定できます。

  • m (分)
  • h (時間)
  • d (日)
  • w (週)
  • y (年)

「CentOS7」のデフォルト設定は4週間(4w)となっていましたが、自分で制限に引っかかったときに悲しい思いをするので、今回は1時間で解除されるように変更しました。

PURGE_DENY = 1h  

下記のように時間を指定しなかった場合は、永久に自動解除されませんのでご注意を…

PURGE_DENY =  

PURGE_THRESHOLD

接続拒否の設定を自動的に解除する回数を設定します。

初期設定では「0」に設定されていて自動解除の回数に制限はなく、何度でも「PURGE_DENY」で設定した期間が経過すると接続拒否の設定を解除してくれます。

何度も攻撃を受けるのは嫌なので、接続拒否の自動解除は2回までとしそれ以後は自動的に接続遮断の解除を行わないように設定します。

PURGE_THRESHOLD = 2  

この回数を超えた場合は別のIPアドレスから接続するかコンソールからログインして、手動で解除する必要があるので注意してください。

BLOCK_SERVICE

接続拒否をするサービスを指定します。

初期設定で「sshd」と設定されているはずなので確認しておいてください。

BLOCK_SERVICE = sshd  

DENY_THRESHOLD_INVALID

サーバに存在しないユーザでログインした場合に接続を拒否するまでの回数を設定します。

初期設定が5回だったのでそのままにしておきます。

DENY_THRESHOLD_INVALID = 5  

DENY_THRESHOLD_VALID

サーバに存在しているユーザでログインを失敗した場合に接続を拒否するまでの回数を設定します。

初期設定は10回と回数が多めなので、今回は5回に変更してみます。

DENY_THRESHOLD_VALID = 5  

DENY_THRESHOLD_ROOT

rootユーザでログインしようとして失敗した場合に接続を拒否するまでの回数を設定します。

DENY_THRESHOLD_ROOT = 1  

ADMIN_EMAIL

メール通知を行うメールアドレスを設定します。

ADMIN_EMAIL = 通知先メールアドレス  

複数指定する場合は「,」で区切ってメールアドレスを指定してください。

ADMIN_EMAIL = foo@bar.com, bar@foo.com, etc@foobar.com  

ローカルのユーザに送信したい場合はユーザ名を設定します。

SMTP_HOST

メールを送信するために使用するSMTPサーバの設定を行います。

初期設定では自サーバ(localhost)に設定されています。

SMTP_HOST = localhost  

SMTP_PORT

SMTPサーバに接続する際に使用するポート番号を設定します。

SMTP_PORT = 25  

SMTP_FROM

メール通知を行う際の「From:」欄を設定します。

SMTP_FROM = 送信元メールアドレス  

SMTP_SUBJECT

メールの件名(Subject)を設定します。

特にこだわりが無ければそのままで良いでしょう

SMTP_SUBJECT = DenyHosts Report from $[HOSTNAME]  

DAEMON_SLEEP

通信を遮断するかどうかチェックする間隔を設定します。

初期設定は「30s」(30秒)に設定されているので、たとえば「DENY_THRESHOLD_VALID = 5」とかのルールに該当した場合でも即時遮断されるのではなく、次回のチ
ェック時に通信が遮断されるということです。

DAEMON_SLEEP = 30s  

DAEMON_PURGE

遮断解除のチェックを行う間隔を設定します。

ここで設定されている間隔で自動解除を行うか確認するため、「DAEMON_PURGE = 1h」(1時間)と設定されている場合は「PURGE_DENY = 10m」(10分)と設定されている場合でも1時間ごとにしかチェックを行わないため、最大1時間ほど解除までの時間がかかることもあるため、この項目は「PURGE_DENY」と同じか短い値を設定しておいたほうが良いでしょう。

今回はチェックの間隔を5分に設定しておきます。

DAEMON_PURGE = 5m  

denyhosts起動

設定が終わりましたら「denyhosts」を起動させましょう。

CentOS7の場合

# systemctl start denyhosts  

CentOS6までの場合

# /etc/init.d/denyhosts start  

自動起動設定

自動起動の設定も忘れずに行いましょう。

CentOS7の場合

# systemctl enable denyhosts  

CentOS6までの場合

# chkconfig denyhosts on  

動作テスト

「denyhosts」を起動しましたら、故意にログインを失敗させて実際に遮断されるかを確認してみましょう。

テストを行う場合は、「PURGE_DENY」「DAEMON_SLEEP」「DAEMON_PURGE」の値を短く設定しておいたほうが、実際の挙動を確認しやすくなるでしょう。

設定を変更した場合は「denyhosts」を再起動して、設定を反映させてください。

テスト時の注意点

SSHでしか接続できないサーバでテストを行う場合は、まずSSHでログインして接続を確保しておいてください。

設定に不具合があった場合、接続遮断が自動的に解除されず最悪SSHでサーバに接続することが出来なくなる可能性がありますが、すでに接続された接続は遮断されないので、そこから手動で接続遮断の設定を解除することが出来ます。

denyhosts解除方法

「denyhosts」によって登録された接続を遮断するIPアドレスの設定を、すぐに手動で解除する方法です。

denyhosts停止

まず最初に「denyhosts」を停止します。

CentOS7の場合

# systemctl stop denyhosts  

CentOS6までの場合

# /etc/init.d/denyhosts stop  

IPアドレス削除

下記のファイルから対象のIPアドレスを削除します。

  • /etc/hosts.deny
  • /var/lib/denyhosts/hosts
  • /var/lib/denyhosts/hosts-restricted
  • /var/lib/denyhosts/hosts-root
  • /var/lib/denyhosts/hosts-valid
  • /var/lib/denyhosts/users-valid

※「/var/lib/denyhosts」の部分は「/etc/denyhosts.conf」の「WORK_DIR」で設定されているディレクトリとなります。(初期設定では「/var/lib/denyhosts」)

denyhosts起動

IPアドレスの削除が終わったら、「denyhosts」を起動します。

CentOS7の場合

# systemctl start denyhosts  

CentOS6までの場合

# /etc/init.d/denyhosts start  

その後、接続可能になっているか実際に確認を行ってください。

 – SSH

  関連記事

Shared via Inoreader

denyhostsでSSHの不正アタック対策

WordPress 3.8开启Google AMP的问题

by Uranus Zhou

Soul Of Free Loop / 2017-11-02 22:20

关于 AMP

Accelerated Mobile Pages(简称 AMP)是 Google 带领开发的开源项目,主要为了提升移动设备对网站的访问速度,大概包括 AMP HTMLAMP JSAMP Cache 三部分,非常适合博客及新闻展示类的静态网站。

有关 AMP 的详细介绍可以参考其官方网站:

https://www.ampproject.org/zh_cn/

AMP 网页只能使用 AMP HTML 中允许的有限的标签,不允许使用 AMP Runtime、AMP Components 之外的 JS,而且 AMP Runtime、Components 都必须从 cdn.ampproject.org 加载,因此目前 AMP 网页都必须在翻墙状态下才能正常浏览。

AMP Cache 是负责缓存并传输 AMP 页面的 CDN 服务器,用户在 Google 移动端中点击 AMP 网站时,实际访问的是缓存过的页面。

WordPress 启用 AMP 的问题

WordPress 官方提供了 AMP 插件以支持直接为博客开启 AMP 加速,只是官方插件要求 WordPress 最低为 4.4 版本。

由于我的博客安装了 qTranslate 这款多语言插件,而该插件却只支持到 WordPress 3.8.1 版本,如果升级 WordPress 到最新版本,qTranslate 插件就会自动禁用。然而 qTranslate 插件是对 WordPress 数据库直接进行修改的,禁用之后博客文章就可能会同时显示多种语言的内容,这样显然是不可接受的。

虽然网上已经有人继承 qTranslate 原作发布了例如 qTranslate X 之类的支持新版本 WordPress 的插件,但为了数据安全(其实是懒)考虑我暂时还是不打算直接升级 WordPress 了。

不过还好我在 WordPress 插件库里找到一款看起来支持 WordPress 3.8 的 AMP 插件:AMP for WP,毕竟看此插件的介绍和评分都比官方的 AMP 插件要强哦,而且最低只要求 WordPress 3.0 版本。

AMP for WP 安装起来还是非常顺利的,插件选项里目前基本只提供了 Google Analytics UID 的设置选项,启用之后访问 AMP 版网站的方法是在地址后面加上 ?wpamp 后缀。

不过我刚开始测试访问 AMP 首页和文章就碰到问题了,直接没有任何显示了,看网页源代码输出信息不全,显然是 AMP for WP 插件对老版本 WordPress 兼容性还是有点问题。

修改 AMP for WP

首先修改 AMP for WP 插件的 wp-amp-ninja/admin/common.php 文件,打开 PHP 报错开关:

error_reporting(1);  

这下访问博客首页和文章页面就能看到详细报错了:

Fatal error: Call to undefined function get_the_permalink() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/includes/wpamp-front.php on line 24    Fatal error: Call to undefined function the_posts_pagination() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/template/wpamp-content.php on line 53      Fatal error: Call to undefined function the_archive_title() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/includes/wpamp-functions.php on line 600    Fatal error: Call to undefined function the_archive_description() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/includes/wpamp-functions.php on line 601  

经过查阅 WordPress 官方文档发现:

  1. get_the_permalink 函数在 WordPress 3.9.0 版本开始引入,其实就是 1.0.0 版本就已经有的 get_permalink 函数;
  2. the_posts_pagination 函数在 WordPress 4.1.0 版本开始引入,一同引入的还有 the_posts_navigation 函数,与 get_the_permalink 函数一样都位于 WordPress 代码库的 wp-includes/link-template.php 文件;
  3. 至于 the_archive_titlethe_archive_description 函数的功能比较类似,支持显示文章的标题和描述并在前后插入自定义代码,同样也在 WordPress 4.1.0 版本开始引入,位于 WordPress 代码库的 wp-includes/general-template.php 文件。

由此可见,AMP for WP 实际上应该至少要求 WordPress 4.1.0 版本,只是作者并没有测试过是否兼容较低版本的 WordPress。

不过还好我看了下 WordPress 最新 4.8 版本的代码,上面缺少的几个 WordPress 内置函数都是功能比较独立的。

因此我稍微修改了一下博客目录下的 link-template.phpgeneral-template.php 文件,将最新版本的 get_the_permalinkthe_posts_paginationthe_posts_navigationthe_archive_titlethe_archive_description 函数实现分别 Backport 到 WordPress 3.8 版本。Backport 完成之后再访问博客首页和文章,就没有 PHP 报错了。

不过初步试用下来,我还是发现 AMP for WP 插件的一些小问题:

  1. 如果通过 qTranslate 插件访问博客的英文版本(通过增加 ?lang=en 地址后缀),对应的 AMP 版页面地址会不正确,AMP for WP 插件在转换地址时并没有考虑地址中会带有其它后缀的情况;
  2. AMP for WP 对于带有多种语言标题的文章直接显示数据库中多种语言合并在一起的标题效果,而并不是 qTranslate 插件根据客户端语言自动处理后的效果,这个问题在我使用的几个 WordPress 主题中都不会出现;
  3. 文章中插入的多行代码被合并成一行显示了,这是明显不能接受的;
  4. 文章的中文发表时间无法正确显示,统一显示为当前日期。

第一个问题需要修改 AMP for WP 插件的 wpamp-front.phpwpamp-functions.phpwpamp-header.php 这几个文件中转换地址的代码,例如 wpamp-front.php 文件的修改:

                $the_permalink = get_the_permalink( $post->ID );          }            -       $the_permalink = rtrim( $the_permalink, '/' ) . '/';  +       $the_permalink = rtrim( $the_permalink, '/' );          $perma = strpos( $the_permalink, "?" );          if ( $perma === false ) {  +               $the_permalink .= '/';                  $sConnector = "?";          } else {                  $sConnector = "&";  

第二个问题需要修改 wpamp-content.php 文件,文章标题要做 apply_filters 处理:

                 

第三个问题就比较简单了,将 simple_html_dom.php 文件中替换回车和换行符的处理去掉就可以了。

第四个文章发表时间的问题也可以修改 wpamp-content.php 文件简化处理:

                     <?php endif; ?>                       By <span class="amp-wp-author"><a href="<?php echo wp_amp_permalink( NULL, AMP_CONSTANT, get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a></span>  -                    <?php $the_time = date_i18n( get_option( 'date_format' ), strtotime( get_the_time('F jS, Y') ) ); ?>  -                    on <time datetime="<?php echo $the_time; ?>"><?php echo $the_time; ?></time>  +                    on <time datetime="<?php echo get_the_date('c'); ?>"><?php echo get_the_date('c'); ?></time>                   </li>  

另外我还对 AMP for WP 插件的页脚处理做了点小修改,将页脚上切换到博客首页桌面版本的链接改为切换到当前文章的桌面版本,方便访问者使用。

总结

对 AMP for WP 插件修改完成之后就可以访问博客的 AMP 版进行验证了,例如我的博客的 AMP 版首页地址就是:

https://zohead.com/?wpamp

普通用户可以通过安装 Google 推出的 AMP Validator 官方 Chrome 扩展检测所访问的 AMP 站点是否配置正确。

网站所有者可以用 Google 搜索的 AMP 在线测试工具进行检测:

https://search.google.com/test/amp

输入要测试的 AMP 版网址就可以开始检测,另外这个测试工具的另一个好处就是检测通过之后可以直接把你的 AMP 站点提交给 Google,让 Google 搜索蜘蛛尽快收录并进行 CDN 缓存。

现在通过 Google 移动端(例如 Android Chrome 浏览器)搜索我的博客,应该就能在搜索结果里看到下面的 AMP 闪电标志了:

Google 搜索结果 AMP 标志

点击搜索结果,你会发现访问的其实是 Google 缓存过的 AMP 地址,下面地址只有移动端浏览器才会显示 AMP 网页:

https://www.google.com/amp/s/zohead.com/%3fwpamp

初步启用 AMP 的大体感受就是如果你的梯子还可以,那访问 Google 缓存过的 AMP 网站速度还是非常快的,我这里估计访问 AMP 版网站所需要的流量大概为桌面版本的 1/3。

后面有时间的话还是希望能对 AMP for WP 插件继续改进,例如支持其它的 AMP 后缀或者使用专门的 AMP 子域名方便自己的服务器进行缓存之类的,最后祝大家玩的开心。

无相关文章.

Shared via Inoreader

WordPress 3.8开启Google AMP的问题