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

Hayley McAdam 96

Hayley McAdam

Hayley McAdam

New member
Tham gia
26/9/24
Bài viết
2
Cảm xúc
0
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
 
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.
 
Back
Top