dnscrypt-proxy with masking host

Written by

in

  • 회사 내부에 DNS 서버를 두는 것은 문제가 없다.
  • 로그에 host의 IP가 있는 것은 개인정보 침해 가능성이 있다.
  • dnscrypt-proxy 옵션으로는 이것을 해결할 수 없다.
  • 다른 대안을 찾자.

Gemini 도움으로 다음과 같이 했다. 파이프를 이용하는 것이다. 다음과 같이 서비스를 등록한다. 내용에서 알 수 있는 것으로 dnscrypt-proxy 실행 이전에 로그를 받을 준비를 해야 한다.

/etc/systemd/system/dnscrypt-log-anonymizer.service

[Unit]
Description=dnscrypt-proxy Log Anonymizer (FIFO Pipe Processor)
# dnscrypt-proxy가 실행되기 전에 먼저 준비되어야 함
Before=dnscrypt-proxy.service
Wants=network.target

[Service]
Type=simple
User=root
# 서비스 시작 전 파이프 파일 존재 확인 및 자동 생성
ExecStartPre=/bin/sh -c '[ -p /var/log/dnscrypt-proxy/query_pipe.log ] || mkfifo /var/log/dnscrypt-proxy/query_pipe.log'
# 파이프 데이터를 실시간 마스킹하여 파일로 저장
ExecStart=/bin/sh -c 'exec sed -u -E "s/host:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|\[[0-9a-fA-F:]+\])/host:0.0.0.0/" /var/log/dnscrypt-proxy/query_pipe.log >> /var/log/dnscrypt-proxy/queries_anonymized.log'
Restart=always
RestartSec=2s

[Install]
WantedBy=multi-user.target

dnscrypt-proxy는 다음과 같이 수정해 둔다.

/etc/systemd/system/dnscrypt-proxy.service

[Unit]
Description=DNSCrypt client proxy
Documentation=https://github.com/DNSCrypt/dnscrypt-proxy/wiki
After=network.target dnscrypt-log-anonymizer.service
Wants=network-online.target
Requires=dnscrypt-log-anonymizer.service

[Service]
Type=simple
NonBlocking=true
ExecStart=/usr/local/bin/dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml
Restart=on-failure
RestartSec=5
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target