tcpprox.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. <table class="ui celled padded table">
  45. <thead>
  46. <tr><th class="single line">Mode</th>
  47. <th>Public-IP</th>
  48. <th>Concurrent Access</th>
  49. <th>Flow Diagram</th>
  50. </tr></thead>
  51. <tbody>
  52. <tr>
  53. <td>
  54. <h3 class="ui center aligned header">Listen</h3>
  55. </td>
  56. <td class="single line">
  57. Server: <i class="ui green check icon"></i><br>
  58. A: <i class="ui remove icon"></i><br>
  59. B: <i class="ui remove icon"></i><br>
  60. </td>
  61. <td>
  62. <i class="ui red times icon"></i>
  63. </td>
  64. <td>Port A (e.g. 8080) <i class="arrow right icon"></i> Server<br>
  65. Port B (e.g. 8081) <i class="arrow right icon"></i> Server<br>
  66. <small>Server will act as a bridge to proxy traffic between Port A and B</small>
  67. </td>
  68. </tr>
  69. <tr>
  70. <td>
  71. <h3 class="ui center aligned header">Transport</h3>
  72. </td>
  73. <td class="single line">
  74. Server: <i class="ui green check icon"></i><br>
  75. A: <i class="ui remove icon"></i><br>
  76. B: <i class="ui green check icon"></i> (or same LAN)<br>
  77. </td>
  78. <td>
  79. <i class="ui green check icon"></i>
  80. </td>
  81. <td>Port A (e.g. 25565) <i class="arrow right icon"></i> Server<br>
  82. Server <i class="arrow right icon"></i> Port B (e.g. 192.168.0.2:25565)<br>
  83. <small>Traffic from Port A will be forward to Port B's (IP if provided and) Port</small>
  84. </td>
  85. </tr>
  86. <tr>
  87. <td>
  88. <h3 class="ui center aligned header">Starter</h3>
  89. </td>
  90. <td class="single line">
  91. Server: <i class="ui times icon"></i><br>
  92. A: <i class="ui green check icon"></i><br>
  93. B: <i class="ui green check icon"></i><br>
  94. </td>
  95. <td>
  96. <i class="ui red times icon"></i>
  97. </td>
  98. <td>Server <i class="arrow right icon"></i> Port A (e.g. remote.local.:8080) <br>
  99. Server <i class="arrow right icon"></i> Port B (e.g. recv.local.:8081) <br>
  100. <small>Port A and B will be actively bridged</small>
  101. </td>
  102. </tr>
  103. </tbody>
  104. </table>
  105. </form>
  106. <div class="ui divider"></div>
  107. </div>
  108. <div class="ui basic segment">
  109. <div style="overflow-x: auto;">
  110. <h3>TCP Proxy Configs</h3>
  111. <p>A list of TCP proxy configs created on this host. To enable them, use the toggle button on the right.</p>
  112. <table id="proxyTable" class="ui basic celled unstackable table">
  113. <thead>
  114. <tr>
  115. <th>Name</th>
  116. <th>PortA</th>
  117. <th>PortB</th>
  118. <th>Mode</th>
  119. <th>Timeout (s)</th>
  120. <th>Actions</th>
  121. </tr>
  122. </thead>
  123. <tbody>
  124. </tbody>
  125. </table>
  126. </div>
  127. </div>
  128. <script>
  129. let editingTCPProxyConfigUUID = ""; //The current editing TCP Proxy config UUID
  130. $("#tcpProxyForm .dropdown").dropdown();
  131. $('#tcpProxyForm').on('submit', function(event) {
  132. event.preventDefault();
  133. var form = $(this);
  134. var formValid = validateTCPProxyConfig(form);
  135. if (!formValid){
  136. return;
  137. }
  138. // Send the AJAX POST request
  139. $.ajax({
  140. type: 'POST',
  141. url: '/api/tcpprox/config/add',
  142. data: form.serialize(),
  143. success: function(response) {
  144. if (response.error) {
  145. msgbox(response.error, false, 6000);
  146. }else{
  147. msgbox("Config Added");
  148. }
  149. clearTCPProxyAddEditForm();
  150. initProxyConfigList();
  151. $("#addproxyConfig").slideUp("fast");
  152. },
  153. error: function() {
  154. msgbox('An error occurred while processing the request', false);
  155. }
  156. });
  157. });
  158. //Add proxy button pressed. Show add TCP proxy menu
  159. $("#addProxyConfigButton").on("click", function(){
  160. $('#addproxyConfig').slideToggle('fast');
  161. $("#addTcpProxyButton").show();
  162. $("#editTcpProxyButton").hide();
  163. });
  164. function clearTCPProxyAddEditForm(){
  165. $('#tcpProxyForm input, #tcpProxyForm select').val('');
  166. }
  167. function validateTCPProxyConfig(form){
  168. // Validate timeout is an integer
  169. var timeout = parseInt(form.find('input[name="timeout"]').val());
  170. if (isNaN(timeout)) {
  171. form.find('input[name="timeout"]').parent().addClass("error");
  172. msgbox('Timeout must be a valid integer', false, 5000);
  173. return false;
  174. }else{
  175. form.find('input[name="timeout"]').parent().removeClass("error");
  176. }
  177. // Validate mode is selected
  178. var mode = form.find('select[name="mode"]').val();
  179. if (mode === '') {
  180. form.find('select[name="mode"]').parent().addClass("error");
  181. msgbox('Please select a mode', false, 5000);
  182. return false;
  183. }else{
  184. form.find('select[name="mode"]').parent().removeClass("error");
  185. }
  186. return true;
  187. }
  188. function renderProxyConfigs(proxyConfigs) {
  189. var tableBody = $('#proxyTable tbody');
  190. tableBody.empty();
  191. if (proxyConfigs.length === 0) {
  192. var noResultsRow = $('<tr><td colspan="7"><i class="green check circle icon"></i>No Proxy Configs</td></tr>');
  193. tableBody.append(noResultsRow);
  194. } else {
  195. proxyConfigs.forEach(function(config) {
  196. var runningLogo = '<i class="red circle icon"></i>';
  197. var startButton = `<button onclick="startTcpProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="play icon"></i></button>`;
  198. if (config.Running){
  199. runningLogo = '<i class="green circle icon"></i>';
  200. startButton = `<button onclick="stopTcpProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="red stop icon"></i></button>`;
  201. }
  202. var modeText = "Unknown";
  203. if (config.Mode == 0){
  204. modeText = "Listen";
  205. }else if (config.Mode == 1){
  206. modeText = "Transport";
  207. }else if (config.Mode == 2){
  208. modeText = "Starter";
  209. }
  210. var thisConfig = encodeURIComponent(JSON.stringify(config));
  211. var row = $(`<tr class="tcproxConfig" uuid="${config.UUID}" config="${thisConfig}">`);
  212. row.append($('<td>').html(runningLogo + config.Name));
  213. row.append($('<td>').text(config.PortA));
  214. row.append($('<td>').text(config.PortB));
  215. row.append($('<td>').text(modeText));
  216. row.append($('<td>').text(config.Timeout));
  217. row.append($('<td>').html(`
  218. <div class="ui basic icon buttons">
  219. <button class="ui button" onclick="validateProxyConfig('${config.UUID}', this);" title="Validate Config"><i class="teal question circle outline icon"></i></button>
  220. ${startButton}
  221. <button onclick="editTCPProxyConfig('${config.UUID}');" class="ui button" title="Edit Config"><i class="edit icon"></i></button>
  222. <button onclick="deleteTCPProxyConfig('${config.UUID}');" class="ui red button" title="Delete Config"><i class="trash icon"></i></button>
  223. </div>
  224. `));
  225. tableBody.append(row);
  226. });
  227. }
  228. }
  229. function getConfigDetailsFromDOM(configUUID){
  230. let thisConfig = null;
  231. $(".tcproxConfig").each(function(){
  232. let uuid = $(this).attr("uuid");
  233. if (configUUID == uuid){
  234. //This is the one we are looking for
  235. thisConfig = JSON.parse(decodeURIComponent($(this).attr("config")));
  236. }
  237. });
  238. return thisConfig;
  239. }
  240. function validateProxyConfig(configUUID, btn){
  241. $(btn).html(`<i class="ui loading spinner icon"></i>`);
  242. $.ajax({
  243. url: "/api/tcpprox/config/validate",
  244. data: {uuid: configUUID},
  245. success: function(data){
  246. if (data.error != undefined){
  247. $(btn).html(`<i class="red times icon"></i>`);
  248. msgbox(data.error, false, 6000);
  249. }else{
  250. $(btn).html(`<i class="green check icon"></i>`);
  251. msgbox("Config Check Passed");
  252. }
  253. }
  254. })
  255. }
  256. function editTCPProxyConfig(configUUID){
  257. let targetConfig = getConfigDetailsFromDOM(configUUID);
  258. if (targetConfig != null){
  259. $("#addTcpProxyButton").hide();
  260. $("#editTcpProxyButton").show();
  261. $.each(targetConfig, function(key, value) {
  262. var field = $("#tcpProxyForm").find('[name="' + key.toLowerCase() + '"]');
  263. if (field.length > 0) {
  264. if (field.is('input')) {
  265. field.val(value);
  266. }else if (field.is('select')){
  267. if (key.toLowerCase() == "mode"){
  268. if (value == 0){
  269. value = "listen";
  270. }else if (value == 1){
  271. value = "transport";
  272. }else if (value == 2){
  273. value = "starter";
  274. }
  275. }
  276. $(field).dropdown("set selected", value);
  277. }
  278. }
  279. });
  280. editingTCPProxyConfigUUID = configUUID;
  281. $("#addproxyConfig").slideDown("fast");
  282. }else{
  283. msgbox("Unable to load target config", false);
  284. }
  285. }
  286. function confirmEditTCPProxyConfig(event){
  287. event.preventDefault();
  288. event.stopImmediatePropagation();
  289. var form = $("#tcpProxyForm");
  290. var formValid = validateTCPProxyConfig(form);
  291. if (!formValid){
  292. return;
  293. }
  294. // Send the AJAX POST request
  295. $.ajax({
  296. type: 'POST',
  297. url: '/api/tcpprox/config/edit',
  298. data: form.serialize(),
  299. success: function(response) {
  300. if (response.error) {
  301. msgbox(response.error, false, 6000);
  302. }else{
  303. msgbox("Config Updated");
  304. }
  305. initProxyConfigList();
  306. clearTCPProxyAddEditForm();
  307. $("#addproxyConfig").slideUp("fast");
  308. },
  309. error: function() {
  310. msgbox('An error occurred while processing the request', false);
  311. }
  312. });
  313. }
  314. function deleteTCPProxyConfig(configUUID){
  315. }
  316. function initProxyConfigList(){
  317. $.ajax({
  318. type: 'GET',
  319. url: '/api/tcpprox/config/list',
  320. success: function(response) {
  321. renderProxyConfigs(response);
  322. },
  323. error: function() {
  324. msgbox('Unable to load proxy configs', false);
  325. }
  326. });
  327. }
  328. initProxyConfigList();
  329. </script>
  330. </div>