分类 全部文章 下的文章

树莓派使用kcptun搭建透明代理,看1080p油管

之前写过使用shadowsocks搭建透明代理,由于shadowsocks使用的是tcp连接,而tcp连接速度比较好的服务器如阿里云香港都比较贵,基本看不起油管,最近比较火的kcptun是一个使用udp的加速代理,自带加密,只要不扩散基本没有隐私泄露危险,这个教程与shadowsocks基本相似。
大致原理:iptables转发出国ip数据到redsocks,然后redsocks转发给kcptun-client,kcptun-client发给kcptun-server,kcptun-server转发给要访问的墙外服务器。

1.解决dns解析被污染

1.安装chinadns和dnsmasq

git clone https://github.com/clowwindy/ChinaDNS
cd ChinaDNS
./configure && make
make install
apt-get install dnsmasq

dnsmasq是dns缓存服务器,目的是减少再次查询域名时间,首次dns查询可能200ms以上,再次查询只需要10ms。

2.编辑chinadns配置文件
添加开机启动脚本 /etc/init.d/chinadns
nano /etc/init.d/chinadns

#!/bin/sh
### BEGIN INIT INFO
# Provides: chinadns
# Required-Start: $network $local_fs $remote_fs $syslog
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start ChinaDNS at boot time
### END INIT INFO

DAEMON=/usr/local/bin/chinadns
DESC=ChinaDNS
NAME=chinadns
PIDFILE=/var/run/$NAME.pid

test -x $DAEMON || exit 0

case "$1" in start)
    echo -n "Starting $DESC: "
    $DAEMON \
        -m \
        -c /usr/local/share/chnroute.txt \
        -p 15353 \
        1> /var/log/$NAME.log \
        2> /var/log/$NAME.err.log &
    echo $! > $PIDFILE
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    kill `cat $PIDFILE`
    rm -f $PIDFILE
    echo "$NAME."
    ;;
  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

其中,-m 参数表示启用压缩指针(DNS pointer mutation),-c 指定 chnroute 文件,-p 指定监听的端口,没有指定将使用 dns 默认的 53 端口,-s 指定下游 dns 服务器。
更新chnroute文件,

cd /usr/local/share/
curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > chnroute.txt

可以用 crontab 添加定时任务,每周更新一次。
3.编辑dnsmasq配置文件

cd /etc/dnsmasq.s/
nano dnsmasq.conf
no-resolv
server=127.0.0.1#15353

2.数据穿过防火墙

下载对应的安装包,解压 https://github.com/xtaci/kcptun/releases
编辑配置文件client.json

{

"localaddr": ":8888",
"remoteaddr": "x.x.x.x:port",
"key": "password",
"crypt": "aes",
"mode": "fast2",
"conn": 1,
"autoexpire": 60,
"mtu": 1350,
"sndwnd": 128,
"rcvwnd": 1024,
"datashard": 5,
"parityshard": 5,
"dscp": 46,
"nocomp": false,
"acknodelay": false,
"nodelay": 0,
"interval": 40,
"resend": 0,
"nc": 0,
"sockbuf": 4194304,
"keepalive": 10 }

使用命令 client_darwin_amd64 -c client.json &启动,并把命令添加到rc.local实现开机启动

kcptun-server端安装方法见https://github.com/clangcn/kcp-server

3.解决墙内外数据分流

1.安装redsocks,和ipset

配置redsocks

redsocks {

local_ip = 0.0.0.0;
local_port = 12345; 
ip = 127.0.0.1;
port = 8888; }

配置完后记得重启redsocks

配置ipset

导入chnroutes

curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' |
grep ipv4 | grep CN | awk -F| '{ printf("%s/%d\n", $4,
32-log($5)/log(2)) }' > chnroute.txt

ipset create chnroute hash:net

cat chnroute.txt xargs -I ip ipset add chnroute ip

ipset save chnroute > /etc/chnroute.ipset #保存ipset
ipset restore < /etc/chnroute.ipset #导入ipset

以上可以设置一个定时任务每周刷新一次chnroutes

2.配置iptabels
可以先iptables-save > iptables,然后编辑iptables,再iptables-restore < iptables
添加如下

-A PREROUTING -p tcp -j SHADOWSOCKS
-A OUTPUT -p tcp -j SHADOWSOCKS
-A SHADOWSOCKS -d x.x.x.x/32 -j RETURN #kcptun-server ip
-A SHADOWSOCKS -d 0.0.0.0/8 -j RETURN
-A SHADOWSOCKS -d 10.0.0.0/8 -j RETURN
-A SHADOWSOCKS -d 127.0.0.0/8 -j RETURN
-A SHADOWSOCKS -d 169.254.0.0/16 -j RETURN
-A SHADOWSOCKS -d 172.16.0.0/12 -j RETURN
-A SHADOWSOCKS -d 192.168.0.0/16 -j RETURN
-A SHADOWSOCKS -d 224.0.0.0/4 -j RETURN
-A SHADOWSOCKS -d 240.0.0.0/4 -j RETURN
-A SHADOWSOCKS -m set --match-set chnroute dst -j RETURN
-A SHADOWSOCKS -p tcp -j REDIRECT --to-ports 12345

最后在rc.local添加iptables-restore < iptables,开机加载防火墙规则

4.上机测试

配置客户端网关和dns为树莓派地址,打开浏览器测试墙外网站

如果有问题,检查树莓派上几个服务配置完后是否重启,
使用ping,traceroute,dig,telnet等命令排查

Have a good luck!

nginx跨根目录rewrite配置

最近遇到了一个nginx配置难题
一个路由使用的不是root配置的根目录,然后还需要跳转,一下卡了一个月,幸亏不着急,昨天总算解决了

server {

listen 443 ssl;
server_name example.com;
root /www/current/public;

...

location ^~ /nginx/test/ {
    alias /home/test/nginxtest/;
    try_files $uri $uri/ /nginx/test/index.html;
    }

...

问题出在alias上,下面的location里使用root的话会在访问example.com/nginx/test会去找/home/test/nginxtest/nginx/test/index.html,相当于把路由插到路径里面了,使用alias就会直接找/home/test/nginxtest/index.html。需求是在访问example.com/nginx/test/{任意字符串}rewrite到example.com/nginx/test/index.html,而使用alias不支持rewrite,问题就出在这,虽然有很多解决办法,如后端改写路由等,但都不够完美。
nginx上一种解决办法是在前面插入一个location,rewrite到这个

location /nginx/test {

rewrite /nginx/test(.*) /test/index.html last;

}

location /test/ {

alias /home/test/nginxtest/;

index index.html;

}

但是这有个问题就是原来目录下的其他文件不能载入,如.js,.css,图片等,虽然可以使用if来解决但是都太麻烦

后来想到了try_files这个配置

一开始配置try_files $uri $uri/ /index.html
发现还是404,查看log发现找的文件是/www/current/public/index.html,去找根目录的index.html
这时隐隐感觉可以在index.html前插入路由/nginx/test/可以,因为之前在哪看到过nginx是会先找路径然后找路由,都没有才会返回404.
修改之后成功。访问example.com/nginx/test/{任意字符串},浏览器显示的还是example.com/nginx/test/{任意字符串},但是上找到文件是/home/test/nginxtest/下的index.html和js等文件。

以上方法只适用于一级路由,多级路由不行

更新配置

location ~ \.(js|jpg|jpeg|png|bmp|swf|css|ico)$ {
    rewrite (.*)/((.*)\.(js|jpg|jpeg|png|bmp|swf|css|ico))$ /$2 break;
}

location /api/v1 {
   proxy_pass https://eexample.com/api/v1
}

location / {
    try_files $uri $uri/ /index.html;
}

说明:
第一个location捕获js,css等静态文件
第二个location捕获api后台接口
第三个location捕获index.html

alias不能用于if和location @{字符串}字段

ps:如果报500错误,log里出现rewrite or internal redirection cycle,是因为找不到index.html文件

fail2ban封锁ip效果与防火墙封锁ip

fail2ban封锁ip后ssh登录显示connection refused
iptables封锁也是显示connection refused
ssh server退出后,没有22端口监听连接也是connection refused

cisco ap2702i胖ap关闭led指示灯

ap指示灯对家用来说有点多余,而且亮度不低,正常状态下又是蓝光,晚上影响睡眠,所以决定关闭。搜索到的方法大部分是针对ac的,要不就是设置led状态的,没有说胖ap怎么关的,试了一下成功了,记录下来。
方法:
telnet管理员登陆ap

config
led display off


User Access Verification

Username: admin
Password:
ap#?
Exec commands:
  <1-99>           Session number to resume
  access-enable    Create a temporary Access-List entry
  access-profile   Apply user-profile to interface
  access-template  Create a temporary Access-List entry
  archive          manage archive files
  cd               Change current directory
  clear            Reset functions
  clock            Manage the system clock
  configure        Enter configuration mode
  connect          Open a terminal connection
  copy             Copy from one file to another
  crypto           Encryption related commands.
  debug            Debugging functions (see also 'undebug')
  delete           Delete a file
  dir              List files on a filesystem
  disable          Turn off privileged commands
  disconnect       Disconnect an existing network connection
  do-exec          Mode-independent "do-exec" prefix support
  dot11            IEEE 802.11 commands
  dot1x            IEEE 802.1X Exec Commands
  eap              EAP Exec Commands
  enable           Turn on privileged commands
  erase            Erase a filesystem
  exit             Exit from the EXEC
  format           Format a filesystem
  fsck             Fsck a filesystem
  help             Description of the interactive help system
  if-mgr           IF-MGR operations
  ip               Exec commands for IP features
  led              LED functions
  lock             Lock the terminal
  logging          Handles logging operations
  login            Log in as a particular user
  logout           Exit from the EXEC
  mkdir            Create new directory
  monitor          Monitoring different system events
  more             Display the contents of a file
  name-connection  Name an existing network connection
  no               Disable debugging functions
  ping             Send echo messages
  pwd              Display current working directory
  radius           radius exec commands
  release          Release a resource
  reload           Halt and perform a cold restart
  rename           Rename a file
  renew            Renew a resource
  resume           Resume an active network connection
  rmdir            Remove existing directory
  routing-context  Routing Context
  rsh              Execute a remote command
  save             Start to save raise_interrupt_level stack
  send             Send a message to other tty lines
  set              Set system parameter (not config)
  show             Show running system information
  ssh              Open a secure shell client connection
  systat           Display information about terminal lines
  telnet           Open a telnet connection
  terminal         Set terminal line parameters
  test             Test subsystems, memory, and interfaces
  traceroute       Trace route to destination
  tunnel           Open a tunnel connection
  undebug          Disable debugging functions (see also 'debug')
  verify           Verify a file
  where            List active connections
  write            Write running configuration to memory, network, or terminal
  xconnect         Xconnect EXEC commands

ap#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line.  End with CNTL/Z.
ap(config)#led?
led

ap(config)#led
% Incomplete command.

ap(config)#led-stat
              ^
% Invalid input detected at '^' marker.

ap(config)#led display
% Incomplete command.

ap(config)#led display?
display

ap(config)#led display ?
  dim  Turn LED display dim.
  off  Turn LED display off.

ap(config)#led display off

记录一下2016年8月willnet.net迁移

这个美国服务器选的是ubuntu16.04,默认安装了apache2,对apache不熟悉,偷懒想apache简单配一下也能凑合用,大不了关了https。
途中遇到502,原因是php7的apache2 的module没装,apt-get安装后重启服务可以了,想着letsencrypt可以auto自动配置apache的证书,结果不行。而且chrome访问老是跳到https,然后无法访问。
最后还是安装了nginx,从香港服务器拷贝过来的配置文件不是很匹配,香港服务器使用的是oneinstack一键脚本安装的,配置文件路径和用apt安装的不一致,修改后还是不行,出现502,排查发现是fastcgi的socks路径不对,修改之后成功。
总之,一键脚本是方便,但是也有很多坑,现在大部分软件都有自己的源,完全可以用apt方式安装最新版,没有太大必要编译安装,而且自己写conf也有助于理解软件逻辑。

网站内容直接mysql导出数据库,新服务器导入即可