tcpprox.html 19 KB

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