From ec6d1a95dc75817de366531b4c4575da23fb23e2 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 7 Aug 2019 22:14:43 +0200 Subject: [PATCH] Set up mobile-proxy --- configuration/data/mobile-proxy/config.jsx | 61 +++++++++++++++++++ configuration/default.nix | 56 ++++++++++++++++- configuration/lib/fetch/from-cryto-git.nix | 10 +++ configuration/lib/node-application.nix | 10 +++ .../packages/mobile-proxy/default.nix | 19 ++++++ configuration/presets/nginx/reverse-proxy.nix | 9 +++ 6 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 configuration/data/mobile-proxy/config.jsx create mode 100644 configuration/lib/fetch/from-cryto-git.nix create mode 100644 configuration/lib/node-application.nix create mode 100644 configuration/packages/mobile-proxy/default.nix create mode 100644 configuration/presets/nginx/reverse-proxy.nix diff --git a/configuration/data/mobile-proxy/config.jsx b/configuration/data/mobile-proxy/config.jsx new file mode 100644 index 0000000..1b3d344 --- /dev/null +++ b/configuration/data/mobile-proxy/config.jsx @@ -0,0 +1,61 @@ +"use strict"; + +const React = require("react"); +const url = require("url"); + +function NoticeBox({ children, siteName }) { + return ( +
+ This is a mobile proxy. It is intended to visit {siteName} on devices that would otherwise not correctly display the site. {children} +
+ ); +} + +module.exports = { + port: 3000, + hosts: { + "awesomedude.cryto.net": { + prefix: () => ( + + ), + filters: [{ + matchPath: "/", + mapUrl: "http://www.awesomedude.com/", + mapContent: ($) => $("#container").html() + }, { + mapUrl: ({path}) => url.resolve("http://www.awesomedude.com/", path), + mapContent: ($) => $("body").html() + }] + }, + "castleroland.cryto.net": { + prefix: () => ( + + Please direct all your feedback to CastleRoland.net directly! + + ), + filters: [{ + matchPath: "/", + mapUrl: "https://castleroland.net/stories-categories/", + mapContent: ($) => $("section.home_about").html() + }, { + mapUrl: ({path}) => url.resolve("https://www.castleroland.net/", path), + mapContent: ($) => $("section.home_about").html() + }] + }, + "iomfats.cryto.net": { + prefix: () => ( + + Please direct all your feedback to the friendly guy over at IOMfAtS! + + ), + filters: [{ + matchPath: "/", + mapUrl: "http://iomfats.org/storyshelf/", + mapContent: ($) => $("div#homelinks").html() + }, { + mapUrl: ({path}) => url.resolve("http://iomfats.org/", path), + mapContent: ($) => $("div#content").html() + }] + } + } +}; diff --git a/configuration/default.nix b/configuration/default.nix index 17dea7a..37321a5 100644 --- a/configuration/default.nix +++ b/configuration/default.nix @@ -1,5 +1,18 @@ let - nixpkgsOptions = {}; + nixpkgsOptions = { + overlays = [ + (self: super: { + /* NOTE: Namespaced under `pkgs.cryto.*` to prevent naming conflicts with upstream nixpkgs */ + cryto = { + # FIXME: Remove default.nix suffix? + fetchFromCrytoGit = self.callPackage ./lib/fetch/from-cryto-git.nix {}; + nodeApplication = self.callPackage ./lib/node-application.nix {}; + unpack = self.callPackage ./lib/unpack.nix {}; + mobileProxy = self.callPackage ./packages/mobile-proxy/default.nix { configFile = null; }; + }; + }) + ]; + }; pkgs = (import (fetchTarball "https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.03.tar.gz") nixpkgsOptions); pkgs1803 = (import (fetchTarball "https://github.com/NixOS/nixpkgs-channels/archive/nixos-18.03.tar.gz") nixpkgsOptions); presets = { @@ -9,6 +22,7 @@ let nginxPresets = { php = (import ./presets/nginx/php.nix); cphpApplication = (import ./presets/nginx/cphp-application.nix); + reverseProxy = (import ./presets/nginx/reverse-proxy.nix); }; in { network = { @@ -30,6 +44,9 @@ in { { scheme = "http"; port = 80; path = "/"; host = "books.cryto.net"; description = "books.cryto.net is up"; } { scheme = "http"; port = 80; path = "/"; host = "learn.cryto.net"; description = "learn.cryto.net is up"; } { scheme = "http"; port = 80; path = "/"; host = "vps-list.cryto.net"; description = "vps-list.cryto.net is up"; } + { scheme = "http"; port = 80; path = "/"; host = "iomfats.cryto.net"; description = "iomfats.cryto.net is up"; } + { scheme = "http"; port = 80; path = "/"; host = "castleroland.cryto.net"; description = "castleroland.cryto.net is up"; } + { scheme = "http"; port = 80; path = "/"; host = "awesomedude.cryto.net"; description = "awesomedude.cryto.net is up"; } ]; networking.firewall.allowedTCPPorts = [ 80 ]; @@ -37,6 +54,12 @@ in { services.nginx = { enable = true; virtualHosts = { + "404.cryto.net" = { + default = true; + extraConfig = '' + return 404; + ''; + }; "haless.cryto.net" = { locations."/shadow" = { alias = ./sources/shadow-generator; @@ -101,6 +124,9 @@ in { ''; })) ]; + "iomfats.cryto.net" = nginxPresets.reverseProxy "http://127.0.0.1:3000/"; + "castleroland.cryto.net" = nginxPresets.reverseProxy "http://127.0.0.1:3000/"; + "awesomedude.cryto.net" = nginxPresets.reverseProxy "http://127.0.0.1:3000/"; }; }; @@ -135,5 +161,33 @@ in { }; }; }; + + users.extraUsers.mobile-proxy = { + description = "mobile-proxy Service User"; + }; + + systemd.services.mobile-proxy = let + package = pkgs.cryto.mobileProxy.override { configFile = ./data/mobile-proxy/config.jsx; }; + in { + description = "Mobile Proxy"; + wantedBy = ["multi-user.target"]; + after = ["network.target"]; + + serviceConfig = { + ExecStart = "${package}/bin/mobile-proxy"; + User = "mobile-proxy"; + Restart = "on-failure"; + PermissionsStartOnly = true; + }; + + preStart = '' + mkdir -m 0700 -p /tmp/mobile-proxy-home + chown mobile-proxy /tmp/mobile-proxy-home + ''; + + environment = { + HOME = "/tmp/mobile-proxy-home"; + }; + }; }; } diff --git a/configuration/lib/fetch/from-cryto-git.nix b/configuration/lib/fetch/from-cryto-git.nix new file mode 100644 index 0000000..b6c87ed --- /dev/null +++ b/configuration/lib/fetch/from-cryto-git.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: + {owner, repo, rev, name ? ("${repo}-${rev}-src.tar.gz"), ...}@sourceArgs: + let + baseUrl = "https://git.cryto.net/${owner}/${repo}"; + in + pkgs.fetchurl ({ + inherit name; + url = "${baseUrl}/archive/${rev}.tar.gz"; + meta.homepage = baseUrl; + } // removeAttrs sourceArgs ["owner" "repo" "rev"]) diff --git a/configuration/lib/node-application.nix b/configuration/lib/node-application.nix new file mode 100644 index 0000000..dc4e206 --- /dev/null +++ b/configuration/lib/node-application.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: + { name, source }: + let + yarn2nix = (import (fetchTarball "https://github.com/joepie91/yarn2nix/archive/patch/remove-no-patch.tar.gz") { inherit pkgs; }); + in yarn2nix.mkYarnPackage { + name = name; + src = source; + packageJSON = "${source}/package.json"; + yarnLock = "${source}/yarn.lock"; + } diff --git a/configuration/packages/mobile-proxy/default.nix b/configuration/packages/mobile-proxy/default.nix new file mode 100644 index 0000000..1024793 --- /dev/null +++ b/configuration/packages/mobile-proxy/default.nix @@ -0,0 +1,19 @@ +{ pkgs, configFile, ... }: + pkgs.cryto.nodeApplication { + name = "mobile-proxy"; + source = pkgs.stdenv.mkDerivation { + name = "mobile-proxy-application"; + src = pkgs.cryto.fetchFromCrytoGit { + owner = "joepie91"; + repo = "mobile-proxy"; + rev = "1628f4be61621c1783e93ef6719b1dae4f352be8"; + sha256 = "1d9zc3phflsi2gsi7hmzybr0q983x7155bildvlbc7za3y8hry78"; + }; + + buildCommand = '' + mkdir -p $out + tar -xzvf $src -C $out + cp ${configFile} $out/config.jsx + ''; + }; + } diff --git a/configuration/presets/nginx/reverse-proxy.nix b/configuration/presets/nginx/reverse-proxy.nix new file mode 100644 index 0000000..5bcb1ac --- /dev/null +++ b/configuration/presets/nginx/reverse-proxy.nix @@ -0,0 +1,9 @@ +destination: { + locations."/" = { + proxyPass = destination; + proxyWebsockets = true; + extraConfig = '' + proxy_set_header Host $host; + ''; + }; +}