Skip to content

Commit

Permalink
Merge branch 'feature/copy-remote' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
poderosaproject committed Jan 3, 2025
2 parents d2d2d8a + 0e31e9a commit b22b8e6
Show file tree
Hide file tree
Showing 13 changed files with 460 additions and 92 deletions.
6 changes: 6 additions & 0 deletions Benchmark/MockSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public void Close() {
Monitor.Exit(_pumpThreadSync);
}
}

public string Remote {
get {
return "(mock)";
}
}
}

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions Pipe/PipeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ ref processInfo
Process process = Process.GetProcessById(processInfo.dwProcessId);

PipedProcess pipedProcess = new PipedProcess(process, childStdInHandle, childStdOutHandle, childStdErrHandle);
PipeSocket socket = new PipeSocket(parentReadStream, parentWriteStream);
PipeSocket socket = new PipeSocket(parentReadStream, parentWriteStream, commandLine);
PipeTerminalConnection connection = new PipeTerminalConnection(param, socket, pipedProcess);

return connection;
Expand Down Expand Up @@ -437,7 +437,10 @@ private static PipeTerminalConnection OpenNamedPipe(PipeTerminalParameter param)
writeStream = readStream;
}

PipeSocket sock = new PipeSocket(readStream, writeStream);
string remote = (hasOutputPipePath)
? param.InputPipePath + " > " + param.OutputPipePath
: param.InputPipePath;
PipeSocket sock = new PipeSocket(readStream, writeStream, remote);
PipeTerminalConnection conn = new PipeTerminalConnection(param, sock, null);

return conn;
Expand Down
12 changes: 11 additions & 1 deletion Pipe/PipeSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace Poderosa.Pipe {
/// </summary>
internal class PipeSocket : IPoderosaSocket {

private readonly string _remote;

private readonly FileStream _inputStream;
private readonly FileStream _outputStream;

Expand All @@ -51,10 +53,12 @@ public bool IsClosed {
/// </summary>
/// <param name="inputStream">Stream to input from.</param>
/// <param name="outputStream">Stream to output to. Can be same instance as inputStream.</param>
public PipeSocket(FileStream inputStream, FileStream outputStream) {
/// <param name="remote">text representing the remote side</param>
public PipeSocket(FileStream inputStream, FileStream outputStream, string remote) {
Debug.Assert(inputStream != null);
Debug.Assert(outputStream != null);

_remote = remote;
_inputStream = inputStream;
_outputStream = outputStream;
}
Expand Down Expand Up @@ -132,6 +136,12 @@ public void RepeatAsyncRead(IByteAsyncInputStream receiver) {
_inputThread.Start();
}

public string Remote {
get {
return _remote;
}
}

public bool Available {
get {
return false;
Expand Down
11 changes: 6 additions & 5 deletions Protocols/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace Poderosa.Protocols {

internal class SSHConnector : InterruptableConnector {

private ISSHLoginParameter _destination;
private HostKeyVerifierBridge _keycheck;
private readonly ISSHLoginParameter _destination;
private readonly HostKeyVerifierBridge _keycheck;
private TerminalConnection _result;

public SSHConnector(ISSHLoginParameter destination, HostKeyVerifierBridge keycheck) {
Expand All @@ -43,7 +43,8 @@ protected override void Negotiate() {
ITerminalParameter term = (ITerminalParameter)_destination.GetAdapter(typeof(ITerminalParameter));
ITCPParameter tcp = (ITCPParameter)_destination.GetAdapter(typeof(ITCPParameter));

SSHTerminalConnection terminalConnection = new SSHTerminalConnection(_destination);
SSHTerminalConnection terminalConnection =
new SSHTerminalConnection(_destination, tcp.Destination, _socket.RemoteEndPoint as IPEndPoint);

SSHConnectionParameter con =
new SSHConnectionParameter(
Expand Down Expand Up @@ -138,7 +139,7 @@ internal override TerminalConnection Result {
}

internal class TelnetConnector : InterruptableConnector {
private ITCPParameter _destination;
private readonly ITCPParameter _destination;
private TelnetTerminalConnection _result;

public TelnetConnector(ITCPParameter destination) {
Expand All @@ -148,7 +149,7 @@ public TelnetConnector(ITCPParameter destination) {
protected override void Negotiate() {
ITerminalParameter term = (ITerminalParameter)_destination.GetAdapter(typeof(ITerminalParameter));
TelnetNegotiator neg = new TelnetNegotiator(term.TerminalType, term.InitialWidth, term.InitialHeight);
TelnetTerminalConnection r = new TelnetTerminalConnection(_destination, neg, new PlainPoderosaSocket(_socket));
TelnetTerminalConnection r = new TelnetTerminalConnection(_destination, neg, _socket, _destination.Destination);
//BACK-BURNER r.UsingSocks = _socks!=null;
_result = r;
}
Expand Down
46 changes: 32 additions & 14 deletions Protocols/LocalShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ public ITerminalConnection Connect() {
args += " -- " + _param.ShellName;
}
ProcessStartInfo psi = new ProcessStartInfo(cygwinBridgePath, args);
PrepareEnv(psi, _param);
string cygwinDir = _param.CygwinDir;
if (cygwinDir == null || cygwinDir.Length == 0)
cygwinDir = CygwinUtil.GuessRootDirectory();
PrepareEnv(psi, cygwinDir);
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
Expand Down Expand Up @@ -180,7 +183,7 @@ public ITerminalConnection Connect() {
return null;
}

return new CygwinTerminalConnection(term, sock);
return new CygwinTerminalConnection(term, sock, cygwinDir);
}
}

Expand Down Expand Up @@ -225,11 +228,8 @@ private IEnumerable<string> EnumerateCygwinBridgeParentPath() {

}

protected static void PrepareEnv(ProcessStartInfo psi, ICygwinParameter p) {
protected static void PrepareEnv(ProcessStartInfo psi, string cygwinDir) {
string path = psi.EnvironmentVariables["PATH"];
string cygwinDir = p.CygwinDir;
if (cygwinDir == null || cygwinDir.Length == 0)
cygwinDir = CygwinUtil.GuessRootDirectory();
if (path == null)
path = String.Empty;
else if (!path.EndsWith(";"))
Expand Down Expand Up @@ -411,17 +411,30 @@ private static string GetCygwinRootDirectory(RegistryKey baseKey, bool check64Bi
/// </summary>
internal class CygwinTerminalConnection : TerminalConnection, ITerminalConnection {

private readonly CygwinSocket _cygwinSocket;

/// <summary>
/// Constructor
/// </summary>
/// <param name="terminalParameter">Terminal parameter</param>
/// <param name="socket">Socket object</param>
public CygwinTerminalConnection(ITerminalParameter terminalParameter, Socket socket)
/// <param name="remote">text representing the remote side</param>
public CygwinTerminalConnection(ITerminalParameter terminalParameter, Socket socket, string remote)
: base(terminalParameter) {
_destination = terminalParameter;
CygwinSocket s = new CygwinSocket(socket, this);
_socket = s;
_terminalOutput = s;

_cygwinSocket = new CygwinSocket(socket, this, remote);
}

public override ITerminalOutput TerminalOutput {
get {
return _cygwinSocket;
}
}

public override IPoderosaSocket Socket {
get {
return _cygwinSocket;
}
}
}

Expand All @@ -432,9 +445,8 @@ internal class CygwinSocket : IPoderosaSocket, ITerminalOutput {
private byte[] _buff = new byte[0];
private readonly object _buffSync = new object();

public CygwinSocket(Socket socket, TerminalConnection conn) {
_inner = new PlainPoderosaSocket(socket);
_inner.SetOwnerConnection(conn);
public CygwinSocket(Socket socket, TerminalConnection conn, string remote) {
_inner = new PlainPoderosaSocket(conn, socket, remote);
}

public void Transmit(ByteDataFragment data) {
Expand Down Expand Up @@ -512,5 +524,11 @@ public void Resize(int width, int height) {
}
}
}

public string Remote {
get {
return _inner.Remote;
}
}
}
}
47 changes: 37 additions & 10 deletions Protocols/SSHSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,44 @@
// limitations under the License.

using System;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Granados;
using Granados.SSH2;
using Granados.IO;
using Granados.KeyboardInteractive;
using Granados.SSH;
using System.Threading.Tasks;

namespace Poderosa.Protocols {
//SSHの入出力系
internal abstract class SSHConnectionEventReceiverBase : ISSHConnectionEventHandler {
protected SSHTerminalConnection _parent;
protected readonly SSHTerminalConnection _parent;
protected ISSHConnection _connection;
protected IByteAsyncInputStream _callback;
private bool _normalTerminationCalled;

public SSHConnectionEventReceiverBase(SSHTerminalConnection parent) {
_parent = parent;
}

//SSHConnection確立時に呼ぶ
public void SetSSHConnection(ISSHConnection connection) {
_connection = connection;
}

public ISSHConnection Connection {
get {
return _connection;
}
}

public virtual void CleanupErrorStatus() {
if (_connection != null && _connection.IsOpen) {
_connection.Close();
Expand Down Expand Up @@ -123,16 +127,21 @@ private void CloseError(Exception ex) {

internal class SSHSocket
: SSHConnectionEventReceiverBase,
IPoderosaSocket, ITerminalOutput, IKeyboardInteractiveAuthenticationHandler {
IPoderosaSocketInet, ITerminalOutput, IKeyboardInteractiveAuthenticationHandler {

private readonly string _remote;
private readonly IPEndPoint _endPoint;

private SSHChannelHandler _channelHandler;
private ByteDataFragment _data;
private readonly ByteDataFragment _data;
private MemoryStream _buffer = new MemoryStream();

private KeyboardInteractiveAuthHanlder _keyboardInteractiveAuthHanlder;

public SSHSocket(SSHTerminalConnection parent)
public SSHSocket(SSHTerminalConnection parent, string remote, IPEndPoint endPoint)
: base(parent) {
_remote = remote;
_endPoint = endPoint;
_data = new ByteDataFragment();
}

Expand Down Expand Up @@ -187,7 +196,7 @@ public void ForceDisposed() {
_connection.Disconnect(DisconnectionReasonCode.ByApplication, "bye");
}
}
catch(Exception e) {
catch (Exception e) {
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
Expand Down Expand Up @@ -245,12 +254,30 @@ public bool Available {
}
}

public string Remote {
get {
return _remote;
}
}
public IPAddress RemoteAddress {
get {
return (_endPoint != null) ? _endPoint.Address : null;
}
}

public int? RemotePortNumber {
get {
return (_endPoint != null) ? _endPoint.Port : (int?)null;
}
}

#region IKeyboardInteractiveAuthenticationHandler

public string[] KeyboardInteractiveAuthenticationPrompt(string[] prompts, bool[] echoes) {
if (_keyboardInteractiveAuthHanlder != null) {
return _keyboardInteractiveAuthHanlder.KeyboardInteractiveAuthenticationPrompt(prompts, echoes);
} else {
}
else {
return prompts.Select(s => "").ToArray();
}
}
Expand Down
Loading

0 comments on commit b22b8e6

Please sign in to comment.