webfast 0.1.32 → 0.1.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/content/js/webfast.js +173 -3
- package/ejs/collect/event.ejs +2 -2
- package/ejs/view/list.ejs +6 -1
- package/example.js +4 -1
- package/modules/bots/applications/telegram/middleware/message/location.js +4 -0
- package/modules/bots/applications/telegram/middleware/message/photo.js +63 -0
- package/modules/bots/applications/telegram/que.js +22 -1
- package/modules/bots/applications/telegram/scripts/function.js +1 -0
- package/modules/bots/applications/telegram/scripts/test/script.json +4 -2
- package/modules/bots/applications/telegram/set.js +1 -1
- package/modules/bots/applications/telegram.js +2 -2
- package/modules/data/mongo/file.js +1 -1
- package/modules/data/mongo/find.js +1 -1
- package/modules/express/init.js +280 -142
- package/modules/express/routes/view/list.get.js +3 -2
- package/package.json +4 -2
@@ -1,4 +1,174 @@
|
|
1
1
|
console.log(`WebFast!`);
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
let web= {};
|
3
|
+
web.fast = {
|
4
|
+
ts : Date.now(),
|
5
|
+
action : function(data,ell) {
|
6
|
+
console.log(`Action Function`,data,ell);
|
7
|
+
},
|
8
|
+
functions : {
|
9
|
+
form : function(data,ell) {
|
10
|
+
console.log(`Handle Form Function`);
|
11
|
+
}
|
12
|
+
},
|
13
|
+
que : {
|
14
|
+
list : [],
|
15
|
+
state : false,
|
16
|
+
run : function(count){
|
17
|
+
// Grab first before sending
|
18
|
+
item = web.fast.que.list[0];
|
19
|
+
if (item != undefined) {
|
20
|
+
delete web.fast.que.list[0];
|
21
|
+
web.fast.que.list.slice(1);
|
22
|
+
console.log(`Sending Item`,item);
|
23
|
+
|
24
|
+
web.fast.socket.send(JSON.stringify(item)); // Send data to the server
|
25
|
+
}
|
26
|
+
}
|
27
|
+
},
|
28
|
+
tmp : {
|
29
|
+
int : {}
|
30
|
+
},
|
31
|
+
socket : undefined
|
32
|
+
}
|
33
|
+
// Connect to the Socket.IO server
|
34
|
+
const telegram = window.Telegram.WebApp;
|
35
|
+
// Step 1: Parse the query string
|
36
|
+
if (webfastSocket == undefined) {
|
37
|
+
webfastSocket = window.location.host;
|
38
|
+
}
|
39
|
+
|
40
|
+
let setData;
|
41
|
+
if (telegram.initData == ``) {
|
42
|
+
setData = `hybrid.institute.anonymous`
|
43
|
+
} else {
|
44
|
+
setData = telegram.initData;
|
45
|
+
}
|
46
|
+
|
47
|
+
const socketURL = `wss://${webfastSocket.replace(`https://`,``)}socket.io/?qbt=${setData}`;
|
48
|
+
|
49
|
+
// Assuming you have a variable `socketURL` containing your WebSocket URL
|
50
|
+
web.fast.connectWebSocket = function(socketURL,maxRetries = 40, retries = 0) {
|
51
|
+
web.fast.createWebSocket = function(socketURL, maxRetries, retries) {
|
52
|
+
const ws = new WebSocket(socketURL);
|
53
|
+
web.fast.socket = ws;
|
54
|
+
ws.onopen = () => {
|
55
|
+
console.log('WebSocket connected');
|
56
|
+
// Start other things (e.g., send initial data)
|
57
|
+
web.fast.que.state = true;
|
58
|
+
};
|
59
|
+
|
60
|
+
ws.onmessage = (event) => {
|
61
|
+
console.log('Received:', event.data);
|
62
|
+
// Handle received data
|
63
|
+
web.fast.que.state = Date.now();
|
64
|
+
web.fast.receive(`socket`,event.data); // Placeholder for processing response
|
65
|
+
};
|
66
|
+
|
67
|
+
ws.onclose = (event) => {
|
68
|
+
web.fast.que.state = false;
|
69
|
+
if (retries < maxRetries) {
|
70
|
+
retries++;
|
71
|
+
console.log(`WebSocket closed. Retrying in 10 seconds...`);
|
72
|
+
setTimeout(web.fast.createWebSocket(socketURL,maxRetries,retries), 10000); // Retry after 10 seconds
|
73
|
+
} else {
|
74
|
+
console.log(`WebSocket connection failed after ${maxRetries} attempts.`);
|
75
|
+
// Handle failure (e.g., show an error message)
|
76
|
+
}
|
77
|
+
};
|
78
|
+
|
79
|
+
ws.onerror = (error) => {
|
80
|
+
console.error('WebSocket error:', error);
|
81
|
+
// Handle error (e.g., show an error message)
|
82
|
+
web.fast.que.state = false;
|
83
|
+
};
|
84
|
+
|
85
|
+
// Send message to path for example api.example.event
|
86
|
+
web.fast.sendMessage = function(path,message) {
|
87
|
+
// Add to que
|
88
|
+
web.fast.que.list.push({
|
89
|
+
path : path,
|
90
|
+
message : message});
|
91
|
+
}
|
92
|
+
};
|
93
|
+
|
94
|
+
web.fast.createWebSocket(socketURL, maxRetries, retries); // Initial connection
|
95
|
+
}
|
96
|
+
|
97
|
+
// Call the function to start the WebSocket connection
|
98
|
+
web.fast.connectWebSocket(socketURL);
|
99
|
+
web.fast.receive = function(data) {
|
100
|
+
// Placeholder for processing the received data
|
101
|
+
// Implement your logic here (e.g., update UI, handle specific messages)
|
102
|
+
console.log('Processing received data:', data);
|
103
|
+
}
|
104
|
+
|
105
|
+
// On Ready desable all forms with jquery
|
106
|
+
jQuery(document).ready(function() {
|
107
|
+
|
108
|
+
// Make function
|
109
|
+
web.fast.tmp.int.que = setInterval(function(){
|
110
|
+
// This is the function
|
111
|
+
if (web.fast.que.state != false && web.fast.que.list.length > 0) {
|
112
|
+
// State check
|
113
|
+
web.fast.que.run();
|
114
|
+
}
|
115
|
+
},10);
|
116
|
+
|
117
|
+
// Scan for all webfast ellements
|
118
|
+
jQuery(`[webfast]`).each(function() {
|
119
|
+
let elementType = jQuery(this).prop('nodeName');
|
120
|
+
console.log('Element type:', elementType);
|
121
|
+
|
122
|
+
// Get now the data
|
123
|
+
let webAction = jQuery(this).attr(`webfast`);
|
124
|
+
let action = jQuery(this).attr(`webfast-${webAction}`);
|
125
|
+
console.log(`Data Actions webAction, action`,webAction,action);
|
126
|
+
|
127
|
+
// Create action runner
|
128
|
+
// Check if id
|
129
|
+
if (jQuery(this).attr(`id`) == undefined) {
|
130
|
+
const array = new Uint32Array(1);
|
131
|
+
window.crypto.getRandomValues(array);
|
132
|
+
const randomValue = array[0];
|
133
|
+
|
134
|
+
console.log(`Set UUID: ${randomValue}`);
|
135
|
+
jQuery(this).attr(`id`,randomValue);
|
136
|
+
}
|
137
|
+
|
138
|
+
switch (elementType) {
|
139
|
+
case "FORM":
|
140
|
+
jQuery(this).submit(function(e) {
|
141
|
+
e.preventDefault(); // Prevent the default form submission
|
142
|
+
|
143
|
+
// Check what to do like socketpath
|
144
|
+
web.fast.action(action,this,e);
|
145
|
+
return false;
|
146
|
+
});
|
147
|
+
break;
|
148
|
+
case "DIV":
|
149
|
+
// We will do the div element part
|
150
|
+
// check whyt type etc
|
151
|
+
// Make request to server with websocket thingy
|
152
|
+
// Set in QUE
|
153
|
+
let other;
|
154
|
+
if (action == `list`) {
|
155
|
+
other = {
|
156
|
+
type : action,
|
157
|
+
html : jQuery(this).html()
|
158
|
+
}
|
159
|
+
}
|
160
|
+
web.fast.sendMessage(`socket.api.${webAction}`,{
|
161
|
+
ts : Date.now(),
|
162
|
+
ell : jQuery(this).attr(`id`),
|
163
|
+
other : other
|
164
|
+
})
|
165
|
+
break;
|
166
|
+
case "BUTTON":
|
167
|
+
// Check button interaction
|
168
|
+
console.error(`SETUP BUTTON INTERACTION`);
|
169
|
+
break;
|
170
|
+
default:
|
171
|
+
console.error(`No Action for : ${elementType}`);
|
172
|
+
}
|
173
|
+
});
|
174
|
+
});
|
package/ejs/collect/event.ejs
CHANGED
@@ -4,10 +4,10 @@
|
|
4
4
|
<meta charset="UTF-8">
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
6
|
<title><%= title %></title>
|
7
|
-
<script src="<%= url
|
7
|
+
<script src="<%= url %>app/content/js/jquery-3.7.1.min.js"></script>
|
8
8
|
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
9
9
|
<script src="https://unpkg.com/@tonconnect/ui@latest/dist/tonconnect-ui.min.js"></script>
|
10
|
-
<script src="<%= url
|
10
|
+
<script src="<%= url %>app/content/js/webfast.js"></script>
|
11
11
|
</head>
|
12
12
|
<body>
|
13
13
|
|
package/ejs/view/list.ejs
CHANGED
@@ -3,7 +3,12 @@
|
|
3
3
|
<head>
|
4
4
|
<meta charset="UTF-8">
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
-
<title><%= title %></title
|
6
|
+
<title><%= title %></title><!-- Include Socket.IO from CDN -->
|
7
|
+
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
|
8
|
+
<script src="<%= url %>app/content/js/jquery-3.7.1.min.js"></script>
|
9
|
+
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
10
|
+
<script src="https://unpkg.com/@tonconnect/ui@latest/dist/tonconnect-ui.min.js"></script>
|
11
|
+
<script src="<%= url %>app/content/js/webfast.js"></script>
|
7
12
|
</head>
|
8
13
|
<body>
|
9
14
|
|
package/example.js
CHANGED
@@ -5,6 +5,10 @@ module.exports = async function(req,res,body,params,command,middleValue) {
|
|
5
5
|
if (middleValue.location != undefined) {
|
6
6
|
locSendMessage = `Thank you for sending your location\n ${middleValue.location.longitude}\n${middleValue.location.latitude}`;
|
7
7
|
}
|
8
|
+
|
9
|
+
const scripting = await program.modules.telegram.script.function.check(program,command,middleValue.chat.id,middleValue,body);
|
10
|
+
console.log(scripting);
|
11
|
+
|
8
12
|
return {
|
9
13
|
message : locSendMessage,
|
10
14
|
response : {
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module.exports = async function(program,req,res,body,params,command,middleValue) {
|
2
|
+
console.log(`Location Telegram Function`);
|
3
|
+
// We have the start function
|
4
|
+
let locSendMessage = false;
|
5
|
+
|
6
|
+
// Now get the photos and save them in db files
|
7
|
+
// loop through photos
|
8
|
+
const user = middleValue.from.id;
|
9
|
+
const uuid = program.uuid.v4();
|
10
|
+
for (let photoI in middleValue.photo) {
|
11
|
+
let photo = middleValue.photo[photoI];
|
12
|
+
// photo url user
|
13
|
+
const userPhotoURL = await program.modules.telegram.functions.get.file(program,photo.file_id);
|
14
|
+
console.log(userPhotoURL);
|
15
|
+
// create url
|
16
|
+
const photoURL = `https://api.telegram.org/file/bot${process.env.telegram}/${userPhotoURL.result.file_path}`;;
|
17
|
+
|
18
|
+
//res.status(200);
|
19
|
+
// Now do the fetch process thingy
|
20
|
+
await program.modules.telegram.functions.get.downloadAndConvertToBase64(program,photoURL,async function(program,response){
|
21
|
+
console.log(`We have now file data`);
|
22
|
+
// Create id for file that is unique uuid
|
23
|
+
const meta = {
|
24
|
+
user : response.data.user,
|
25
|
+
type : response.imageData.type,
|
26
|
+
size : {
|
27
|
+
width : response.imageData.width,
|
28
|
+
height : response.imageData.height
|
29
|
+
}
|
30
|
+
}
|
31
|
+
await program.modules.data.file.uploadBuffer(program,response.buffer,uuid,meta,async function(progam,upload,meta){
|
32
|
+
console.log(`Uploaded Buffer`);
|
33
|
+
// Save to membe
|
34
|
+
// Now we have received so we can add marker to the receive message
|
35
|
+
});
|
36
|
+
|
37
|
+
},{
|
38
|
+
user : user,
|
39
|
+
data : middleValue
|
40
|
+
});
|
41
|
+
|
42
|
+
}
|
43
|
+
|
44
|
+
res.status(200);
|
45
|
+
|
46
|
+
locSendMessage = `Image succesfull received`;
|
47
|
+
|
48
|
+
const scripting = await program.modules.telegram.script.function.check(program,command,middleValue.chat.id,middleValue,body);
|
49
|
+
console.log(scripting);
|
50
|
+
|
51
|
+
// Send back
|
52
|
+
if (scripting == true) {
|
53
|
+
return true;
|
54
|
+
} else {
|
55
|
+
return {
|
56
|
+
message : locSendMessage,
|
57
|
+
response : {
|
58
|
+
message : locSendMessage,
|
59
|
+
uuid : uuid
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
@@ -76,7 +76,27 @@ module.exports = {
|
|
76
76
|
}
|
77
77
|
}
|
78
78
|
} else {
|
79
|
-
|
79
|
+
// Check if something with type:
|
80
|
+
for (let li in question.match.data) {
|
81
|
+
let matchCheck = question.match.data[li];
|
82
|
+
if (typeof matchCheck.anwser == `object`) {
|
83
|
+
// Grab type if it's there
|
84
|
+
switch (matchCheck.anwser.type) {
|
85
|
+
case command:
|
86
|
+
console.log(`It's the dynamic thing`);
|
87
|
+
matched = matchCheck;
|
88
|
+
let variable = data.message[command]
|
89
|
+
anwserData = {
|
90
|
+
type : command,
|
91
|
+
data : variable
|
92
|
+
};
|
93
|
+
break;
|
94
|
+
default:
|
95
|
+
console.log(`No matching type found`);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
80
100
|
}
|
81
101
|
|
82
102
|
// End of match process fo matched checking
|
@@ -132,6 +152,7 @@ module.exports = {
|
|
132
152
|
// Create replacelist
|
133
153
|
let replace = {
|
134
154
|
"{{URL}}" : process.env.url,
|
155
|
+
"{{WEBURL}}" : process.env.webURL,
|
135
156
|
"{{TEST}}" : "TEST REPLACED"
|
136
157
|
}
|
137
158
|
|
@@ -15,7 +15,9 @@
|
|
15
15
|
"function" : "program.modules.bots.scripts.function.response",
|
16
16
|
"next" : "nextFunc"
|
17
17
|
},{
|
18
|
-
"anwser" :
|
18
|
+
"anwser" : {
|
19
|
+
"type" : "photo"
|
20
|
+
},
|
19
21
|
"function" : "program.modules.bots.scripts.function.response",
|
20
22
|
"next" : "nextFunc"
|
21
23
|
}]
|
@@ -31,7 +33,7 @@
|
|
31
33
|
"text" : "Testing Title : {{START}}",
|
32
34
|
"buttons" : [
|
33
35
|
[
|
34
|
-
{ "text": "EventGO!", "web_app" : { "url" : "{{
|
36
|
+
{ "text": "EventGO!", "web_app" : { "url" : "{{WEBURL}}concepts/eventgo/events-list"}},
|
35
37
|
{ "text": "Create Event", "callback_data": "create_event" }
|
36
38
|
]
|
37
39
|
]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
const { MongoClient } = require('mongodb');
|
2
2
|
const uri = process.env.mongo;
|
3
|
-
const client = new MongoClient(uri
|
3
|
+
const client = new MongoClient(uri);
|
4
4
|
|
5
5
|
module.exports = {
|
6
6
|
profilePhoto : async function(program,content,meta) {
|
@@ -207,7 +207,7 @@ module.exports = async function(program,folder) {
|
|
207
207
|
} catch (message) {
|
208
208
|
// Process as other
|
209
209
|
console.log(`Process Different`);
|
210
|
-
let checkArray = [`location`];
|
210
|
+
let checkArray = [`location`,`photo`];
|
211
211
|
// Loop through checkArray
|
212
212
|
for (let c in checkArray) {
|
213
213
|
const command = checkArray[c];
|
@@ -215,7 +215,7 @@ module.exports = async function(program,folder) {
|
|
215
215
|
if (indexCheck != -1) {
|
216
216
|
console.log(`Run this as middleware`);
|
217
217
|
try {
|
218
|
-
const runFunc = await program.modules.telegram.middleware[key][command](req,res,body,params,command,middleValue);
|
218
|
+
const runFunc = await program.modules.telegram.middleware[key][command](program,req,res,body,params,command,middleValue);
|
219
219
|
const respFunc = runFunc;
|
220
220
|
// PRocess response for object
|
221
221
|
if (respFunc == undefined) {
|
@@ -3,7 +3,7 @@ const fs = require('fs');
|
|
3
3
|
|
4
4
|
// Define the MongoDB URI
|
5
5
|
const uri = process.env.mongo;
|
6
|
-
const client = new MongoClient(uri
|
6
|
+
const client = new MongoClient(uri);
|
7
7
|
|
8
8
|
module.exports = {
|
9
9
|
uploadBuffer: async function (progam,buffer, filename, metadata = {}, callback) {
|
@@ -18,7 +18,7 @@ module.exports = function(db,collection) {
|
|
18
18
|
|
19
19
|
async function main() {
|
20
20
|
// Create a new MongoClient
|
21
|
-
const client = new MongoClient(uri
|
21
|
+
const client = new MongoClient(uri);
|
22
22
|
|
23
23
|
try {
|
24
24
|
// Connect to the MongoDB server
|
package/modules/express/init.js
CHANGED
@@ -1,178 +1,316 @@
|
|
1
1
|
module.exports = async function (program) {
|
2
|
-
|
3
|
-
|
2
|
+
console.log(`Starting UP Express`);
|
3
|
+
program.express = {
|
4
4
|
ts: Date.now(),
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
process : program.set.process
|
6
|
+
};
|
7
|
+
|
8
|
+
const express = require('express');
|
9
|
+
const cors = require('cors');
|
10
|
+
const bodyParser = require('body-parser');
|
11
|
+
const WebSocket = require('ws');
|
12
|
+
const crypto = require(`crypto`)
|
13
|
+
const port = 1221;
|
14
|
+
const basePath = `/api`;
|
15
|
+
|
16
|
+
const app = express();
|
17
|
+
|
18
|
+
const corsOptions = {
|
16
19
|
origin: '*',
|
17
20
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
18
21
|
credentials: true,
|
19
22
|
optionsSuccessStatus: 204,
|
20
|
-
|
21
|
-
|
22
|
-
app.use(cors(corsOptions));
|
23
|
-
app.use(bodyParser.json());
|
24
|
-
app.use(bodyParser.urlencoded({ extended: true }));
|
25
|
-
app.set('view engine', 'ejs');
|
26
|
-
|
27
|
-
let routesPath = program.path.join(__dirname, `routes`);
|
28
|
-
// Check if custom routes path
|
29
|
-
if (program.set.path != undefined) {
|
30
|
-
routesPath = program.path.join(program.set.path,`routes`);
|
31
|
-
}
|
23
|
+
};
|
32
24
|
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
app.use(cors(corsOptions));
|
26
|
+
app.use(bodyParser.json());
|
27
|
+
app.use(bodyParser.urlencoded({ extended: true }));
|
28
|
+
app.set('view engine', 'ejs');
|
29
|
+
|
30
|
+
let routesPath = program.path.join(__dirname, `routes`);
|
31
|
+
// Check if custom routes path
|
32
|
+
if (program.set.path != undefined) {
|
33
|
+
routesPath = program.path.join(program.set.path, `routes`);
|
34
|
+
}
|
35
|
+
|
36
|
+
let exprs = {};
|
37
|
+
|
38
|
+
try {
|
36
39
|
let routesData = await program.modules.walkDirectory(routesPath);
|
37
|
-
|
40
|
+
|
38
41
|
for (let routeData of routesData) {
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
42
|
+
let routePath = `${basePath}/${routeData.name}`;
|
43
|
+
|
44
|
+
const split = routeData.name.split('.');
|
45
|
+
routeData.type = split.length > 1 ? split[split.length - 1] : 'get';
|
46
|
+
routeData.name = split.length > 1 ? split[0] : routeData.name;
|
47
|
+
|
48
|
+
const routeID = program.uuid.v4();
|
49
|
+
routeData.tempID = routeID;
|
50
|
+
|
51
|
+
try {
|
52
|
+
const stats = await program.fs.statSync(routeData.path);
|
53
|
+
const isDirectory = stats.isDirectory();
|
54
|
+
|
55
|
+
if (isDirectory) {
|
56
|
+
for (let subData of routeData.sub) {
|
57
|
+
if (subData !== undefined) {
|
58
|
+
const routeName = subData.name.replace('.get', '').replace('.post', '');
|
59
|
+
const subDataSplit = subData.path.split('.');
|
60
|
+
const type = subDataSplit[subDataSplit.length - 2];
|
61
|
+
|
62
|
+
subData.name = routeName;
|
63
|
+
delete subData.sub;
|
64
|
+
subData.type = type;
|
65
|
+
|
66
|
+
subData.func = require(subData.path);
|
67
|
+
exprs[routePath + '/' + routeName] = subData;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
} else {
|
71
|
+
routeData.func = require(routeData.path);
|
72
|
+
exprs[routePath] = routeData;
|
65
73
|
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
exprs[routePath] = routeData;
|
74
|
+
} catch (err) {
|
75
|
+
console.error(`Error Route Func`, routePath);
|
76
|
+
console.error(err);
|
70
77
|
}
|
71
|
-
|
72
|
-
|
73
|
-
console.error(err);
|
74
|
-
}
|
75
|
-
|
76
|
-
routeData.webwalk = 0;
|
78
|
+
|
79
|
+
routeData.webwalk = 0;
|
77
80
|
}
|
78
|
-
|
81
|
+
|
79
82
|
program.express.routes = exprs;
|
80
|
-
|
83
|
+
|
81
84
|
for (let route in exprs) {
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
let routeData = exprs[route];
|
86
|
+
let state = false;
|
87
|
+
|
88
|
+
try {
|
89
|
+
app[routeData.type](route, async (req, res) => {
|
90
|
+
try {
|
91
|
+
exprs[route].webwalk++;
|
89
92
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
+
// Get body
|
94
|
+
const requestBody = req.body;
|
95
|
+
const reqParams = req.params;
|
93
96
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
+
await routeData.func(program, req, res, route, requestBody, reqParams);
|
98
|
+
} catch (err) {
|
99
|
+
console.error(`Error With Route:`, route);
|
100
|
+
console.error(err);
|
101
|
+
}
|
102
|
+
});
|
103
|
+
state = true;
|
104
|
+
} catch (err) {
|
97
105
|
console.error(err);
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
|
102
|
-
console.error(err);
|
103
|
-
console.error(`Error Setting Up Route`);
|
104
|
-
}
|
105
|
-
|
106
|
-
exprs[route].state = state;
|
106
|
+
console.error(`Error Setting Up Route`);
|
107
|
+
}
|
108
|
+
|
109
|
+
exprs[route].state = state;
|
107
110
|
}
|
108
|
-
|
111
|
+
|
109
112
|
console.log(`Routes are set up successfully`);
|
110
|
-
|
113
|
+
} catch (err) {
|
111
114
|
console.error(err);
|
112
115
|
console.error(`Error Setting Up Routes`);
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
+
}
|
117
|
+
|
118
|
+
program.express.app = app;
|
116
119
|
|
117
|
-
|
118
|
-
|
120
|
+
// Let app listen for content
|
121
|
+
app.get(`/app/content/ton/manifest.json`, async (req, res) => {
|
119
122
|
// Let's create a json
|
120
123
|
const manifest = {
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
+
url: process.env.url,
|
125
|
+
name: process.env.name,
|
126
|
+
iconUrl: process.env.image
|
124
127
|
};
|
125
|
-
|
128
|
+
|
126
129
|
res.setHeader('Content-Type', 'application/json');
|
127
130
|
res.json(manifest);
|
128
|
-
|
131
|
+
});
|
129
132
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
// Check if minify at the end
|
138
|
-
const fileName = req.params.file;
|
139
|
-
const isMinified = /-min\.js$/.test(fileName);
|
140
|
-
|
141
|
-
if (isMinified) {
|
142
|
-
console.log(`${fileName} ends with -min.js`);
|
143
|
-
const toRequestFile = req.params.file.replace(`-min.js`,`.js`);
|
144
|
-
contentFolder = program.path.join(__dirname,`..`,`..`,`app`,`content`,req.params.type,toRequestFile);
|
145
|
-
} else {
|
146
|
-
console.log(`${fileName} does not end with -min.js`);
|
147
|
-
}
|
133
|
+
app.get(`/app/content/:type/:file`, async (req, res) => {
|
134
|
+
console.log(`Content Get`);
|
135
|
+
// Try To get file from content folder
|
136
|
+
try {
|
137
|
+
const filePath = program.path.join(__dirname, `..`, `..`, `app`, `content`, req.params.type, req.params.file);
|
138
|
+
let contentFolder = filePath;
|
148
139
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
140
|
+
// Check if minify at the end
|
141
|
+
const fileName = req.params.file;
|
142
|
+
const isMinified = /-min\.js$/.test(fileName);
|
143
|
+
|
144
|
+
if (isMinified) {
|
145
|
+
console.log(`${fileName} ends with -min.js`);
|
146
|
+
const toRequestFile = req.params.file.replace(`-min.js`, `.js`);
|
147
|
+
contentFolder = program.path.join(__dirname, `..`, `..`, `app`, `content`, req.params.type, toRequestFile);
|
148
|
+
} else {
|
149
|
+
console.log(`${fileName} does not end with -min.js`);
|
150
|
+
}
|
151
|
+
|
152
|
+
res.sendFile(contentFolder);
|
153
|
+
} catch (err) {
|
154
|
+
console.error(err);
|
155
|
+
console.error(`Error Getting : ${req.params.type}`, req.params.file);
|
156
|
+
}
|
157
|
+
})
|
155
158
|
|
156
|
-
|
159
|
+
app.listen(port, () => {
|
157
160
|
console.log(`Server Listening`, port, basePath);
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
+
});
|
162
|
+
|
163
|
+
program.express.url = {
|
161
164
|
adaptive: {
|
162
|
-
|
163
|
-
|
165
|
+
get: [],
|
166
|
+
post: [],
|
164
167
|
},
|
165
168
|
set: function (requestPath, actionType, callback) {
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
169
|
+
program.express.url.adaptive[actionType] = app[actionType](requestPath, async (req, res) => {
|
170
|
+
let run = await callback(req, res, req.body, req.params);
|
171
|
+
return run;
|
172
|
+
});
|
173
|
+
return true;
|
171
174
|
},
|
172
|
-
};
|
173
|
-
|
174
|
-
program.express.setted = true;
|
175
|
-
|
176
|
-
return program;
|
177
175
|
};
|
178
|
-
|
176
|
+
|
177
|
+
program.express.setted = true;
|
178
|
+
|
179
|
+
let clients = new Map();
|
180
|
+
// Start socket thingy
|
181
|
+
const PORT = process.env.socket || 3000;
|
182
|
+
const wss = new WebSocket.Server({ port: PORT });
|
183
|
+
|
184
|
+
|
185
|
+
wss.on('connection', (ws, req) => {
|
186
|
+
console.log(`Socket Connected`);
|
187
|
+
|
188
|
+
// Generate a unique ID for the WebSocket connection
|
189
|
+
const clientId = program.uuid.v4();
|
190
|
+
const reqURL = req.url;
|
191
|
+
console.log(`We have some data`, reqURL);
|
192
|
+
const queryStringWithoutQBT = reqURL.replace('/socket.io/?qbt=', '');
|
193
|
+
const queryParamsArray = queryStringWithoutQBT.split('&');
|
194
|
+
|
195
|
+
const parsedQuery = queryParamsArray.reduce((acc, param) => {
|
196
|
+
const [key, value] = param.split('=');
|
197
|
+
acc[key] = decodeURIComponent(value);
|
198
|
+
return acc;
|
199
|
+
}, {});
|
200
|
+
|
201
|
+
// Extract data from the parsed query
|
202
|
+
const { auth_date, query_id, user, hash } = parsedQuery;
|
203
|
+
|
204
|
+
// Stringify the 'user' field if it contains JSON data
|
205
|
+
if (user != undefined) {
|
206
|
+
try {
|
207
|
+
parsedQuery.user = JSON.stringify(parsedQuery.user);
|
208
|
+
} catch (error) {
|
209
|
+
console.error('Error parsing JSON in user field:', error);
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
// Construct the data check string
|
214
|
+
const sortedKeys = Object.keys(parsedQuery).sort();
|
215
|
+
const data_check_string = sortedKeys.map(key => `${key}=${String(parsedQuery[key])}`).join('\n');
|
216
|
+
|
217
|
+
function HMAC_SHA256(data, key) {
|
218
|
+
const hmac = crypto.createHmac('sha256', key);
|
219
|
+
hmac.update(data);
|
220
|
+
return hmac.digest('hex');
|
221
|
+
}
|
222
|
+
|
223
|
+
const bot_token = process.env.telegram; // replace with your actual bot token
|
224
|
+
const secret_key = HMAC_SHA256(bot_token, 'WebAppData');
|
225
|
+
const calculated_hash = HMAC_SHA256(data_check_string, secret_key);
|
226
|
+
|
227
|
+
const received_hash = hash; // replace with the actual received hash
|
228
|
+
|
229
|
+
if (calculated_hash === received_hash) {
|
230
|
+
// Data is from Telegram and has not been tampered with
|
231
|
+
// Additional check for auth_date if needed
|
232
|
+
const currentUnixTimestamp = Math.floor(new Date().getTime() / 1000);
|
233
|
+
if (parseInt(auth_date, 10) <= currentUnixTimestamp) {
|
234
|
+
// Data is not outdated
|
235
|
+
// Use the validated data as needed
|
236
|
+
console.log('Data from Telegram is valid');
|
237
|
+
} else {
|
238
|
+
console.error('Received data is outdated');
|
239
|
+
}
|
240
|
+
} else {
|
241
|
+
console.error('Received data has been tampered with');
|
242
|
+
}
|
243
|
+
|
244
|
+
console.log(parsedQuery);
|
245
|
+
|
246
|
+
// Store the WebSocket connection with its ID in the map
|
247
|
+
clients.set(clientId, ws);
|
248
|
+
|
249
|
+
// Send the client ID to the connected client
|
250
|
+
ws.send(JSON.stringify({ type: 'clientId', id: clientId, params: parsedQuery }));
|
251
|
+
|
252
|
+
// Set up a ping interval to keep the connection alive
|
253
|
+
const pingInterval = setInterval(() => {
|
254
|
+
if (ws.readyState === WebSocket.OPEN) {
|
255
|
+
ws.ping();
|
256
|
+
} else {
|
257
|
+
// If the connection is closed, remove it from the map
|
258
|
+
clearInterval(pingInterval);
|
259
|
+
clients.delete(clientId);
|
260
|
+
console.log(`Removed disconnected socket with ID: ${clientId}`);
|
261
|
+
}
|
262
|
+
}, 5000); // Adjust the interval as needed
|
263
|
+
|
264
|
+
ws.on('close', () => {
|
265
|
+
console.log(`Socket Disconnected`);
|
266
|
+
clearInterval(pingInterval);
|
267
|
+
clients.delete(clientId);
|
268
|
+
});
|
269
|
+
|
270
|
+
// WebSocket on message event
|
271
|
+
ws.on('message', async (message) => {
|
272
|
+
console.log(`Received message from ${clientId}: ${message}`);
|
273
|
+
|
274
|
+
try {
|
275
|
+
// Check for function
|
276
|
+
const json = JSON.parse(message.toString(`utf-8`));
|
277
|
+
const data = json.message;
|
278
|
+
const path = json.path;
|
279
|
+
const split = path.split(".")
|
280
|
+
|
281
|
+
// Check if function is running in program modules that you can add in the init scirpt when using remote
|
282
|
+
if (program.express.process != undefined) {
|
283
|
+
try {
|
284
|
+
let resp = await program.express.process[split[0]][split[1]][split[2]](program,ws,json,data,path);
|
285
|
+
if (resp != false) {
|
286
|
+
ws.send(JSON.stringify(resp));
|
287
|
+
}
|
288
|
+
} catch (err) {
|
289
|
+
console.error(`Error Running program.express.process for `,split[0],split[1],split[2]);
|
290
|
+
ws.send(JSON.stringify({
|
291
|
+
ts : Date.now(),
|
292
|
+
error : true,
|
293
|
+
message : `Error Event Program receive`
|
294
|
+
}));
|
295
|
+
}
|
296
|
+
}
|
297
|
+
|
298
|
+
// Add your custom on message logic here
|
299
|
+
// For example, you can broadcast the message to all connected clients
|
300
|
+
clients.forEach((client, id) => {
|
301
|
+
if (client.readyState === WebSocket.OPEN && id !== clientId) {
|
302
|
+
//ws.send(`Broadcast from ${clientId}: ${message}`);
|
303
|
+
}
|
304
|
+
});
|
305
|
+
|
306
|
+
// Check if
|
307
|
+
} catch(err) {
|
308
|
+
console.error(`Error Something`);
|
309
|
+
|
310
|
+
}
|
311
|
+
});
|
312
|
+
});
|
313
|
+
|
314
|
+
|
315
|
+
return program;
|
316
|
+
};
|
@@ -1,12 +1,13 @@
|
|
1
1
|
module.exports = async function(program,req,res,route) {
|
2
2
|
console.log(`Create`);
|
3
|
-
const fullPath = program.path.join(__dirname, `..`,`..`,`..`,`..`,`ejs`,`
|
3
|
+
const fullPath = program.path.join(__dirname, `..`,`..`,`..`,`..`,`ejs`,`view`,`list.ejs`);
|
4
4
|
|
5
5
|
// Render the EJS template
|
6
6
|
res.render(fullPath, {
|
7
7
|
title: 'EJS Example',
|
8
8
|
name: 'John Doe',
|
9
9
|
isAdmin: true,
|
10
|
-
fruits: ['Apple', 'Banana', 'Orange']
|
10
|
+
fruits: ['Apple', 'Banana', 'Orange'],
|
11
|
+
url : process.env.url
|
11
12
|
});
|
12
13
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "webfast",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.37",
|
4
4
|
"description": "WebFast! Bot Application, including TON mini-apps for makign it easy and fast to build ini-apps",
|
5
5
|
"main": "index.js",
|
6
6
|
"repository": {
|
@@ -39,6 +39,7 @@
|
|
39
39
|
"dependencies": {
|
40
40
|
"body-parser": "^1.20.2",
|
41
41
|
"cors": "^2.8.5",
|
42
|
+
"crypto": "^1.0.1",
|
42
43
|
"dotenv": "^16.0.3",
|
43
44
|
"ejs": "^3.1.9",
|
44
45
|
"express": "^4.18.2",
|
@@ -49,6 +50,7 @@
|
|
49
50
|
"mongodb": "^6.3.0",
|
50
51
|
"node-fetch": "^3.3.2",
|
51
52
|
"path": "^0.12.7",
|
52
|
-
"uuid": "^9.0.1"
|
53
|
+
"uuid": "^9.0.1",
|
54
|
+
"ws": "^8.16.0"
|
53
55
|
}
|
54
56
|
}
|