通过 SSH 隧道的 OpenVPN 连接

Pet*_*ter 7 networking ssh vpn openvpn iptables

我目前正在访问中国,所以我有一些设置 VPN 的选项。但是,我的 VPN 服务器有一个习惯,在我使用一段时间后突然从网络中消失。

我认为使用 SSH 隧道连接到另一台服务器并通过它连接 VPN 可能是一种选择,以防止检测到 VPN 流量。这样,大概,流量只是作为到提供商的 SSH 连接读取。

所以,我连接到这样的服务器:

ssh peter@some-server -L 4444:vpn-server:1194 -N
Run Code Online (Sandbox Code Playgroud)

然后将其添加到我的 openvpn 客户端配置中:

remote localhost 1194
Run Code Online (Sandbox Code Playgroud)

可悲的是,这行不通。连接通过身份验证,但之后,我无法连接到 VPN 内部 ( ping 10.8.0.1) 或外部 ( ping 8.8.8.8)。这应该有效,还是我误解了什么?

我应该添加一些 iptables nat 规则吗?到目前为止,我添加的唯一 nat 规则是:

-A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)

cri*_*ret 6

通过 SSH 隧道设置 VPN 连接的简单方法是行不通的。第一个问题:你只隧道到VPN服务器本身,它不那么允许所有其他流量通过VPN服务器路由连接OVERssh连接(从而混淆了连接)。对此的解决方法是使用动态 SOCKS[5] 代理并告诉 OpenVPN 通过该代理进行连接。添加到您的 OpenVPN 配置文件:

socks-proxy localhost 6886
socks-proxy-retry
Run Code Online (Sandbox Code Playgroud)

然后,ssh使用动态 SOCKS 代理启动会话:

ssh -D 6886 -N REMOTE
Run Code Online (Sandbox Code Playgroud)

然后你就可以开始你的 OpenVPN 连接了。但是,这仍然有一个失败,至少假设您想通过 VPN(OpenVPN 指令)重定向所有流量redirect-gateway def1。为此,您需要维护到 SOCKS 代理端点的路由,该路由不会被 OpenVPN 客户端添加的路由所掩盖。为此,请在您的 OpenVPN 配置中添加另一个指令,如下所示:

route REMOTE-IP 255.255.255.255 net_gateway default
Run Code Online (Sandbox Code Playgroud)

可能能够在该指令中使用主机名 REMOTE,但您可能需要手动将其解析为 IP 地址。

这应该有效,至少对于 ipv4 流量。一个快速的谷歌搜索出现了这篇博客文章,它基本上做了同样的事情,对正在发生的事情有很好的描述,但在解决方案中似乎更复杂(使用连接脚本)

或者,您也可以考虑使用obfs4proxy(例如thisthis为 ubuntu 打包


Bos*_*rot 6

我可以通过 TCP 和外部 VPS 成功建立到我的 Raspberry Pi(使用 PiVPN)的连接。然后我这样做了:

允许在您的服务器(VPS/VPN)上转发到公共。如果尚不存在,则更改为或GatewayPorts添加。yes

/etc/ssh/sshd_config:
GatewayPorts yes
Run Code Online (Sandbox Code Playgroud)

更改proto udptcp(因为ssh使用tcp协议)

/etc/openvpn/server.conf:
proto tcp
Run Code Online (Sandbox Code Playgroud)

再次更改proto udptcp-client

openvpn-client.ovpn:
proto tcp-client
Run Code Online (Sandbox Code Playgroud)

现在启动 ssh 转发(0.0.0.0 允许所有 IPv4 连接;-N 不在服务器上启动 bash)

ssh -R 0.0.0.0:1194:localhost:1194 user@server -N
Run Code Online (Sandbox Code Playgroud)

您还应该检查您的 VPS/VPN 服务器是否允许连接并具有正确的防火墙设置。如果您将 ufw 用于 iptables,只需执行以下操作:

ufw allow 1194/tcp
ufw enable
Run Code Online (Sandbox Code Playgroud)

这可能不是最快的,但它对我有用。


小智 5

您不需要通过ssh 运行其他 VPN。您可以使用 ssh作为VPN。请参阅 ssh 的“-w X:Y”选项。

这是一个以Linux为中心的答案。假设有一个服务器,我们将其称为“集线器”,ssh 隧道客户端连接到该服务器。他们将使用 172.17.2.0/24 作为 VPN“运营商”网络。某些主机是常见且众所周知的,我们给它们固定的配置。其他人也可以连接,但必须使用其他配置。

#!/bin/sh -e
#
# make-ssh-tunnel HITHER YON INTERFACE GW
#
# HITHER ...... this host's VPN address.
# YON ......... $GW's VPN address.
# INTERFACE ... number of the tun device, same at both ends.
# GW .......... server host.
#
# /etc/ssh/sshd_config on $GW must include:
#   PermitRootLogin yes
#   PermitTunnel yes
# and clients must be able to ssh to $GW as root.
#
# this creates a star topology with $GW at center,
# which is generally the .1 address in the VPN /24 net.
# others connect and route through $GW to reach
# the other members of the /24 net.
# ________________________________________________
#
# the setup.
#
if [ "$#" = 0 ] ; then
    # automatic, for specific, known, regularly-connected hosts.
    case "`hostname -s`" in
    ThisHost) HITHER=172.17.2.2 IFACE=12 ;;
    ThatHost) HITHER=172.17.2.3 IFACE=13 ;;
    Another)  HITHER=172.17.2.4 IFACE=14 ;;
    *)        echo who are you? ; exit 1 ;;
    esac
    YON=172.17.2.1
    GW=hub
    SUBNET=172.17.2.0
else
    # manual, for random, probably temporary connections.
    HITHER="${1:-172.17.2.5}"
    YON="${2:-172.17.2.1}"
    IFACE="${3:-15}"
    GW="${4:-hub}"
    SUBNET="${YON%.[0-9]*}".0
    #
    [ "$HITHER" = "$YON" ] && echo "${0##*/}": identical addresses not allowed && exit 1
fi
#
# the business.
#
set -x
# 1st: do it there.
ssh -f -T -w $IFACE:$IFACE root@$GW \
"ifconfig tun$IFACE $YON pointopoint $HITHER netmask 255.255.255.255
 ifconfig tun$IFACE
 route -n | grep tun$IFACE"
# 2nd: do it here.
 ifconfig tun$IFACE $HITHER pointopoint $YON netmask 255.255.255.255
 ifconfig tun$IFACE
 route add -net $SUBNET/24 gw $YON
 route -n | grep tun$IFACE
#
# the afterthought.
#
# to make the tunnel a default route replacement:
#
# route add -host $GW gw 192.168.0.1 # that is, your real.defa.ult.route
# ip route add   0.0.0.0/1 via $YON
# ip route add 128.0.0.0/1 via $YON
#
# 1st preserves real route to $GW, avoiding route loop.
# 2nd and 3rd override default route without replacing it.
#
# in addition, $GW must NAT on behalf of the VPN hosts.
# iptables -t nat -A POSTROUTING -s 172.17.2.0/24 -o eth0 -j SNAT --to-source 11.22.33.44
# where --to-source is $GW's real external address.
#
# Embellish to taste.
Run Code Online (Sandbox Code Playgroud)