-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSshCommand.cs
151 lines (107 loc) · 3.82 KB
/
SshCommand.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using KeePassLib.Serialization;
using System.Text;
using KeePassLib.Utility;
namespace OpenSSHSync
{
public class SshCommand : WebResponse
{
private readonly Process process;
private readonly WebHeaderCollection headers;
private readonly WebRequest request;
public SshCommand (WebRequest request)
{
this.request = request;
this.headers = new WebHeaderCollection ();
process = Process.Start (new ProcessStartInfo {
FileName = OpenSSHSyncExt.SSH,
Arguments = "-q -o 'BatchMode=yes' -o 'PasswordAuthentication=no' " + GetSession (request) + " " + GetCommand (request),
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
});
if (request.GetRequestStream () != null) {
request.GetRequestStream ().CopyTo (process.StandardInput.BaseStream);
}
}
public override WebHeaderCollection Headers { get { return headers; } }
static string GetSession (WebRequest request)
{
StringBuilder retval = new StringBuilder ();
NetworkCredential cred = (request.Credentials as NetworkCredential);
if (cred != null) {
if (!string.IsNullOrEmpty (cred.UserName) && "anonymous" != cred.UserName) {
retval.AppendFormat ("'{0}'", cred.UserName);
if (!string.IsNullOrEmpty (cred.Password))
retval.AppendFormat (":'{0}'", cred.Password);
retval.Append ('@');
}
}
retval.Append (request.RequestUri.Host);
return retval.ToString ();
}
static string GetCommand (WebRequest request)
{
string path = request.RequestUri.AbsolutePath.Substring (1);
switch (request.Method) {
case IOConnection.WrmDeleteFile:
case WebRequestMethods.Ftp.DeleteFile:
return string.Format ("rm -f '{0}'", path);
case WebRequestMethods.Ftp.AppendFile:
return string.Format ("cat >> '{0}'", path);
case WebRequestMethods.Ftp.DownloadFile:
return string.Format ("cat '{0}'", path);
case WebRequestMethods.Ftp.ListDirectory:
return string.Format ("ls -1 '{0}'", path);
case WebRequestMethods.Ftp.GetFileSize:
return string.Format ("stat -c '%s' '{0}'", path);
case WebRequestMethods.Ftp.MakeDirectory:
return string.Format ("mkdir '{0}'", path);
case IOConnection.WrmMoveFile:
string fromPath = path;
string toPath = request.Headers.Get (IOConnection.WrhMoveFileTo);
if (string.IsNullOrEmpty (toPath))
throw new InvalidOperationException ("empty destination path");
try {
toPath = new Uri (toPath).AbsolutePath.Substring (1);
} catch (Exception ex) {
throw new InvalidOperationException (string.Format ("path '{0}' is not absolute", toPath), ex);
}
if (string.IsNullOrEmpty (toPath))
throw new InvalidOperationException ("empty destination path");
return string.Format ("mv '{0}' '{1}'", fromPath, toPath);
case WebRequestMethods.Http.Post:
case WebRequestMethods.Http.Put:
case WebRequestMethods.Ftp.UploadFile:
return string.Format ("cat > '{0}'", path);
default:
MessageService.ShowWarning ("unsupported method '{0}'", request.Method);
throw new InvalidOperationException (string.Format ("unsupported method '{0}'", request.Method));
}
}
private Stream outputStream = null;
public override Stream GetResponseStream ()
{
if (outputStream == null) {
outputStream = new MemoryStream ();
process.StandardOutput.BaseStream.CopyTo (outputStream);
process.WaitForExit (request.Timeout);
}
return outputStream;
}
public override void Close ()
{
if (outputStream != null)
outputStream.Close ();
process.Close ();
}
public override string ToString ()
{
return string.Format ("[SshCommand({0:d}: Command={1} {2}]", GetHashCode (), process.StartInfo.FileName, process.StartInfo.Arguments);
}
}
}