tcpprox.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>TCP Proxy</h2>
  4. <p>Proxy traffic flow on layer 3 via TCP/IP</p>
  5. </div>
  6. <button class="ui basic orange button" id="addProxyConfigButton"><i class="ui add icon"></i> Add Proxy Config</button>
  7. <button class="ui basic circular right floated icon button" title="Refresh List"><i class="ui green refresh icon"></i></button>
  8. <div class="ui divider"></div>
  9. <div class="ui basic segment" id="addproxyConfig" style="display:none;">
  10. <h3>TCP Proxy Config</h3>
  11. <p>Create or edit a new proxy instance</p>
  12. <form id="tcpProxyForm" class="ui form">
  13. <div class="field" style="display:none;">
  14. <label>UUID</label>
  15. <input type="text" name="uuid">
  16. </div>
  17. <div class="field">
  18. <label>Name</label>
  19. <input type="text" name="name" placeholder="Config Name">
  20. </div>
  21. <div class="field">
  22. <label>Port A</label>
  23. <input type="text" name="porta" placeholder="First address or port">
  24. </div>
  25. <div class="field">
  26. <label>Port B</label>
  27. <input type="text" name="portb" placeholder="Second address or port">
  28. </div>
  29. <div class="field">
  30. <label>Timeout (s)</label>
  31. <input type="text" name="timeout" placeholder="Timeout (s)">
  32. </div>
  33. <div class="field">
  34. <label>Mode</label>
  35. <select name="mode" class="ui dropdown">
  36. <option value="">Select Mode</option>
  37. <option value="listen">Listen</option>
  38. <option value="transport">Transport</option>
  39. <option value="starter">Starter</option>
  40. </select>
  41. </div>
  42. <button id="addTcpProxyButton" class="ui basic button" type="submit"><i class="ui blue add icon"></i> Create</button>
  43. <button id="editTcpProxyButton" class="ui basic button" onclick="confirmEditTCPProxyConfig(event);"><i class="ui blue save icon"></i> Update</button>
  44. </form>
  45. <div class="ui divider"></div>
  46. </div>
  47. <div class="ui basic segment">
  48. <h3>TCP Proxy Configs</h3>
  49. <p>A list of TCP proxy configs created on this host. To enable them, use the toggle button on the right.</p>
  50. <div style="overflow-x: auto; min-height: 400px;">
  51. <table id="proxyTable" class="ui celled unstackable table">
  52. <thead>
  53. <tr>
  54. <th>Name</th>
  55. <th>Port/Addr A</th>
  56. <th>Port/Addr B</th>
  57. <th>Mode</th>
  58. <th>Timeout (s)</th>
  59. <th>Actions</th>
  60. </tr>
  61. </thead>
  62. <tbody>
  63. </tbody>
  64. </table>
  65. </div>
  66. </div>
  67. <div class="ui basic inverted segment" style="background-color: #414141; border-radius: 0.6em;">
  68. <h3>Proxy Mode</h3>
  69. <p>TCP Proxy support the following TCP sockets proxy modes</p>
  70. <table class="ui celled padded inverted basic table">
  71. <thead>
  72. <tr><th class="single line">Mode</th>
  73. <th>Public-IP</th>
  74. <th>Concurrent Access</th>
  75. <th>Flow Diagram</th>
  76. </tr></thead>
  77. <tbody>
  78. <tr>
  79. <td>
  80. <h4 class="ui center aligned inverted header">Transport</h4>
  81. </td>
  82. <td class="single line">
  83. Server: <i class="ui green check icon"></i><br>
  84. A: <i class="ui remove icon"></i><br>
  85. B: <i class="ui green check icon"></i> (or same LAN)<br>
  86. </td>
  87. <td>
  88. <i class="ui green check icon"></i>
  89. </td>
  90. <td>Port A (e.g. 25565) <i class="arrow right icon"></i> Server<br>
  91. Server <i class="arrow right icon"></i> Port B (e.g. 192.168.0.2:25565)<br>
  92. <small>Traffic from Port A will be forward to Port B's (IP if provided and) Port</small>
  93. </td>
  94. </tr>
  95. <tr>
  96. <td>
  97. <h4 class="ui center aligned inverted header">Listen</h4>
  98. </td>
  99. <td class="single line">
  100. Server: <i class="ui green check icon"></i><br>
  101. A: <i class="ui remove icon"></i><br>
  102. B: <i class="ui remove icon"></i><br>
  103. </td>
  104. <td>
  105. <i class="ui red times icon"></i>
  106. </td>
  107. <td>Port A (e.g. 8080) <i class="arrow right icon"></i> Server<br>
  108. Port B (e.g. 8081) <i class="arrow right icon"></i> Server<br>
  109. <small>Server will act as a bridge to proxy traffic between Port A and B</small>
  110. </td>
  111. </tr>
  112. <tr>
  113. <td>
  114. <h4 class="ui center aligned inverted header">Starter</h4>
  115. </td>
  116. <td class="single line">
  117. Server: <i class="ui times icon"></i><br>
  118. A: <i class="ui green check icon"></i><br>
  119. B: <i class="ui green check icon"></i><br>
  120. </td>
  121. <td>
  122. <i class="ui red times icon"></i>
  123. </td>
  124. <td>Server <i class="arrow right icon"></i> Port A (e.g. remote.local.:8080) <br>
  125. Server <i class="arrow right icon"></i> Port B (e.g. recv.local.:8081) <br>
  126. <small>Port A and B will be actively bridged</small>
  127. </td>
  128. </tr>
  129. </tbody>
  130. </table>
  131. </div>
  132. </div>
  133. <script>
  134. let editingTCPProxyConfigUUID = ""; //The current editing TCP Proxy config UUID
  135. $("#tcpProxyForm .dropdown").dropdown();
  136. $('#tcpProxyForm').on('submit', function(event) {
  137. event.preventDefault();
  138. var form = $(this);
  139. var formValid = validateTCPProxyConfig(form);
  140. if (!formValid){
  141. return;
  142. }
  143. // Send the AJAX POST request
  144. $.ajax({
  145. type: 'POST',
  146. url: '/api/tcpprox/config/add',
  147. data: form.serialize(),
  148. success: function(response) {
  149. if (response.error) {
  150. msgbox(response.error, false, 6000);
  151. }else{
  152. msgbox("Config Added");
  153. }
  154. clearTCPProxyAddEditForm();
  155. initProxyConfigList();
  156. $("#addproxyConfig").slideUp("fast");
  157. },
  158. error: function() {
  159. msgbox('An error occurred while processing the request', false);
  160. }
  161. });
  162. });
  163. //Add proxy button pressed. Show add TCP proxy menu
  164. $("#addProxyConfigButton").on("click", function(){
  165. $('#addproxyConfig').slideToggle('fast');
  166. $("#addTcpProxyButton").show();
  167. $("#editTcpProxyButton").hide();
  168. });
  169. function clearTCPProxyAddEditForm(){
  170. $('#tcpProxyForm input, #tcpProxyForm select').val('');
  171. $('#tcpProxyForm select').dropdown('clear');
  172. }
  173. function validateTCPProxyConfig(form){
  174. // Validate timeout is an integer
  175. var timeout = parseInt(form.find('input[name="timeout"]').val());
  176. if (isNaN(timeout)) {
  177. form.find('input[name="timeout"]').parent().addClass("error");
  178. msgbox('Timeout must be a valid integer', false, 5000);
  179. return false;
  180. }else{
  181. form.find('input[name="timeout"]').parent().removeClass("error");
  182. }
  183. // Validate mode is selected
  184. var mode = form.find('select[name="mode"]').val();
  185. if (mode === '') {
  186. form.find('select[name="mode"]').parent().addClass("error");
  187. msgbox('Please select a mode', false, 5000);
  188. return false;
  189. }else{
  190. form.find('select[name="mode"]').parent().removeClass("error");
  191. }
  192. return true;
  193. }
  194. function renderProxyConfigs(proxyConfigs) {
  195. var tableBody = $('#proxyTable tbody');
  196. tableBody.empty();
  197. if (proxyConfigs.length === 0) {
  198. var noResultsRow = $('<tr><td colspan="7"><i class="green check circle icon"></i>No Proxy Configs</td></tr>');
  199. tableBody.append(noResultsRow);
  200. } else {
  201. proxyConfigs.forEach(function(config) {
  202. var runningLogo = '<i class="red circle icon"></i>';
  203. var startButton = `<button onclick="startTcpProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="play icon"></i> Start Proxy</button>`;
  204. if (config.Running){
  205. runningLogo = '<i class="green circle icon"></i>';
  206. startButton = `<button onclick="stopTcpProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="red stop icon"></i> Stop Proxy</button>`;
  207. }
  208. var modeText = "Unknown";
  209. if (config.Mode == 0){
  210. modeText = "Listen";
  211. }else if (config.Mode == 1){
  212. modeText = "Transport";
  213. }else if (config.Mode == 2){
  214. modeText = "Starter";
  215. }
  216. var thisConfig = encodeURIComponent(JSON.stringify(config));
  217. var row = $(`<tr class="tcproxConfig" uuid="${config.UUID}" config="${thisConfig}">`);
  218. row.append($('<td>').html(runningLogo + config.Name));
  219. row.append($('<td>').text(config.PortA));
  220. row.append($('<td>').text(config.PortB));
  221. row.append($('<td>').text(modeText));
  222. row.append($('<td>').text(config.Timeout));
  223. row.append($('<td>').html(`
  224. <div class="ui basic vertical fluid tiny buttons">
  225. <button class="ui button" onclick="validateProxyConfig('${config.UUID}', this);" title="Validate Config"><i class="teal question circle outline icon"></i> CXN Test</button>
  226. ${startButton}
  227. <button onclick="editTCPProxyConfig('${config.UUID}');" class="ui button" title="Edit Config"><i class="edit icon"></i> Edit </button>
  228. <button onclick="deleteTCPProxyConfig('${config.UUID}');" class="ui red basic button" title="Delete Config"><i class="trash icon"></i> Remove</button>
  229. </div>
  230. `));
  231. tableBody.append(row);
  232. });
  233. }
  234. }
  235. function getConfigDetailsFromDOM(configUUID){
  236. let thisConfig = null;
  237. $(".tcproxConfig").each(function(){
  238. let uuid = $(this).attr("uuid");
  239. if (configUUID == uuid){
  240. //This is the one we are looking for
  241. thisConfig = JSON.parse(decodeURIComponent($(this).attr("config")));
  242. }
  243. });
  244. return thisConfig;
  245. }
  246. function validateProxyConfig(configUUID, btn){
  247. $(btn).html(`<i class="ui loading spinner icon"></i>`);
  248. $.ajax({
  249. url: "/api/tcpprox/config/validate",
  250. data: {uuid: configUUID},
  251. success: function(data){
  252. if (data.error != undefined){
  253. let errormsg = data.error.charAt(0).toUpperCase() + data.error.slice(1);
  254. $(btn).html(`<i class="red times icon"></i> ${errormsg}`);
  255. msgbox(data.error, false, 6000);
  256. }else{
  257. $(btn).html(`<i class="green check icon"></i> Config Valid`);
  258. msgbox("Config Check Passed");
  259. }
  260. }
  261. })
  262. }
  263. function editTCPProxyConfig(configUUID){
  264. let targetConfig = getConfigDetailsFromDOM(configUUID);
  265. if (targetConfig != null){
  266. $("#addTcpProxyButton").hide();
  267. $("#editTcpProxyButton").show();
  268. $.each(targetConfig, function(key, value) {
  269. var field = $("#tcpProxyForm").find('[name="' + key.toLowerCase() + '"]');
  270. if (field.length > 0) {
  271. if (field.is('input')) {
  272. field.val(value);
  273. }else if (field.is('select')){
  274. if (key.toLowerCase() == "mode"){
  275. if (value == 0){
  276. value = "listen";
  277. }else if (value == 1){
  278. value = "transport";
  279. }else if (value == 2){
  280. value = "starter";
  281. }
  282. }
  283. $(field).dropdown("set selected", value);
  284. }
  285. }
  286. });
  287. editingTCPProxyConfigUUID = configUUID;
  288. $("#addproxyConfig").slideDown("fast");
  289. }else{
  290. msgbox("Unable to load target config", false);
  291. }
  292. }
  293. function confirmEditTCPProxyConfig(event){
  294. event.preventDefault();
  295. event.stopImmediatePropagation();
  296. var form = $("#tcpProxyForm");
  297. var formValid = validateTCPProxyConfig(form);
  298. if (!formValid){
  299. return;
  300. }
  301. // Send the AJAX POST request
  302. $.ajax({
  303. type: 'POST',
  304. url: '/api/tcpprox/config/edit',
  305. data: form.serialize(),
  306. success: function(response) {
  307. if (response.error) {
  308. msgbox(response.error, false, 6000);
  309. }else{
  310. msgbox("Config Updated");
  311. }
  312. initProxyConfigList();
  313. clearTCPProxyAddEditForm();
  314. $("#addproxyConfig").slideUp("fast");
  315. },
  316. error: function() {
  317. msgbox('An error occurred while processing the request', false);
  318. }
  319. });
  320. }
  321. function deleteTCPProxyConfig(configUUID){
  322. $.ajax({
  323. url: "/api/tcpprox/config/delete",
  324. method: "POST",
  325. data: {uuid: configUUID},
  326. success: function(data){
  327. if (data.error != undefined){
  328. msgbox(data.error, false, 6000);
  329. }else{
  330. msgbox("Proxy Config Removed");
  331. initProxyConfigList();
  332. }
  333. }
  334. })
  335. }
  336. //Start a TCP proxy by their config UUID
  337. function startTcpProx(configUUID){
  338. $.ajax({
  339. url: "/api/tcpprox/config/start",
  340. method: "POST",
  341. data: {uuid: configUUID},
  342. success: function(data){
  343. if (data.error != undefined){
  344. msgbox(data.error, false, 6000);
  345. }else{
  346. msgbox("Service Started");
  347. initProxyConfigList();
  348. }
  349. }
  350. });
  351. }
  352. //Stop a TCP proxy by their config UUID
  353. function stopTcpProx(configUUID){
  354. $.ajax({
  355. url: "/api/tcpprox/config/stop",
  356. method: "POST",
  357. data: {uuid: configUUID},
  358. success: function(data){
  359. if (data.error != undefined){
  360. msgbox(data.error, false, 6000);
  361. }else{
  362. msgbox("Service Stopped");
  363. initProxyConfigList();
  364. }
  365. }
  366. });
  367. }
  368. function initProxyConfigList(){
  369. $.ajax({
  370. type: 'GET',
  371. url: '/api/tcpprox/config/list',
  372. success: function(response) {
  373. renderProxyConfigs(response);
  374. },
  375. error: function() {
  376. msgbox('Unable to load proxy configs', false);
  377. }
  378. });
  379. }
  380. initProxyConfigList();
  381. </script>
  382. </div>