Ramesh-vani commited on
Commit
03da6c5
·
verified ·
1 Parent(s): 6c8ebf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +198 -197
app.py CHANGED
@@ -971,218 +971,219 @@ async def exe(websocket,connected,key,process_ids):
971
  souptmp = BeautifulSoup(response.text, "html.parser")
972
 
973
  # Step 1: Define the initial HTML string
 
 
 
974
  html_str = """
975
  <!DOCTYPE html>
976
  <html lang="en">
977
  <head>
978
  <meta charset="UTF-8">
979
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
980
-
981
  </head>
982
  <body>
983
  <h1>Hello, World!</h1>
984
  </body>
985
  <script>
986
- window.addEventListener('message', function(event) {
987
- if (event.origin === '${fullURL}|| https://nottri.com') {
988
- console.log('Message received from parent:', event.data);
989
- // Handle the received message from parent
990
- var receivedMessage = event.data;
991
- // Get the div with the ID "web-data" or create it if it doesn't exist
992
- var webDataDiv = document.getElementById('web-data');
993
- if (!webDataDiv) {
994
- webDataDiv = document.createElement('div');
995
- webDataDiv.id = 'web-data';
996
- webDataDiv.style.all='unset';
997
- document.body.appendChild(webDataDiv);
998
- }
999
- // Insert the received message into the web-data div
1000
- webDataDiv.innerHTML = receivedMessage;
1001
- }
1002
- });
1003
-
1004
- // Open WebSocket connection
1005
- const ws = new WebSocket('wss://ramesh-vani-ws-bridge2.hf.space');
1006
-
1007
- // When WebSocket connection is open
1008
- ws.onopen = function(event) {
1009
- console.log('WebSocket connection opened');
1010
- let events = { "type": "init" ,
1011
- };
1012
-
1013
- ws.send(JSON.stringify(events));
1014
- };
1015
-
1016
- // Intercept fetch requests
1017
- const originalFetch = window.fetch;
1018
- window.fetch = function(input, init) {
1019
- console.log('Element tried to make a fetch request:');
1020
- console.log('Requested URL: ' + input);
1021
- console.log('Request method: ' + (init && init.method ? init.method : 'GET'));
1022
- console.log('Request body: ' + (init && init.body ? init.body : ''));
1023
- console.log('Request headers:', init && init.headers ? init.headers : '');
1024
- // Send fetch request information to WebSocket server
1025
- const requestData = {
1026
- "type": "cmd",
1027
- 'project_name': 'project_files',
1028
- 'command': {"type": "curl"},
1029
- method: init && init.method ? init.method : 'GET',
1030
- url: input,
1031
- headers: init && init.headers ? init.headers : {},
1032
- data: init && init.body ? init.body : ''
1033
- };
1034
-
1035
- ws.send(JSON.stringify(requestData));
1036
-
1037
- // Return a promise that resolves with the response from WebSocket server
1038
- return new Promise((resolve, reject) => {
1039
- ws.onmessage = function(event) {
1040
- console.log('Message received from WebSocket server:', event.data);
1041
- const responseFromServer = JSON.parse(event.data);
1042
-
1043
- // Create a new Response with the data received from the WebSocket server
1044
- const customResponse = new Response(responseFromServer.data, { status: 200 });
1045
- resolve(customResponse);
1046
- };
1047
- });
1048
- };
1049
- // Select all CSS links
1050
- var cssLinks = document.querySelectorAll('link');
1051
-
1052
- // Loop through each CSS link
1053
- cssLinks.forEach(function(link) {
1054
- // Intercept the link here, you can modify attributes or perform other actions
1055
- console.log("Intercepted CSS link: ", link.href);
1056
- });
1057
-
1058
- document.addEventListener("submit", function(event) {
1059
- // Check if the submitted element is a form
1060
- if (event.target.tagName.toLowerCase() === 'form') {
1061
- event.preventDefault(); // Prevent default form submission behavior
1062
-
1063
- // Access form input values
1064
- var formData = {};
1065
- var formElements = event.target.elements;
1066
- for (var i = 0; i < formElements.length; i++) {
1067
- var element = formElements[i];
1068
- if (element.tagName === 'INPUT') {
1069
- formData[element.name] = element.value;
1070
- }
1071
- }
1072
-
1073
- // Print form action, method, and form data
1074
- console.log("Form Action: " + event.target.getAttribute("action"));
1075
- console.log("Form Method: " + event.target.getAttribute("method"));
1076
- console.log("Form Data:", formData);
1077
-
1078
- // You can also send the data to a server using AJAX or fetch
1079
- // For example:
1080
- fetch(event.target.getAttribute("action"), {
1081
- method: event.target.getAttribute("method"),
1082
- body: JSON.stringify(formData),
1083
- headers: {
1084
- 'Content-Type': 'application/json'
1085
- }
1086
- }).then(response => {
1087
- // Handle response from server
1088
- });
1089
- }
1090
- });
1091
-
1092
- document.body.addEventListener('AjaxDetected', function (e) {
1093
- console.log(e.detail.method, e.detail.url, e.detail.data);
1094
- }, false);
1095
-
1096
- (function () {
1097
- const arl = new Object();
1098
- arl._open = XMLHttpRequest.prototype.open;
1099
- arl._send = XMLHttpRequest.prototype.send;
1100
- arl.callback = function () {
1101
- const event = new CustomEvent('AjaxDetected', {
1102
- detail: {
1103
- url: this.url,
1104
- method: this.method,
1105
- data: this.data
1106
- }
1107
- });
1108
- document.body.dispatchEvent(event);
1109
- }
1110
-
1111
- function notNullString(input) {
1112
- return input || '';
1113
- }
1114
-
1115
- XMLHttpRequest.prototype.open = function (a, b) {
1116
- a = notNullString(a);
1117
- b = notNullString(b);
1118
-
1119
- arl._open.apply(this, arguments);
1120
- arl.method = a;
1121
- arl.url = b;
1122
-
1123
- if (a.toLowerCase() == 'get') {
1124
- arl.data = b.split('?');
1125
- arl.data = notNullString(arl.data[1]);
1126
- }
1127
- }
1128
-
1129
- XMLHttpRequest.prototype.send = function (a, b) {
1130
- a = notNullString(a);
1131
- b = notNullString(b);
1132
-
1133
- arl._send.apply(this, arguments);
1134
-
1135
- if (arl.method.toLowerCase() == 'post') {
1136
- arl.data = a;
1137
- }
1138
-
1139
- arl.callback();
1140
- }
1141
- }());
1142
-
1143
- const currentUrl = new URL("{url}");
1144
- alert(currentUrl)
1145
-
1146
- function handleLinkClick(event) {
1147
- event.preventDefault();
1148
- const link = event.target.href;
1149
- const absoluteUrl = new URL(link);
1150
- // Changing the host
1151
- absoluteUrl.hostname = currentUrl.hostname;
1152
- // Converting back to string
1153
- let modifiedUrl = absoluteUrl.toString();
1154
-
1155
- window.parent.postMessage({ url: modifiedUrl }, '*');
1156
- }
1157
-
1158
- document.addEventListener('click', function(event) {
1159
- // Check if the clicked element is a link
1160
- alert(event)
1161
- if (event.target.tagName === 'A') {
1162
- // Determine the location of the clicked link
1163
- let clickedElement = event.target;
1164
- let parentElement = clickedElement.parentElement;
1165
-
1166
- while (parentElement) {
1167
- if (parentElement.tagName === 'HEADER') {
1168
 
1169
- break;
1170
- } else if (parentElement.tagName === 'FOOTER') {
1171
 
1172
- break;
1173
- } else if (parentElement.tagName === 'NAV') {
 
 
1174
 
1175
- break;
1176
- }
1177
- parentElement = parentElement.parentElement;
1178
- }
1179
-
1180
- // Call the function to handle link clicks
1181
- handleLinkClick(event);
1182
- }
1183
- });</script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1184
  </html>
1185
- """.format(url=url)
 
 
1186
 
1187
  # Step 2: Parse the initial HTML content
1188
  soup = BeautifulSoup(html_str, 'html.parser')
 
971
  souptmp = BeautifulSoup(response.text, "html.parser")
972
 
973
  # Step 1: Define the initial HTML string
974
+
975
+ fullURL = "http://127.0.0.1:8000/"
976
+
977
  html_str = """
978
  <!DOCTYPE html>
979
  <html lang="en">
980
  <head>
981
  <meta charset="UTF-8">
982
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
 
983
  </head>
984
  <body>
985
  <h1>Hello, World!</h1>
986
  </body>
987
  <script>
988
+ window.addEventListener('message', function(event) {{
989
+ if (event.origin === '{fullURL} || https://nottri.com') {{
990
+ console.log('Message received from parent:', event.data);
991
+ // Handle the received message from parent
992
+ var receivedMessage = event.data;
993
+ // Get the div with the ID "web-data" or create it if it doesn't exist
994
+ var webDataDiv = document.getElementById('web-data');
995
+ if (!webDataDiv) {{
996
+ webDataDiv = document.createElement('div');
997
+ webDataDiv.id = 'web-data';
998
+ webDataDiv.style.all = 'unset';
999
+ document.body.appendChild(webDataDiv);
1000
+ }}
1001
+ // Insert the received message into the web-data div
1002
+ webDataDiv.innerHTML = receivedMessage;
1003
+ }}
1004
+ }});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1005
 
1006
+ // Open WebSocket connection
1007
+ const ws = new WebSocket('wss://ramesh-vani-ws-bridge2.hf.space');
1008
 
1009
+ // When WebSocket connection is open
1010
+ ws.onopen = function(event) {{
1011
+ console.log('WebSocket connection opened');
1012
+ let events = {{ "type": "init" }};
1013
 
1014
+ ws.send(JSON.stringify(events));
1015
+ }};
1016
+
1017
+ // Intercept fetch requests
1018
+ const originalFetch = window.fetch;
1019
+ window.fetch = function(input, init) {{
1020
+ console.log('Element tried to make a fetch request:');
1021
+ console.log('Requested URL: ' + input);
1022
+ console.log('Request method: ' + (init && init.method ? init.method : 'GET'));
1023
+ console.log('Request body: ' + (init && init.body ? init.body : ''));
1024
+ console.log('Request headers:', init && init.headers ? init.headers : '');
1025
+ // Send fetch request information to WebSocket server
1026
+ const requestData = {{
1027
+ "type": "cmd",
1028
+ 'project_name': 'project_files',
1029
+ 'command': {{ "type": "curl" }},
1030
+ method: init && init.method ? init.method : 'GET',
1031
+ url: input,
1032
+ headers: init && init.headers ? init.headers : {{}},
1033
+ data: init && init.body ? init.body : ''
1034
+ }};
1035
+
1036
+ ws.send(JSON.stringify(requestData));
1037
+
1038
+ // Return a promise that resolves with the response from WebSocket server
1039
+ return new Promise((resolve, reject) => {{
1040
+ ws.onmessage = function(event) {{
1041
+ console.log('Message received from WebSocket server:', event.data);
1042
+ const responseFromServer = JSON.parse(event.data);
1043
+
1044
+ // Create a new Response with the data received from the WebSocket server
1045
+ const customResponse = new Response(responseFromServer.data, {{ status: 200 }});
1046
+ resolve(customResponse);
1047
+ }};
1048
+ }});
1049
+ }};
1050
+ // Select all CSS links
1051
+ var cssLinks = document.querySelectorAll('link');
1052
+
1053
+ // Loop through each CSS link
1054
+ cssLinks.forEach(function(link) {{
1055
+ // Intercept the link here, you can modify attributes or perform other actions
1056
+ console.log("Intercepted CSS link: ", link.href);
1057
+ }});
1058
+
1059
+ document.addEventListener("submit", function(event) {{
1060
+ // Check if the submitted element is a form
1061
+ if (event.target.tagName.toLowerCase() === 'form') {{
1062
+ event.preventDefault(); // Prevent default form submission behavior
1063
+
1064
+ // Access form input values
1065
+ var formData = {{}};
1066
+ var formElements = event.target.elements;
1067
+ for (var i = 0; i < formElements.length; i++) {{
1068
+ var element = formElements[i];
1069
+ if (element.tagName === 'INPUT') {{
1070
+ formData[element.name] = element.value;
1071
+ }}
1072
+ }}
1073
+
1074
+ // Print form action, method, and form data
1075
+ console.log("Form Action: " + event.target.getAttribute("action"));
1076
+ console.log("Form Method: " + event.target.getAttribute("method"));
1077
+ console.log("Form Data:", formData);
1078
+
1079
+ // You can also send the data to a server using AJAX or fetch
1080
+ // For example:
1081
+ fetch(event.target.getAttribute("action"), {{
1082
+ method: event.target.getAttribute("method"),
1083
+ body: JSON.stringify(formData),
1084
+ headers: {{
1085
+ 'Content-Type': 'application/json'
1086
+ }}
1087
+ }}).then(response => {{
1088
+ // Handle response from server
1089
+ }});
1090
+ }}
1091
+ }});
1092
+
1093
+ document.body.addEventListener('AjaxDetected', function (e) {{
1094
+ console.log(e.detail.method, e.detail.url, e.detail.data);
1095
+ }}, false);
1096
+
1097
+ (function () {{
1098
+ const arl = new Object();
1099
+ arl._open = XMLHttpRequest.prototype.open;
1100
+ arl._send = XMLHttpRequest.prototype.send;
1101
+ arl.callback = function () {{
1102
+ const event = new CustomEvent('AjaxDetected', {{
1103
+ detail: {{
1104
+ url: this.url,
1105
+ method: this.method,
1106
+ data: this.data
1107
+ }}
1108
+ }});
1109
+ document.body.dispatchEvent(event);
1110
+ }}
1111
+
1112
+ function notNullString(input) {{
1113
+ return input || '';
1114
+ }}
1115
+
1116
+ XMLHttpRequest.prototype.open = function (a, b) {{
1117
+ a = notNullString(a);
1118
+ b = notNullString(b);
1119
+
1120
+ arl._open.apply(this, arguments);
1121
+ arl.method = a;
1122
+ arl.url = b;
1123
+
1124
+ if (a.toLowerCase() == 'get') {{
1125
+ arl.data = b.split('?');
1126
+ arl.data = notNullString(arl.data[1]);
1127
+ }}
1128
+ }}
1129
+
1130
+ XMLHttpRequest.prototype.send = function (a, b) {{
1131
+ a = notNullString(a);
1132
+ b = notNullString(b);
1133
+
1134
+ arl._send.apply(this, arguments);
1135
+
1136
+ if (arl.method.toLowerCase() == 'post') {{
1137
+ arl.data = a;
1138
+ }}
1139
+
1140
+ arl.callback();
1141
+ }}
1142
+ }}());
1143
+
1144
+ const currentUrl = new URL("{url}");
1145
+ alert(currentUrl);
1146
+
1147
+ function handleLinkClick(event) {{
1148
+ event.preventDefault();
1149
+ const link = event.target.href;
1150
+ const absoluteUrl = new URL(link);
1151
+ // Changing the host
1152
+ absoluteUrl.hostname = currentUrl.hostname;
1153
+ // Converting back to string
1154
+ let modifiedUrl = absoluteUrl.toString();
1155
+
1156
+ window.parent.postMessage({{ url: modifiedUrl }}, '*');
1157
+ }}
1158
+
1159
+ document.addEventListener('click', function(event) {{
1160
+ // Check if the clicked element is a link
1161
+ alert(event);
1162
+ if (event.target.tagName === 'A') {{
1163
+ // Determine the location of the clicked link
1164
+ let clickedElement = event.target;
1165
+ let parentElement = clickedElement.parentElement;
1166
+
1167
+ while (parentElement) {{
1168
+ if (parentElement.tagName === 'HEADER') {{
1169
+ break;
1170
+ }} else if (parentElement.tagName === 'FOOTER') {{
1171
+ break;
1172
+ }} else if (parentElement.tagName === 'NAV') {{
1173
+ break;
1174
+ }}
1175
+ parentElement = parentElement.parentElement;
1176
+ }}
1177
+
1178
+ // Call the function to handle link clicks
1179
+ handleLinkClick(event);
1180
+ }}
1181
+ }});
1182
+ </script>
1183
  </html>
1184
+ """.format(url=url, fullURL=fullURL)
1185
+
1186
+
1187
 
1188
  # Step 2: Parse the initial HTML content
1189
  soup = BeautifulSoup(html_str, 'html.parser')