ocserv

来自Gea-Suan Lin's Wiki
跳到导航 跳到搜索

ocserv是一套支援Cisco AnyConnect协定的VPN伺服器软体。

安装

sudo apt install -y ocserv; sudo apt clean

设定

安装完后,在/etc/oserv/ocserv.conf内设定:

#
auth = "pam"
tcp-port = 8443
udp-port = 8443
run-as-user = nobody
run-as-group = daemon
socket-file = /var/run/ocserv-socket
server-cert = /etc/dehydrated/certs/vpn.example.com/fullchain.pem
server-key = /etc/dehydrated/certs/vpn.example.com/privkey.pem
ca-cert = /etc/ssl/certs/ca-certificates.crt
isolate-workers = true
max-clients = 120
max-same-clients = 32
keepalive = 32400
dpd = 90
mobile-dpd = 1800
try-mtu-discovery = false
cert-user-oid = 0.9.2342.19200300.100.1.1
tls-priorities = "NORMAL:%SERVER_PRECEDENCE:%COMPAT:-VERS-SSL3.0"
auth-timeout = 240
min-reauth-time = 3
max-ban-score = 50
ban-reset-time = 300
cookie-timeout = 300
deny-roaming = false
rekey-time = 172800
rekey-method = ssl
use-utmp = true
use-occtl = true
pid-file = /var/run/ocserv.pid
device = vpns
predictable-ips = true
default-domain = example.com
ipv4-network = 192.168.254.0
ipv4-netmask = 255.255.255.128
ipv6-network = fda9:4efe:7e3b:03ea::/48
dns = 8.8.8.8
split-dns = example.com
split-dns = example.net
ping-leases = false
route = 10.10.0.0/16
route = 10.20.0.0/16
cisco-client-compat = true
dtls-legacy = true

其中:

  • route设为default表示全部,或是逐条设定想要导入的路由。
  • split-dns主要用在内部网段(外部无法查询的),不一定会用到。

NAT

上面的例子中我们需要将192.168.254.0/25设定NAT:

sudo iptables -t nat -A POSTROUTING -s 192.168.254.0/25 -o ens5 -j MASQUERADE

然后用iptables-persistent套件提供的指令存入:

sudo iptables-save | sudo tee /etc/iptables/rules.v4

效率

另外,Ubuntu预设的systemd设定会使得DTLS(UDP)无法使用,只能使用TLS(TCP)而导致传输效率不佳,所以要修正:

sudo sed -i -E -e 's/^(Requires=ocserv.socket)/#\1/' -e 's/^(Also=ocserv.socket)/#\1/' /lib/systemd/system/ocserv.service; sudo systemctl stop ocserv; sudo systemctl disable ocserv.service; sudo systemctl disable ocserv.socket; sudo systemctl daemon-reload; sudo systemctl start ocserv; sudo systemctl enable ocserv

外部连结