-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
227 lines (190 loc) · 7.2 KB
/
server.js
File metadata and controls
227 lines (190 loc) · 7.2 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const express = require('express');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const app = express();
const PORT = 3000;
let nodes = [];
let transactions = [];
let addresses = {};
app.use(express.json());
app.get('/', (req, res) => {
res.json({
name: 'V-Blockchain Network',
type: 'IP Registry & Boot Node Server',
version: '1.0',
status: 'running',
endpoints: {
'POST /register-node': 'Register new node',
'GET /get-peers/:nodeId': 'Get peer list',
'POST /register-address': 'Register wallet address',
'GET /get-address-info/:address': 'Get address info',
'GET /get-all-addresses': 'Get all registered addresses',
'GET /get-all-nodes': 'Get all registered nodes',
'POST /heartbeat/:nodeId': 'Update node heartbeat',
'GET /network-stats': 'Get network statistics',
'GET /health': 'Server health check'
}
});
});
const nodesFile = 'nodes_registry.json';
const addressesFile = 'addresses_registry.json';
const loadRegistry = () => {
if (fs.existsSync(nodesFile)) {
nodes = JSON.parse(fs.readFileSync(nodesFile, 'utf8'));
}
if (fs.existsSync(addressesFile)) {
addresses = JSON.parse(fs.readFileSync(addressesFile, 'utf8'));
}
};
const saveRegistry = () => {
fs.writeFileSync(nodesFile, JSON.stringify(nodes, null, 2));
fs.writeFileSync(addressesFile, JSON.stringify(addresses, null, 2));
};
const addWindowsFirewallRule = (port, ruleName = `V-Blockchain-${port}`) => {
try {
const inboundRule = `netsh advfirewall firewall add rule name="${ruleName}-IN" dir=in action=allow protocol=tcp localport=${port} enable=yes`;
const outboundRule = `netsh advfirewall firewall add rule name="${ruleName}-OUT" dir=out action=allow protocol=tcp remoteport=${port} enable=yes`;
try {
execSync(inboundRule, { stdio: 'pipe' });
console.log(`✓ Added inbound firewall rule for port ${port}`);
} catch (e) {
console.log(`✓ Inbound firewall rule for port ${port} already exists or added`);
}
try {
execSync(outboundRule, { stdio: 'pipe' });
console.log(`✓ Added outbound firewall rule for port ${port}`);
} catch (e) {
console.log(`✓ Outbound firewall rule for port ${port} already exists or added`);
}
return true;
} catch (error) {
console.error(`✗ Failed to add firewall rules: ${error.message}`);
return false;
}
};
app.post('/register-node', (req, res) => {
const { nodeId, host, port, publicKey } = req.body;
if (!nodeId || !host || !port) {
return res.status(400).json({ error: 'Missing required fields' });
}
const existingNode = nodes.find(n => n.nodeId === nodeId);
if (existingNode) {
return res.status(400).json({ error: 'Node already registered' });
}
const node = {
nodeId,
host,
port,
publicKey,
registeredAt: Date.now(),
lastSeen: Date.now()
};
nodes.push(node);
saveRegistry();
addWindowsFirewallRule(port, `V-Node-${nodeId}`);
res.json({
success: true,
message: 'Node registered successfully',
peers: nodes.filter(n => n.nodeId !== nodeId)
});
});
app.get('/get-peers/:nodeId', (req, res) => {
const { nodeId } = req.params;
const peers = nodes.filter(n => n.nodeId !== nodeId);
res.json({ peers });
});
app.post('/register-address', (req, res) => {
const { address, username, publicKey } = req.body;
if (!address || !username || !publicKey) {
return res.status(400).json({ error: 'Missing required fields' });
}
if (addresses[address]) {
return res.status(400).json({ error: 'Address already registered' });
}
addresses[address] = {
username,
publicKey,
registeredAt: Date.now(),
balance: 50
};
saveRegistry();
res.json({
success: true,
message: 'Address registered with 50 V welcome bonus',
data: addresses[address]
});
});
app.get('/get-address-info/:address', (req, res) => {
const { address } = req.params;
const info = addresses[address];
if (!info) {
return res.status(404).json({ error: 'Address not found' });
}
res.json(info);
});
app.get('/get-all-addresses', (req, res) => {
res.json(Object.entries(addresses).map(([address, info]) => ({
address,
...info
})));
});
app.get('/get-all-nodes', (req, res) => {
const activeNodes = nodes.map(n => ({
...n,
isActive: Date.now() - n.lastSeen < 60000
}));
res.json(activeNodes);
});
app.post('/heartbeat/:nodeId', (req, res) => {
const { nodeId } = req.params;
const node = nodes.find(n => n.nodeId === nodeId);
if (node) {
node.lastSeen = Date.now();
saveRegistry();
}
res.json({ success: true });
});
app.get('/network-stats', (req, res) => {
res.json({
totalNodes: nodes.length,
totalAddresses: Object.keys(addresses).length,
totalV: Object.values(addresses).reduce((sum, addr) => sum + addr.balance, 0),
activeNodes: nodes.filter(n => Date.now() - n.lastSeen < 60000).length
});
});
app.get('/health', (req, res) => {
res.json({
status: 'OK',
timestamp: Date.now(),
nodes: nodes.length,
addresses: Object.keys(addresses).length
});
});
loadRegistry();
const SERVER = app.listen(PORT, 'localhost', () => {
console.log('\n╔════════════════════════════════════════════════════════════╗');
console.log('║ V-BLOCKCHAIN IP REGISTRY SERVER STARTED ║');
console.log('╚════════════════════════════════════════════════════════════╝');
console.log(`Server running on port ${PORT}`);
console.log(`Access at: http://localhost:${PORT}`);
console.log('────────────────────────────────────────────────────────────');
addWindowsFirewallRule(PORT, 'V-Blockchain-Server');
console.log('────────────────────────────────────────────────────────────');
console.log('Available endpoints:');
console.log(' POST /register-node - Register new node');
console.log(' GET /get-peers/:nodeId - Get peer list');
console.log(' POST /register-address - Register wallet address');
console.log(' GET /get-address-info/:addr - Get address info');
console.log(' GET /get-all-addresses - Get all registered addresses');
console.log(' GET /get-all-nodes - Get all registered nodes');
console.log(' POST /heartbeat/:nodeId - Update node heartbeat');
console.log(' GET /network-stats - Get network statistics');
console.log(' GET /health - Server health check');
console.log('════════════════════════════════════════════════════════════\n');
});
process.on('SIGINT', () => {
console.log('\nShutting down server...');
SERVER.close();
process.exit(0);
});