Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firefox addon 带有firefox插件的GetAdapterInfo_Firefox Addon_Firefox Addon Sdk_Jsctypes - Fatal编程技术网

Firefox addon 带有firefox插件的GetAdapterInfo

Firefox addon 带有firefox插件的GetAdapterInfo,firefox-addon,firefox-addon-sdk,jsctypes,Firefox Addon,Firefox Addon Sdk,Jsctypes,我想使用firefox插件获得一些独特的机器信息 使用此代码,我可以获得以下名称: var dns = Cu.components.classes["@mozilla.org/network/dns-service;1"] .getService(Cu.components.interfaces.nsIDNSService); var myName = dns.myHostName; 现在我需要NIC信息(如mac地址、适配器名称)。我试图使用getAd

我想使用firefox插件获得一些独特的机器信息

使用此代码,我可以获得以下名称:

var dns = Cu.components.classes["@mozilla.org/network/dns-service;1"]
                    .getService(Cu.components.interfaces.nsIDNSService);
var myName = dns.myHostName;
现在我需要NIC信息(如mac地址、适配器名称)。我试图使用
getAdapterInfo
函数,但这只适用于Windows,我需要一些跨平台的解决方案

有人有主意吗

这是我的GetAdapterInfo代码:

function getMacAddress(){

    const MAX_ADAPTER_NAME_LENGTH = 256;
    const MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
    const ERROR_BUFFER_OVERFLOW = 111;
    const NO_ERROR = 0;

    var IP_ADDRESS_STRING = ctypes.StructType('IP_ADDRESS_STRING',[
        {'String': ctypes.char}
        ]);


    var IP_ADDR_STRING = ctypes.StructType('IP_ADDR_STRING');
    IP_ADDR_STRING.define([
        {'Next': IP_ADDR_STRING.ptr},
        {'IpAddress': IP_ADDRESS_STRING},
        {'IpMask': IP_ADDRESS_STRING},
        {'Context': ctypes.long}
        ]);


    var IP_ADAPTER_INFO = ctypes.StructType('IP_ADAPTER_INFO');

    const DWORD = ctypes.uint32_t;

    IP_ADAPTER_INFO.define([
        {'Next': IP_ADAPTER_INFO.ptr},
        {'ComboIndex': DWORD},
        {'AdapterName': ctypes.char},
        {'Description': ctypes.char},
        {'AddressLength': ctypes.int32_t},
        {'Address': ctypes.uint8_t.array(10).ptr},
        {'Index': DWORD},
        {'Type': ctypes.uintptr_t},
        {'DhcpEnabled': ctypes.uintptr_t},
        {'CurrentIpAddress': IP_ADDR_STRING.ptr},
        {'IpAddressList': IP_ADDR_STRING},
        {'GatewayList' : IP_ADDR_STRING},
        {'DhcpServer': IP_ADDR_STRING},
        {'HaveWins': ctypes.int32_t},
        {'PrimaryWinsServer': IP_ADDR_STRING},
        {'SecondaryWinsServer':IP_ADDR_STRING},
        {'LeaseObtained': ctypes.uintptr_t},
        {'LeaseExpires': ctypes.uintptr_t}
        ]);

    const nomes = [ctypes.libraryName('IPHLPAPI')];
    const lib = loadLibrary(nomes);

    try{
        if(!lib)
            return null

        console.log("Biblioteca aberta!");

        /*DWORD GetAdaptersInfo(
          _Out_    PIP_ADAPTER_INFO pAdapterInfo,
          _Inout_  PULONG pOutBufLen
        );*/
        var getAdaptersInfo = lib.declare(
            "GetAdaptersInfo", 
            ctypes.default_abi,
            DWORD,
            IP_ADAPTER_INFO.ptr,
            ctypes.long.ptr);


        let buf = (IP_ADAPTER_INFO)();
        let pbuf = buf.address();

        let ret = ctypes.long(2000);
        let pret = ret.address();

        let sts = getAdaptersInfo(pbuf,pret);

        if(sts == ERROR_BUFFER_OVERFLOW){ //o tamanho do buffer enviado não foi o suficiente.
            buf = null;
            //agora mando novamente usando o que a lib me retornou.
            sts = getAdaptersInfo(pbuf,pret);
        }

        if(sts != NO_ERROR){ //aqui ocorreu um erro que não conseguiremos resolver ainda.
            throw Error("Erro buscando configuracoes do adaptador.");
        }

    }catch(ex){
        console.log("Erro na biblioteca", ex, ex.message);
    }finally{
        lib.close();
    }

}
但是当我运行cfx run时,firefox崩溃。

忽略此解决方案这是非常错误的-请参阅我的第二个解决方案,它正在进行中。 您的代码正常工作,只需访问
pbuf
.contents
,将下面的代码复制粘贴到scratchpad,将环境设置为browser并点击run。请参见此处,了解如何从草稿行启用浏览器环境:

好了,现在可以了:)问题是我告诉函数接受结构列表,将其定义为
IP\u ADAPTER\u INFO.ptr.array()
然后我创建了一个结构数组,就像这样
让pbuf=IP\u ADAPTER\u INFO.ptr.array(1)(
但是这会崩溃,当我们增加到2个或更多的尺寸时,崩溃速度更快。所以我修改了,在函数
IP\u ADAPTER\u INFO.ptr
中这样声明它,然后我创建了一个类似这样的结构数组
let pbuv=IP\u ADAPTER\u INFO.array(?)
其中
可以是任何数字,并且工作正常:)


当我有机会的时候,我会进入,现在正忙于我自己的一个项目。不过,看看这个主题:谢谢。这个主题的解决方案很酷,但我需要一些更独家的信息,比如mac地址。我也会尝试不同的方法,比如生成一个唯一的id并保存到某个地方(可能是简单的存储)嗨,谢谢!你测试过wich OS吗?运行此代码时,使用Windows 8 firefox会崩溃。你知道为什么吗?我用的是Windows8.1。我将在Win7和WinXP上进行测试,并向您报告,您是否复制粘贴了我放在这里的内容?哦,哎哟,我也在Win81上崩溃了。让我修改代码。好的,在@Sabbathman复制粘贴,现在它可以工作了。在我没有回顾WinAPI之前,你的一些类型是错误的。在Win81和WinXP上测试。我的Win7机器出现网络问题,因此此代码不断尝试访问一些不可用的信息,因此可能会崩溃,但这是意料之中的。因此,如果有网络问题,它会崩溃,如果你运行这个,所以你必须找出这些网络问题是什么,并在运行这个代码之前检测它们。事实上,我不知道为什么它会在我的Win7上崩溃,这里有一些评论:当你明白了,请分享。但是代码应该是好的。它适用于XP和8.1是的,很奇怪。。。我有,我很快会测试并在这里报告。请看注释掉的那一行
//在这里崩溃
我需要帮助使那一行不崩溃。不要使用上面的代码,使用这个代码,这是正确的方法,即使它现在崩溃了。它只有在我第二次运行时才会崩溃。我想我们需要清除DLL中的某些内容。我会设法弄明白的。我们可以指定主MAC地址吗?好的@Sabbathman solution找到并发布了,我留下了旧的东西,这样我们可以看到差异并从中学习:)谢谢@Noitidart,我当然从这个问题中学到了很多。对于abi,我使用了ctypes.winapi_abi,因为我的ff是32位的,但对于64位,你必须使用ctypes.default\u abi,如下所示:
Cu.import('resource://gre/modules/ctypes.jsm');
function getMacAddress(){

    const MAX_ADAPTER_NAME_LENGTH = 256;
    const MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
    const ERROR_BUFFER_OVERFLOW = 111;
    const NO_ERROR = 0;
    const MAX_ADAPTER_ADDRESS_LENGTH = 8;

    var IP_ADDRESS_STRING = ctypes.StructType('IP_ADDRESS_STRING',[
        {'String': ctypes.char.array(4 * 4)}
        ]);


    const DWORD = ctypes.uint32_t;

    var IP_ADDR_STRING = ctypes.StructType('IP_ADDR_STRING');
    IP_ADDR_STRING.define([
        {'Next': IP_ADDR_STRING.ptr},
        {'IpAddress': IP_ADDRESS_STRING},
        {'IpMask': IP_ADDRESS_STRING},
        {'Context': DWORD}
    ]);
    var PIP_ADDR_STRING = IP_ADDR_STRING.ptr;
    var time_t = ctypes.long; // based on this github search https://github.com/search?utf8=%E2%9C%93&q=time_t+ctypes&type=Code&ref=searchresults AND based on this answer here: http://stackoverflow.com/a/471287/1828637

    var IP_ADAPTER_INFO = ctypes.StructType('IP_ADAPTER_INFO');

    IP_ADAPTER_INFO.define([ // have to use .define because one of the fields "Next" is a ptr to itself
        {'Next': IP_ADAPTER_INFO.ptr},
        {'ComboIndex': DWORD},
        {'AdapterName': ctypes.char.array(MAX_ADAPTER_NAME_LENGTH + 4)},
        {'Description': ctypes.char.array(MAX_ADAPTER_DESCRIPTION_LENGTH + 4)},
        {'AddressLength': ctypes.unsigned_int},
        {'Address': ctypes.unsigned_char.array(MAX_ADAPTER_ADDRESS_LENGTH)}, // BYTE is ctypes.unsigned_char
        {'Index': DWORD},
        {'Type': ctypes.unsigned_int},
        {'DhcpEnabled': ctypes.unsigned_int},
        {'CurrentIpAddress': PIP_ADDR_STRING},
        {'IpAddressList': IP_ADDR_STRING},
        {'GatewayList' : IP_ADDR_STRING},
        {'DhcpServer': IP_ADDR_STRING},
        {'HaveWins': ctypes.bool},
        {'PrimaryWinsServer': IP_ADDR_STRING},
        {'SecondaryWinsServer':IP_ADDR_STRING},
        {'LeaseObtained': time_t},
        {'LeaseExpires': time_t}
        ])

    const lib = ctypes.open(ctypes.libraryName('IPHLPAPI'));

    try{
        if(!lib)
            return null

        console.log("Biblioteca aberta!");

        /*DWORD GetAdaptersInfo(
          _Out_    PIP_ADAPTER_INFO pAdapterInfo,
          _Inout_  PULONG pOutBufLen
        );*/
        var getAdaptersInfo = lib.declare(
            "GetAdaptersInfo", 
            ctypes.default_abi,
            DWORD,
            IP_ADAPTER_INFO.ptr,
            ctypes.long.ptr);


        let buf = (IP_ADAPTER_INFO)();
        let pbuf = buf.address();

        let ret = ctypes.long(2000);
        let pret = ret.address();

        let sts = getAdaptersInfo(pbuf,pret);

        if(sts == ERROR_BUFFER_OVERFLOW){ //o tamanho do buffer enviado não foi o suficiente.
            buf = null;
            //agora mando novamente usando o que a lib me retornou.
            sts = getAdaptersInfo(pbuf,pret);
        } else {
            console.log('got it', pbuf.contents.toString());
        }

        if(sts != NO_ERROR){ //aqui ocorreu um erro que não conseguiremos resolver ainda.
            throw Error("Erro buscando configuracoes do adaptador.");
        }

    }catch(ex){
        console.log("Erro na biblioteca", ex, ex.message);
    }finally{
        lib.close();
    }

}

getMacAddress();
Cu.import('resource://gre/modules/ctypes.jsm');
function getMacAddress(){

    var MAX_ADAPTER_NAME_LENGTH = 256;
    var MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
    var ERROR_BUFFER_OVERFLOW = 111;
    var NO_ERROR = 0;
    var MAX_ADAPTER_ADDRESS_LENGTH = 8;

    var IP_ADDRESS_STRING = ctypes.StructType('IP_ADDRESS_STRING',[
        {'String': ctypes.char.array(4 * 4)}
    ]);

    var IP_MASK_STRING = ctypes.StructType('IP_MASK_STRING',[
        {'String': ctypes.char.array(4 * 4)}
    ]);


    var DWORD = ctypes.unsigned_long;

    var IP_ADDR_STRING = ctypes.StructType('IP_ADDR_STRING');
    IP_ADDR_STRING.define([
        {'Next': IP_ADDR_STRING.ptr},
        {'IpAddress': IP_ADDRESS_STRING},
        {'IpMask': IP_MASK_STRING},
        {'Context': DWORD}
    ]);
    var PIP_ADDR_STRING = IP_ADDR_STRING.ptr;

    var time_t = ctypes.long; // based on this github search https://github.com/search?utf8=%E2%9C%93&q=time_t+ctypes&type=Code&ref=searchresults AND based on this answer here: http://stackoverflow.com/a/471287/1828637

    var IP_ADAPTER_INFO = ctypes.StructType('IP_ADAPTER_INFO');

    IP_ADAPTER_INFO.define([ // have to use .define because one of the fields "Next" is a ptr to itself
        {'Next': IP_ADAPTER_INFO.ptr},
        {'ComboIndex': DWORD},
        {'AdapterName': ctypes.char.array(MAX_ADAPTER_NAME_LENGTH + 4)},
        {'Description': ctypes.char.array(MAX_ADAPTER_DESCRIPTION_LENGTH + 4)},
        {'AddressLength': ctypes.unsigned_int},
        {'Address': ctypes.unsigned_char.array(MAX_ADAPTER_ADDRESS_LENGTH)}, // BYTE is ctypes.unsigned_char
        {'Index': DWORD},
        {'Type': ctypes.unsigned_int},
        {'DhcpEnabled': ctypes.unsigned_int},
        {'CurrentIpAddress': PIP_ADDR_STRING},
        {'IpAddressList': IP_ADDR_STRING},
        {'GatewayList' : IP_ADDR_STRING},
        {'DhcpServer': IP_ADDR_STRING},
        {'HaveWins': ctypes.bool},
        {'PrimaryWinsServer': IP_ADDR_STRING},
        {'SecondaryWinsServer':IP_ADDR_STRING},
        {'LeaseObtained': time_t},
        {'LeaseExpires': time_t}
    ]);

    var lib = ctypes.open(ctypes.libraryName('IPHLPAPI'));

    try{
        if(!lib)
            return null

        console.log("Biblioteca aberta!");

        /*DWORD GetAdaptersInfo(
          _Out_    PIP_ADAPTER_INFO pAdapterInfo,
          _Inout_  PULONG pOutBufLen
        );*/
        var getAdaptersInfo = lib.declare("GetAdaptersInfo", ctypes.winapi_abi,
            DWORD,
            IP_ADAPTER_INFO.array(),
            ctypes.unsigned_long.ptr
        );


        let pbuf = IP_ADAPTER_INFO.array(0)();
        console.info('pbuf.length:', pbuf, pbuf.length, 'IP_ADAPTER_INFO.size:', IP_ADAPTER_INFO.size, 'pbuf size:', pbuf.length * IP_ADAPTER_INFO.size);
        let ret = ctypes.unsigned_long(pbuf.length * IP_ADAPTER_INFO.size);

        let sts = getAdaptersInfo(pbuf, ret.address()); // initial fetch to get size needed
        console.info('sts:', ctypes.UInt64.lo(sts));
        console.info('sts:', sts, sts.toString(), uneval(sts));
        console.info('ret:', ret, ret.toString(), uneval(ret));

        if(sts == ERROR_BUFFER_OVERFLOW){ //o tamanho do buffer enviado não foi o suficiente.
            // ret is defined as unsigned_long which is always a UInt64 in jsctypes `CData { value: UInt64 } `
            var ret_jsInt = parseInt(ret.value.toString());
            console.info('ret_jsInt:', ret_jsInt);
            var neededLength = Math.round(ret_jsInt / IP_ADAPTER_INFO.size);
            pbuf = IP_ADAPTER_INFO.array(neededLength)();
            console.info('pbuf RE-size:', pbuf.length * IP_ADAPTER_INFO.size);

            if (pbuf.length * IP_ADAPTER_INFO.size != ret_jsInt) {
                throw new Error('winapi says the size needed is ' + ret_jsInt + ' and i calculated the length by dividing the ID_ADAPTER_INFO.size which is ' + ID_ADAPTER_INFO.size + ' so the needed length was  ' + neededLength + ' but the size of this list of neededLength DOES NOT match what winapi says it needs, the size of the neededLength resized list is ' + (pbuf.length * IP_ADAPTER_INFO.size));
            }

            //agora mando novamente usando o que a lib me retornou.
            console.error('going for 2nd time');

            sts = getAdaptersInfo(pbuf, ret.address()); // we just pass same ret, as it was updated to the right size // crashing here!!!
            if (sts != NO_ERROR) {
                throw new Error('after 2nd fetch it still failed, now i need to add more error handling here, error was: ' + sts.toString());
            } else {
                console.info('succesfully obtained after 2nd time:', pbuf.toString());
                for (var i=0; i<pbuf.length; i++) {
                    console.log(i, pbuf[i].addressOfField('AdapterName').contents.readString(), pbuf[i].addressOfField('IpAddressList').contents.IpAddress.String.readString(), pbuf[i].addressOfField('Description').contents.readString());
                }
            }
        } else {
            console.error('this should never happen! i passed 0 size into it it should always overflow!! unless there is no adapater info but there has to be!!');
        }
        /*
        if(sts != NO_ERROR){ //aqui ocorreu um erro que não conseguiremos resolver ainda.
            throw Error("Erro buscando configuracoes do adaptador.");
        }
        */

    }catch(ex){
        console.log("Erro na biblioteca", ex, ex.message);
    }finally{
        lib.close();
    }

}

getMacAddress();
Cu.import('resource://gre/modules/ctypes.jsm');
function getMacAddress(){

    const MAX_ADAPTER_NAME_LENGTH = 256;
    const MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
    const ERROR_BUFFER_OVERFLOW = 111;
    const NO_ERROR = 0;
    const MAX_ADAPTER_ADDRESS_LENGTH = 8;

    var IP_ADDRESS_STRING = ctypes.StructType('IP_ADDRESS_STRING',[
        {'String': ctypes.char.array(4 * 4)}
    ]);


    const DWORD = ctypes.uint32_t;

    var IP_ADDR_STRING = ctypes.StructType('IP_ADDR_STRING');
    IP_ADDR_STRING.define([
        {'Next': IP_ADDR_STRING.ptr},
        {'IpAddress': IP_ADDRESS_STRING},
        {'IpMask': IP_ADDRESS_STRING},
        {'Context': DWORD}
    ]);
    var PIP_ADDR_STRING = IP_ADDR_STRING.ptr;
    var time_t = ctypes.long; // based on this github search https://github.com/search?utf8=%E2%9C%93&q=time_t+ctypes&type=Code&ref=searchresults AND based on this answer here: http://stackoverflow.com/a/471287/1828637

    var IP_ADAPTER_INFO = ctypes.StructType('IP_ADAPTER_INFO');

    IP_ADAPTER_INFO.define([ // have to use .define because one of the fields "Next" is a ptr to itself
        {'Next': IP_ADAPTER_INFO.ptr},
        {'ComboIndex': DWORD},
        {'AdapterName': ctypes.char.array(MAX_ADAPTER_NAME_LENGTH + 4)},
        {'Description': ctypes.char.array(MAX_ADAPTER_DESCRIPTION_LENGTH + 4)},
        {'AddressLength': ctypes.unsigned_int},
        {'Address': ctypes.unsigned_char.array(MAX_ADAPTER_ADDRESS_LENGTH)}, // BYTE is ctypes.unsigned_char
        {'Index': DWORD},
        {'Type': ctypes.unsigned_int},
        {'DhcpEnabled': ctypes.unsigned_int},
        {'CurrentIpAddress': PIP_ADDR_STRING},
        {'IpAddressList': IP_ADDR_STRING},
        {'GatewayList' : IP_ADDR_STRING},
        {'DhcpServer': IP_ADDR_STRING},
        {'HaveWins': ctypes.bool},
        {'PrimaryWinsServer': IP_ADDR_STRING},
        {'SecondaryWinsServer':IP_ADDR_STRING},
        {'LeaseObtained': time_t},
        {'LeaseExpires': time_t}
        ])

    const lib = ctypes.open(ctypes.libraryName('IPHLPAPI'));

    try{
        if(!lib)
            return null

        console.log("Biblioteca aberta!");

        /*DWORD GetAdaptersInfo(
          _Out_    PIP_ADAPTER_INFO pAdapterInfo,
          _Inout_  PULONG pOutBufLen
        );*/
        var getAdaptersInfo = lib.declare(
            "GetAdaptersInfo", 
            ctypes.default_abi,
            DWORD,
            IP_ADAPTER_INFO.ptr.array(),
            ctypes.unsigned_long.ptr);


        let pbuf = IP_ADAPTER_INFO.ptr.array(1)();
        console.info('pbuf.length:', pbuf.length, 'IP_ADAPTER_INFO.size:', IP_ADAPTER_INFO.size, 'pbuf size:', pbuf.length * IP_ADAPTER_INFO.size);
        let ret = ctypes.unsigned_long(pbuf.length * IP_ADAPTER_INFO.size);

        let sts = getAdaptersInfo(pbuf, ret.address()); // initial fetch to get size needed
        console.info('sts:', sts.toString(), 'ret:', ret.toString());

        if(sts == ERROR_BUFFER_OVERFLOW){ //o tamanho do buffer enviado não foi o suficiente.
            // ret is defined as unsigned_long which is always a UInt64 in jsctypes `CData { value: UInt64 } `
            var ret_jsInt = parseInt(ret.value.toString());
            console.info('ret_jsInt:', ret_jsInt);
            var neededLength = ret_jsInt / IP_ADAPTER_INFO.size;
            pbuf = IP_ADAPTER_INFO.ptr.array(neededLength)();
            console.info('pbuf RE-size:', pbuf.length * IP_ADAPTER_INFO.size);
            if (pbuf.length * IP_ADAPTER_INFO.size != ret_jsInt) {
                throw new Error('winapi says the size needed is ' + ret_jsInt + ' and i calculated the length by dividing the ID_ADAPTER_INFO.size which is ' + ID_ADAPTER_INFO.size + ' so the needed length was  ' + neededLength + ' but the size of this list of neededLength DOES NOT match what winapi says it needs, the size of the neededLength resized list is ' + (pbuf.length * IP_ADAPTER_INFO.size));
            }
            //agora mando novamente usando o que a lib me retornou.
            console.error('going for 2nd time');

            //sts = getAdaptersInfo(pbuf, ret.address()); // we just pass same ret, as it was updated to the right size // crashing here!!!
            if (sts != NO_ERROR) {
                throw new Error('after 2nd fetch it still failed, now i need to add more error handling here, error was: ' + sts.toString());
            } else {
                console.info('succesfully obtained after 2nd time:');
            }
        } else {
            console.error('this should never happen! i passed 0 size into it it should always overflow!! unless there is no adapater info but there has to be!!');
        }
        /*
        if(sts != NO_ERROR){ //aqui ocorreu um erro que não conseguiremos resolver ainda.
            throw Error("Erro buscando configuracoes do adaptador.");
        }
        */

    }catch(ex){
        console.log("Erro na biblioteca", ex, ex.message);
    }finally{
        lib.close();
    }

}

getMacAddress();