/
SSHTunnel.cs
69 lines (61 loc) · 2.04 KB
/
SSHTunnel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Diagnostics;
using Tamir.SharpSsh.jsch;
using WowPacketParser.Misc;
namespace WowPacketParser.SQL
{
public class MyUserInfo : UserInfo
{
private readonly String _passwd;
public MyUserInfo(string password)
{
_passwd = password;
}
public String getPassword() { return _passwd; }
public bool promptYesNo(String str) { return true; }
public String getPassphrase() { return null; }
public bool promptPassphrase(String message) { return true; }
public bool promptPassword(String message) { return true; }
public void showMessage(String message) { }
}
public static class SSHTunnel
{
[ThreadStatic]
private static Session Session;
public static bool Enabled = Settings.SSHEnabled;
public static void Connect()
{
try
{
var jsch = new JSch();
Session = jsch.getSession(Settings.SSHUsername, Settings.SSHHost, Settings.SSHPort);
Session.setHost(Settings.SSHHost);
Session.setPassword(Settings.SSHPassword);
UserInfo ui = new MyUserInfo(Settings.SSHPassword);
Session.setUserInfo(ui);
Session.connect();
int port;
if (!int.TryParse(Settings.Port, out port))
port = 3306;
Session.setPortForwardingL(Settings.SSHLocalPort, "localhost", port);
if (!Session.isConnected())
Enabled = false;
}
catch (Exception ex)
{
Enabled = false;
Trace.WriteLine(ex.Message + " at ssh connect.");
Disconnect();
}
}
public static bool Connected()
{
return Session != null && Session.isConnected();
}
public static void Disconnect()
{
if (Session != null)
Session.disconnect();
}
}
}