1
0

tcpprox.html 19 KB

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