forked from revenz/Fenrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminalController.cs
227 lines (196 loc) · 8.39 KB
/
TerminalController.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Fenrus.Models;
using Fenrus.Terminals;
using Microsoft.AspNetCore.Mvc;
namespace Fenrus.Controllers;
/// <summary>
/// Controller for Terminals (docker or SSH)
/// </summary>
[Route("terminal")]
[Authorize]
public class TerminalController : BaseController
{
/// <summary>
/// Opens a terminal connection
/// </summary>
/// <param name="uid">the Uid of the app container</param>
/// <param name="rows">the number of rows for the terminal</param>
/// <param name="cols">the number of columns for the terminal</param>
[HttpGet("{uid}")]
public async Task Get([FromRoute] Guid uid, [FromQuery] int rows = 24, [FromQuery] int cols = 24)
{
var settings = GetUserSettings();
if (settings == null)
throw new UnauthorizedAccessException();
var groups = new GroupService().GetAllForUser(settings.UserUid);
var item = groups?.SelectMany(x => x.Items)?.FirstOrDefault(x => x.Uid == uid);
if (item == null)
{
// try and find the item from the system groups
item = new GroupService().GetSystemGroups(enabledOnly: true)
.SelectMany(x => x.Items).FirstOrDefault(x => x.Uid == uid);
}
if (item == null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
Terminal? terminal = null;
using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
if (item is AppItem app)
{
if (string.IsNullOrEmpty(app.DockerContainer) == false && app.DockerUid != null)
{
var docker = new DockerService().GetByUid(app.DockerUid.Value);
if (docker == null)
{
var translator = GetTranslator(settings);
throw new Exception(translator.Instant("ErrorMessages.DockerServerNotFound",
new { uid = app }));
}
terminal = new DockerTerminal(ws, rows, cols, docker.Address, docker.Port, app.DockerContainer, app.DockerCommand);
}
else if (string.IsNullOrEmpty(app.SshServer) == false)
{
terminal = new SshTerminal(ws, rows, cols, app.SshServer, 0, app.SshUserName,
EncryptionHelper.Decrypt(app.SshPassword));
}
}
if(terminal == null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
await terminal.Connect();
}
/// <summary>
/// Opens a terminal connection
/// </summary>
/// <param name="rows">the number of rows for the terminal</param>
/// <param name="cols">the number of columns for the terminal</param>
[HttpGet("ssh")]
public async Task Ssh([FromQuery] string info, [FromQuery] int rows = 24, [FromQuery] int cols = 24)
{
var profile = GetUserProfile();
if (profile == null)
throw new UnauthorizedAccessException();
string decrypted = EncryptionHelper.DecryptAes(info);
var serverInfo = JsonSerializer.Deserialize<SshInfo>(decrypted, new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
});
if (Guid.TryParse(serverInfo.User, out Guid uid))
{
var app = profile.AppGroups.SelectMany(x => x.Items).FirstOrDefault(x => x.Uid == uid);
if (app != null)
{
(serverInfo.User, serverInfo.Password) = ParseAddress(app.Address, serverInfo.User, serverInfo.Password);
}
}
using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
var terminal = new SshTerminal(ws, rows, cols, serverInfo.Server, 0, serverInfo.User, serverInfo.Password);
await terminal.Connect();
}
/// <summary>
/// Opens a docker terminal connection
/// </summary>
/// <param name="uid">the UID of the docker server</param>
/// <param name="name">the name of the docker container</param>
/// <param name="rows">the number of rows for the terminal</param>
/// <param name="cols">the number of columns for the terminal</param>
/// <param name="command">the command to run to open the terminal inside the docker container</param>
[HttpGet("docker/{uid}/{name}")]
public async Task Docker([FromRoute] Guid uid, [FromRoute] string name, [FromQuery] int rows = 24, [FromQuery] int cols = 24, [FromQuery] string? command = null)
{
var settings = GetUserSettings();
if (settings == null)
throw new UnauthorizedAccessException();
var docker = new DockerService().GetByUid(uid);
if (docker == null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
// ensure only these commands are allowed
if(command != "/bin/sh" && command != "/bin/ash")
command = "/bin/bash";
using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
var terminal = new DockerTerminal(ws, rows, cols, docker.Address, docker.Port, name, command);
await terminal.Connect();
}
/// <summary>
/// Parses an address and gets the username/password from it if set
/// </summary>
/// <param name="address">the address</param>
/// <param name="currentUser">the current username to return if not set here</param>
/// <param name="currentPassword">the current password to return if not set here</param>
/// <returns>the username and password if available</returns>
private (string User, string Password) ParseAddress(string address, string currentUser, string currentPassword)
{
if (string.IsNullOrEmpty(address))
return (currentUser, currentPassword);
int atIndex = address.IndexOf("@");
if (atIndex < 0)
return (currentUser, currentPassword);
string user = address[..atIndex];
int caretIndex = user.IndexOf(":");
if (caretIndex < 0)
return (user, currentPassword);
string pwd = user.Substring(caretIndex + 1);
user = user[..caretIndex];
return (user, pwd);
}
/// <summary>
/// Opens a terminal log
/// </summary>
/// <param name="uid">the Uid of the app container</param>
/// <param name="rows">the number of rows for the terminal</param>
/// <param name="cols">the number of columns for the terminal</param>
[HttpGet("log/{uid}")]
public async Task GetLog([FromRoute] Guid uid, [FromQuery] int rows = 24, [FromQuery] int cols = 24)
{
var settings = GetUserSettings();
if (settings == null)
throw new UnauthorizedAccessException();
var groups = new GroupService().GetAllForUser(settings.UserUid);
var item = groups?.SelectMany(x => x.Items)?.FirstOrDefault(x => x.Uid == uid);
if (item == null)
{
// try and find the item from the system groups
item = new GroupService().GetSystemGroups(enabledOnly: true)
.SelectMany(x => x.Items).FirstOrDefault(x => x.Uid == uid);
}
if (item == null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
DockerTerminal? terminal = null;
using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
if (item is AppItem app)
{
if (string.IsNullOrEmpty(app.DockerContainer) == false && app.DockerUid != null)
{
var docker = new DockerService().GetByUid(app.DockerUid.Value);
if (docker == null)
{
var translator = GetTranslator(settings);
throw new Exception(translator.Instant("ErrorMessages.DockerServerNotFound",
new { uid = app }));
}
terminal = new DockerTerminal(ws, rows, cols, docker.Address, docker.Port, app.DockerContainer, app.DockerCommand);
}
}
if(terminal == null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
await terminal.Log();
}
private class SshInfo
{
public string Server { get; set; }
public string User { get; set; }
public string Password { get; set; }
}
}