-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
203 lines (171 loc) · 4.51 KB
/
server.js
File metadata and controls
203 lines (171 loc) · 4.51 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
/**
* What's My IP Server
* Express-based service to display external IP addresses with geolocation
*
* @author Paul Git <paulgit@pm.me>
* @license MIT
*/
require("dotenv").config();
const express = require("express");
const path = require("path");
const FLAG_ICONS_PATH = path.join(__dirname, "node_modules", "flag-icons");
const app = express();
const PORT = process.env.PORT || 3000;
const IPINFO_TOKEN = process.env.IPINFO_TOKEN || "";
// Middleware to disable caching
app.use((req, res, next) => {
res.set({
"Cache-Control": "no-cache, no-store, max-age=1",
Pragma: "no-cache",
Vary: "*",
});
next();
});
/**
* Extract the real IP address from request headers
* Handles proxies, load balancers, and CDNs
*/
function getClientIP(req) {
const headers = [
"cf-connecting-ip", // Cloudflare
"x-forwarded-for", // Standard proxy header
"x-real-ip", // Nginx proxy
"x-cluster-client-ip", // Rackspace LB
"x-forwarded",
"forwarded-for",
"forwarded",
];
for (const header of headers) {
const value = req.headers[header];
if (value) {
// Handle comma-separated IPs (take the first one)
const ip = value.split(",")[0].trim();
// Validate it's a proper IP (not private/reserved)
if (isValidPublicIP(ip)) {
return ip;
}
}
}
// Fallback to socket address
return req.socket.remoteAddress || req.connection.remoteAddress;
}
/**
* Basic validation for public IP addresses
* This is a simplified check - in production you might want more robust validation
*/
function isValidPublicIP(ip) {
if (!ip) return false;
// Remove IPv6 prefix if present
const cleanIP = ip.replace(/^::ffff:/, "");
// Check for private IP ranges (simplified)
const privateRanges = [
/^10\./,
/^172\.(1[6-9]|2[0-9]|3[0-1])\./,
/^192\.168\./,
/^127\./,
/^169\.254\./,
/^::1$/,
/^fe80:/,
];
for (const range of privateRanges) {
if (range.test(cleanIP)) {
return false;
}
}
return true;
}
/**
* Fetch geolocation data from ipinfo.io
*/
async function getIPInfo(ip) {
if (!IPINFO_TOKEN) {
return null;
}
try {
const url = `https://ipinfo.io/${ip}?token=${IPINFO_TOKEN}`;
const response = await fetch(url);
if (!response.ok) {
console.error(`IPInfo API error: ${response.status}`);
return null;
}
return await response.json();
} catch (error) {
console.error("Error fetching IP info:", error);
return null;
}
}
// API Endpoints
/**
* GET / - Serve the HTML page
*/
app.get("/", async (req, res) => {
const format = (req.query.format || "html").toLowerCase();
const clientIP = getClientIP(req);
if (format === "json") {
const ipInfo = await getIPInfo(clientIP);
return res.json({
ip: clientIP,
hostname: ipInfo?.hostname || "Unknown",
...ipInfo,
});
}
if (format === "text") {
return res.type("text/plain").send(clientIP);
}
// Default: serve HTML
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Serve static files from public directory (after route handlers)
app.use(express.static(path.join(__dirname, "public")));
// Serve flag-icons CSS and SVG assets
app.use("/flag-icons/css", express.static(path.join(FLAG_ICONS_PATH, "css")));
app.use(
"/flag-icons/flags",
express.static(path.join(FLAG_ICONS_PATH, "flags")),
);
/**
* GET /api/ip - Get just the IP address
*/
app.get("/api/ip", (req, res) => {
const clientIP = getClientIP(req);
res.json({ ip: clientIP });
});
/**
* GET /api/info - Get IP address with geolocation data
*/
app.get("/api/info", async (req, res) => {
const clientIP = getClientIP(req);
const ipInfo = await getIPInfo(clientIP);
if (!ipInfo) {
return res.json({
ip: clientIP,
hostname: "Unknown",
error: "Geolocation data unavailable",
});
}
res.json({
ip: clientIP,
hostname: ipInfo.hostname || "Unknown",
...ipInfo,
});
});
// Health check endpoint
app.get("/health", (req, res) => {
res.json({ status: "ok", timestamp: new Date().toISOString() });
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: "Not found" });
});
// Error handler
app.use((err, req, res, next) => {
console.error("Server error:", err);
res.status(500).json({ error: "Internal server error" });
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 What's My IP server running on http://localhost:${PORT}`);
console.log(
`📍 IPInfo token configured: ${IPINFO_TOKEN ? "Yes" : "No (geolocation disabled)"}`,
);
});