158 lines
4.9 KiB
Perl
158 lines
4.9 KiB
Perl
#!/usr/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
use IO::Socket::INET;
|
|
use IO::Select;
|
|
|
|
my $config = '/private/etc/reims-host-dns/dns-proxy.conf';
|
|
my $host = '10.0.2.2';
|
|
my $max_query = 4096;
|
|
my $max_reply = 65535;
|
|
my ($port, $token);
|
|
my $next_reload = 0;
|
|
$SIG{PIPE} = 'IGNORE';
|
|
$SIG{HUP} = sub { $next_reload = 0 };
|
|
|
|
my $udp = IO::Socket::INET->new(LocalAddr => '127.0.0.1', LocalPort => 53, Proto => 'udp')
|
|
or die "DNS UDP listen: $!";
|
|
my $tcp = IO::Socket::INET->new(LocalAddr => '127.0.0.1', LocalPort => 53, Proto => 'tcp',
|
|
Listen => 32, ReuseAddr => 1) or die "DNS TCP listen: $!";
|
|
my $listeners = IO::Select->new($udp, $tcp);
|
|
my $children = 0;
|
|
|
|
sub reload_config {
|
|
$next_reload = time + 10;
|
|
if (!open(my $fh, '<', $config)) {
|
|
warn "Read DNS proxy config: $!\n";
|
|
return;
|
|
} else {
|
|
my %values;
|
|
while (my $line = <$fh>) {
|
|
if ($line =~ /\A(REIMS_HOST_PORT|REIMS_TOKEN)=([0-9a-f]+)\n?\z/) {
|
|
$values{$1} = $2;
|
|
}
|
|
}
|
|
close $fh;
|
|
if (($values{REIMS_HOST_PORT} // '') !~ /\A[0-9]{1,5}\z/ ||
|
|
$values{REIMS_HOST_PORT} < 1 || $values{REIMS_HOST_PORT} > 65535 ||
|
|
($values{REIMS_TOKEN} // '') !~ /\A[0-9a-f]{64}\z/) {
|
|
warn "Invalid DNS proxy config\n";
|
|
return;
|
|
}
|
|
($port, $token) = @values{qw(REIMS_HOST_PORT REIMS_TOKEN)};
|
|
}
|
|
}
|
|
|
|
sub write_all {
|
|
my ($sock, $data) = @_;
|
|
my $offset = 0;
|
|
while ($offset < length $data) {
|
|
my $n = syswrite($sock, $data, length($data) - $offset, $offset);
|
|
return unless $n;
|
|
$offset += $n;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
sub read_exact {
|
|
my ($sock, $size) = @_;
|
|
my $data = '';
|
|
while (length($data) < $size) {
|
|
my $n = sysread($sock, $data, $size - length($data), length($data));
|
|
return unless $n;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
sub forward_query {
|
|
my ($query) = @_;
|
|
return unless defined $port;
|
|
my $upstream = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port,
|
|
Proto => 'tcp', Timeout => 5) or return;
|
|
my $request = "POST /v1/dns-query HTTP/1.0\r\nHost: $host:$port\r\n" .
|
|
"Authorization: Bearer $token\r\nContent-Type: application/dns-message\r\n" .
|
|
"Content-Length: " . length($query) . "\r\nConnection: close\r\n\r\n" . $query;
|
|
return unless write_all($upstream, $request);
|
|
|
|
# HTTP/1.0 lets the host delimit the body by closing the connection.
|
|
my $response = '';
|
|
while (1) {
|
|
my $n = sysread($upstream, $response, 8192, length($response));
|
|
return unless defined $n;
|
|
last if $n == 0;
|
|
return if length($response) > $max_reply + 8192;
|
|
}
|
|
my ($header, $body) = split /\r\n\r\n/, $response, 2;
|
|
return unless defined $body && $header =~ /\AHTTP\/1\.[01] 200(?: |\r\n)/ &&
|
|
length($body) >= 12 && length($body) <= $max_reply;
|
|
if ($header =~ /\r\nContent-Length:\s*(\d+)\r?$/im) {
|
|
return unless length($body) == $1;
|
|
}
|
|
return $body;
|
|
}
|
|
|
|
sub udp_reply {
|
|
my ($reply) = @_;
|
|
# 512 bytes works even for clients that did not send an EDNS option.
|
|
return $reply if length($reply) <= 512;
|
|
my $end = 12;
|
|
while ($end < length($reply)) {
|
|
my $size = ord(substr($reply, $end++, 1));
|
|
last if $size == 0;
|
|
if ($size >= 192) {
|
|
++$end;
|
|
last;
|
|
}
|
|
$end += $size;
|
|
}
|
|
my ($id, $flags) = unpack('nn', $reply);
|
|
return pack('n6', $id, $flags | 0x0200, 1, 0, 0, 0) . substr($reply, 12, $end + 4 - 12);
|
|
}
|
|
|
|
while (1) {
|
|
while (waitpid(-1, 1) > 0) {
|
|
--$children;
|
|
}
|
|
reload_config() if time >= $next_reload;
|
|
for my $ready ($listeners->can_read(1)) {
|
|
my ($peer, $query, $client);
|
|
if ($ready == $udp) {
|
|
$peer = recv($udp, $query, $max_query + 1, 0);
|
|
next unless defined $peer && length($query) >= 12 && length($query) <= $max_query;
|
|
} else {
|
|
$client = $tcp->accept() or next;
|
|
}
|
|
next if $children >= 32;
|
|
my $pid = fork();
|
|
if (!defined $pid) {
|
|
warn "Fork DNS worker: $!\n";
|
|
next;
|
|
}
|
|
if ($pid) {
|
|
++$children;
|
|
next;
|
|
}
|
|
|
|
close $tcp;
|
|
alarm 7;
|
|
if (defined $peer) {
|
|
my $reply = forward_query($query);
|
|
send($udp, udp_reply($reply), 0, $peer) if defined $reply;
|
|
} else {
|
|
close $udp;
|
|
while (defined(my $prefix = read_exact($client, 2))) {
|
|
my $size = unpack('n', $prefix);
|
|
last if $size < 12 || $size > $max_query;
|
|
my $packet = read_exact($client, $size);
|
|
last unless defined $packet;
|
|
alarm 7;
|
|
reload_config() if time >= $next_reload;
|
|
my $reply = forward_query($packet);
|
|
last unless defined $reply && write_all($client, pack('n', length($reply)) . $reply);
|
|
alarm 7;
|
|
}
|
|
}
|
|
exit 0;
|
|
}
|
|
}
|