mitm-cache: init at 0.1.1

This commit is contained in:
chayleaf 2023-12-04 23:18:06 +07:00
parent be2d3dc2e5
commit 62d13413f4
No known key found for this signature in database
GPG Key ID: 78171AD46227E68E
4 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,44 @@
{ lib
, stdenv
, fetchFromGitHub
, callPackage
, rustPlatform
, substituteAll
, openssl
, Security
, python3Packages
}:
rustPlatform.buildRustPackage rec {
pname = "mitm-cache";
version = "0.1.1";
src = fetchFromGitHub {
owner = "chayleaf";
repo = "mitm-cache";
rev = "v${version}";
hash = "sha256-l9dnyA4Zo4jlbiCMRzUqW3NkiploVpmvxz9i896JkXU=";
};
buildInputs = lib.optionals stdenv.isDarwin [
Security
];
cargoHash = "sha256-6eYOSSlswJGR2IrFo17qVnwI+h2FkyTjLFvwf62nG2c=";
setupHook = substituteAll {
src = ./setup-hook.sh;
inherit openssl;
ephemeral_port_reserve = python3Packages.ephemeral-port-reserve;
};
passthru.fetch = callPackage ./fetch.nix { };
meta = with lib; {
description = "A MITM caching proxy for use in nixpkgs";
homepage = "https://github.com/chayleaf/mitm-cache#readme";
license = licenses.mit;
maintainers = with maintainers; [ chayleaf ];
mainProgram = "mitm-cache";
};
}

View File

@ -0,0 +1,49 @@
{ lib
, fetchurl
, runCommand
, writeText
}:
{ name ? "deps"
, data
, dontFixup ? true
, ...
}
@ attrs:
let
data' = builtins.removeAttrs
(if builtins.isPath data then lib.importJSON data else data)
[ "!version" ];
urlToPath = url:
if lib.hasPrefix "https://" url then (
let
url' = lib.drop 2 (lib.splitString "/" url);
in "https/${builtins.concatStringsSep "/" url'}"
)
else builtins.replaceStrings ["://"] ["/"] url;
code = ''
mkdir -p "$out"
cd "$out"
'' + builtins.concatStringsSep "" (lib.mapAttrsToList (url: info:
let
key = builtins.head (builtins.attrNames info);
val = info.${key};
path = urlToPath url;
name = baseNameOf path;
source = {
redirect = "$out/${urlToPath val}";
hash = fetchurl { inherit url; hash = val; };
text = writeText name val;
}.${key} or (throw "Unknown key: ${url}");
in ''
mkdir -p "${dirOf path}"
ln -s "${source}" "${path}"
'') data');
in
runCommand name (builtins.removeAttrs attrs [ "name" "data" ] // {
passthru = (attrs.passthru or {}) // {
data = writeText "deps.json" (builtins.toJSON data);
};
}) code

View File

@ -0,0 +1,21 @@
mitmCacheConfigureHook() {
if [ -d "$mitmCache" ] && [ -z "$MITM_CACHE_CERT_DIR" ]; then
MITM_CACHE_CERT_DIR="$(mktemp -d)"
pushd "$MITM_CACHE_CERT_DIR"
MITM_CACHE_CA="$MITM_CACHE_CERT_DIR/ca.cer"
@openssl@/bin/openssl genrsa -out ca.key 2048
@openssl@/bin/openssl req -x509 -new -nodes -key ca.key -sha256 -days 1 -out ca.cer -subj "/C=AL/ST=a/L=a/O=a/OU=a/CN=example.org"
MITM_CACHE_HOST="127.0.0.1"
MITM_CACHE_PORT="${mitmCachePort:-$(@ephemeral_port_reserve@/bin/ephemeral-port-reserve "$MITM_CACHE_HOST")}"
MITM_CACHE_ADDRESS="$MITM_CACHE_HOST:$MITM_CACHE_PORT"
export http_proxy="$MITM_CACHE_ADDRESS"
export https_proxy="$MITM_CACHE_ADDRESS"
export SSL_CERT_FILE="$MITM_CACHE_CA"
export NIX_SSL_CERT_FILE="$MITM_CACHE_CA"
mitm-cache -l"$MITM_CACHE_ADDRESS" replay "$mitmCache" >/dev/null 2>/dev/null &
popd
fi
}
# prepend it so any other configure hooks can use the generated root CA
preConfigureHooks=(mitmCacheConfigureHook "${preConfigureHooks[@]}")

View File

@ -1391,6 +1391,10 @@ with pkgs;
makeHardcodeGsettingsPatch = callPackage ../build-support/make-hardcode-gsettings-patch { };
mitm-cache = callPackage ../build-support/mitm-cache {
inherit (darwin.apple_sdk.frameworks) Security;
};
# intended to be used like nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'
enableDebugging = pkg: pkg.override { stdenv = stdenvAdapters.keepDebugInfo pkg.stdenv; };