public void TunnelTest() { const int port = 65534; const string listenUrl = "http://127.0.0.1:65534/ping/"; const string expectedResponse = "pong"; // Create a local webserver using (HttpListener webserver = this.StartTestServer(listenUrl, expectedResponse)) { // Create the tunnel Tunnel tunnel = new Tunnel(port); tunnel.Execute(); // Check it is connected Assert.IsTrue(tunnel.IsConnected); Assert.IsFalse(tunnel.IsStopped); // Check the new host responds with the expected response. WebRequest request = WebRequest.Create(string.Format("http://{0}/ping/",tunnel.TunnelHost)); string response = (new StreamReader(request.GetResponse().GetResponseStream())).ReadToEnd(); Assert.AreEqual(response, expectedResponse); webserver.Close(); } }
/// <summary> /// Creates the localtunnel. /// </summary> /// <param name="port"></param> /// <param name="sshKeyName"></param> /// <param name="serviceHost"></param> private void CreateTunnel(string host, int port, string sshKeyName, string serviceHost) { stripStatus.Text = string.Format("Creating tunnel to port {0}...", port); Tunnel tunnel; if (File.Exists(_SSHPrivateKeyName)) { tunnel = new Tunnel(host, port, sshKeyName); } else { tunnel = new Tunnel(host, port); // We save the private key only since the public key will be // obtained saved later by Tunnel.Library. We could however save it, doesn't really matter. TextWriter tw = new StreamWriter(_SSHPrivateKeyName); tw.WriteLine(tunnel.PrivateKey); tw.Close(); } int idx = tunnelBindingSource.List.Count; tunnel.ServiceHost = serviceHost; tunnel.OnSocketException = new Tunnel.EventSocketException((tun, message) => { tun.StopTunnel(); List<Tunnel> list = new List<Tunnel>(); foreach (Tunnel t in tunnelBindingSource.List) { if (!t.TunnelHost.Equals(tun.TunnelHost)) { list.Add(t); } } SetBindingSourceDataSource(tunnelBindingSource, list); MessageBox.Show(message, "Connection closed"); }); tunnel.Execute(); tunnelBindingSource.Add(tunnel); Clipboard.SetDataObject(string.Format("http://{0}/", tunnel.TunnelHost)); stripStatus.Text = string.Format("Tunnel to {0} created!, copied to clipboard", port); txtPort.Value = 80; cmdTunnel.Enabled = true; // Update jump list CreateJumpList(); }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: "); Console.WriteLine(" localtunnel.exe [host:]port [/path/to/privatekey] - Will default to 127.0.0.1"); Console.WriteLine(" localtunnel.exe host[:port] [/path/to/privatekey] - Will default to port 80"); Console.WriteLine(""); } else { string[] connectTo = args[0].Split(':'); string hostname = connectTo[0]; string port = connectTo.Length > 1 ? connectTo[1] : string.Empty; int localPort = 0; // Try to guess if the first parameter is a host or a port if (string.IsNullOrEmpty(port)) { int.TryParse(hostname, out localPort); if (localPort == 0) { // Was a host, otherwise we would have gotten a number localPort = 80; } else { hostname = "127.0.0.1"; } } else { int.TryParse(port, out localPort); } // TODO check correctness of hostname syntax. if (localPort == 0) { Console.WriteLine("Introduce a valid port"); Console.WriteLine(""); } else { try { // Let the fun start! Tunnel tunnel; if (args.Length == 1) { tunnel = new Tunnel(localPort); } else { string sshkeypath = args[1]; if (!File.Exists(sshkeypath)) { Console.WriteLine("Private key file not found!"); Environment.Exit(1); } tunnel = new Tunnel(localPort, sshkeypath); } tunnel.OnSocketException = new Tunnel.EventSocketException((tun, msg) => { tun.ReOpenTunnel(); }); tunnel.Execute(); Console.WriteLine(string.Format("LocalTunnel created, you can now access {0}:{1} through: http://{2}/", tunnel.LocalHost, tunnel.LocalPort, tunnel.TunnelHost) ); Console.WriteLine("[Press any key to terminate tunnel]"); Console.ReadKey(); } catch { Console.WriteLine("Oops! there was an error while connecting. \nPlease check your port, and key files and try again."); } } } }