0

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:

> my-electron-app@1.0.0 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?

PPLong
  • 1
  • Did that error message originate from the native module that you compiled or it came from somewhere else? – Asesh May 29 '22 at 05:34
  • Run **node-gyp configure build** is ok. And I run **npm start** , no problem. But when my electron app starts through npm start and running the code I mentioned above , it shows the message above. – PPLong May 29 '22 at 07:51

1 Answers1

0

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.