streamprox.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>Stream Proxy</h2>
  4. <p>Proxy traffic flow on layer 3 via TCP or UDP</p>
  5. </div>
  6. <div class="ui divider"></div>
  7. <div class="ui basic segment" style="margin-top: 0;">
  8. <h4>TCP / UDP 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 basic unstackable table">
  12. <thead>
  13. <tr>
  14. <th>Name</th>
  15. <th>Listening Address</th>
  16. <th>Target Address</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 Stream Proxy</h4>
  32. <p>Create or edit a new stream proxy instance</p>
  33. <form id="streamProxyForm" 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>Listening Port / Address with Port</label>
  44. <input type="text" name="listenAddr" placeholder="">
  45. <small>Port to listen on this host. e.g. :25565 or 127.0.0.1:25565. <br>
  46. If you are using Docker, you will need to expose this port to host network.</small>
  47. </div>
  48. <div class="field">
  49. <label>Proxy Target Address with Port</label>
  50. <input type="text" name="proxyAddr" placeholder="">
  51. <small>Server address to forward TCP / UDP package. e.g. 192.168.1.100:25565</small>
  52. </div>
  53. <div class="field">
  54. <label>Timeout (s)</label>
  55. <input type="text" name="timeout" placeholder="" value="10">
  56. <small>Connection timeout in seconds</small>
  57. </div>
  58. <Br>
  59. <div class="field">
  60. <div class="ui toggle checkbox">
  61. <input type="checkbox" tabindex="0" name="useTCP" class="hidden">
  62. <label>Enable TCP<br>
  63. <small>Forward TCP request on this listening socket</small>
  64. </label>
  65. </div>
  66. </div>
  67. <div class="field">
  68. <div class="ui toggle checkbox">
  69. <input type="checkbox" tabindex="0" name="useUDP" class="hidden">
  70. <label>Enable UDP<br>
  71. <small>Forward UDP request on this listening socket</small></label>
  72. </div>
  73. </div>
  74. <button id="addStreamProxyButton" class="ui basic button" type="submit"><i class="ui green add icon"></i> Create</button>
  75. <button id="editStreamProxyButton" class="ui basic button" onclick="confirmEditTCPProxyConfig(event);" style="display:none;"><i class="ui green check icon"></i> Update</button>
  76. <button class="ui basic red button" onclick="event.preventDefault(); cancelStreamProxyEdit(event);"><i class="ui red remove icon"></i> Cancel</button>
  77. <!--
  78. <div class="ui basic inverted segment" style="background: var(--theme_background_inverted); border-radius: 0.6em;">
  79. <p>TCP Proxy support the following TCP sockets proxy modes</p>
  80. <table class="ui celled padded inverted basic table">
  81. <thead>
  82. <tr><th class="single line">Mode</th>
  83. <th>Public-IP</th>
  84. <th>Concurrent Access</th>
  85. <th>Flow Diagram</th>
  86. </tr></thead>
  87. <tbody>
  88. <tr>
  89. <td>
  90. <h4 class="ui center aligned inverted header">Transport</h4>
  91. </td>
  92. <td class="single line">
  93. Server: <i class="ui green check icon"></i><br>
  94. A: <i class="ui remove icon"></i><br>
  95. B: <i class="ui green check icon"></i> (or same LAN)<br>
  96. </td>
  97. <td>
  98. <i class="ui green check icon"></i>
  99. </td>
  100. <td>Port A (e.g. 25565) <i class="arrow right icon"></i> Server<br>
  101. Server <i class="arrow right icon"></i> Port B (e.g. 192.168.0.2:25565)<br>
  102. <small>Traffic from Port A will be forward to Port B's (IP if provided and) Port</small>
  103. </td>
  104. </tr>
  105. <tr>
  106. <td>
  107. <h4 class="ui center aligned inverted header">Listen</h4>
  108. </td>
  109. <td class="single line">
  110. Server: <i class="ui green check icon"></i><br>
  111. A: <i class="ui remove icon"></i><br>
  112. B: <i class="ui remove icon"></i><br>
  113. </td>
  114. <td>
  115. <i class="ui red times icon"></i>
  116. </td>
  117. <td>Port A (e.g. 8080) <i class="arrow right icon"></i> Server<br>
  118. Port B (e.g. 8081) <i class="arrow right icon"></i> Server<br>
  119. <small>Server will act as a bridge to proxy traffic between Port A and B</small>
  120. </td>
  121. </tr>
  122. <tr>
  123. <td>
  124. <h4 class="ui center aligned inverted header">Starter</h4>
  125. </td>
  126. <td class="single line">
  127. Server: <i class="ui times icon"></i><br>
  128. A: <i class="ui green check icon"></i><br>
  129. B: <i class="ui green check icon"></i><br>
  130. </td>
  131. <td>
  132. <i class="ui red times icon"></i>
  133. </td>
  134. <td>Server <i class="arrow right icon"></i> Port A (e.g. remote.local.:8080) <br>
  135. Server <i class="arrow right icon"></i> Port B (e.g. recv.local.:8081) <br>
  136. <small>Port A and B will be actively bridged</small>
  137. </td>
  138. </tr>
  139. </tbody>
  140. </table>
  141. </div>
  142. -->
  143. </form>
  144. </div>
  145. </div>
  146. <script>
  147. let editingStreamProxyConfigUUID = ""; //The current editing TCP Proxy config UUID
  148. $("#streamProxyForm .dropdown").dropdown();
  149. $('#streamProxyForm').on('submit', function(event) {
  150. event.preventDefault();
  151. //Check if update mode
  152. if ($("#editStreamProxyButton").is(":visible")){
  153. confirmEditTCPProxyConfig(event);
  154. return;
  155. }
  156. var form = $(this);
  157. var formValid = validateTCPProxyConfig(form);
  158. if (!formValid){
  159. return;
  160. }
  161. // Send the AJAX POST request
  162. $.ajax({
  163. type: 'POST',
  164. url: '/api/streamprox/config/add',
  165. data: form.serialize(),
  166. success: function(response) {
  167. if (response.error) {
  168. msgbox(response.error, false, 6000);
  169. }else{
  170. msgbox("Config Added");
  171. }
  172. clearStreamProxyAddEditForm();
  173. initProxyConfigList();
  174. $("#addproxyConfig").slideUp("fast");
  175. },
  176. error: function() {
  177. msgbox('An error occurred while processing the request', false);
  178. }
  179. });
  180. });
  181. function clearStreamProxyAddEditForm(){
  182. $('#streamProxyForm input, #streamProxyForm select').val('');
  183. $('#streamProxyForm select').dropdown('clear');
  184. }
  185. function cancelStreamProxyEdit(event=undefined) {
  186. clearStreamProxyAddEditForm();
  187. $("#addStreamProxyButton").show();
  188. $("#editStreamProxyButton").hide();
  189. }
  190. function validateTCPProxyConfig(form){
  191. //Check if name is filled. If not, generate a random name for it
  192. var name = form.find('input[name="name"]').val()
  193. if (name == ""){
  194. let randomName = "Proxy Rule (#" + Math.round(Date.now()/1000) + ")";
  195. form.find('input[name="name"]').val(randomName);
  196. }
  197. // Validate timeout is an integer
  198. var timeout = parseInt(form.find('input[name="timeout"]').val());
  199. if (form.find('input[name="timeout"]').val() == ""){
  200. //Not set. Assign a random one to it
  201. form.find('input[name="timeout"]').val("10");
  202. timeout = 10;
  203. }
  204. if (isNaN(timeout)) {
  205. form.find('input[name="timeout"]').parent().addClass("error");
  206. msgbox('Timeout must be a valid integer', false, 5000);
  207. return false;
  208. }else{
  209. form.find('input[name="timeout"]').parent().removeClass("error");
  210. }
  211. // Validate mode is selected
  212. var mode = form.find('select[name="mode"]').val();
  213. if (mode === '') {
  214. form.find('select[name="mode"]').parent().addClass("error");
  215. msgbox('Please select a mode', false, 5000);
  216. return false;
  217. }else{
  218. form.find('select[name="mode"]').parent().removeClass("error");
  219. }
  220. return true;
  221. }
  222. function renderProxyConfigs(proxyConfigs) {
  223. var tableBody = $('#proxyTable tbody');
  224. tableBody.empty();
  225. if (proxyConfigs.length === 0) {
  226. var noResultsRow = $('<tr><td colspan="7"><i class="green check circle icon"></i>No Proxy Configs</td></tr>');
  227. tableBody.append(noResultsRow);
  228. } else {
  229. proxyConfigs.forEach(function(config) {
  230. var runningLogo = 'Stopped';
  231. var runningClass = "stopped";
  232. var startButton = `<button onclick="startStreamProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="green play icon"></i> Start Proxy</button>`;
  233. if (config.Running){
  234. runningLogo = 'Running';
  235. startButton = `<button onclick="stopStreamProx('${config.UUID}');" class="ui button" title="Start Proxy"><i class="red stop icon"></i> Stop Proxy</button>`;
  236. runningClass = "running"
  237. }
  238. var modeText = [];
  239. if (config.UseTCP){
  240. modeText.push("TCP")
  241. }
  242. if (config.UseUDP){
  243. modeText.push("UDP")
  244. }
  245. modeText = modeText.join(" | ")
  246. var thisConfig = encodeURIComponent(JSON.stringify(config));
  247. var row = $(`<tr class="tcproxConfig ${runningClass}" uuid="${config.UUID}" config="${thisConfig}">`);
  248. row.append($('<td>').html(`
  249. ${config.Name}
  250. <div class="statusText">${runningLogo}</div>`));
  251. row.append($('<td>').text(config.ListeningAddress));
  252. row.append($('<td>').text(config.ProxyTargetAddr));
  253. row.append($('<td>').text(modeText));
  254. row.append($('<td>').text(config.Timeout));
  255. row.append($('<td>').html(`
  256. <div class="ui basic vertical fluid tiny buttons">
  257. ${startButton}
  258. <button onclick="editTCPProxyConfig('${config.UUID}');" class="ui button" title="Edit Config"><i class="edit icon"></i> Edit </button>
  259. <button onclick="deleteTCPProxyConfig('${config.UUID}');" class="ui red basic button" title="Delete Config"><i class="trash icon"></i> Remove</button>
  260. </div>
  261. `));
  262. tableBody.append(row);
  263. });
  264. }
  265. }
  266. function getConfigDetailsFromDOM(configUUID){
  267. let thisConfig = null;
  268. $(".tcproxConfig").each(function(){
  269. let uuid = $(this).attr("uuid");
  270. if (configUUID == uuid){
  271. //This is the one we are looking for
  272. thisConfig = JSON.parse(decodeURIComponent($(this).attr("config")));
  273. }
  274. });
  275. return thisConfig;
  276. }
  277. function editTCPProxyConfig(configUUID){
  278. let targetConfig = getConfigDetailsFromDOM(configUUID);
  279. if (targetConfig != null){
  280. $("#addStreamProxyButton").hide();
  281. $("#editStreamProxyButton").show();
  282. $.each(targetConfig, function(key, value) {
  283. var field;
  284. if (key == "UseTCP"){
  285. let checkboxEle = $("#streamProxyForm input[name=useTCP]").parent();
  286. if (value === true){
  287. $(checkboxEle).checkbox("set checked");
  288. }else{
  289. $(checkboxEle).checkbox("set unchecked");
  290. }
  291. return;
  292. }else if (key == "UseUDP"){
  293. let checkboxEle = $("#streamProxyForm input[name=useUDP]").parent();
  294. if (value === true){
  295. $(checkboxEle).checkbox("set checked");
  296. }else{
  297. $(checkboxEle).checkbox("set unchecked");
  298. }
  299. return;
  300. }else if (key == "ListeningAddress"){
  301. field = $("#streamProxyForm input[name=listenAddr]");
  302. }else if (key == "ProxyTargetAddr"){
  303. field = $("#streamProxyForm input[name=proxyAddr]");
  304. }else if (key == "UUID"){
  305. field = $("#streamProxyForm input[name=uuid]");
  306. }else if (key == "Name"){
  307. field = $("#streamProxyForm input[name=name]");
  308. }else if (key == "Timeout"){
  309. field = $("#streamProxyForm input[name=timeout]");
  310. }
  311. if (field != undefined && field.length > 0) {
  312. field.val(value);
  313. }
  314. });
  315. editingStreamProxyConfigUUID = configUUID;
  316. $("#addproxyConfig").slideDown("fast");
  317. }else{
  318. msgbox("Unable to load target config", false);
  319. }
  320. }
  321. function confirmEditTCPProxyConfig(event){
  322. event.preventDefault();
  323. event.stopImmediatePropagation();
  324. var form = $("#streamProxyForm");
  325. var formValid = validateTCPProxyConfig(form);
  326. if (!formValid){
  327. return;
  328. }
  329. // Send the AJAX POST request
  330. $.ajax({
  331. type: 'POST',
  332. url: '/api/streamprox/config/edit',
  333. method: "POST",
  334. data: {
  335. uuid: $("#streamProxyForm input[name=uuid]").val().trim(),
  336. name: $("#streamProxyForm input[name=name]").val().trim(),
  337. listenAddr: $("#streamProxyForm input[name=listenAddr]").val().trim(),
  338. proxyAddr: $("#streamProxyForm input[name=proxyAddr]").val().trim(),
  339. useTCP: $("#streamProxyForm input[name=useTCP]")[0].checked ,
  340. useUDP: $("#streamProxyForm input[name=useUDP]")[0].checked ,
  341. timeout: parseInt($("#streamProxyForm input[name=timeout]").val().trim()),
  342. },
  343. success: function(response) {
  344. if (response.error) {
  345. msgbox(response.error, false, 6000);
  346. }else{
  347. msgbox("Config Updated");
  348. }
  349. initProxyConfigList();
  350. cancelStreamProxyEdit();
  351. },
  352. error: function() {
  353. msgbox('An error occurred while processing the request', false);
  354. }
  355. });
  356. }
  357. function deleteTCPProxyConfig(configUUID){
  358. $.ajax({
  359. url: "/api/streamprox/config/delete",
  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("Proxy Config Removed");
  367. initProxyConfigList();
  368. }
  369. }
  370. })
  371. }
  372. //Start a TCP proxy by their config UUID
  373. function startStreamProx(configUUID){
  374. $.ajax({
  375. url: "/api/streamprox/config/start",
  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 Started");
  383. initProxyConfigList();
  384. }
  385. }
  386. });
  387. }
  388. //Stop a TCP proxy by their config UUID
  389. function stopStreamProx(configUUID){
  390. $.ajax({
  391. url: "/api/streamprox/config/stop",
  392. method: "POST",
  393. data: {uuid: configUUID},
  394. success: function(data){
  395. if (data.error != undefined){
  396. msgbox(data.error, false, 6000);
  397. }else{
  398. msgbox("Service Stopped");
  399. initProxyConfigList();
  400. }
  401. }
  402. });
  403. }
  404. function initProxyConfigList(){
  405. $.ajax({
  406. type: 'GET',
  407. url: '/api/streamprox/config/list',
  408. success: function(response) {
  409. renderProxyConfigs(response);
  410. },
  411. error: function() {
  412. msgbox('Unable to load proxy configs', false);
  413. }
  414. });
  415. }
  416. initProxyConfigList();
  417. </script>
  418. </div>