--{module_name}_binary_host_mirror和--{module_name}_binary_site

--{module_name}_binary_host_mirror--{module_name}_binary_site

demo

// .npmrc文件
sass_binary_site=https://npmmirror.com/mirrors/node-sass/
nodejieba_binary_host_mirror=https://npm.taobao.org/mirrors/nodejieba

gyp

gyp全称Generate Your Projects(构建你的项目)

node-gyp

抹平了不同平台之间的差异(比如mac和windows),将源代码编译为可执行的二进制文件。由于某些npm包底层是使用C++/C这种依赖平台的代码,比如node-sass。所以就不能直接像其他普通包一样从npm中直接下载文件到本地,而是需要将源代码拉下来使用node-gyp将源代码编译为可执行的二进制文件。

node-pre-gyp

每次安装包都需要使用node-gyp本地构建,这样很麻烦。库开发者将编译好的各种平台的二进制包发布到node-pre-gyp中,用户进行install时会首先根据本地的环境看远端是否有编译好的二进制文件,如果有就直接使用编译好的二进制文件。如果没有则本地使用node-gyp编译生成本地的可执行二进制文件。

如何指定二进制包的位置

使用package.json文件的binary字段指定。

binary: {
  ...
}

指定的位置网络被墙,比如github。使用--{module_name}_binary_host_mirror

// .npmrc文件
nodejieba_binary_host_mirror=https://npm.taobao.org/mirrors/nodejieba

什么时候使用--{module_name}_binary_host_mirror

使用node-pre-gyp包的就需要使用binary_host_mirror,因为node-pre-gyp内部在install时会优先读取npm config中配置的binary_host_mirror。并且module_name的值为包的package.json中配置的"binary.module_name"的值。

sqlite3举例:

sqlite3package.json文件

{
  "binary": {
    "module_name": "node_sqlite3",
    "module_path": "./lib/binding/napi-v{napi_build_version}-{platform}-{libc}-{arch}",
    "host": "https://github.com/TryGhost/node-sqlite3/releases/download/",
    "remote_path": "v{version}",
    "package_name": "napi-v{napi_build_version}-{platform}-{libc}-{arch}.tar.gz",
    "napi_versions": [
      3,
      6
    ]
  },
  "scripts": {
      // ....
      "install": "node-pre-gyp install --fallback-to-build",
      //...
    },
}

执行npm i sqlite3时会首先从npm中下载sqlite3的源代码,下载完后检测到源码的package.json中的script标签中有install指令,就会执行install,执行node-pre-gyp install --fallback-to-buildnode-pre-gypnode_modules/.bin/node-pre-gyp中,执行node-pre-gyplib/main.js文件。

这里的install命令在js中可以使用process.argv[2]process.argv[3]分别读取到install--fallback-to-buildprocess.argv[0]的值为nodejs可执行文件所在的位置,process.argv[1]的值为当前执行的 JavaScript 文件所在的路径。从2开始都是命令行参数了。

node-pre-gyp中下载预编译二进制文件的代码

const opts = {
    // ...
    module_name: package_json.binary.module_name,
}
const validModuleName = opts.module_name.replace('-', '_');
const host = process.env['npm_config_' + validModuleName + '_binary_host_mirror'] || package_json.binary.host;

可以通过process.env读取npm的config,比如读取registryprocess.env.npm_config_registry

所以这里的host优先从npm config里面去读binary_host_mirror,其中的module_name的值为包的package.json文件中的"binary.module_name"字段。

什么时候使用--{module_name}_binary_site

node-sasspackage.json文件:

{
  "nodeSassConfig": {
    "binarySite": "https://github.com/sass/node-sass/releases/download"
  },
  "scripts": {
    // ...
    "install": "node scripts/install.js",
    // ...
  },
}

使用npm i node-sass安装node-sass时会先从npm中下载node-sass的源码。源码下载完成后检测到script中有install命令,如何执行install命令。

scripts/install.js文件

function checkAndDownloadBinary() {
  var cachedBinary = sass.getCachedBinary(),
      cachePath = sass.getBinaryCachePath(),
      binaryPath = sass.getBinaryPath();
  // 下载二进制可执行文件    
  download(sass.getBinaryUrl(), binaryPath, function(err) {
    // ....
  });
}

// If binary does not exist, download it
checkAndDownloadBinary();

调用checkAndDownloadBinary方法进行下载预编译文件。

getBinaryUrlgetArgument函数:

function getArgument(name, args) {
  var flags = args || process.argv.slice(2),
    index = flags.lastIndexOf(name);

  if (index === -1 || index + 1 >= flags.length) {
    return null;
  }

  return flags[index + 1];
}

function getBinaryUrl() {
  var site = getArgument('--sass-binary-site') ||
              process.env.SASS_BINARY_SITE  ||
              process.env.npm_config_sass_binary_site ||
              (pkg.nodeSassConfig && pkg.nodeSassConfig.binarySite) ||
              'https://github.com/sass/node-sass/releases/download';

  return [site, 'v' + pkg.version, getBinaryName()].join('/');
}

下载预编译文件时优先使用在命令行中指定的--sass-binary-site。比如:npm install node-sass --sass-binary-site=https://npmmirror.com/mirrors/node-sass/

其次使用环境变量中指定的SASS_BINARY_SITE。比如:export SASS_BINARY_SITE=http://example.com/

再然后就是读取npm配置(常用的是.npmrc文件)中的sass_binary_site,通过process.env.npm_config_sass_binary_site读取。

再然后读取package.json中配置的binarySite字段。

{
  // ...
  "nodeSassConfig": {
    "binarySite": "https://github.com/sass/node-sass/releases/download"
  },
  // ...
}

最后就是一个写死的github地址'https://github.com/sass/node-sass/releases/download'

总结:

要安装的包如果使用了node-pre-gyp(比如nodejieba),在.npmrc文件中配置国内预编译文件镜像URL就需要使用--{module_name}_binary_host_mirror。其中的module_name的值为包的package.json中配置的"binary.module_name"的值。

什么时候使用--{module_name}_binary_site实际是由要安装的包内部自己实现的,典型的案例就是node-sass

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Frida-Gum is a powerful instrumentation framework for dynamic binary analysis and reverse engineering. It provides a high-level API for writing custom code to interact with native code running on different platforms. To use Gum to find a C++ class method function using C++ code, you can start by loading your target binary into memory using the `gum_process_attach()` function. Once attached, you can use `gum_module_find_export_by_name()` function to find the address of the C++ class method function inside the target binary. Here is an example code snippet that demonstrates how to use Gum to find a C++ class method function: ```c++ #include <gum/gum.h> class MyClass { public: void myMethod(int arg1, int arg2) { // implementation of myMethod } }; int main() { gum_init_embedded(); GumAddress myMethodAddress = 0; GumAddress targetAddress = gum_module_find_base_address("mybinary"); if (targetAddress != 0) { GumExportDetails exportDetails; if (gum_module_find_export_by_name("mybinary", "_ZN7MyClass8myMethodEii", &exportDetails)) { myMethodAddress = exportDetails.address; } } // myMethodAddress now contains the address of MyClass::myMethod() gum_deinit_embedded(); return 0; } ``` In this example, we defined a simple C++ class `MyClass` with a method `myMethod()`. We then attached to the target binary using `gum_process_attach()`, and used `gum_module_find_export_by_name()` to find the address of the `MyClass::myMethod()` function inside the target binary. The function signature `_ZN7MyClass8myMethodEii` corresponds to the mangled name of the `MyClass::myMethod()` function in C++. Note that this is just a basic example, and you will need to adapt the code to your specific use case. Also, keep in mind that using Gum to interact with native code requires a good understanding of the target binary and its environment.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值