tcpprox.html 14 KB

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