Spaces:
Running
Running
File size: 8,953 Bytes
10a683f c28eefd e0b5600 c28eefd 10a683f c28eefd c5dd7ed c28eefd 36421f3 bdecacc c28eefd bf2df30 c28eefd bf2df30 c28eefd 10a683f c28eefd 10a683f c28eefd 10a683f c28eefd 10a683f c28eefd 3f48589 c28eefd bdecacc c28eefd 36421f3 bdecacc 10a683f c28eefd bdecacc c28eefd 36421f3 c28eefd 36421f3 c28eefd 36421f3 c28eefd 36421f3 c28eefd 36421f3 c28eefd 36421f3 c28eefd 10a683f c28eefd 83ed759 c28eefd 10a683f c28eefd 10a683f c28eefd |
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
const express = require('express');
const bodyParser = require('body-parser');
const DomainNameAPI = require('./dna');
const crypto = require('crypto');
const app = express();
app.use(bodyParser.json());
// Initialize the Domain API client
const api = new DomainNameAPI(process.env.API_USERNAME || 'harmon', process.env.API_PASSWORD || 'PrivatePass123#');
// Authorization middleware
const authorizeRequest = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
success: false,
error: 'Authorization header missing or invalid format'
});
}
const token = authHeader.split(' ')[1];
const today = new Date().toLocaleDateString('en-US', { weekday: 'long' }).toLowerCase();
const expectedToken = crypto.createHash('sha256').update(today + 'harmon').digest('hex');
if (token !== expectedToken) {
return res.status(401).json({
success: false,
error: 'Invalid token'
});
}
next();
};
// Apply authorization middleware to all /api routes
app.use('/api', authorizeRequest);
// Root endpoint
app.get('/', (req, res) => {
const today = new Date().toLocaleDateString('en-US', { weekday: 'long' }).toLowerCase();
const token = crypto.createHash('sha256').update(today + 'harmon').digest('hex');
res.json({
success: true,
message: 'Domain Name API Server',
version: '1.0.0',
token,
endpoints: {
root: '/',
domains: '/api/domains',
balance: '/api/balance',
tlds: '/api/tlds',
reseller: '/api/reseller'
}
});
});
// Middleware to handle errors
const errorHandler = (err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
success: false,
error: err.message
});
};
// Add Child Name Server endpoint
app.post('/api/domains/:domainName/childNameServer', async (req, res, next) => {
try {
const { domainName } = req.params;
const { nameServer, ipAddress } = req.body;
const result = await api.AddChildNameServer(domainName, nameServer, ipAddress);
res.json(result);
} catch (error) {
next(error);
}
});
// Delete Child Name Server endpoint
app.delete('/api/domains/:domainName/childNameServer/:nameServer', async (req, res, next) => {
try {
const { domainName, nameServer } = req.params;
const result = await api.DeleteChildNameServer(domainName, nameServer);
res.json(result);
} catch (error) {
next(error);
}
});
// Modify Child Name Server endpoint
app.put('/api/domains/:domainName/childNameServer', async (req, res, next) => {
try {
const { domainName } = req.params;
const { nameServer, ipAddress } = req.body;
const result = await api.ModifyChildNameServer(domainName, nameServer, ipAddress);
res.json(result);
} catch (error) {
next(error);
}
});
// Get Contacts endpoint
app.get('/api/domains/:domainName/contacts', async (req, res, next) => {
try {
const { domainName } = req.params;
const result = await api.GetContacts(domainName);
res.json(result);
} catch (error) {
next(error);
}
});
// Save Contacts endpoint
app.put('/api/domains/:domainName/contacts', async (req, res, next) => {
try {
const { domainName } = req.params;
const { contacts } = req.body;
const result = await api.SaveContacts(domainName, contacts);
res.json(result);
} catch (error) {
next(error);
}
});
// Transfer domain endpoint
app.post('/api/domains/transfer', async (req, res, next) => {
try {
const { domainName, eppCode, period = 1 } = req.body;
const result = await api.Transfer(domainName, eppCode, period);
res.json(result);
} catch (error) {
next(error);
}
});
// Cancel Transfer endpoint
app.post('/api/domains/:domainName/cancelTransfer', async (req, res, next) => {
try {
const { domainName } = req.params;
const result = await api.CancelTransfer(domainName);
res.json(result);
} catch (error) {
next(error);
}
});
// Approve Transfer endpoint
app.post('/api/domains/:domainName/approveTransfer', async (req, res, next) => {
try {
const { domainName } = req.params;
const result = await api.ApproveTransfer(domainName);
res.json(result);
} catch (error) {
next(error);
}
});
// Reject Transfer endpoint
app.post('/api/domains/:domainName/rejectTransfer', async (req, res, next) => {
try {
const { domainName } = req.params;
const result = await api.RejectTransfer(domainName);
res.json(result);
} catch (error) {
next(error);
}
});
// Renew domain endpoint
app.post('/api/domains/:domainName/renew', async (req, res, next) => {
try {
const { domainName } = req.params;
const { period } = req.body;
const result = await api.Renew(domainName, period);
res.json(result);
} catch (error) {
next(error);
}
});
// Register domain with contact info endpoint
app.post('/api/domains/register', async (req, res, next) => {
try {
const {
domainName,
period = 1,
contacts,
nameServers = ["ns1.harmon.web.tr", "ns2.harmon.web.tr"],
eppLock = true,
privacyLock = false,
additionalAttributes
} = req.body;
const result = await api.RegisterWithContactInfo(
domainName,
period,
contacts,
nameServers,
eppLock,
privacyLock,
additionalAttributes
);
res.json(result);
} catch (error) {
next(error);
}
});
// Modify Privacy Protection Status endpoint
app.put('/api/domains/:domainName/privacy', async (req, res, next) => {
try {
const { domainName } = req.params;
const { status, reason = "Owner request" } = req.body;
const result = await api.ModifyPrivacyProtectionStatus(domainName, status, reason);
res.json(result);
} catch (error) {
next(error);
}
});
// Sync From Registry endpoint
app.post('/api/domains/:domainName/sync', async (req, res, next) => {
try {
const { domainName } = req.params;
const result = await api.SyncFromRegistry(domainName);
res.json(result);
} catch (error) {
next(error);
}
});
// Get Current Balance endpoint
app.get('/api/balance', async (req, res, next) => {
try {
const { currencyId = 2 } = req.query;
const result = await api.GetCurrentBalance(currencyId);
res.json(result);
} catch (error) {
next(error);
}
});
// Check Domain Availability endpoint
app.post('/api/domains/check', async (req, res, next) => {
try {
const { domains, extensions, period = 1, command = 'create' } = req.body;
const result = await api.CheckAvailability(domains, extensions, period, command);
res.json(result);
} catch (error) {
next(error);
}
});
// Get Domain List endpoint
app.get('/api/domains', async (req, res, next) => {
try {
const extraParameters = req.query;
const result = await api.GetList(extraParameters);
res.json(result);
} catch (error) {
next(error);
}
});
// Get TLD List endpoint
app.get('/api/tlds', async (req, res, next) => {
try {
const { count = 20 } = req.query;
const result = await api.GetTldList(count);
res.json(result);
} catch (error) {
next(error);
}
});
// Get Domain Details endpoint
app.get('/api/domains/:domainName', async (req, res, next) => {
try {
const { domainName } = req.params;
const result = await api.GetDetails(domainName);
res.json(result);
} catch (error) {
next(error);
}
});
// Get Reseller Details endpoint
app.get('/api/reseller', async (req, res, next) => {
try {
const result = await api.GetResellerDetails();
res.json(result);
} catch (error) {
next(error);
}
});
// 404 handler - must be placed after all other routes
app.use((req, res) => {
res.status(404).json({
success: false,
error: 'Endpoint not found'
});
});
app.use(errorHandler);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
// Log today's token for testing
const today = new Date().toLocaleDateString('en-US', { weekday: 'long' }).toLowerCase();
const token = crypto.createHash('sha256').update(today + 'harmon').digest('hex');
console.log(`Today's auth token: ${token}`);
}); |