-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ws.cpp
146 lines (121 loc) · 4.02 KB
/
test_ws.cpp
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
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//For debug
std::ostream& writeString(std::ostream& out, std::string const& s)
{
for ( auto ch : s )
{
switch (ch)
{
case '\'':
out << "\\'";
break;
case '\"':
out << "\\\"";
break;
case '\?':
out << "\\?";
break;
case '\\':
out << "\\\\";
break;
case '\a':
out << "\\a";
break;
case '\b':
out << "\\b";
break;
case '\f':
out << "\\f";
break;
case '\n':
out << "\\n";
break;
case '\r':
out << "\\r";
break;
case '\t':
out << "\\t";
break;
case '\v':
out << "\\v";
break;
default:
out << ch;
}
}
return out;
}
// Sends a WebSocket message and prints the response
int main(int argc, char** argv)
{
try
{
// Check command line arguments.
if(argc != 4)
{
std::cerr <<
"Usage: websocket-client-sync <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-sync echo.websocket.org 80 \"Hello, world!\"\n";
return EXIT_FAILURE;
}
auto const host = argv[1];
auto const port = argv[2];
// auto const text = argv[3];
auto const text = "ls\r";
// The io_context is required for all I/O
net::io_context ioc;
// These objects perform our I/O
tcp::resolver resolver{ioc};
websocket::stream<tcp::socket> ws{ioc};
// Look up the domain name
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
net::connect(ws.next_layer(), results.begin(), results.end());
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the websocket handshake
ws.handshake(host, "/containers/2088223769d2/attach/ws?stream=1&stdout=1&stdin=0&logs=0");
// Send the message
for (int i = 0; i < 1; ++i){
ws.write(net::buffer(std::string("python3 /home/code/main.py <<< '1 2'; echo 'SHTP_EXECUTION_FINISH'\r")));
int exit_sequences_find = 0;
while (exit_sequences_find <= 1){
beast::flat_buffer buffer;
ws.read(buffer);
std::string buffer_processed = beast::buffers_to_string(buffer.data());
//std::cout << buffer_processed << std::endl;
//writeString(std::cout, buffer_processed);
std::cout << buffer_processed;
std::cout << "\n";
if (buffer_processed.find("SHTP_EXECUTION_FINISH") == std::string::npos) exit_sequences_find++;
}
std::cout << "--TEST--";
ws.write(net::buffer(std::string("clear\n\r")));
}
ws.close(websocket::close_code::normal); // Finish socket
}
catch(std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return 0;
}