Browse Source

Set up mobile-proxy

Sven Slootweg 1 year ago
parent
commit
ec6d1a95dc

+ 61 - 0
configuration/data/mobile-proxy/config.jsx

@ -0,0 +1,61 @@
1
"use strict";
2
3
const React = require("react");
4
const url = require("url");
5
6
function NoticeBox({ children, siteName }) {
7
	return (
8
		<div style={{backgroundColor: "#3C0000", color: "white", padding: "4px", margin: "4px"}}>
9
			<strong>This is a mobile proxy.</strong> It is intended to visit {siteName} on devices that would otherwise not correctly display the site. {children}
10
		</div>
11
	);
12
}
13
14
module.exports = {
15
	port: 3000,
16
	hosts: {
17
		"awesomedude.cryto.net": {
18
			prefix: () => (
19
				<NoticeBox siteName="AwesomeDude" />
20
			),
21
			filters: [{
22
				matchPath: "/",
23
				mapUrl: "http://www.awesomedude.com/",
24
				mapContent: ($) => $("#container").html()
25
			}, {
26
				mapUrl: ({path}) => url.resolve("http://www.awesomedude.com/", path),
27
				mapContent: ($) => $("body").html()
28
			}]
29
		},
30
		"castleroland.cryto.net": {
31
			prefix: () => (
32
				<NoticeBox siteName="CastleRoland.net">
33
					Please direct all your feedback to <a style={{ color: "white" }} href="http://www.castleroland.net/">CastleRoland.net</a> directly!
34
				</NoticeBox>
35
			),
36
			filters: [{
37
				matchPath: "/",
38
				mapUrl: "https://castleroland.net/stories-categories/",
39
				mapContent: ($) => $("section.home_about").html()
40
			}, {
41
				mapUrl: ({path}) => url.resolve("https://www.castleroland.net/", path),
42
				mapContent: ($) => $("section.home_about").html()
43
			}]
44
		},
45
		"iomfats.cryto.net": {
46
			prefix: () => (
47
				<NoticeBox siteName="the IOMfAtS Story Shelf">
48
					Please direct all your feedback to the <a style={{ color: "white" }} href="http://iomfats.org/aboutme/">friendly guy over at IOMfAtS</a>!
49
				</NoticeBox>
50
			),
51
			filters: [{
52
				matchPath: "/",
53
				mapUrl: "http://iomfats.org/storyshelf/",
54
				mapContent: ($) => $("div#homelinks").html()
55
			}, {
56
				mapUrl: ({path}) => url.resolve("http://iomfats.org/", path),
57
				mapContent: ($) => $("div#content").html()
58
			}]
59
		}
60
	}
61
};

+ 55 - 1
configuration/default.nix

@ -1,5 +1,18 @@
1 1
let
2
	nixpkgsOptions = {};
2
	nixpkgsOptions = {
3
		overlays = [
4
			(self: super: {
5
				/* NOTE: Namespaced under `pkgs.cryto.*` to prevent naming conflicts with upstream nixpkgs */
6
				cryto = {
7
					# FIXME: Remove default.nix suffix?
8
					fetchFromCrytoGit = self.callPackage ./lib/fetch/from-cryto-git.nix {};
9
					nodeApplication = self.callPackage ./lib/node-application.nix {};
10
					unpack = self.callPackage ./lib/unpack.nix {};
11
					mobileProxy = self.callPackage ./packages/mobile-proxy/default.nix { configFile = null; };
12
				};
13
			})
14
		];
15
	};
3 16
	pkgs = (import (fetchTarball "https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.03.tar.gz") nixpkgsOptions);
4 17
	pkgs1803 = (import (fetchTarball "https://github.com/NixOS/nixpkgs-channels/archive/nixos-18.03.tar.gz") nixpkgsOptions);
5 18
	presets = {
@ -9,6 +22,7 @@ let
9 22
	nginxPresets = {
10 23
		php = (import ./presets/nginx/php.nix);
11 24
		cphpApplication = (import ./presets/nginx/cphp-application.nix);
25
		reverseProxy = (import ./presets/nginx/reverse-proxy.nix);
12 26
	};
13 27
in {
14 28
	network = {
@ -30,6 +44,9 @@ in {
30 44
			{ scheme = "http"; port = 80; path = "/"; host = "books.cryto.net"; description = "books.cryto.net is up"; }
31 45
			{ scheme = "http"; port = 80; path = "/"; host = "learn.cryto.net"; description = "learn.cryto.net is up"; }
32 46
			{ scheme = "http"; port = 80; path = "/"; host = "vps-list.cryto.net"; description = "vps-list.cryto.net is up"; }
47
			{ scheme = "http"; port = 80; path = "/"; host = "iomfats.cryto.net"; description = "iomfats.cryto.net is up"; }
48
			{ scheme = "http"; port = 80; path = "/"; host = "castleroland.cryto.net"; description = "castleroland.cryto.net is up"; }
49
			{ scheme = "http"; port = 80; path = "/"; host = "awesomedude.cryto.net"; description = "awesomedude.cryto.net is up"; }
33 50
		];
34 51
35 52
		networking.firewall.allowedTCPPorts = [ 80 ];
@ -37,6 +54,12 @@ in {
37 54
		services.nginx = {
38 55
			enable = true;
39 56
			virtualHosts = {
57
				"404.cryto.net" = {
58
					default = true;
59
					extraConfig = ''
60
						return 404;
61
					'';
62
				};
40 63
				"haless.cryto.net" = {
41 64
					locations."/shadow" = {
42 65
						alias = ./sources/shadow-generator;
@ -101,6 +124,9 @@ in {
101 124
						'';
102 125
					}))
103 126
				];
127
				"iomfats.cryto.net" = nginxPresets.reverseProxy "http://127.0.0.1:3000/";
128
				"castleroland.cryto.net" = nginxPresets.reverseProxy "http://127.0.0.1:3000/";
129
				"awesomedude.cryto.net" = nginxPresets.reverseProxy "http://127.0.0.1:3000/";
104 130
			};
105 131
		};
106 132
@ -135,5 +161,33 @@ in {
135 161
				};
136 162
			};
137 163
		};
164
165
		users.extraUsers.mobile-proxy = {
166
			description = "mobile-proxy Service User";
167
		};
168
169
		systemd.services.mobile-proxy = let
170
			package = pkgs.cryto.mobileProxy.override { configFile = ./data/mobile-proxy/config.jsx; };
171
		in {
172
			description = "Mobile Proxy";
173
			wantedBy = ["multi-user.target"];
174
			after = ["network.target"];
175
176
			serviceConfig = {
177
				ExecStart = "${package}/bin/mobile-proxy";
178
				User = "mobile-proxy";
179
				Restart = "on-failure";
180
				PermissionsStartOnly = true;
181
			};
182
183
			preStart = ''
184
				mkdir -m 0700 -p /tmp/mobile-proxy-home
185
				chown mobile-proxy /tmp/mobile-proxy-home
186
			'';
187
188
			environment =  {
189
				HOME = "/tmp/mobile-proxy-home";
190
			};
191
		};
138 192
	};
139 193
}

+ 10 - 0
configuration/lib/fetch/from-cryto-git.nix

@ -0,0 +1,10 @@
1
{ pkgs, ... }:
2
	{owner, repo, rev, name ? ("${repo}-${rev}-src.tar.gz"), ...}@sourceArgs:
3
		let
4
			baseUrl = "https://git.cryto.net/${owner}/${repo}";
5
		in
6
			pkgs.fetchurl ({
7
				inherit name;
8
				url = "${baseUrl}/archive/${rev}.tar.gz";
9
				meta.homepage = baseUrl;
10
			} // removeAttrs sourceArgs ["owner" "repo" "rev"])

+ 10 - 0
configuration/lib/node-application.nix

@ -0,0 +1,10 @@
1
{ pkgs, ... }:
2
	{ name, source }:
3
		let
4
			yarn2nix = (import (fetchTarball "https://github.com/joepie91/yarn2nix/archive/patch/remove-no-patch.tar.gz") { inherit pkgs; });
5
		in yarn2nix.mkYarnPackage {
6
			name = name;
7
			src = source;
8
			packageJSON = "${source}/package.json";
9
			yarnLock = "${source}/yarn.lock";
10
		}

+ 19 - 0
configuration/packages/mobile-proxy/default.nix

@ -0,0 +1,19 @@
1
{ pkgs, configFile, ... }:
2
	pkgs.cryto.nodeApplication {
3
		name = "mobile-proxy";
4
		source = pkgs.stdenv.mkDerivation {
5
			name = "mobile-proxy-application";
6
			src = pkgs.cryto.fetchFromCrytoGit {
7
				owner = "joepie91";
8
				repo = "mobile-proxy";
9
				rev = "1628f4be61621c1783e93ef6719b1dae4f352be8";
10
				sha256 = "1d9zc3phflsi2gsi7hmzybr0q983x7155bildvlbc7za3y8hry78";
11
			};
12
13
			buildCommand = ''
14
				mkdir -p $out
15
				tar -xzvf $src -C $out
16
				cp ${configFile} $out/config.jsx
17
			'';
18
		};
19
	}

+ 9 - 0
configuration/presets/nginx/reverse-proxy.nix

@ -0,0 +1,9 @@
1
destination: {
2
	locations."/" = {
3
		proxyPass = destination;
4
		proxyWebsockets = true;
5
		extraConfig = ''
6
			proxy_set_header Host $host;
7
		'';
8
	};
9
}