|
@ -12,8 +12,12 @@ const GITEA_SECRET = "123"; |
|
|
// Serve static files from the 'public' directory
|
|
|
// Serve static files from the 'public' directory
|
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
|
|
|
|
|
|
// Middleware to parse JSON payloads
|
|
|
// Middleware to capture raw body
|
|
|
app.use(bodyParser.json()); |
|
|
app.use(bodyParser.json({ |
|
|
|
|
|
verify: (req, res, buf, encoding) => { |
|
|
|
|
|
req.rawBody = buf.toString(encoding || 'utf8'); |
|
|
|
|
|
} |
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
// Custom middleware to handle URLs without .html for specific routes
|
|
|
// Custom middleware to handle URLs without .html for specific routes
|
|
|
app.use((req, res, next) => { |
|
|
app.use((req, res, next) => { |
|
@ -53,38 +57,40 @@ app.get('/articles/:articleName', (req, res) => { |
|
|
|
|
|
|
|
|
// Webhook handler
|
|
|
// Webhook handler
|
|
|
app.post('/api', (req, res) => { |
|
|
app.post('/api', (req, res) => { |
|
|
const signature = req.headers['x-gitea-signature']; |
|
|
const signature = req.headers['x-gitea-signature']; |
|
|
const payload = JSON.stringify(req.body); |
|
|
const payload = req.rawBody; |
|
|
|
|
|
|
|
|
if (!signature || !payload) { |
|
|
if (!signature || !payload) { |
|
|
return res.status(400).send('Invalid payload or missing signature'); |
|
|
return res.status(400).send('Invalid payload or missing signature'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Verify the secret
|
|
|
// Verify the secret
|
|
|
const hmac = crypto.createHmac('sha256', GITEA_SECRET); |
|
|
const hmac = crypto.createHmac('sha256', GITEA_SECRET); |
|
|
const digest = hmac.update(payload).digest('hex'); |
|
|
const digest = hmac.update(payload).digest('hex'); |
|
|
|
|
|
|
|
|
console.log('Signature from Gitea:', signature); |
|
|
console.log('Signature from Gitea:', signature); |
|
|
console.log('Computed digest:', digest); |
|
|
console.log('Computed digest:', digest); |
|
|
|
|
|
console.log('Payload:', payload); |
|
|
const bufferSignature = Buffer.from(signature, 'hex'); |
|
|
console.log('Secret:', GITEA_SECRET); |
|
|
const bufferDigest = Buffer.from(digest, 'hex'); |
|
|
|
|
|
|
|
|
const bufferSignature = Buffer.from(signature, 'hex'); |
|
|
if (bufferSignature.length === bufferDigest.length && crypto.timingSafeEqual(bufferSignature, bufferDigest)) { |
|
|
const bufferDigest = Buffer.from(digest, 'hex'); |
|
|
// Secret is valid, update the repository
|
|
|
|
|
|
res.status(200).send('Repository updated successfully'); |
|
|
if (bufferSignature.length === bufferDigest.length && crypto.timingSafeEqual(bufferSignature, bufferDigest)) { |
|
|
// Optionally, execute a shell command to pull the latest changes
|
|
|
// Secret is valid, update the repository
|
|
|
exec('git pull', (error, stdout, stderr) => { |
|
|
res.status(200).send('Repository updated successfully'); |
|
|
if (error) { |
|
|
// Optionally, execute a shell command to pull the latest changes
|
|
|
console.error(`exec error: ${error}`); |
|
|
exec('git pull', (error, stdout, stderr) => { |
|
|
return; |
|
|
if (error) { |
|
|
} |
|
|
console.error(`exec error: ${error}`); |
|
|
console.log(`stdout: ${stdout}`); |
|
|
return; |
|
|
console.error(`stderr: ${stderr}`); |
|
|
} |
|
|
}); |
|
|
console.log(`stdout: ${stdout}`); |
|
|
} else { |
|
|
console.error(`stderr: ${stderr}`); |
|
|
res.status(401).send('Invalid secret'); |
|
|
}); |
|
|
} |
|
|
} else { |
|
|
|
|
|
res.status(401).send('Invalid secret'); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Error handling
|
|
|
// Error handling
|
|
|