1
0

streamprox.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. <h3>TCP / UDP Proxy Rules</h3>
  9. <p>A list of TCP / UDP proxy rules created on this host.</p>
  10. <div style="overflow-x: auto; ">
  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. <br>
  27. <button class="ui basic right floated button" onclick="initProxyConfigList();" title="Refresh List"><i class="ui green refresh icon"></i>Refresh</button>
  28. <br><br>
  29. </div>
  30. <div class="ui divider"></div>
  31. <div class="ui basic segment" id="addproxyConfig">
  32. <h3>Add or Edit Stream Proxy</h3>
  33. <p>Create or edit a new stream proxy instance</p>
  34. <form id="streamProxyForm" class="ui form">
  35. <div class="field" style="display:none;">
  36. <label>UUID</label>
  37. <input type="text" name="uuid">
  38. </div>
  39. <div class="field">
  40. <label>Name</label>
  41. <input type="text" name="name" placeholder="Config Name">
  42. </div>
  43. <div class="field">
  44. <label>Listening Address with Port</label>
  45. <input type="text" name="listenAddr" placeholder="">
  46. <small>Address to listen on this host. e.g. :25565 or 127.0.0.1:25565. <br>
  47. If you are using Docker, you will also need to expose this port to host network.</small>
  48. </div>
  49. <div class="field">
  50. <label>Proxy Target Address with Port</label>
  51. <input type="text" name="proxyAddr" placeholder="">
  52. <small>Server address to forward TCP / UDP package. e.g. 192.168.1.100:25565</small>
  53. </div>
  54. <div class="field">
  55. <label>Timeout (s)</label>
  56. <input type="text" name="timeout" placeholder="" value="10">
  57. <small>Connection timeout in seconds</small>
  58. </div>
  59. <Br>
  60. <div class="field">
  61. <div class="ui toggle checkbox">
  62. <input type="checkbox" tabindex="0" name="useTCP" class="hidden">
  63. <label>Enable TCP<br>
  64. <small>Forward TCP request on this listening socket</small>
  65. </label>
  66. </div>
  67. </div>
  68. <div class="field">
  69. <div class="ui toggle checkbox">
  70. <input type="checkbox" tabindex="0" name="useUDP" class="hidden">
  71. <label>Enable UDP<br>
  72. <small>Forward UDP request on this listening socket</small></label>
  73. </div>
  74. </div>
  75. <button id="addStreamProxyButton" class="ui basic button" type="submit"><i class="ui green add icon"></i> Create</button>
  76. <button id="editStreamProxyButton" class="ui basic button" onclick="confirmEditTCPProxyConfig(event, this);" style="display:none;"><i class="ui green check icon"></i> Update</button>
  77. <button class="ui basic red button" onclick="event.preventDefault(); cancelStreamProxyEdit(event);"><i class="ui red remove icon"></i> Cancel</button>
  78. </form>
  79. </div>
  80. </div>
  81. <script>
  82. let editingStreamProxyConfigUUID = ""; //The current editing TCP Proxy config UUID
  83. $("#streamProxyForm .dropdown").dropdown();
  84. $('#streamProxyForm').on('submit', function(event) {
  85. event.preventDefault();
  86. //Check if update mode
  87. if ($("#editStreamProxyButton").is(":visible")){
  88. confirmEditTCPProxyConfig(event,$("#editStreamProxyButton")[0]);
  89. return;
  90. }
  91. var form = $(this);
  92. var formValid = validateTCPProxyConfig(form);
  93. if (!formValid){
  94. return;
  95. }
  96. // Send the AJAX POST request
  97. $.cjax({
  98. type: 'POST',
  99. url: '/api/streamprox/config/add',
  100. data: form.serialize(),
  101. success: function(response) {
  102. if (response.error) {
  103. msgbox(response.error, false, 6000);
  104. }else{
  105. msgbox("Config Added");
  106. }
  107. clearStreamProxyAddEditForm();
  108. initProxyConfigList();
  109. },
  110. error: function() {
  111. msgbox('An error occurred while processing the request', false);
  112. }
  113. });
  114. });
  115. function clearStreamProxyAddEditForm(){
  116. $('#streamProxyForm input, #streamProxyForm select').val('');
  117. $('#streamProxyForm select').dropdown('clear');
  118. $("#streamProxyForm input[name=timeout]").val(10);
  119. $("#streamProxyForm .toggle.checkbox").checkbox("set unchecked");
  120. }
  121. function cancelStreamProxyEdit(event=undefined) {
  122. clearStreamProxyAddEditForm();
  123. $("#addStreamProxyButton").show();
  124. $("#editStreamProxyButton").hide();
  125. }
  126. function validateTCPProxyConfig(form){
  127. //Check if name is filled. If not, generate a random name for it
  128. var name = form.find('input[name="name"]').val()
  129. if (name == ""){
  130. let randomName = "Proxy Rule (#" + Math.round(Date.now()/1000) + ")";
  131. form.find('input[name="name"]').val(randomName);
  132. }
  133. // Validate timeout is an integer
  134. var timeout = parseInt(form.find('input[name="timeout"]').val());
  135. if (form.find('input[name="timeout"]').val() == ""){
  136. //Not set. Assign a random one to it
  137. form.find('input[name="timeout"]').val("10");
  138. timeout = 10;
  139. }
  140. if (isNaN(timeout)) {
  141. form.find('input[name="timeout"]').parent().addClass("error");
  142. msgbox('Timeout must be a valid integer', false, 5000);
  143. return false;
  144. }else{
  145. form.find('input[name="timeout"]').parent().removeClass("error");
  146. }
  147. // Validate mode is selected
  148. var mode = form.find('select[name="mode"]').val();
  149. if (mode === '') {
  150. form.find('select[name="mode"]').parent().addClass("error");
  151. msgbox('Please select a mode', false, 5000);
  152. return false;
  153. }else{
  154. form.find('select[name="mode"]').parent().removeClass("error");
  155. }
  156. return true;
  157. }
  158. function renderProxyConfigs(proxyConfigs) {
  159. var tableBody = $('#proxyTable tbody');
  160. tableBody.empty();
  161. if (proxyConfigs.length === 0) {
  162. var noResultsRow = $('<tr><td colspan="7"><i class="green check circle icon"></i>No Proxy Configs</td></tr>');
  163. tableBody.append(noResultsRow);
  164. } else {
  165. proxyConfigs.forEach(function(config) {
  166. var runningLogo = 'Stopped';
  167. var runningClass = "stopped";
  168. var startButton = `<button onclick="startStreamProx('${config.UUID}');" class="ui basic mini circular icon button" title="Start Proxy"><i class="green play icon"></i></button>`;
  169. if (config.Running){
  170. runningLogo = 'Running';
  171. startButton = `<button onclick="stopStreamProx('${config.UUID}');" class="ui basic mini circular icon button" title="Stop Proxy"><i class="red stop icon"></i></button>`;
  172. runningClass = "running"
  173. }
  174. var modeText = [];
  175. if (config.UseTCP){
  176. modeText.push("TCP")
  177. }
  178. if (config.UseUDP){
  179. modeText.push("UDP")
  180. }
  181. modeText = modeText.join(" & ")
  182. var thisConfig = encodeURIComponent(JSON.stringify(config));
  183. var row = $(`<tr class="streamproxConfig ${runningClass}" uuid="${config.UUID}" config="${thisConfig}">`);
  184. row.append($('<td>').html(`
  185. ${config.Name}
  186. <div class="statusText">${runningLogo}</div>`));
  187. row.append($('<td>').text(config.ListeningAddress));
  188. row.append($('<td>').text(config.ProxyTargetAddr));
  189. row.append($('<td>').text(modeText));
  190. row.append($('<td>').text(config.Timeout));
  191. row.append($('<td>').html(`
  192. ${startButton}
  193. <button onclick="editTCPProxyConfig('${config.UUID}');" class="ui circular basic mini icon button" title="Edit Config"><i class="edit icon"></i></button>
  194. <button onclick="deleteTCPProxyConfig('${config.UUID}');" class="ui circular red basic mini icon button" title="Delete Config"><i class="trash icon"></i></button>
  195. `));
  196. tableBody.append(row);
  197. });
  198. }
  199. }
  200. function getConfigDetailsFromDOM(configUUID){
  201. let thisConfig = null;
  202. $(".streamproxConfig").each(function(){
  203. let uuid = $(this).attr("uuid");
  204. if (configUUID == uuid){
  205. //This is the one we are looking for
  206. thisConfig = JSON.parse(decodeURIComponent($(this).attr("config")));
  207. }
  208. });
  209. return thisConfig;
  210. }
  211. function editTCPProxyConfig(configUUID){
  212. let targetConfig = getConfigDetailsFromDOM(configUUID);
  213. if (targetConfig != null){
  214. $("#addStreamProxyButton").hide();
  215. $("#editStreamProxyButton").show();
  216. $.each(targetConfig, function(key, value) {
  217. var field;
  218. if (key == "UseTCP"){
  219. let checkboxEle = $("#streamProxyForm input[name=useTCP]").parent();
  220. if (value === true){
  221. $(checkboxEle).checkbox("set checked");
  222. }else{
  223. $(checkboxEle).checkbox("set unchecked");
  224. }
  225. return;
  226. }else if (key == "UseUDP"){
  227. let checkboxEle = $("#streamProxyForm input[name=useUDP]").parent();
  228. if (value === true){
  229. $(checkboxEle).checkbox("set checked");
  230. }else{
  231. $(checkboxEle).checkbox("set unchecked");
  232. }
  233. return;
  234. }else if (key == "ListeningAddress"){
  235. field = $("#streamProxyForm input[name=listenAddr]");
  236. }else if (key == "ProxyTargetAddr"){
  237. field = $("#streamProxyForm input[name=proxyAddr]");
  238. }else if (key == "UUID"){
  239. field = $("#streamProxyForm input[name=uuid]");
  240. }else if (key == "Name"){
  241. field = $("#streamProxyForm input[name=name]");
  242. }else if (key == "Timeout"){
  243. field = $("#streamProxyForm input[name=timeout]");
  244. }
  245. if (field != undefined && field.length > 0) {
  246. field.val(value);
  247. }
  248. });
  249. editingStreamProxyConfigUUID = configUUID;
  250. }else{
  251. msgbox("Unable to load target config", false);
  252. }
  253. }
  254. function confirmEditTCPProxyConfig(event, btn){
  255. event.preventDefault();
  256. event.stopImmediatePropagation();
  257. var form = $("#streamProxyForm");
  258. let originalButtonHTML = $(btn).html();
  259. $(btn).html(`<i class="ui loading spinner icon"></i> Updating`);
  260. $(btn).addClass("disabled");
  261. var formValid = validateTCPProxyConfig(form);
  262. if (!formValid){
  263. $(btn).html(originalButtonHTML);
  264. $(btn).removeClass("disabled");
  265. return;
  266. }
  267. // Send the AJAX POST request
  268. $.cjax({
  269. type: 'POST',
  270. url: '/api/streamprox/config/edit',
  271. method: "POST",
  272. data: {
  273. uuid: $("#streamProxyForm input[name=uuid]").val().trim(),
  274. name: $("#streamProxyForm input[name=name]").val().trim(),
  275. listenAddr: $("#streamProxyForm input[name=listenAddr]").val().trim(),
  276. proxyAddr: $("#streamProxyForm input[name=proxyAddr]").val().trim(),
  277. useTCP: $("#streamProxyForm input[name=useTCP]")[0].checked ,
  278. useUDP: $("#streamProxyForm input[name=useUDP]")[0].checked ,
  279. timeout: parseInt($("#streamProxyForm input[name=timeout]").val().trim()),
  280. },
  281. success: function(response) {
  282. $(btn).html(originalButtonHTML);
  283. $(btn).removeClass("disabled");
  284. if (response.error) {
  285. msgbox(response.error, false, 6000);
  286. }else{
  287. msgbox("Config Updated");
  288. }
  289. initProxyConfigList();
  290. cancelStreamProxyEdit();
  291. clearStreamProxyAddEditForm();
  292. },
  293. error: function() {
  294. $(btn).html(originalButtonHTML);
  295. $(btn).removeClass("disabled");
  296. msgbox('An error occurred while processing the request', false);
  297. }
  298. });
  299. }
  300. function deleteTCPProxyConfig(configUUID){
  301. $.cjax({
  302. url: "/api/streamprox/config/delete",
  303. method: "POST",
  304. data: {uuid: configUUID},
  305. success: function(data){
  306. if (data.error != undefined){
  307. msgbox(data.error, false, 6000);
  308. }else{
  309. msgbox("Proxy Config Removed");
  310. initProxyConfigList();
  311. }
  312. }
  313. })
  314. }
  315. //Start a TCP proxy by their config UUID
  316. function startStreamProx(configUUID){
  317. $.cjax({
  318. url: "/api/streamprox/config/start",
  319. method: "POST",
  320. data: {uuid: configUUID},
  321. success: function(data){
  322. if (data.error != undefined){
  323. msgbox(data.error, false, 6000);
  324. }else{
  325. msgbox("Service Started");
  326. initProxyConfigList();
  327. }
  328. }
  329. });
  330. }
  331. //Stop a TCP proxy by their config UUID
  332. function stopStreamProx(configUUID){
  333. $.cjax({
  334. url: "/api/streamprox/config/stop",
  335. method: "POST",
  336. data: {uuid: configUUID},
  337. success: function(data){
  338. if (data.error != undefined){
  339. msgbox(data.error, false, 6000);
  340. }else{
  341. msgbox("Service Stopped");
  342. initProxyConfigList();
  343. }
  344. }
  345. });
  346. }
  347. function initProxyConfigList(){
  348. $.ajax({
  349. type: 'GET',
  350. url: '/api/streamprox/config/list',
  351. success: function(response) {
  352. renderProxyConfigs(response);
  353. },
  354. error: function() {
  355. msgbox('Unable to load proxy configs', false);
  356. }
  357. });
  358. }
  359. initProxyConfigList();
  360. </script>
  361. </div>