Electron cannot execute c++ library (libpcap) through node_api

189 views Asked by At

I am trying to use pcap(libpcap library in Linux) in Electron through NodeJS's node_api. The key code block in C++ is followed.

static napi_value RunCallback(napi_env env, const napi_callback_info info)
{
    char errBuf[PCAP_ERRBUF_SIZE];
    //network device name
    char *devName = "wlo1";

    pcap_t *device = pcap_open_live(devName, 65535, 1, 0, errBuf);
    .......
}

The binding.gyp:

{ 
 "targets":[
    {
      "target_name":"net",
      "sources": [ "./code/net.cpp" ],
       "libraries": [
            "-lpcap"
        ],
    }
  ]
}

The log is here:

> [email protected] start
> electron .

Error: wlo1: You don't have permission to capture on that device (socket: 不允许的操作)

I tried using sudo -E npm start, but it didn't work for me. However I can simply run code like pcap_open_live() in a single file compiled by g++, like sudo ./test. So I wonder if there is any setting I should do in Electron or Node.JS, like in binding.gyp or js files?

1

There are 1 answers

0
Itamar Yi. On

This might help:

sudo setcap cap_net_raw,cap_net_admin=eip /path/to/your-app

This command grants the CAP_NET_RAW and CAP_NET_ADMIN capabilities to your Electron application, allowing it to capture network packets.