校园网 IPv6 免流量上网配置过程

压榨吃灰树莓派的剩余价值(一)

根据 RUC 校园网计费规则,通过 IPv6 访问互联网无论上传还是下载均不收费。一个自然的想法是合理利用这个规则来节省高达 $100 \%$ 的网费。运营商正好给我家分配了公网 v6 地址,所以我 19 年就买了个树莓派搭了代理服务器。

今年暑假我闲得没事想在树莓派上搭一个 QQbot 结果玩崩了,现在正巧又赶上因为疫情回不了学校,心想着索性把树莓派上的代理服务器之类的东西全部推倒重搭一遍。

系统采用的是 Ubuntu Server 22.04.1 LTS。在云服务器上配置操作同理。

安装 Shadowsocks

首先安装 pip,然后通过 pip 安装 shadowsocks 模块。

sudo apt install python3-pip
sudo pip install shadowsocks

这个过程中可能 pip 会报警告劝你不要用管理员权限运行 pip。但是由于 shadowsocks 运行时需要管理员权限,为了图省事还是直接 sudo 了。当然这里相对更好的办法是新建一个用户,给上管理员权限,禁掉登录之后专门用来跑 ssserver,虽然也就三四行指令,但我懒得搞。

完成后可以运行 ssserver -help 检查是否已经安装成功。这之后需要创建 Shadowsocks 的配置文件。我的配置文件大概长这个样子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
    "server": "server-name",
    "port_password": {
        "port": "password"
    },
    "local_address": "127.0.0.1",
    "local_port": 1080,
    "timeout": 300,
    "method": "aes-256-cfb",
    "fast_open": false
}

如果想多用户分开端口的话可以在 port_password 中添加。配置文件的具体写法可以参考 Shadowsocks 在 Github 上的 Wiki

之后直接启动即可。

sudo ssserver -c ~/.local/shadowsocks/config.json -d start

根据几年前的使用经验,这个 ssserver 运行时间过长会寄掉,原因不明,因此我还在 crontab 里设置了每天定期重启代理服务器。

至此代理服务器配置完成,在代理软件客户端设置连接即可。如果连接不上,先检查一下机器是否开了防火墙,如果开了记得添加相应端口的放行规则。

脚本兼容性

由于我的系统是 Ubuntu 22.04,预装的 python 版本是 3.10.6,其中个别 API 与 shadowsocks 模块的调用不一致,需要进行调整。

在运行 ssserver 时可能会遇到如下报错:

$ ssserver --help
Traceback (most recent call last):
  File "/usr/local/bin/ssserver", line 5, in <module>
    from shadowsocks.server import main
  File "/usr/local/lib/python3.10/dist-packages/shadowsocks/server.py", line 27, in <module>
    from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, \
  File "/usr/local/lib/python3.10/dist-packages/shadowsocks/udprelay.py", line 71, in <module>
    from shadowsocks import encrypt, eventloop, lru_cache, common, shell
  File "/usr/local/lib/python3.10/dist-packages/shadowsocks/lru_cache.py", line 34, in <module>
    class LRUCache(collections.MutableMapping):
AttributeError: module 'collections' has no attribute 'MutableMapping'

解决方案是将 /usr/local/lib/python3.10/dist-packages/shadowsocks/lru_cache.py 中的全部 collections.MutableMapping 替换为 collections.abc.MutableMapping

另外还有可能遇到类似 undefined symbol: EVP_CIPHER_CTX_cleanup 的错误,解决方案是将对应文件中的 EVP_CIPHER_CTX_cleanup() 全部替换为 EVP_CIPHER_CTX_reset()

上述两个问题都是由使用的系统/软件版本过新导致,并不影响使用。

参考文献:Ubuntu 18.04 下解决 shadowsocks 服务报错问题

DDNS

出于安全考虑,运营商分配的公网地址大概率不是静态的,为了保证长期相对稳定的使用体验还需要配一下 DDNS。

相关教程网上一搜一大堆,我自己用的基于 Cloudflare 的方案,至于脚本早就忘了从哪个教程贴偷的了。贴到这里供有需要的人使用。

  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
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#!/bin/bash

# CHANGE THESE
auth_email="__1__"      # CloudFlare 账户邮箱
auth_key="__2__"        # cloudflare 的 Globel ID
zone_name="__3__"       # 根域名
record_name="__4__"     # 完整域名
record_type="AAAA"      # A for ipv4 and AAAA for ipv6

ip_index="internet"     # "internet" or "local",使用本地方式还是网络方式获取地址
eth_card="__5__"        # 使用本地方式获取ip绑定的网卡,默认为eth0,仅本地方式有效

ip_file="$HOME/.local/ddns/ip.txt"            #保存地址信息,save ip information in the ip.txt
id_file="$HOME/.local/ddns/cloudflare.ids"
log_file="$HOME/.local/ddns/cloudflare.log"


if [ $record_type = "AAAA" ];then
    if [ $ip_index = "internet" ];then
        ip=$(curl -6 ip.sb)
    elif [ $ip_index = "local" ];then
        if [ "$user" = "root" ];then
            ip=$(ifconfig $eth_card | grep 'inet6'| grep -v '::1'|grep -v 'fe80' | cut -f2 | awk '{ print $2}' | head -1)
        else
            ip=$(/sbin/ifconfig $eth_card | grep 'inet6'| grep -v '::1'|grep -v 'fe80' | cut -f2 | awk '{ print $2}' | head -1)
        fi
    else
        echo "Error IP index, please input the right type"
        exit 0
    fi
elif [ $record_type = "A" ];then
    if [ $ip_index = "internet" ];then
        ip=$(curl -4 ip.sb)
    elif [ $ip_index = "local" ];then
        if [ "$user" = "root" ];then
            ip=$(ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')
        else
            ip=$(/sbin/ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')
        fi
    else
        echo "Error IP index, please input the right type"
        exit 0
    fi
else
    echo "Error DNS type"
    exit 0
fi

# 日志 log file
log() {
    if [ "$1" ]; then
        echo -e "[$(date)] - $1" >> $log_file
    fi
}

# SCRIPT START
# log "Check Initiated"

#判断ip是否发生变化,check the ip had been changed?
if [ -f $ip_file ]; then
    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
        echo "IP has not changed."
        exit 0
    fi
fi

#获取域名和授权 get the domain and authentic
if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
    zone_identifier=$(head -1 $id_file)
    record_identifier=$(tail -1 $id_file)
else
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" \
        -H "X-Auth-Email: $auth_email" \
        -H "X-Auth-Key: $auth_key" \
        -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=${record_type}&name=$record_name" \
        -H "X-Auth-Email: $auth_email" \
        -H "X-Auth-Key: $auth_key" \
        -H "Content-Type: application/json"  | grep -Po '(?<="id":")[^"]*')
    echo "$zone_identifier" > $id_file
    echo "$record_identifier" >> $id_file
fi

#更新DNS记录 update the dns
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
    -H "X-Auth-Email: $auth_email" \
    -H "X-Auth-Key: $auth_key" \
    -H "Content-Type: application/json" \
    --data "{\"type\":\"$record_type\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":300,\"proxied\":false}")


#反馈更新情况 gave the feedback about the update statues
if [[ $update == *"\"success\":true"* ]]; then
    message="IP changed to: $ip"
    echo "$ip" > $ip_file
    log "$message"
    echo "$message"
else
    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
    exit 1
fi

使用时候注意修改前几行的信息即可。至于 zone_namerecord_name 的区别,举个例子,假设你的域名是 example.com,然后你打算给你的代理服务器分配 proxy.example.com,则 zone_nameexample.comrecord_nameproxy.example.com

后记

随着本科毕业,成功升学可以免费使用千兆网络的 SJTU,树莓派上的代理服务器也完成了其历史使命,光荣退休。

Licensed under CC BY-NC-SA 4.0
最后更新于 Feb 04, 2024
ぶちまけちゃおうか 星に!
Built with Hugo
主题 StackJimmy 设计