0 votes
by (160 points)
edited

I want to use rebex to creat a SSH port forwarding capabilitie applicatication like as tunnelier. who can help me ?

3 Answers

+2 votes
by (148k points)
edited by

Update: A simple API for local and remote SSH port forwarding (tunneling) has been added in Rebex Terminal Emulation 2016 R1. Check out some sample code, and sorry for the long wait!

The SSH functionality required to make this possible is already included in both Rebex SFTP and Rebex SSH Shell and can be used to implement a Tunnelier-like application, but we have yet to add a simple API to make this easy. This is planned for one of the future releases.

If you don't want to wait, download our SshTunnel sample application (C# and VB.NET) and give it a try - it uses the current version of Rebex SFTP or SSH Shell to tunnel connections from the specified local port through the specified SSH server to the specified target IP address and port.

The application forwards all traffic from the IP/port in the "Local" field (127.0.0.1:8080 by default) to IP/port in the "Remote" field (209.85.129.99:80 by default). The default tunnelling setting tunnels connections to port 8080 to port 80 of google.com, making it possible to connect to Google through the specified SSH server by typing http://localhost:8080/ in the local web browser. Replacing Google's IP/port (209.85.129.99) will make it tunnel to a different address.

C# and VB.NET source code is included with the sample application. The two classes in Tunnel.cs are the sample core. Any feedback is welcome!

by (160 points)
Thanks for your reply. That's mean i only possible to connect to a server (not a hosting) ? Then, it's so hard. Have any way to connect to a any website through the ssh server ? Thanks
by (148k points)
Well, you CAN specify a hostname instead of IP address, but that won't make it possible to "connect to any website" from your web browser. To do that, you would have to add a HTTP or Socks proxy server to accept incoming connections (from a web browser) and tunnel them through an SSH server (which is what the SshTunnel sample does). We don't offer any proxy server implementation at the moment (only proxy clients), so you would have to write that yourself. What are you actually trying to achieve? Perhaps a third-party application like Bitvise Tunnelier might be more suitable for your needs?
by (160 points)
Yes. That's all i need. Can you give a sample ?
by (148k points)
What sample? It looks like you are interested in a general-purpose Tunnelier-like application. I tried to explain that a HTTP or Socks proxy server functionality would be needed for this and that we DON'T currently have such component (or sample code). Sorry!
by (160 points)
Then, if i have a Socks proxy, how can i do that with sshtunnel sample ?
by (148k points)
I am affraid I don't quite understand your question. If you already have a Socks4/Socks5 server code, enhance it to take advantage of the OutgoingTunnel class (from the sample above) to be able to tunnel through SSH. We don't have any Socks4/Socks5 server code yet, so I'm unable to provide any sample for that, unfortunately.
by (160 points)
Sorry but i'm still can't understand how can i do that. I already have Socks 5 sever code, can you let me know how can i edit Outgoingtunnel class to be able to tunnel through SSH ? Thanks :)
by (148k points)
If you already have Socks 5 server code, then there is a place in your code where you have a Socket (connected client) and a remote hostname (or IP address)/port you need to tunnel it to. Normally, you would now open a new Socket to the remote address. But instead, use SshSession object's OpenTcpIpTunnel (like the sample app) to open an SSH tunnel and then use Tunnel.StartTunnelling (from the sample app) to forward data between the Socket (connected client) and the SshChannel (SSH tunnel). I can't be more specific because I don't have access to your Socks5 server code.
by (160 points)
Can you give a vb.net source with a example socks5 server code ?
by (148k points)
Sorry, we don't have any example Socks5 server code.
by (160 points)
Is it like this: 67.78.543.2:2040 ?
by (148k points)
I don't understand this question. What does "it" mean? Also, 67.78.543.2 is not even a valid IP address.
by (160 points)
How does socks5 server code form?
by (148k points)
I am sorry, but I don't understand the last question either. I asked one of my colleagues to send you an e-mail and try to explain what we can offer and what we can't. Sorry for inconvenience!
0 votes
by (160 points)
edited

I only can access to google page when use it. How can i access to another page such as whatismyip.com or microsoft.com with it ?

I like VB.NET code, can you give me it ? tks

by (148k points)
I edited my original answer to include a link to a ZIP file with both C# and VB.NET version, please download it again. I also added more thorough description of what it actually does. To tunnel to a different website, use another IP address instead of 209.85.129.99, but it has to be a website that actually runs at that IP address (unlike google.com, microsoft.com does listen at 207.46.192.254, but redirects the browser to a hostname-based address). If you have more questions, please add your comments to my original reply.
by (270 points)
edited

Lukas, thank you for the sample. It's helping a lot. One question though: can you do tunneling in the other direction? So that when something connects to a specific port on the SSH server, the communication is redirected to the SSH client to a specific port?

by (148k points)
edited

Stefan - yes, there are StartTcpIpForward/StopTcpIpForward/AcceptTcpIpForward and OpenTcpIpTunnel methods in the SshSession class that can be used for this, but these are not sufficiently documented yet. We plan to write a sample application for them soon.

However, there are not hard to use - you just have to establish a tunnel using StartTcpIpForward method and then call the AcceptTcpIpForward to accept incoming connections. Once the AcceptTcpIpForward returns a channel, you can (for example) pass it to a worker thread and call AcceptTcpIpForward again to accept another connection (in case you support multiple simultaneous connections).

The "address" value is passed to the server as-is and it's described here in RFC 4254. Please note that many servers will only allow listens on loopback addresses (such as locahost/127.0.0.1).

by (270 points)
edited

Hi Lukas, there's an issue with your sample you've provided: if the remote end of tunnel isn't listening when I connect the app will crash when I connect to the local port.

It's crashing at the line StartTunneling(socket, channel); in the OutgoingTunnel implementation. The exception is: [Rebex.Networking]Rebex.Net.SshException: Cannot open channel; connect failed. Connection refused.

Any idea how to handle this situation?

by (148k points)
edited

We'll look into this. In the meantime, please try a different tunneling sample from http://www.rebex.net/getfile/901f8b8b1b5549b7ad8243dc2dfc412f/SshTunnelNew-VB.zip (VB only at the moment). It solves several issues reported here. To add tunnels, edit Form1.vb file.

0 votes
by (140 points)
edited

Hi,

I'm trying to run your sample code, but after 15-30 seconds of inactivity, Receiver method gets a SocketException with ErrorCode 10053 or 10054. If I ignore this error, the connection re-establish itself and the data continues to go through the tunnel. On the other hand, when I pull my Ethernet cable from my computer I get exactly the same exception.

I need to distinguish between the too cases, so I will be able to display the connection status. Do you have any idea how to do it?

Thanks, Roy

by (148k points)
edited

We'll look into this. In the meantime, please try a different tunneling sample from http://www.rebex.net/getfile/901f8b8b1b5549b7ad8243dc2dfc412f/SshTunnelNew-VB.zip (VB only at the moment). It solves several issues reported here. To add tunnels, edit Form1.vb file.

by (140 points)
edited

Hi Lukas. Thank you for the code you have sent. It does look and handle errors better. I don't get false disconnection exceptions which is very good. I currently have several problems: 1. The code does not work on the .NET3.5CF. 2. It still takes ~30 seconds to detect disconnection. 3. After disconnection, I can't reconnect. I'm getting the following exception: "System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted"

...