블로그 사이드바에 네이버 블로그 이름을 적어 두었는데 이름이 바뀌어서 손으로 고쳤다. 다음에 또 바뀌면 스킨이 알아서 이름을 읽어 오게 할 수 있을지 시험해 봤다.
브라우저에서 불러 보기
티스토리 화면의 콘솔에서 네이버 쪽 주소 세 개를 불러 봤다.
for (const u of [
'https://rss.blog.naver.com/ilutoo.xml',
'https://rss.blog.naver.com/ilutoo',
'https://blog.naver.com/ilutoo',
]) {
try {
const r = await fetch(u, { credentials: 'omit', cache: 'no-store' });
console.log(u, r.status);
} catch (e) {
console.log(u, e.message);
}
}
셋 다 응답 코드를 찍기 전에 Failed to fetch로 끝났다.
서버 쪽에서 보면
다른 사이트에서 보낸 요청처럼 Origin 헤더를 붙여 curl로 불렀다.
curl -s -D - -o /dev/null -H "Origin: https://life-in-beta.tistory.com" https://rss.blog.naver.com/ilutoo.xml
# HTTP/1.1 200 OK (Access-Control-Allow-Origin 없음)
curl -s -o /dev/null -w "%{http_code}\n" -H "Origin: https://life-in-beta.tistory.com" https://m.blog.naver.com/ilutoo
# 403
curl -s -o /dev/null -w "%{http_code}\n" https://m.blog.naver.com/ilutoo
# 200
RSS는 200을 주지만 응답에 Access-Control-Allow-Origin이 없다. 브라우저는 다른 출처로 보내는 요청에 Origin을 붙이고 응답에 그 출처를 허락하는 헤더가 없으면 결과를 스크립트에 넘기지 않는다. 블로그 화면 주소는 한 단계 더 나가서 다른 사이트의 Origin이 붙으면 403을 돌려줬고 Origin을 빼면 200이었다. curl이나 서버 코드는 이런 검사를 하지 않으니 같은 주소를 문제없이 읽는다.
중계 서버로 시험
그래서 이름을 읽는 일은 서버가 하고 브라우저는 그 서버에 묻는 구조가 필요하다. Node.js 22로 작은 중계 서버를 만들어 봤다. 모바일 블로그 화면의 og:title에서 이름을 꺼내고 허락한 두 블로그에서 온 요청에만 Access-Control-Allow-Origin을 붙인다. 한 번 읽은 값은 한 시간 동안 다시 쓴다.
import http from 'node:http';
const ALLOWED = new Set(['https://life-in-beta.tistory.com', 'https://ilutoo.tistory.com']);
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36';
let cache = { name: null, at: 0 };
async function readBlogName(id) {
if (cache.name && Date.now() - cache.at < 60 * 60 * 1000) return cache.name;
const res = await fetch(`https://m.blog.naver.com/${encodeURIComponent(id)}`, { headers: { 'User-Agent': UA } });
const html = await res.text();
const m = html.match(/<meta property="og:title" content="([^"]*)"/);
if (!m) throw new Error('og:title 없음');
const name = m[1].replace(/&/g, '&').replace(/\s*:\s*네이버 블로그$/, '');
cache = { name, at: Date.now() };
return name;
}
http.createServer(async (req, res) => {
const origin = req.headers.origin;
if (origin && ALLOWED.has(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Vary', 'Origin');
}
try {
const name = await readBlogName('ilutoo');
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ name }));
} catch (e) {
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ error: e.message }));
}
}).listen(8787, '127.0.0.1');
로컬에서 띄우고 curl로 확인했다.
curl -s -D - -H "Origin: https://life-in-beta.tistory.com" http://127.0.0.1:8787/
# HTTP/1.1 200 OK
# Access-Control-Allow-Origin: https://life-in-beta.tistory.com
# Vary: Origin
# {"name":"두잇 & 스테이"}
curl -s -D - -H "Origin: https://example.com" http://127.0.0.1:8787/
# HTTP/1.1 200 OK (Access-Control-Allow-Origin 없음, 이 사이트의 스크립트는 읽지 못한다)
스킨에는 아직 붙이지 않았다
스킨에 붙이려면 이 서버를 늘 켜 둘 곳이 있어야 하고 네이버 화면의 og:title 형식이 바뀌면 중계 서버도 같이 고쳐야 한다. 지금은 사이드바 이름을 손으로 고쳐 두었다.
댓글
GitHub 계정으로 로그인하면 댓글과 공감을 남길 수 있습니다.