[Hỏi ngu] vấn đề Forward port trong SSH.NET

doihai123

Junior
Joined
Jun 7, 2013
Messages
295
Reactions
203
MR
0.264
Skype
Chat with me via Skype
mình gặp 1 vấn đề nhỏ mong anh em đi trước có kinh nghiệm chỉ giáo vài đường
mìn đang test thử viết vài dòng code .NET fake IP bằng SSH cái này quá phổ biến ở mmo4me rồi mấy pro chắc ai cũng biết. cơ mà với mình thì nó vẫn mơ hồ quá, sau một hồi tìm hiểu thì viết dc đến đây, đã thiết lập được kết nối nhưng firefox vẫn báo lỗi The connection was reset. mặc dù port đã mở và listen được sau một hồi google ko hiệu quả mình đành post lên đây, hy vọng có ai đi qua chỉ mình giáo mình vài đường. Mình rất cảm ơn. mới SSH.net đã phức tạp vậy rồi để code được đùng bitwise client chắc mất cả mấy tháng quá :eek:
PHP:
public void Start()
{
      using (var client = new SshClient("host", "username", "pass"))
      {
           client.KeepAliveInterval = new TimeSpan(0, 0, 30);
           client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
           client.Connect();
           ForwardedPortDynamic port = new ForwardedPortDynamic("127.0.0.1", 8080);
           client.AddForwardedPort(port);
           port.Exception += delegate(object sender, ExceptionEventArgs e)
           {
                Console.WriteLine(e.Exception.ToString());
           };
           port.Start();
           System.Threading.Thread.Sleep(1000 * 60 * 60 * 8);
           port.Stop();
           client.Disconnect();
     }
đây là config trong firefox
http://prntscr.com/9tppus
 
Last edited:
Bitvise luôn đi, thằng nỳ connection dễ gãy.

Class của anh Phương đây:
PHP:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using ManagedWinapi.Windows;

namespace WindowsFormsApplication7
{
  public static class BitviseHandle
  {
  #region Windows API - user32.dll configs
  private const int WM_CLOSE = 16;
  private const int BN_CLICKED = 245;
  private const int WM_LBUTTONDOWN = 0x0201;
  private const int WM_LBUTTONUP = 0x0202;

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
  //Click - Worked Perfect
  //SendMessage((int)hwnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  //Thread.Sleep(100);
  //SendMessage((int)hwnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  //---
  //Close Window
  //SendMessage((int)hwnd, WM_CLOSE ,0 , IntPtr.Zero);
  #endregion

  private static Hashtable BitviseList = new Hashtable();
  public static int TimeoutSeconds = 30;

  private static int PortIndex = 1079;
  public static int GetPortAvailable()
  {
  PortIndex++;
  if (PortIndex >= 1181) PortIndex = 1079;
  Process BitviseApp = new Process();
  BitviseList.Add(PortIndex, BitviseApp);
  return PortIndex;
  }

  public static bool Connect(string Host, string User, string Pass, int ForwardPort)
  {
  bool Connected = false;

  //Start Bitvise - Auto Login
  ProcessStartInfo sinfo = new ProcessStartInfo();
  sinfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient\\BvSsh.exe";
  sinfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient";
  sinfo.Arguments = "-profile=\"" + AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient\\" + ForwardPort + ".bscp\" -host=" + Host + " -user=" + User + " -password=" + Pass + " -loginOnStartup -hide=trayIcon";
  Process BitviseApp = Process.Start(sinfo);

  BitviseList[ForwardPort] = BitviseApp;

  Thread.Sleep(2000);

  //Bitvise Login Checking...
  for (int i = 0; i < TimeoutSeconds; i++)
  {
  //Detect Host Key Verification
  SystemWindow[] wins = SystemWindow.FilterToplevelWindows((SystemWindow w) => { return w.Title == "Host Key Verification"; });
  if (wins.Length > 0)
  {
  SystemWindow[] wins2 = wins[0].FilterDescendantWindows(false, (SystemWindow w) => { return w.Title == "&Accept for This Session"; }); //Accept and &Save
  if (wins2.Length > 0)
  {
  //Click 4 times to effected !
  SendMessage((int)wins2[0].HWnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  Thread.Sleep(10);
  SendMessage((int)wins2[0].HWnd, WM_LBUTTONUP, 0, IntPtr.Zero);

  SendMessage((int)wins2[0].HWnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  Thread.Sleep(10);
  SendMessage((int)wins2[0].HWnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  }
  }

  //Detect Connected
  SystemWindow[] wins3 = SystemWindow.FilterToplevelWindows((SystemWindow w) => { return w.Title == "Bitvise SSH Client - " + ForwardPort + ".bscp - " + Host + ":22"; });
  if (wins3.Length > 0)
  {
  Connected = true;
  break;
  }

  Thread.Sleep(1000);
  }

  if (Connected == false)
  {
  try
  {
  BitviseApp.Kill();
  BitviseApp.Dispose();
  }
  catch { }
  }


  return Connected;
  }

  public static void Disconnect(int ForwardPort)
  {
  if (BitviseList[ForwardPort] == null) return;

  try
  {
  Process BitviseApp = BitviseList[ForwardPort] as Process;
  BitviseApp.Kill();
  BitviseApp.Dispose();
  }
  catch { }
  }

  private static bool GetPort(string Host, int Port)
  {
  return true;

  //using (TcpClient tcpClient = new TcpClient())
  //{
  //  IAsyncResult result = tcpClient.BeginConnect(Host, Port, null, null);
  //  WaitHandle timeoutHandler = result.AsyncWaitHandle;
  //  try
  //  {
  //  if (!result.AsyncWaitHandle.WaitOne(200, false))
  //  {
  //  tcpClient.Close();
  //  return false;
  //  }

  //  tcpClient.EndConnect(result);
  //  }
  //  catch
  //  {
  //  return false;
  //  }
  //  finally
  //  {
  //  timeoutHandler.Close();
  //  }
  //  return true;
  //}
  }
  }
}
Nhớ tải thêm cái ManagedWinapi rồi thêm ref vào.
 
Bitvise luôn đi, thằng nỳ connection dễ gãy.

Class của anh Phương đây:
PHP:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using ManagedWinapi.Windows;

namespace WindowsFormsApplication7
{
  public static class BitviseHandle
  {
  #region Windows API - user32.dll configs
  private const int WM_CLOSE = 16;
  private const int BN_CLICKED = 245;
  private const int WM_LBUTTONDOWN = 0x0201;
  private const int WM_LBUTTONUP = 0x0202;

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
  //Click - Worked Perfect
  //SendMessage((int)hwnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  //Thread.Sleep(100);
  //SendMessage((int)hwnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  //---
  //Close Window
  //SendMessage((int)hwnd, WM_CLOSE ,0 , IntPtr.Zero);
  #endregion

  private static Hashtable BitviseList = new Hashtable();
  public static int TimeoutSeconds = 30;

  private static int PortIndex = 1079;
  public static int GetPortAvailable()
  {
  PortIndex++;
  if (PortIndex >= 1181) PortIndex = 1079;
  Process BitviseApp = new Process();
  BitviseList.Add(PortIndex, BitviseApp);
  return PortIndex;
  }

  public static bool Connect(string Host, string User, string Pass, int ForwardPort)
  {
  bool Connected = false;

  //Start Bitvise - Auto Login
  ProcessStartInfo sinfo = new ProcessStartInfo();
  sinfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient\\BvSsh.exe";
  sinfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient";
  sinfo.Arguments = "-profile=\"" + AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient\\" + ForwardPort + ".bscp\" -host=" + Host + " -user=" + User + " -password=" + Pass + " -loginOnStartup -hide=trayIcon";
  Process BitviseApp = Process.Start(sinfo);

  BitviseList[ForwardPort] = BitviseApp;

  Thread.Sleep(2000);

  //Bitvise Login Checking...
  for (int i = 0; i < TimeoutSeconds; i++)
  {
  //Detect Host Key Verification
  SystemWindow[] wins = SystemWindow.FilterToplevelWindows((SystemWindow w) => { return w.Title == "Host Key Verification"; });
  if (wins.Length > 0)
  {
  SystemWindow[] wins2 = wins[0].FilterDescendantWindows(false, (SystemWindow w) => { return w.Title == "&Accept for This Session"; }); //Accept and &Save
  if (wins2.Length > 0)
  {
  //Click 4 times to effected !
  SendMessage((int)wins2[0].HWnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  Thread.Sleep(10);
  SendMessage((int)wins2[0].HWnd, WM_LBUTTONUP, 0, IntPtr.Zero);

  SendMessage((int)wins2[0].HWnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  Thread.Sleep(10);
  SendMessage((int)wins2[0].HWnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  }
  }

  //Detect Connected
  SystemWindow[] wins3 = SystemWindow.FilterToplevelWindows((SystemWindow w) => { return w.Title == "Bitvise SSH Client - " + ForwardPort + ".bscp - " + Host + ":22"; });
  if (wins3.Length > 0)
  {
  Connected = true;
  break;
  }

  Thread.Sleep(1000);
  }

  if (Connected == false)
  {
  try
  {
  BitviseApp.Kill();
  BitviseApp.Dispose();
  }
  catch { }
  }


  return Connected;
  }

  public static void Disconnect(int ForwardPort)
  {
  if (BitviseList[ForwardPort] == null) return;

  try
  {
  Process BitviseApp = BitviseList[ForwardPort] as Process;
  BitviseApp.Kill();
  BitviseApp.Dispose();
  }
  catch { }
  }

  private static bool GetPort(string Host, int Port)
  {
  return true;

  //using (TcpClient tcpClient = new TcpClient())
  //{
  //  IAsyncResult result = tcpClient.BeginConnect(Host, Port, null, null);
  //  WaitHandle timeoutHandler = result.AsyncWaitHandle;
  //  try
  //  {
  //  if (!result.AsyncWaitHandle.WaitOne(200, false))
  //  {
  //  tcpClient.Close();
  //  return false;
  //  }

  //  tcpClient.EndConnect(result);
  //  }
  //  catch
  //  {
  //  return false;
  //  }
  //  finally
  //  {
  //  timeoutHandler.Close();
  //  }
  //  return true;
  //}
  }
  }
}
Nhớ tải thêm cái ManagedWinapi rồi thêm ref vào.
Class của anh Phương em đã xem qua trên blog của anh rồi nhưng nó vẫn hơi thủ công dùng truyền tham số qua file+ send key, em muốn auto qua API hoặc command CMD cho nó chuyên nghiệp hơn 1 xíu
 
Port thì 1080, mà config thì 8080, đến tết sang năm cũng ko kết nối đc :D
thử post 1080 ko đc mình chuyển sang port khác 8080 forward xong llisten ở post đó giống nhau.
chứ ko phải tụi nó khác nhau đâu
 
Dùng renci hơi chuối tý.
Gợi ý bạn thêm selenium webdriver c# nữa. Set socks rồi tự open fifrefox luôn đỡ phải config!
Thank @sandaru
 
firefox config sai rồi, xoá hết các dòng đi, để dòng sock host thôi :D
ra vậy Thanks đại ca. làm mình tìm mãi :p
Dùng renci hơi chuối tý.
Gợi ý bạn thêm selenium webdriver c# nữa. Set socks rồi tự open fifrefox luôn đỡ phải config!
Thank @sandaru
uk bác mình cũng đang tính dùng selenium phantomjs
 
Last edited:
bác forward port được chưa? mình cũng không thể kết nối được chỗ này? hỏi bên support tụi SSH.NET chắc cũng 3 tháng rồi mà éo thằng nào trả lời :)
 
Có bác nào fake đc referer khi dùng selelium Firefox không nhỉ :(. Tìm suốt mấy ngày mà k đc
 
Dùng renci chuối lắm, class sock của nó lỗi tè le hột me luôn, nếu muốn chuyên nghiệp thì mình khuyên nên mua license flowssh của bitvise.
 
Mình cũng khuyên là dùng bitvssh client nếu không có điều kiện mua lic của các lib bản quyền, còn cmd của bitvssh thì bạn vào run gõ bvssh /hep là ra cái bạn cần
 
html Referer khác với user agent nha bạn :(

mình hay fake cùng lúc 2 cái này nên nhầm lẫn tí
giải pháp là
install custom vào profile firefox.
thay đổi webdriver theo mình biết thì cái ghóstdrive có hộ trợ change
phần lớn tool ở đây viết qua webclient nên mấy cái này đơn giản còn với selenium nó hơi vòng vo tí
 

Announcements

Today's birthdays

Forum statistics

Threads
421,150
Messages
7,106,236
Members
173,241
Latest member
tieubao00

Most viewed of week

Most discussed of week

Most viewed of week

Most discussed of week

Back
Top Bottom