3

SSH的三种端口转发

 1 year ago
source link: https://blog.p2hp.com/archives/9633
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本地转发,顾名思义就是把本地主机端口转发到远程主机端口。

ssh -L 本地主机端口:远程主机:远程主机端口

举例:ssh -L 50000:www.google.com:80 user@host

当成功执行上面的命令之后,访问本地的50000端口,就等同于访问 www.google.com 的 80 端口。

这次是通过ssh主机来安全转发数据的,没有人知道你和远程主机之间传输了何种数据。

现在就算你能访问 www.google.com,你却依然不能其它主机,比如www.youtobe.com。

想要更全面的端口转发功能,还需动态转发。

2、远程转发

设想你在本机开发了一个web应用,想拿给别人看看,但现在你却处在内网,怎么办?

远程转发,顾名思义就是把远程主机端口转发到本地主机端口。

ssh -R 登录主机端口:远程主机:远程主机端口

举例:ssh -R 8080:localhost:80 user@host

当成功执行上面的命令之后,访问ssh主机的 8080 端口就相当于访问 localhost:80

3、动态转发

动态转发无需指定远程主机及其端口

这个端口号需要在本地通过协议指定,该协议就是简单、安全、实用的 SOCKS 协议。

ssh -D 本地主机:本地主机端口

举例:ssh -D 127.0.0.1:50000 user@host

通过协议协商后,告诉你要访问的远程主机及端口,然后你与远程主机之间的数据就通过ssh主机传输了。

最常见的用途:在浏览器中设置代理类型为 SOCKS5,主机及端口:127.0.0.1:50000。

4、微信公众号调试

外网环境:

112.x.x.x  主机有固定ip和域名

192.168.1.251

内网访问微信服务器,需要在251执行

ssh -D 0.0.0.0:1085 -l root 112.x.x.x -p xx -Nf -o ServerAliveInterval=60

http设置socks代理

    public HttpClientExecuteWithSocks() {

        Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", new CustomConnectionSocketFactory(SSLContexts.createSystemDefault()))
                .build();

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
        this.httpclient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();

        InetSocketAddress socksaddr = new InetSocketAddress("192.168.1.251", 1085);
        this.httpClientContext = HttpClientContext.create();
        this.httpClientContext.setAttribute("socks.address", socksaddr);
    }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK