r/Devvit • • 1d ago

Help Using the Devvit CLI when Reddit is blocked in your country (DNS-over-HTTPS workaround)

Reddit is blocked at the DNS level in some countries (Tajikistan in my case): the carrier answers Reddit lookups with 127.0.0.1. The site may still open through a VPN in the browser, but devvit login, devvit playtest and devvit upload fail with ENOTFOUND or ECONNREFUSED, because Node does its own DNS lookups.

What worked for me without changing any system settings: a small Node preload that resolves only Reddit hostnames over DNS-over-HTTPS (Cloudflare, by IP, so it doesn't need DNS itself) and leaves everything else alone.

  1. Save this as tools/reddit-dns.cjs in your project:
const dns = require('node:dns');
const https = require('node:https');

const REDDIT_HOST = /(^|\.)(reddit\.com|redd\.it|redditmedia\.com|redditstatic\.com)$/i;
const cache = new Map();
const originalLookup = dns.lookup;

function resolveOverHttps(hostname) {
  if (cache.has(hostname)) return cache.get(hostname);
  const pending = new Promise((resolve, reject) => {
    const request = https.get(
      {
        host: '1.1.1.1',
        servername: 'cloudflare-dns.com',
        path: `/dns-query?name=${encodeURIComponent(hostname)}&type=A`,
        headers: { accept: 'application/dns-json', host: 'cloudflare-dns.com' },
        timeout: 8000,
      },
      (response) => {
        let body = '';
        response.on('data', (chunk) => (body += chunk));
        response.on('end', () => {
          try {
            const answers = (JSON.parse(body).Answer || []).filter((a) => a.type === 1);
            if (answers.length === 0) throw new Error(`No A record for ${hostname}`);
            resolve(answers.map((a) => a.data));
          } catch (error) {
            reject(error);
          }
        });
      }
    );
    request.on('timeout', () => request.destroy(new Error(`DoH timed out for ${hostname}`)));
    request.on('error', reject);
  });
  cache.set(hostname, pending);
  pending.catch(() => cache.delete(hostname));
  return pending;
}

dns.lookup = function lookup(hostname, options, callback) {
  const done = typeof options === 'function' ? options : callback;
  const opts = typeof options === 'object' && options !== null ? options : { family: options };
  if (typeof hostname !== 'string' || !REDDIT_HOST.test(hostname)) {
    return originalLookup.call(dns, hostname, options, callback);
  }
  resolveOverHttps(hostname).then(
    (addresses) => {
      if (opts.all) done(null, addresses.map((address) => ({ address, family: 4 })));
      else done(null, addresses[0], 4);
    },
    (error) => done(error)
  );
};
  1. Load it in your npm scripts:
"login": "NODE_OPTIONS=\"--require ./tools/reddit-dns.cjs\" devvit login",
"dev": "NODE_OPTIONS=\"--require ./tools/reddit-dns.cjs\" devvit playtest",
"deploy": "NODE_OPTIONS=\"--require ./tools/reddit-dns.cjs\" devvit upload"
  1. Run npm run login, then npm run dev as usual.

Notes:

  • If the block is by IP rather than DNS, this won't help; you'll need a VPN that also covers the terminal (WARP worked for me).
  • On a flaky connection, a playtest upload can die halfway with "write EPIPE". Restarting npm run dev re-uploads. If you then see "AppVersion already exists", restarting once more cleared it for me.

Happy to help if you're stuck on the same thing.

3 Upvotes

1 comment sorted by

1

u/Icy-Examination-2255 1d ago

Really neat solution.