tcpprox.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }
  172. function validateTCPProxyConfig(form){
  173. // Validate timeout is an integer
  174. var timeout = parseInt(form.find('input[name="timeout"]').val());
  175. if (isNaN(timeout)) {
  176. form.find('input[name="timeout"]').parent().addClass("error");
  177. msgbox('Timeout must be a valid integer', false, 5000);
  178. return false;
  179. }else{
  180. form.find('input[name="timeout"]').parent().removeClass("error");
  181. }
  182. // Validate mode is selected
  183. var mode = form.find('select[name="mode"]').val();
  184. if (mode === '') {
  185. form.find('select[name="mode"]').parent().addClass("error");
  186. msgbox('Please select a mode', false, 5000);
  187. return false;
  188. }else{
  189. form.find('select[name="mode"]').parent().removeClass("error");
  190. }
  191. return true;
  192. }
  193. function renderProxyConfigs(proxyConfigs) {
  194. var tableBody = $('#proxyTable tbody');
  195. tableBody.empty();
  196. if (proxyConfigs.length === 0) {
  197. var noResultsRow = $('<tr><td colspan="7"><i class="green check circle icon"></i>No Proxy Configs</td></tr>');
  198. tableBody.append(noResultsRow);
  199. } else {
  200. proxyConfigs.forEach(function(config) {
  201. var runningLogo = '<i class="red circle icon"></i>';
  202. var startButton = `<button onclick="startTcpProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="play icon"></i> Start Proxy</button>`;
  203. if (config.Running){
  204. runningLogo = '<i class="green circle icon"></i>';
  205. startButton = `<button onclick="stopTcpProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="red stop icon"></i> Stop Proxy</button>`;
  206. }
  207. var modeText = "Unknown";
  208. if (config.Mode == 0){
  209. modeText = "Listen";
  210. }else if (config.Mode == 1){
  211. modeText = "Transport";
  212. }else if (config.Mode == 2){
  213. modeText = "Starter";
  214. }
  215. var thisConfig = encodeURIComponent(JSON.stringify(config));
  216. var row = $(`<tr class="tcproxConfig" uuid="${config.UUID}" config="${thisConfig}">`);
  217. row.append($('<td>').html(runningLogo + config.Name));
  218. row.append($('<td>').text(config.PortA));
  219. row.append($('<td>').text(config.PortB));
  220. row.append($('<td>').text(modeText));
  221. row.append($('<td>').text(config.Timeout));
  222. row.append($('<td>').html(`
  223. <div class="ui basic vertical fluid tiny buttons">
  224. <button class="ui button" onclick="validateProxyConfig('${config.UUID}', this);" title="Validate Config"><i class="teal question circle outline icon"></i> CXN Test</button>
  225. ${startButton}
  226. <button onclick="editTCPProxyConfig('${config.UUID}');" class="ui button" title="Edit Config"><i class="edit icon"></i> Edit </button>
  227. <button onclick="deleteTCPProxyConfig('${config.UUID}');" class="ui red basic button" title="Delete Config"><i class="trash icon"></i> Remove</button>
  228. </div>
  229. `));
  230. tableBody.append(row);
  231. });
  232. }
  233. }
  234. function getConfigDetailsFromDOM(configUUID){
  235. let thisConfig = null;
  236. $(".tcproxConfig").each(function(){
  237. let uuid = $(this).attr("uuid");
  238. if (configUUID == uuid){
  239. //This is the one we are looking for
  240. thisConfig = JSON.parse(decodeURIComponent($(this).attr("config")));
  241. }
  242. });
  243. return thisConfig;
  244. }
  245. function validateProxyConfig(configUUID, btn){
  246. $(btn).html(`<i class="ui loading spinner icon"></i>`);
  247. $.ajax({
  248. url: "/api/tcpprox/config/validate",
  249. data: {uuid: configUUID},
  250. success: function(data){
  251. if (data.error != undefined){
  252. let errormsg = data.error.charAt(0).toUpperCase() + data.error.slice(1);
  253. $(btn).html(`<i class="red times icon"></i> ${errormsg}`);
  254. msgbox(data.error, false, 6000);
  255. }else{
  256. $(btn).html(`<i class="green check icon"></i> Config Valid`);
  257. msgbox("Config Check Passed");
  258. }
  259. }
  260. })
  261. }
  262. function editTCPProxyConfig(configUUID){
  263. let targetConfig = getConfigDetailsFromDOM(configUUID);
  264. if (targetConfig != null){
  265. $("#addTcpProxyButton").hide();
  266. $("#editTcpProxyButton").show();
  267. $.each(targetConfig, function(key, value) {
  268. var field = $("#tcpProxyForm").find('[name="' + key.toLowerCase() + '"]');
  269. if (field.length > 0) {
  270. if (field.is('input')) {
  271. field.val(value);
  272. }else if (field.is('select')){
  273. if (key.toLowerCase() == "mode"){
  274. if (value == 0){
  275. value = "listen";
  276. }else if (value == 1){
  277. value = "transport";
  278. }else if (value == 2){
  279. value = "starter";
  280. }
  281. }
  282. $(field).dropdown("set selected", value);
  283. }
  284. }
  285. });
  286. editingTCPProxyConfigUUID = configUUID;
  287. $("#addproxyConfig").slideDown("fast");
  288. }else{
  289. msgbox("Unable to load target config", false);
  290. }
  291. }
  292. function confirmEditTCPProxyConfig(event){
  293. event.preventDefault();
  294. event.stopImmediatePropagation();
  295. var form = $("#tcpProxyForm");
  296. var formValid = validateTCPProxyConfig(form);
  297. if (!formValid){
  298. return;
  299. }
  300. // Send the AJAX POST request
  301. $.ajax({
  302. type: 'POST',
  303. url: '/api/tcpprox/config/edit',
  304. data: form.serialize(),
  305. success: function(response) {
  306. if (response.error) {
  307. msgbox(response.error, false, 6000);
  308. }else{
  309. msgbox("Config Updated");
  310. }
  311. initProxyConfigList();
  312. clearTCPProxyAddEditForm();
  313. $("#addproxyConfig").slideUp("fast");
  314. },
  315. error: function() {
  316. msgbox('An error occurred while processing the request', false);
  317. }
  318. });
  319. }
  320. function deleteTCPProxyConfig(configUUID){
  321. }
  322. function initProxyConfigList(){
  323. $.ajax({
  324. type: 'GET',
  325. url: '/api/tcpprox/config/list',
  326. success: function(response) {
  327. renderProxyConfigs(response);
  328. },
  329. error: function() {
  330. msgbox('Unable to load proxy configs', false);
  331. }
  332. });
  333. }
  334. initProxyConfigList();
  335. </script>
  336. </div>