-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
176 lines (157 loc) · 5.46 KB
/
Copy pathserver.js
File metadata and controls
176 lines (157 loc) · 5.46 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
const http = require('http');
const https = require('https');
const url = require('url');
const crypto = require('crypto');
const server = http.createServer((req, res) => {
// CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-MBX-APIKEY, Authorization');
res.setHeader('Content-Type', 'application/json');
// OPTIONS request
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Parse URL
const pathname = req.url.split('?')[0];
const query = url.parse(req.url, true).query;
// Routes
if (pathname === '/api/binance') {
const path = query.path;
const method = query.method || 'GET';
const params = query.params ? JSON.parse(query.params) : {};
const apiKey = query.apiKey;
const apiSecret = query.apiSecret;
proxyBinanceRequest(path, method, params, apiKey, apiSecret, res);
} else if (pathname === '/api/coingecko') {
const coinId = query.coinId || 'bitcoin';
proxyFetch(`https://api.coingecko.com/api/v3/simple/price?ids=${coinId}&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true`, res);
} else if (pathname === '/api/news') {
proxyFetch('https://cryptopanic.com/api/developer/v2/posts/?auth_token=40c83b7c574d9337382242cb9c152303b75d957f&public=true&kind=news', res);
} else if (pathname === '/api/price') {
const symbol = query.symbol || 'BTCUSDT';
proxyFetch(`https://api.binance.com/api/v3/ticker/price?symbol=${symbol}`, res);
} else if (pathname === '/api/whales') {
const api = query.key;
if (!api) {
res.writeHead(400);
res.end(JSON.stringify({error: 'Whale Alert API key required'}));
return;
}
proxyFetch(`https://api.whale-alert.io/v1/transactions?api_key=${api}&min_value=1000000&limit=20`, res);
} else if (pathname === '/api/square') {
const squareKey = query.squareKey;
const bodyText = query.bodyText;
if (!squareKey || !bodyText) {
res.writeHead(400);
res.end(JSON.stringify({error: 'Square API key and bodyText required'}));
return;
}
proxySquarePost(squareKey, bodyText, res);
} else {
res.writeHead(404);
res.end(JSON.stringify({error: 'Not found'}));
}
});
function proxyBinanceRequest(path, method, params, apiKey, apiSecret, res) {
if (!apiKey || !apiSecret) {
res.writeHead(400);
res.end(JSON.stringify({error: 'API key and secret required'}));
return;
}
try {
// Add timestamp
params.timestamp = Date.now();
// Create signature
const qs = new URLSearchParams(params).toString();
const signature = crypto.createHmac('sha256', apiSecret).update(qs).digest('hex');
// Build URL with signature
const fullUrl = `https://api.binance.com${path}?${qs}&signature=${signature}`;
console.log(`[${method}] ${path}`);
// Make request with auth header
const options = {
headers: {
'X-MBX-APIKEY': apiKey,
'User-Agent': 'binance-spot/1.0.2',
'Content-Type': 'application/x-www-form-urlencoded'
}
};
https.get(fullUrl, options, (response) => {
let data = '';
response.on('data', (chunk) => data += chunk);
response.on('end', () => {
try {
const parsed = JSON.parse(data);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(parsed));
} catch (e) {
res.writeHead(500);
res.end(JSON.stringify({error: 'Invalid JSON response from Binance'}));
}
});
}).on('error', (e) => {
console.error('Request error:', e.message);
res.writeHead(500);
res.end(JSON.stringify({error: `Request failed: ${e.message}`}));
});
} catch(e) {
console.error('Server error:', e);
res.writeHead(500);
res.end(JSON.stringify({error: e.message}));
}
}
function proxyFetch(endpoint, res) {
const protocol = endpoint.startsWith('https') ? https : http;
protocol.get(endpoint, (response) => {
let data = '';
response.on('data', (chunk) => data += chunk);
response.on('end', () => {
res.writeHead(200);
res.end(data);
});
}).on('error', (e) => {
res.writeHead(500);
res.end(JSON.stringify({error: e.message}));
});
}
function proxySquarePost(squareKey, bodyText, res) {
const postData = JSON.stringify({bodyTextOnly: bodyText});
const options = {
hostname: 'www.binance.com',
path: '/bapi/composite/v1/public/pgc/openApi/content/add',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Square-OpenAPI-Key': squareKey,
'clienttype': 'binanceSkill',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = https.request(options, (response) => {
let data = '';
response.on('data', (chunk) => data += chunk);
response.on('end', () => {
try {
const jsonData = JSON.parse(data);
res.writeHead(200);
res.end(JSON.stringify(jsonData));
} catch(e) {
res.writeHead(200);
res.end(data);
}
});
});
req.on('error', (e) => {
console.error('Square post error:', e.message);
res.writeHead(500);
res.end(JSON.stringify({error: e.message}));
});
req.write(postData);
req.end();
}
const PORT = 3000;
server.listen(PORT, () => {
console.log(`✅ API Proxy server running on http://localhost:${PORT}`);
});