如何使用 redsocks 将所有互联网流量重定向到 ssh 隧道?

如何使用 redsocks 将所有互联网流量重定向到 ssh 隧道?

我有一个 OpenWrt 路由器,其 IP 为192.168.1.1我想将所有互联网流量重定向到我的 ssh 隧道,这是我使用的 ssh 命令:

ssh -o "ProxyCommand corkscrew 127.0.0.1 3339 %h %p" -vND 1080 username@serverip

这是我的redsocks.conf,如果有任何问题请纠正:

base {
        // debug: connection progress & client list on SIGUSR1
        log_debug = on;

        // info: start and end of client session
        log_info = on;

        /* possible `log' values are:
         *   stderr
         *   file:/path/to/file
         *   syslog:FACILITY  facility is any of "daemon", "local0"..."local7"
         */
        log = stderr;

        // detach from console
        daemon = off;

        /* Change uid, gid and root directory, these options require root
         * privilegies on startup.
         * Note, your chroot may requre /etc/localtime if you write log to syslog.
         * Log is opened before chroot & uid changing.
         */
        // user = nobody;
        // group = nobody;
        // chroot = "/var/chroot";

        /* possible `redirector' values are:
         *   iptables   - for Linux
         *   ipf        - for FreeBSD
         *   pf         - for OpenBSD
         *   generic    - some generic redirector that MAY work
         */
        redirector = iptables;
}

redsocks {
        /* `local_ip' defaults to 127.0.0.1 for security reasons,
         * use 0.0.0.0 if you want to listen on every interface.
         * `local_*' are used as port to redirect to.
         */
        local_ip = 0.0.0.0;
        local_port = 12345;

        // `ip' and `port' are IP and tcp-port of proxy-server
        ip = 127.0.0.1;
        port = 1080;


        // known types: socks4, socks5, http-connect, http-relay
        type = socks5;

        // login = "foobar";
        // password = "baz";
}

redudp {                                                         
        // `local_ip' should not be 0.0.0.0 as it's also used for outgoing
        // packets that are sent as replies - and it should be fixed      
        // if we want NAT to work properly.                               
        local_ip = 127.0.0.1;                                             
        local_port = 10053;                                               

        // `ip' and `port' of socks5 proxy server.                        
        ip = 127.0.0.1;                                                   
        port = 1080;                                                      
        //login = username;                                               
        //password = pazzw0rd;                                            

        // kernel does not give us this information, so we have to duplicate it
        // in both iptables rules and configuration file.  By the way, you can 
        // set `local_ip' to 127.45.67.89 if you need more than 65535 ports to 
        // forward ;-)                                                         
        // This limitation may be relaxed in future versions using contrack-tools.
        dest_ip = 8.8.8.8;                                                        
        dest_port = 53;                                                           

        udp_timeout = 30;                                                         
        udp_timeout_stream = 180;                                                 
}                                                                                 

// you can add more `redsocks' and `redudp' sections if you need.       

如何使用 redsocks 将我的互联网流量重定向到该 ssh 隧道?我希望它像 openvpn 一样,我只需连接到我的 wifi 并浏览互联网,使用 ssh 隧道时,每次我想将所有互联网流量重定向到该隧道时,我都必须在客户端将代理设置为 socks 端口 1080。我是 linux 新手,我被困在 iptables 命令上,我已阅读说明http://darkk.net.ru/redsocks/但我仍然无法让它工作。请告诉我上面路由器的正确 iptables 命令

答案1

我正在使用 transocks,但是 iptables 的配置是相同的:

看起来像这样:https://gist.github.com/andersondanilo/a28e7165fa8a9700d8ead20a224ecf44

您的 redsocks 配置看起来不错。

我的例子是 transocks,但它们的作用是相同的。

transocks 和 redsocks 都会在你的网络上打开一个端口,你可以将所有流量重定向到该端口,redsocks 将重定向到所需的 socks5 代理

对我有用的一个建议的IP表配置是:

#!/usr/bin/bash

# Transocks: https://github.com/cybozu-go/transocks

set -e
stty -echoctl

# Point to the transparent socket port (running in an exclusive user)
TRANSOCKS_PORT=12345
TRANSOCKS_USER=transocks

# Redirect all the network of your computer (except transocks user)
REDIRECT_LOCAL_NETWORK=1

# Redirect access point (wifi hotspot)
AP_SUBNET_ENABLED=1
AP_SUBNET_IFACE=ap0
AP_SUBNET_RANGE="192.168.12.0/24"

function action_up()
{
    echo "-----------------------------"
    echo "# Adding iptables chain rules"
    echo "-----------------------------"
    iptables -v -t nat -N TRANSOCKS
    iptables -v -t nat -A TRANSOCKS -d 0.0.0.0/8 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 10.0.0.0/8 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 100.64.0.0/10 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 127.0.0.0/8 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 169.254.0.0/16 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 172.16.0.0/12 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 192.168.0.0/16 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 198.18.0.0/15 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 224.0.0.0/4 -j RETURN
    iptables -v -t nat -A TRANSOCKS -d 240.0.0.0/4 -j RETURN
    iptables -v -t nat -A TRANSOCKS -p tcp -j REDIRECT --to-ports $TRANSOCKS_PORT

    if [ "$REDIRECT_LOCAL_NETWORK" = 1 ]; then
        echo "--------------------------------"
        echo "# Redirecting non-transocks user"
        echo "--------------------------------"
        iptables -v -t nat -A OUTPUT -p tcp -m owner ! --uid-owner $TRANSOCKS_USER -j TRANSOCKS
    fi

    if [ "$AP_SUBNET_ENABLED" = 1 ]; then
        echo "-----------------------"
        echo "# Redirecting AP subnet"
        echo "-----------------------"
        iptables -v -t nat -I PREROUTING -i $AP_SUBNET_IFACE -s $AP_SUBNET_RANGE -j TRANSOCKS
        iptables -v -I INPUT -i $AP_SUBNET_IFACE -s $AP_SUBNET_RANGE -p tcp -m tcp --dport $TRANSOCKS_PORT -j ACCEPT
    fi
}

function action_down()
{
    if [ "$REDIRECT_LOCAL_NETWORK" = 1 ]; then
        echo "------------------------------"
        echo "# Cleaning non-transocks rules"
        echo "------------------------------"
        iptables -v -t nat -D OUTPUT -p tcp -m owner ! --uid-owner $TRANSOCKS_USER -j TRANSOCKS
    fi

    if [ "$AP_SUBNET_ENABLED" = 1 ]; then
        echo "--------------------------"
        echo "# Cleaning AP subnet rules"
        echo "--------------------------"
        iptables -v -t nat -D PREROUTING -i $AP_SUBNET_IFACE -s $AP_SUBNET_RANGE -j TRANSOCKS
        iptables -v -D INPUT -i $AP_SUBNET_IFACE -s $AP_SUBNET_RANGE -p tcp -m tcp --dport $TRANSOCKS_PORT -j ACCEPT
    fi

    echo "-----------------------------"
    echo "# Cleaning and removing chain"
    echo "-----------------------------"
    iptables -v -F TRANSOCKS -t nat
    iptables -v -X TRANSOCKS -t nat
}

trap 'action_down' SIGINT

action_up

echo
echo "Hit Ctrl+C to remove the ip table rules"
echo


while :
do
    sleep 1
done

相关内容