Skip to content

Commit

Permalink
Add lancache to router
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoriguchi committed Dec 19, 2024
1 parent 41aa201 commit 8be34e2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions configuration/devices/headless/router/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in {
./adguardhome.nix
./broken-emmc.nix
./firewall.nix
./lancache.nix
./librenms.nix
./routing.nix
./web-proxy.nix
Expand Down
66 changes: 66 additions & 0 deletions configuration/devices/headless/router/lancache.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{ config, pkgs, lib, ... }:
let
routerIp = "192.168.1.1";

cachedServices = [ "blizzard" "epicgames" "riot" "steam" ];
lancachePort = 15411;

src = pkgs.fetchFromGitHub {
owner = "uklans";
repo = "cache-domains";
rev = "67594ce10c85250bf5e3cc3c6f4a0abb90abd25c";
hash = "sha256-MRyIz1l23B/ibVc1sPi6BwtB0imi47z1gqQxm5PM60Q=";
};

metadata = (builtins.fromJSON (builtins.readFile "${src}/cache_domains.json")).cache_domains;
filteredMetadata = lib.filter (cacheDomain: builtins.elem cacheDomain.name cachedServices) metadata;
domainFiles = lib.flatten (map (cacheDomain: cacheDomain.domain_files) filteredMetadata);
rawCacheDomains = lib.flatten (map (domainFile: (lib.splitString "\n" (builtins.readFile "${src}/${domainFile}"))) domainFiles);
cachedDomains = lib.filter (domain: domain != "") rawCacheDomains;
in {
# https://hub.docker.com/r/lancachenet/monolithic
virtualisation.oci-containers.containers.lancache = {
image = "lancachenet/monolithic:latest";

environment = rec {
UPSTREAM_DNS = lib.concatStringsSep " "
(map (dns: lib.replaceStrings [ "tls://" ] [ "" ] dns) config.services.adguardhome.settings.dns.bootstrap_dns);
USE_GENERIC_CACHE = "true";

MIN_FREE_DISK = "10g";
CACHE_DISK_SIZE = "2000g";
CACHE_INDEX_SIZE = "${toString (lib.toInt (builtins.head (lib.splitString "g" CACHE_DISK_SIZE)) / 1000 * 250)}m";
CACHE_MAX_AGE = "${toString (365 * 10)}d";

TZ = config.time.timeZone;
};

ports = [ "127.0.0.1:${toString lancachePort}:80" ];

volumes = [ "/var/cache/lancache:/data/cache" "/var/log/lancach:/data/logs" ];
};

system.activationScripts.lancache = ''
${pkgs.coreutils}/bin/mkdir -p /var/cache/lancache
${pkgs.coreutils}/bin/mkdir -p /var/log/lancach
'';

services = {
nginx.virtualHosts = lib.listToAttrs (map (cachedDomain:
lib.nameValuePair cachedDomain {
listen = map (addr: {
inherit addr;
port = config.services.nginx.defaultHTTPListenPort;
}) config.services.nginx.defaultListenAddresses;

locations."/".proxyPass = "http://127.0.0.1:${toString lancachePort}";
}) cachedDomains);

adguardhome.settings.filtering.rewrites = map (cachedDomain: {
domain = lib.replaceStrings [ "*." ] [ "" ] cachedDomain;
# TODO uncomment when https://github.com/AdguardTeam/AdGuardHome/issues/7327 fixed
# answer = "${config.networking.hostName}.local";
answer = routerIp;
}) cachedDomains;
};
}

0 comments on commit 8be34e2

Please sign in to comment.