Spaces:
Running
Running
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}`); | |
}); |