-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
157 lines (140 loc) · 6.27 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const http = require('http');
const fs = require('fs');
const axios = require('axios');
require('dotenv').config();
let port = process.env.server_port;
const userdata = JSON.parse(fs.readFileSync('user.json'));
async function makeUbiaRequest(body, url) {
// Define the request headers
const headers = {
"method": "POST",
"scheme": "https",
"path": url,
"authority": "portal.ubianet.com",
"accept": "*/*",
"content-type": "application/json",
"x-ubia-auth-usertoken": userdata.token
};
try {
const response = await axios.post(`https://portal.ubianet.com${url}`, body, { headers });
const data = response.data;
if (data.code === 0) {
return { "code": response.status, "msg": data.msg, "data": data };
} else {
console.error("Request Failed", data.msg); // Log error message from API
return { "code": response.status, "msg": data.msg };
}
} catch (error) {
// Handle and log errors during the API request
if (error.response) {
console.error("Error response data:", error.response.data);
console.error("Status code:", error.response.status);
return { "code": error.response.status, "msg": error.response.data };
} else if (error.request) {
console.error("No response received:", error.request);
return { "code": 500, "msg": 'Unknown Error' };
} else {
console.error("Request error:", error.message);
return { "code": 500, "msg": error.message };
}
}
}
async function GETrequest(req, res) {
switch (req.url) {
case '/api/v2/user/device_list':
const ubia_response_device_list = await makeUbiaRequest({}, req.url);
res.writeHead(ubia_response_device_list.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_device_list.data)); // Ensure JSON data is stringified
res.end();
break;
case '/api/user/families':
const ubia_response_families = await makeUbiaRequest({"token":userdata.token}, req.url);
res.writeHead(ubia_response_families.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_families.data)); // Ensure JSON data is stringified
res.end();
break;
case '/api/user/get_subscription_ios_device':
const ubia_response_get_subscription_ios_device = await makeUbiaRequest({}, req.url);
res.writeHead(ubia_response_get_subscription_ios_device.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_get_subscription_ios_device.data)); // Ensure JSON data is stringified
res.end();
break;
default:
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('Not Found');
res.end();
break;
}
}
async function POSTrequest(req, res) {
let body = '';
// Accumulate data chunks in the body string
req.on('data', chunk => {
body += chunk.toString(); // Convert Buffer to string
});
// When the body is fully received, proceed
req.on('end', async () => {
// Try to parse the JSON body
try {
const parsedBody = JSON.parse(body);
parsedBody.token = userdata.token; // Attach the token
// Handle different POST routes
switch (req.url) {
case '/api/user/qry/device/device_services':
const ubia_response_device_services = await makeUbiaRequest(parsedBody, req.url);
res.writeHead(ubia_response_device_services.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_device_services.data));
res.end();
break;
case '/api/user/cloud_list':
const ubia_response_cloud_list = await makeUbiaRequest(parsedBody, req.url);
res.writeHead(ubia_response_cloud_list.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_cloud_list.data));
res.end();
break;
case '/api/user/event_calendar':
const ubia_response_event_calendar = await makeUbiaRequest(parsedBody, req.url);
res.writeHead(ubia_response_event_calendar.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_event_calendar.data));
res.end();
break;
case '/api/user/get_cloud_video_url':
const ubia_response_get_cloud_video_url = await makeUbiaRequest(parsedBody, req.url);
res.writeHead(ubia_response_get_cloud_video_url.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_get_cloud_video_url.data));
res.end();
break;
case '/api/v2/user/card4g-info':
const ubia_response_card4g_info = await makeUbiaRequest(parsedBody, req.url);
res.writeHead(ubia_response_card4g_info.code, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(ubia_response_card4g_info.data));
res.end();
break;
default:
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('Not Found');
res.end();
break;
}
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ error: 'Invalid JSON' }));
res.end();
}
});
}
// Create a server object:
http.createServer(function (req, res) {
switch (req.method) {
case 'GET':
GETrequest(req, res);
break;
case 'POST':
console.log(req.body)
POSTrequest(req, res);
break;
default:
break;
}
}).listen(port); // The server object listens on port 8020
console.log(`Server running on port ${port}`);