如何透過 SOCKS 在 SSH 上建立 HTTP 隧道?

如何透過 SOCKS 在 SSH 上建立 HTTP 隧道?

這應該是一個很容易解決的問題,但由於某種原因它對我不起作用。

ssh vps工作得很好(我使用身份驗證金鑰)

我使用以下命令設定隧道:

ssh -C2TNv -D 8080 vps

然後我修改 Firefox 網路設定:

  • 手動配置
    • http代理:本機主機,連接埠:8080
    • 將此代理伺服器用於所有協議
    • 襪子 v5
  • 關於:配置
    • network.proxy.socks_remote_dns:true

終端輸出:

$ ssh -C2TNv -D 8080 vps
OpenSSH_6.0p1, OpenSSL 1.0.1a 19 Apr 2012
debug1: Reading configuration data /home/ting/.ssh/config
debug1: /home/ting/.ssh/config line 47: Applying options for vps
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to vps.server.com [1.1.1.1] port 22.
debug1: Connection established.
debug1: identity file /home/ting/.ssh/id_rsa type 1
debug1: identity file /home/ting/.ssh/id_rsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.5p1 Debian-6+squeeze1
debug1: match: OpenSSH_5.5p1 Debian-6+squeeze1 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 [email protected]
debug1: kex: client->server aes128-ctr hmac-md5 [email protected]
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA <removed>
debug1: Host 'vps.server.com' is known and matches the RSA host key.
debug1: Found key in /home/ting/.ssh/known_hosts:10
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/ting/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).
Authenticated to vps.server.com ([1.1.1.1]:22).
debug1: Local connections to LOCALHOST:8080 forwarded to remote address socks:0
debug1: Local forwarding listening on ::1 port 8080.
debug1: channel 0: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 8080.
debug1: channel 1: new [port listener]
debug1: Requesting [email protected]
debug1: Entering interactive session.

然後我嘗試使用 Firefox 訪問網站,SSH 輸出:

debug1: Connection to port 8080 forwarding to socks port 0 requested.
debug1: channel 2: new [dynamic-tcpip]
debug1: channel 2: free: dynamic-tcpip, nchannels 3
debug1: Connection to port 8080 forwarding to socks port 0 requested.
debug1: channel 2: new [dynamic-tcpip]
debug1: channel 2: free: dynamic-tcpip, nchannels 3

儘管代理看似有效,但使用 Firefox 造訪任何網站都會傳回錯誤「連線已重設」。

答案1

你的 SSH 命令帶有 -D 就可以了(所以 SOCKS 將建立隧道,它只是透過 HTTP 連接到 SOCKS,你必須解決這個問題)

我得到了很好的輸出

curl --socks5 127.0.0.1:8080 http://blah

但如果我這樣做的話,我會得到同樣的錯誤輸出

 curl --proxy 127.0.0.1:8080 http://blah

因此,firefox 的連線就像 HTTP 代理一樣

查看 Firefox 視窗

手動配置 是

你說你勾選了“對所有協定使用此代理伺服器”,這是一個完全錯誤的舉動!您想要輸入 SOCKS 代理,如果您這樣做,那麼 SOCKS 框將變為空/灰色,並且您只能輸入 HTTP 代理。

所以不要勾選它。

然後輸入socks代理ip。

並刪除它說沒有 127.0.0.1,localhost 代理程式的地方。如果它這麼說,那就是預設的。

答案2

要透過curl 檢查連接,您還可以使用標誌-I -v(僅獲取HTTP 標頭並讓輸出更加生動)。

如果選擇這些標誌並捲曲連接 - 您將在輸出字串中看到如下內容:

* Rebuilt URL to: http://www.google.ru/
*   Trying ::1...
* 87
* 245
* 198
* 44
* Connected to localhost (::1) port 8080 (#0)

如果無法連線:

* Rebuilt URL to: http://www.google.ru/
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused

在另一個具有 ssh 連線的終端機標籤中,您將看到如下內容:

debug1: channel 2: new [dynamic-tcpip]
debug1: channel 2: free: direct-tcpip: listening port 9999 for 87.245.198.44 port 80, connect from ::1 port 55034 to ::1 port 8080, nchannels 3

相關內容