Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 2517072

Browse files
committed
surveyor: add basic implementation
1 parent 223568c commit 2517072

File tree

8 files changed

+404
-0
lines changed

8 files changed

+404
-0
lines changed

infra/surveyor/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Nicolas Mattia
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

infra/surveyor/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
surveyor.so: surveyor.c
2+
gcc -Wall -nostartfiles -fpic -shared surveyor.c -o surveyor.so -ldl -D_GNU_SOURCE

infra/surveyor/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Surveyor - Re-map ports and IP addresses
2+

infra/surveyor/default.nix

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
let pkgs = import ./nix {}; in
2+
3+
with rec
4+
{
5+
runTest = test: pkgs.runCommand "test"
6+
{ buildInputs = [ pkgs.netcat pkgs.curl pkgs.haskellPackages.wai-app-static ] ;}
7+
test;
8+
9+
runPort = port:
10+
''
11+
trap 'echo $SRV_TEST_PIDS | xargs kill -9' EXIT
12+
SRV_TEST_PIDS="''${SRV_TEST_PIDS:-""}"
13+
warp -d ${apiPort port} -p ${builtins.toString port} &
14+
SRV_TEST_PIDS="$SRV_TEST_PIDS $!"
15+
while ! nc -z 127.0.0.1 ${builtins.toString port}; do
16+
echo waiting for port ${builtins.toString port}
17+
sleep 1
18+
done
19+
'';
20+
21+
apiPort = port: pkgs.writeTextFile
22+
{ text = builtins.toString port ;
23+
name = "someAPI";
24+
};
25+
};
26+
27+
let tests =
28+
{
29+
# Accessing localhost on port 80 reroutes to port 1234
30+
test_portMap =
31+
''
32+
${runPort 1234}
33+
34+
SRV_MAP="*:80:*:1234" \
35+
LD_PRELOAD="${pkgs.surveyor}/lib/surveyor.so" \
36+
curl localhost | grep -q '1234'
37+
38+
touch $out
39+
'';
40+
41+
# Accessing 127.0.0.2 reroutes to 127.0.0.1
42+
test_addrMap =
43+
''
44+
${runPort 1234}
45+
46+
SRV_MAP="127.0.0.2:*:127.0.0.1:*" \
47+
LD_PRELOAD="${pkgs.surveyor}/lib/surveyor.so" \
48+
curl 127.0.0.2:1234 | grep -q '1234'
49+
50+
touch $out
51+
'';
52+
53+
# Accessing anything on port 1234 reroutes to 1235 and
54+
# accessing anything on port 1235 reroutes to 1234
55+
test_swapPorts =
56+
''
57+
${runPort 1234}
58+
${runPort 1235}
59+
60+
export SRV_MAP="*:1234:*:1235 *:1235:*:1234"
61+
62+
LD_PRELOAD="${pkgs.surveyor}/lib/surveyor.so" \
63+
curl localhost:1234 | grep -q '1235'
64+
LD_PRELOAD="${pkgs.surveyor}/lib/surveyor.so" \
65+
curl localhost:1235 | grep -q '1234'
66+
67+
touch $out
68+
'';
69+
}; in
70+
71+
{ inherit (pkgs) surveyor;
72+
inherit test_portMap;
73+
} // (builtins.mapAttrs (k: v: runTest v) tests)

infra/surveyor/nix/default.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ sources ? import ./sources.nix }:
2+
with
3+
{ overlay = _: pkgs:
4+
{ inherit (import sources.niv {}) niv;
5+
surveyor = pkgs.stdenv.mkDerivation
6+
{ name = "surveyor";
7+
src = pkgs.lib.sourceByRegex ../.
8+
[ "^Makefile$"
9+
"^surveyor.c"
10+
];
11+
installPhase = "mkdir -p $out/lib && cp surveyor.so $out/lib";
12+
} ;
13+
};
14+
};
15+
import sources.nixpkgs
16+
{ overlays = [ overlay ] ; config = {}; }

infra/surveyor/nix/sources.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"nixpkgs": {
3+
"url": "https://github.com/NixOS/nixpkgs-channels/archive/15f08994517a695efa5e9b216c5caaf497a10019.tar.gz",
4+
"owner": "NixOS",
5+
"branch": "nixos-18.09",
6+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
7+
"repo": "nixpkgs-channels",
8+
"type": "tarball",
9+
"sha256": "04y5car353rwnx5hysdc6i5j04hp7bbxnc228fa6r332iiwrj2zg",
10+
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
11+
"rev": "15f08994517a695efa5e9b216c5caaf497a10019"
12+
},
13+
"niv": {
14+
"homepage": "https://github.com/nmattia/niv",
15+
"url": "https://github.com/nmattia/niv/archive/c2698b0780b783880e0b1a520723948fe3b5c26a.tar.gz",
16+
"owner": "nmattia",
17+
"branch": "master",
18+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
19+
"repo": "niv",
20+
"type": "tarball",
21+
"sha256": "0v68x0h9si6kjqg5fcjrgsbsf4x18m32a786yvjmrdkrki9qwmcq",
22+
"description": "Easy dependency management for Nix projects",
23+
"rev": "c2698b0780b783880e0b1a520723948fe3b5c26a"
24+
}
25+
}

infra/surveyor/nix/sources.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file has been generated by Niv.
2+
3+
# A record, from name to path, of the third-party packages
4+
with rec
5+
{
6+
pkgs = import <nixpkgs> {};
7+
8+
sources = builtins.fromJSON (builtins.readFile ./sources.json);
9+
10+
mapAttrs = builtins.mapAttrs or
11+
(f: set: with builtins;
12+
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));
13+
14+
getFetcher = spec:
15+
let fetcherName =
16+
if builtins.hasAttr "type" spec
17+
then builtins.getAttr "type" spec
18+
else "tarball";
19+
in builtins.getAttr fetcherName {
20+
"tarball" = pkgs.fetchzip;
21+
"file" = pkgs.fetchurl;
22+
};
23+
};
24+
# NOTE: spec must _not_ have an "outPath" attribute
25+
mapAttrs (_: spec:
26+
if builtins.hasAttr "outPath" spec
27+
then abort
28+
"The values in sources.json should not have an 'outPath' attribute"
29+
else
30+
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
31+
then
32+
spec //
33+
{ outPath = getFetcher spec { inherit (spec) url sha256; } ; }
34+
else spec
35+
) sources

0 commit comments

Comments
 (0)