Turn any Linux computer into SOCKS5 proxy in one command

src: http://www.catonmat.net/blog/linux-socks5-proxy/

I thought I‘d do a shorter article on catonmat this time. It goes hand in hand with my upcoming article series on "100% technical guide to anonymity" and it‘s much easier to write larger articles in smaller pieces. Then I can edit them together and produce the final article.

This article will be interesting for those who didn‘t know it already -- you can turn any Linux computer into a SOCKS5 (and SOCKS4) proxy in just one command:

ssh -N -D 0.0.0.0:1080 localhost

And it doesn‘t require root privileges. The ssh command starts up dynamic -D port forwarding on port1080 and talks to the clients via SOCSK5 or SOCKS4 protocols, just like a regular SOCKS5 proxy would! The -N option makes sure ssh stays idle and doesn‘t execute any commands on localhost.

If you also wish the command to go into background as a daemon, then add -f option:

ssh -f -N -D 0.0.0.0:1080 localhost

To use it, just make your software use SOCKS5 proxy on your Linux computer‘s IP, port 1080, and you‘re done, all your requests now get proxied.

Access control can be implemented via iptables. For example, to allow only people from the ip1.2.3.4 to use the SOCKS5 proxy, add the following iptables rules:

iptables -A INPUT --src 1.2.3.4 -p tcp --dport 1080 -j ACCEPT
iptables -A INPUT -p tcp --dport 1080 -j REJECT

The first rule says, allow anyone from 1.2.3.4 to connect to port 1080, and the other rule says, deny everyone else from connecting to port 1080.

Surely, executing iptables requires root privileges. If you don‘t have root privileges, and you don‘t want to leave your proxy open (and you really don‘t want to do that), you‘ll have to use some kind of a simple TCP proxy wrapper to do access control.

Here, I wrote one in Perl. It‘s called tcp-proxy.pl and it uses IO::Socket::INET to abstract sockets, and IO::Select to do connection multiplexing.

#!/usr/bin/perl
#

use warnings;
use strict;

use IO::Socket::INET;
use IO::Select;

my @allowed_ips = (‘1.2.3.4‘, ‘5.6.7.8‘, ‘127.0.0.1‘, ‘192.168.1.2‘);
my $ioset = IO::Select->new;
my %socket_map;

my $debug = 1;

sub new_conn {
    my ($host, $port) = @_;
    return IO::Socket::INET->new(
        PeerAddr => $host,
        PeerPort => $port
    ) || die "Unable to connect to $host:$port: $!";
}

sub new_server {
    my ($host, $port) = @_;
    my $server = IO::Socket::INET->new(
        LocalAddr => $host,
        LocalPort => $port,
        ReuseAddr => 1,
        Listen    => 100
    ) || die "Unable to listen on $host:$port: $!";
}

sub new_connection {
    my $server = shift;
    my $client = $server->accept;
    my $client_ip = client_ip($client);

    unless (client_allowed($client)) {
        print "Connection from $client_ip denied.\n" if $debug;
        $client->close;
        return;
    }
    print "Connection from $client_ip accepted.\n" if $debug;

    my $remote = new_conn(‘localhost‘, 55555);
    $ioset->add($client);
    $ioset->add($remote);

    $socket_map{$client} = $remote;
    $socket_map{$remote} = $client;
}

sub close_connection {
    my $client = shift;
    my $client_ip = client_ip($client);
    my $remote = $socket_map{$client};

    $ioset->remove($client);
    $ioset->remove($remote);

    delete $socket_map{$client};
    delete $socket_map{$remote};

    $client->close;
    $remote->close;

    print "Connection from $client_ip closed.\n" if $debug;
}

sub client_ip {
    my $client = shift;
    return inet_ntoa($client->sockaddr);
}

sub client_allowed {
    my $client = shift;
    my $client_ip = client_ip($client);
    return grep { $_ eq $client_ip } @allowed_ips;
}

print "Starting a server on 0.0.0.0:1080\n";
my $server = new_server(‘0.0.0.0‘, 1080);
$ioset->add($server);

while (1) {
    for my $socket ($ioset->can_read) {
        if ($socket == $server) {
            new_connection($server);
        }
        else {
            next unless exists $socket_map{$socket};
            my $remote = $socket_map{$socket};
            my $buffer;
            my $read = $socket->sysread($buffer, 4096);
            if ($read) {
                $remote->syswrite($buffer);
            }
            else {
                close_connection($socket);
            }
        }
    }
}

To use it, you‘ll have to make a change to the previous configuration. Instead of running ssh SOCKS5 proxy on 0.0.0.0:1080, you‘ll need to run it on localhost:55555,

ssh -f -N -D 55555 localhost

After that, run the tcp-proxy.pl,

perl tcp-proxy.pl &

The TCP proxy will start listening on 0.0.0.0:1080 and will redirect only the allowed IPs in@allowed_ips list to localhost:55555.

Another possibility is to use another computer instead of your own as exit node. What I mean is you can do the following:

ssh -f -N -D 1080 other_computer.com

This will set up a SOCKS5 proxy on localhost:1080 but when you use it, ssh will automatically tunnel your requests (encrypted) via other_computer.com. This way you can hide what you‘re doing on the Internet from anyone who might be sniffing your link. They will see that you‘re doing something but the traffic will be encrypted so they won‘t be able to tell what you‘re doing.

That‘s it. You‘re now the proxy king!

Download tcp-proxy.pl

Download link: tcp proxy (tcp-proxy.pl)
Download URL: http://www.catonmat.net/download/tcp-proxy.pl
Downloaded: 6035 times

I also pushed the tcp-proxy.pl to GitHub: tcp-proxy.pl on GitHub. This project is also pretty nifty to generalize and make a program that redirects between any number of hosts:ports, not just two.

PS. I will probably also write "A definitive guide to ssh port forwarding" some time in the future because it‘s an interesting but little understood topic.

时间: 2025-01-17 13:38:49

Turn any Linux computer into SOCKS5 proxy in one command的相关文章

Ubuntu Linux下通过代理(proxy)使用git上github.com

github.com,作为程序员的代码仓库,我们经常会用到.但有时候我们不能直接通过网络链接它,只能通过代理. 这里我有一台代理服务器,起初我以为在终端设置了代理环境就行了,其设置为在你的~/.bashrc里增加以下几行: export http_proxy="http://proxy-server:3128/" export https_proxy="http://proxy-server:3128/" export ftp_proxy="http://

利用tinyproxy在Linux上搭建HTTP Proxy Server

之所以需要用到HTTP Proxy Server并不是为了要翻墙,而是为了让没有公网IP地址的内网主机通过有公网IP地址的外网主机访问Internet.举个例子,阿里云ECS在购买时可以不购买公网IP地址,但这种没有公网IP地址的ECS云主机(实例)是没有访问Internet的能力的,也就是说无法在这台实例上下载文件,这在部署应用如部署MySQL时可能遇到无法完成安装问题.解决的办法有两种,一种是在另一台具有公网访问能力的ECS实例上搭建VPN服务,另一种是在另一台具有公网访问能力的ECS实例上

〖Linux〗Ubuntu设定Proxy及忽略Proxy

1. 设定代理:. ~/.proxyenv 1 #!/bin/sh 2 3 # for terminal 4 export proxyserveraddr=123.123.123.123 5 export proxyserverport=8087 6 export HTTP_PROXY="http://$proxyserveraddr:$proxyserverport/" 7 export HTTPS_PROXY="https://$proxyserveraddr:$prox

〖Linux〗转换Socks Proxy为Http Proxy

使用工具,privoxy,官网: http://www.privoxy.org/ socks5 proxy设定方法: autossh -CgNfD  0.0.0.0:1080 vps-lxb socks5 proxy 转换成 http proxy 方法: sudo apt-get install privoxy vi /etc/privoxy/config,增加或修改以下两行: listen-address localhost:8118 forward-socks5 / 127.0.0.1:10

linux设置http/https proxy及忽略proxy的方法

一,场景: 有些linux服务器处于内网,并且没有公网ip,故要想与外网进行http/https通信只能通过nat或者加proxy的方式.nat服务器有网段的限制,而http/https proxy代理则没有,使用起来也方便.但是,使用http/https proxy的时候遇见两个问题: 1,本机去访问一个没有域名解析但是绑定hosts文件的域名的时候,导致访问失败 2,php-fpm中跑的php代码访问外网有域名解析的接口,访问失败 linux系统设置http/https proxy的方法,在

linux下配置QT5.12 ERROR: Unknown command line option '-no-xcursor'.!

xcursor是个什么东西.为什么会报错,怎么处理!在qt的autoconfigure.sh中添加下三条命令都不行.提示不认识-no-xcursor或者不认识nomake命令. -no-xcursor -no-xfixes -no-xrandr -no-xrender -no-separate-debug-info -no-fontconfig \-nomake examples -nomake tools -nomake tests -no-iconv \ linux下配置QT5.12 ERR

转-linux下配置socks5代理

简介: 在Linux下有各种各样的代理程序可用,象最常用的Squid,是http/https代理,也能代理ftp请求,但它实际上 是个HTTP代理程序,不是ftp代理,但它能处理ftp代理请求,就象浏览器处理ftp请求一样的方法工作,有些 程序只能设置成使用socks代理,象CuteFTP;还有象Wu-FTP只能设置成使用ftp代理(这里的ftp代理是标准的 ftp代理,不是Squid所支持的那种方式);ICQ 2000能同时接受https代理和Socks代理;NetVampire能接受标准 的

[转]linux terminal中使用proxy

转自:http://www.cnblogs.com/JoJosBizarreAdventure/p/5892383.html 在linux terminal中使用代理 方法一: terminal中输入 export http_proxy="http://web-proxy.xxxxxxxx.com" 方法二: .bashrc 中添加 export http_proxy="http://web-proxy.xxxxxxxx.com" 然后执行.bashrc 然后新建的

linux centos cli all proxy

linux centos 下代理http.https.ftp 全局使用代理: export http_proxy=http://host:port/ export https_proxy=http://host:port/ export ftp_proxy=http://host:port/ 使用wget按需代理: shell下:cp /etc/wgetrc ~/.wgetrc 取消注释 ~/.wgetrc 中http_proxy.https_proxy.ftp_proxy.use_proxy=