summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ne/netcap/package.nix
blob: 5cd67f88757d8f5a6b3ae7481d18e234a83e4644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
  lib,
  callPackage,
  buildGoModule,
  fetchFromGitHub,
  withDpi ? true,
  libpcap,
  libprotoident,
  libflowmanager,
  libtrace,
  ndpi,
  pkg-config,
  protobuf,
  protoc-gen-go,
  yara-x,
  versionCheckHook,
  nix-update-script,
}:
let
  ndpi_4_14 = callPackage ./ndpi_4_14.nix { };
in
buildGoModule (finalAttrs: {
  pname = "netcap";
  version = "0.9.0";

  src = fetchFromGitHub {
    owner = "dreadl0ck";
    repo = "netcap";
    tag = "v${finalAttrs.version}";
    hash = "sha256-hk0aPU+pQ+A90GvlFhCRpj4hRiFOcpcP64xznh50Kts=";
  };

  vendorHash = "sha256-DLjSeSXwA4zPVg3ISPqEHTihIp9uVu7dXv9bTSepsaI=";

  nativeBuildInputs = [
    pkg-config
    protobuf
    protoc-gen-go
  ];

  postPatch = ''
    rm go.work go.work.sum
  '';

  preBuild = ''
    # satisfiy go:embed
    mkdir -p cmd/capture/webui/frontend/dist
    touch cmd/capture/webui/frontend/dist/index.html

    # generate protobuf types
    # we need to patch the proto file to have a valid go_package
    substituteInPlace netcap.proto --replace-fail 'option go_package = "types";' 'option go_package = "github.com/dreadl0ck/netcap/types";'
    protoc --go_out=. --go_opt=paths=source_relative netcap.proto
    mv netcap.pb.go types/

    # Patch types to replace calling String() (panics) with string literal
    find types -name "*.go" -exec sed -i 's/Type_NC_\([A-Za-z0-9_]*\).String()/"NC_\1"/g' {} +
  '';

  subPackages = [ "cmd" ];

  buildInputs = [
    libpcap
    yara-x
  ]
  ++ lib.optionals withDpi [
    ndpi_4_14
    libprotoident
    libflowmanager
    libtrace
  ];

  ldflags = [
    "-s"
    "-w"
  ];

  tags = lib.optionals (!withDpi) [
    "nodpi"
  ];

  env = lib.optionalAttrs withDpi {
    CGO_LDFLAGS = toString [
      "-L${ndpi_4_14}/lib"
      "-lndpi"
      "-L${libprotoident}/lib"
      "-lprotoident"
    ];

    CGO_CFLAGS = toString [
      "-I${ndpi_4_14}/include"
      "-I${libprotoident}/include"
    ];
  };

  postInstall = ''
    mv $out/bin/cmd $out/bin/net
  '';

  checkFlags =
    let
      skippedTests = [
        # couldn't open packet socket: operation not permitted
        "TestCaptureLive"
        # requires local test data
        "TestCapturePCAP"
      ];
    in
    [ "-skip=^${builtins.concatStringsSep "$|^" skippedTests}$" ];

  nativeInstallCheckInputs = [ versionCheckHook ];
  versionCheckProgram = "${placeholder "out"}/bin/net";
  doInstallCheck = true;

  passthru.updateScript = nix-update-script { };

  meta = {
    description = "Framework for secure and scalable network traffic analysis";
    homepage = "https://netcap.io";
    downloadPage = "https://github.com/dreadl0ck/netcap";
    changelog = "https://github.com/dreadl0ck/netcap/releases/tag/v${finalAttrs.version}";
    license = lib.licenses.gpl3Only;
    maintainers = with lib.maintainers; [ felbinger ];
    mainProgram = "net";
  };
})