Skip to content

Commit

Permalink
add: some console.debug() with reason for each rejection
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemaccana committed Jan 2, 2024
1 parent 809926d commit c26556f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions pages/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,26 @@ export default async function handler(
const firstForwardedForIp = forwardedForValues[0] || null;
const remoteAddress = req.socket.remoteAddress || null;

if (!remoteAddress) {
const ipAddress = firstForwardedForIp || remoteAddress;
const walletAddress = req.body.walletAddress;
const amount = req.body.amount;

if (!ipAddress) {
console.debug(
`Rejected request for ${amount} SOL to wallet '${walletAddress}' due to remote address missing. Likely a disconnect.`
);
return res
.status(BAD_REQUEST)
.json({ error: "Remote address has disconnected" });
}

const ipAddress = firstForwardedForIp || remoteAddress;
const walletAddress = req.body.walletAddress;
const amount = req.body.amount;

try {
validate(walletAddress, amount);
} catch (thrownObject) {
const error = thrownObject as Error;
console.debug(
`Rejected request for ${amount} SOL to wallet '${walletAddress}' at IP address '${ipAddress}' due to validation failure: ${error.message}`
);
return res.status(BAD_REQUEST).json({ error: error.message });
}

Expand All @@ -64,6 +70,9 @@ export default async function handler(
const isCloudflareApproved = await checkCloudflare(cloudflareCallback);

if (!isCloudflareApproved) {
console.debug(
`Rejected request for ${amount} SOL to wallet '${walletAddress}' at IP address '${ipAddress}' for failing CAPTCHA`
);
return res.status(BAD_REQUEST).json({ error: "Invalid CAPTCHA" });
}

Expand All @@ -85,6 +94,9 @@ export default async function handler(
// An error here means the wallet has exceeded the limit
// We'll throw an error unless they're on the allow list
if (!isAllowListed) {
console.debug(
`Rejected ${amount} SOL to wallet '${walletAddress}' at IP address '${ipAddress}' due to ${error.message}`
);
return res.status(TOO_MANY_REQUESTS).json({ error: error.message });
}

Expand Down Expand Up @@ -113,7 +125,7 @@ export default async function handler(
.json({ error: "Faucet is empty, ping @solana_devs on Twitter" });
}

console.log(
console.debug(
`Airdropped ${amount} SOL to wallet '${walletAddress}' at IP address '${ipAddress}'`
);
return res.status(OK).json({ success: true, message: "Airdrop successful" });
Expand Down

0 comments on commit c26556f

Please sign in to comment.