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
|
const CryptoJS = require('crypto-js');
const { URLSearchParams } = require('url');
const dns = require('dns').promises;
// Configuration and state
const hostIP = "https://app.ecourts.gov.in/ecourt_mobile_DC/";
let jwttoken = "";
let regenerateWebserviceCallFlag = false;
let globaliv = "4B6250655368566D";
let globalIndex = 0;
// Utility: Check internet connection
async function checkDeviceOnlineStatus() {
try {
await dns.lookup('google.com');
return true;
} catch {
console.error("Please check your internet connection and try again");
return false;
}
}
// Show error message (replace alert with console)
function showErrorMessage(message) {
console.error("Error:", message);
}
// Generate random hex
function genRanHex(size) {
return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
}
// Generate global IV
function generateGlobalIv() {
const ivOptions = [
"556A586E32723575", "34743777217A2543", "413F4428472B4B62",
"48404D635166546A", "614E645267556B58", "655368566D597133"
];
const indices = [0, 1, 2, 3, 4, 5];
for (let i = indices.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[indices[i], indices[j]] = [indices[j], indices[i]];
}
globaliv = ivOptions[indices[0]];
globalIndex = indices[0];
return globaliv;
}
// Encrypt data
function encryptData(data) {
const dataEncoded = JSON.stringify(data);
generateGlobalIv();
const randomiv = genRanHex(16);
const key = CryptoJS.enc.Hex.parse('4D6251655468576D5A7134743677397A');
const iv = CryptoJS.enc.Hex.parse(globaliv + randomiv);
const encrypted = CryptoJS.AES.encrypt(dataEncoded, key, { iv });
let encrypted_data = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
encrypted_data = randomiv + globalIndex + encrypted_data;
return encrypted_data;
}
// Decrypt response
function decodeResponse(result) {
const key = CryptoJS.enc.Hex.parse('3273357638782F413F4428472B4B6250');
const iv_random = CryptoJS.enc.Hex.parse(result.trim().slice(0, 32));
const result_split = result.trim().slice(32);
const bytes = CryptoJS.AES.decrypt(result_split, key, { iv: iv_random });
let plaintext = bytes.toString(CryptoJS.enc.Utf8);
plaintext = plaintext.replace(/[\u0000-\u0019]+/g, "");
return plaintext;
}
// API call wrapper
async function callToWebService(url, data, callback) {
const online = await checkDeviceOnlineStatus();
if (!online) return;
try {
const encryptedData = encryptData(data);
const headers = {
'Content-Type': 'application/json',
};
headers['Authorization'] = 'Bearer ' + encryptData(jwttoken);
const params = new URLSearchParams({ data: encryptedData });
const fullUrl = `${url}?${params.toString()}`;
const res = await fetch(fullUrl, {
method: 'GET',
headers
});
const responseText = await res.text();
const decodedResponse = JSON.parse(decodeResponse(responseText));
if (decodedResponse.token) {
jwttoken = decodedResponse.token;
}
console.log(decodedResponse)
if (decodedResponse.status === 'N') {
if (decodedResponse.status_code === '401') {
if (!regenerateWebserviceCallFlag) {
regenerateWebserviceCallFlag = true;
const packageName = "com.eCourts.mobile";
const uidObj = { uid: "324456:" + packageName };
const newData = { ...data, ...uidObj };
return callToWebService(url, newData, callback);
} else {
showErrorMessage("Session expired!");
}
}
if (decodedResponse.msg) {
showErrorMessage(decodedResponse.msg);
}
return;
}
callback(decodedResponse);
regenerateWebserviceCallFlag = false;
} catch (error) {
console.error('Error calling web service:', error.message);
showErrorMessage("An error occurred while processing your request.");
regenerateWebserviceCallFlag = false;
}
}
// Fetch Court Complexes
async function getCourtComplexes(state_code, dist_code, callback) {
const url = hostIP + "courtEstWebService.php";
const data = {
action_code: "fillCourtComplex",
state_code,
dist_code
};
await callToWebService(url, data, callback);
}
getCourtComplexes("1", "101", (res) => {
console.log("Court Complexes:", res.courtComplex);
});
|