-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
131 lines (106 loc) · 4.47 KB
/
Copy pathshell.cpp
File metadata and controls
131 lines (106 loc) · 4.47 KB
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
#include "shell.h"
#include "socket.h"
#include <wx/sizer.h>
#include <wx/statline.h>
#include <mutex>
#include <unordered_map>
#include <string>
static std::unordered_map<int, std::string> g_ShellOutput;
static std::mutex g_ShellMutex;
void Shell_AppendOutput(int clientId, const std::string& data)
{
std::lock_guard<std::mutex> lk(g_ShellMutex);
g_ShellOutput[clientId] += data;
}
ShellDialog::ShellDialog(wxWindow* parent, const wxString& clientName, int clientId)
: wxDialog(parent, wxID_ANY, "Shell - " + clientName,
wxDefaultPosition, wxSize(720, 480),
wxDEFAULT_DIALOG_STYLE | wxMINIMIZE_BOX),
m_clientId(clientId)
{
SetFont(wxFont(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Consolas"));
SetBackgroundColour(wxColour(18, 18, 18));
wxBoxSizer* root = new wxBoxSizer(wxVERTICAL);
m_output = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2 | wxTE_DONTWRAP);
m_output->SetBackgroundColour(wxColour(12, 12, 12));
m_output->SetForegroundColour(wxColour(180, 255, 120));
m_output->SetFont(wxFont(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Consolas"));
root->Add(m_output, 1, wxEXPAND | wxALL, 4);
wxBoxSizer* inputRow = new wxBoxSizer(wxHORIZONTAL);
wxStaticText* prompt = new wxStaticText(this, wxID_ANY, ">");
prompt->SetForegroundColour(wxColour(80, 200, 80));
prompt->SetFont(wxFont(10, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
m_input = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize,
wxTE_PROCESS_ENTER);
m_input->SetBackgroundColour(wxColour(30, 30, 30));
m_input->SetForegroundColour(wxColour(220, 220, 220));
m_input->SetFont(wxFont(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Consolas"));
m_btnSend = new wxButton(this, wxID_ANY, "Send", wxDefaultPosition, wxSize(70, -1));
m_btnClear = new wxButton(this, wxID_ANY, "Clear", wxDefaultPosition, wxSize(70, -1));
inputRow->Add(prompt, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 4);
inputRow->Add(m_input, 1, wxEXPAND | wxRIGHT, 4);
inputRow->Add(m_btnSend, 0, wxRIGHT, 4);
inputRow->Add(m_btnClear, 0, wxRIGHT, 4);
root->Add(inputRow, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 4);
SetSizer(root);
Bind(wxEVT_BUTTON, &ShellDialog::OnSend, this, m_btnSend->GetId());
Bind(wxEVT_BUTTON, &ShellDialog::OnClear, this, m_btnClear->GetId());
Bind(wxEVT_TEXT_ENTER, &ShellDialog::OnInputEnter, this, m_input->GetId());
Bind(wxEVT_CLOSE_WINDOW, &ShellDialog::OnClose, this);
m_timer.SetOwner(this);
Bind(wxEVT_TIMER, &ShellDialog::OnTimer, this);
m_timer.Start(300);
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"shell\",\"action\":\"start\"}");
Centre();
}
ShellDialog::~ShellDialog() { m_timer.Stop(); }
void ShellDialog::OnTimer(wxTimerEvent&)
{
std::string data;
{
std::lock_guard<std::mutex> lk(g_ShellMutex);
auto it = g_ShellOutput.find(m_clientId);
if (it == g_ShellOutput.end() || it->second.empty()) return;
data = std::move(it->second);
it->second.clear();
}
m_output->AppendText(wxString::FromUTF8(data));
}
void ShellDialog::OnSend(wxCommandEvent&)
{
wxString cmd = m_input->GetValue().Trim();
if (cmd.empty()) return;
if (cmd.Lower() == "cls" || cmd.Lower() == "clear")
{
m_output->Clear();
m_input->Clear();
return;
}
std::string c = cmd.ToStdString();
std::string escaped;
for (char ch : c) {
if (ch == '"') escaped += "\\\"";
else if (ch == '\\') escaped += "\\\\";
else escaped += ch;
}
ServerManager::SendToClient(m_clientId,
"{\"cmd\":\"shell\",\"action\":\"input\",\"input\":\"" + escaped + "\"}");
m_input->Clear();
}
void ShellDialog::OnClear(wxCommandEvent&) { m_output->Clear(); }
void ShellDialog::OnInputEnter(wxCommandEvent& e) { OnSend(e); }
void ShellDialog::OnClose(wxCloseEvent&)
{
m_timer.Stop();
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"shell\",\"action\":\"stop\"}");
{
std::lock_guard<std::mutex> lk(g_ShellMutex);
g_ShellOutput.erase(m_clientId);
}
Destroy();
}
void ShellDialog::AppendOutput(const wxString& text)
{
m_output->AppendText(text);
}