whistle插件开发与自定义扩展

whistle插件开发与自定义扩展

【免费下载链接】whistle HTTP, HTTP2, HTTPS, Websocket debugging proxy 【免费下载链接】whistle 项目地址: https://gitcode.com/gh_mirrors/wh/whistle

本文全面介绍了whistle插件的开发与自定义扩展方法,涵盖了从开发环境搭建、自定义规则操作指令开发、界面扩展与功能集成到插件发布与共享的全流程。文章详细讲解了如何搭建基于Node.js的开发环境,创建自定义规则操作指令,开发Web界面扩展,以及遵循最佳实践进行插件发布和社区共享,为开发者提供了完整的whistle插件开发指南。

whistle插件开发环境搭建

要开始开发whistle插件,首先需要搭建一个完整的开发环境。whistle作为一款基于Node.js的跨平台网络调试工具,其插件开发环境搭建相对简单但需要遵循特定的配置流程。

环境要求

在开始之前,请确保您的系统满足以下基本要求:

组件最低版本推荐版本说明
Node.js8.8+14.0+JavaScript运行时环境
npm6.0+8.0+Node.js包管理器
Git2.0+2.30+版本控制系统
操作系统Windows 7+Windows 10+支持跨平台开发
macOS 10.12+macOS 11+
Ubuntu 16.04+Ubuntu 20.04+

安装Node.js和npm

首先需要安装Node.js,建议使用nvm(Node Version Manager)来管理多个Node.js版本:

# 安装nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# 安装最新LTS版本的Node.js
nvm install --lts
nvm use --lts

验证安装是否成功:

node --version
npm --version

获取whistle源码

克隆whistle项目到本地开发环境:

git clone https://gitcode.com/gh_mirrors/wh/whistle.git
cd whistle

安装项目依赖

进入项目目录并安装所有依赖包:

npm install

这个过程会安装所有生产环境和开发环境的依赖,包括:

  • 核心运行时依赖(express、hparser、ws-parser等)
  • 开发工具(webpack、babel、eslint等)
  • 测试框架(tape、should等)

开发环境配置

whistle提供了完善的开发脚本,在package.json中可以看到以下关键命令:

{
  "scripts": {
    "dev": "webpack --config ./biz/webui/htdocs/src/webpack.config -w",
    "start": "node bin/whistle.js run",
    "test": "npm run lintfix && node test/index.test.js",
    "lint": "eslint *.js ./lib ./bin ./biz ./test ./docs/script",
    "lintfix": "eslint --fix *.js ./lib ./bin ./biz ./test ./docs/script"
  }
}

启动开发服务器

使用以下命令启动whistle开发服务器:

npm start

或者直接运行:

node bin/whistle.js run

服务器默认会在8899端口启动,您可以通过浏览器访问 http://local.whistlejs.com:8899 来打开whistle的管理界面。

插件开发目录结构

了解whistle的插件系统目录结构对于开发至关重要:

mermaid

开发工具配置

配置您的代码编辑器以获得更好的开发体验:

.vscode/settings.json:

{
  "editor.tabSize": 2,
  "editor.insertSpaces": true,
  "files.eol": "\n",
  "javascript.validate.enable": true,
  "eslint.validate": [
    "javascript",
    "javascriptreact"
  ]
}

.eslintrc.js(项目已包含):

module.exports = {
  env: {
    node: true,
    es6: true
  },
  extends: 'eslint:recommended',
  rules: {
    // 项目特定的代码规范规则
  }
};

测试环境搭建

whistle使用tape作为测试框架,配置测试环境:

# 运行所有测试
npm test

# 运行特定测试文件
node test/units/plugins.test.js

# 生成测试覆盖率报告
npm run cov

调试配置

配置调试环境以便于插件开发调试:

.vscode/launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug whistle",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/bin/whistle.js",
      "args": ["run"],
      "env": {
        "NODE_ENV": "development"
      },
      "console": "integratedTerminal"
    }
  ]
}

开发工作流

典型的whistle插件开发工作流程如下:

mermaid

环境验证

完成环境搭建后,运行验证脚本来确认一切正常:

# 检查Node.js版本
node -e "console.log('Node.js版本:', process.version)"

# 检查npm版本
npm --version

# 检查项目依赖
npm list --depth=0

# 运行基础测试
npm test

常见问题解决

在环境搭建过程中可能会遇到以下常见问题:

问题解决方案
端口8899被占用修改config.js中的端口配置或使用 w2 start -p 8900
证书安装失败手动运行 w2 ca 并按照提示操作
插件加载失败检查插件语法错误和依赖配置
权限问题使用sudo或管理员权限运行命令

现在您的whistle插件开发环境已经搭建完成,可以开始创建和测试自定义插件了。下一节将详细介绍如何创建第一个whistle插件。

自定义规则操作指令开发指南

Whistle作为一款功能强大的网络调试工具,其核心能力之一就是通过自定义规则操作指令来实现对网络请求的精细化控制。本文将深入探讨如何开发自定义规则操作指令,帮助开发者扩展Whistle的功能边界。

规则操作指令的基本结构

在Whistle中,自定义规则操作指令遵循特定的语法结构:

pattern operatorProtocol://operatorValue

其中:

  • pattern:匹配请求的模式(URL、域名、正则表达式等)
  • operatorProtocol:操作协议,标识要执行的操作类型
  • operatorValue:操作的具体值或配置

核心开发接口解析

Whistle通过插件系统支持自定义规则操作指令的开发。每个自定义指令都需要实现特定的接口:

// 插件基本结构示例
module.exports = {
  // 插件名称
  name: 'custom-operator',
  
  // 规则定义
  rules: `
    # 自定义操作指令示例
    *.example.com custom://value
  `,
  
  // 操作处理器
  onOperator: function(req, res, value) {
    // 处理逻辑实现
  }
};

操作指令处理流程

Whistle处理自定义规则操作指令的完整流程如下:

mermaid

常用操作协议类型

Whistle支持多种类型的操作协议,开发者可以根据需求选择合适的协议类型:

协议类型用途描述示例
file://文件操作file:///path/to/file
tpl://模板处理tpl://{templateName}
plugin://插件调用plugin://pluginName
replace://内容替换replace://old|new
append://内容追加append://additionalContent

开发自定义操作指令

1. 创建插件项目结构

首先创建标准的Whistle插件项目结构:

custom-operator-plugin/
├── package.json
├── index.js
├── rules.txt
└── lib/
    └── operator-handler.js
2. 实现操作处理器

index.js中实现核心的操作处理逻辑:

const { createServer } = require('http');
const url = require('url');

module.exports = (server, options) => {
  // 注册自定义操作协议
  server.on('request', (req, res) => {
    const parsedUrl = url.parse(req.url, true);
    
    if (parsedUrl.pathname === '/custom-operator') {
      handleCustomOperator(req, res, parsedUrl.query);
    } else {
      res.statusCode = 404;
      res.end();
    }
  });

  // 返回插件配置
  return {
    // 规则模板
    rules: `
      # 自定义操作指令
      www.example.com custom://setting=value
      *.test.com custom://{templateVar}
    `,
    
    // 操作处理器
    onOperator: (ctx, value) => {
      console.log('处理自定义操作:', value);
      // 实现具体的操作逻辑
      return processCustomOperation(value);
    }
  };
};

function processCustomOperation(value) {
  // 解析操作值
  const params = new URLSearchParams(value);
  const operation = {
    type: params.get('type') || 'default',
    settings: {}
  };
  
  // 构建操作配置
  for (const [key, val] of params) {
    if (key !== 'type') {
      operation.settings[key] = val;
    }
  }
  
  return operation;
}
3. 配置规则模板

rules.txt中定义规则模板:

# 自定义操作指令模板
{{ruleValue}} custom://type=transform&setting={{settingValue}}

# 使用示例
www.example.com custom://type=modify&header=X-Custom-Header=test
api.example.com custom://type=redirect&target=https://new.example.com

高级操作指令开发技巧

动态参数处理

支持动态参数可以让操作指令更加灵活:

// 支持模板变量的操作处理器
function handleDynamicOperator(ctx, value) {
  const templateVars = {
    now: Date.now(),
    random: Math.random(),
    url: ctx.url,
    method: ctx.method
  };
  
  // 替换模板变量
  const processedValue = value.replace(/\{\{(\w+)\}\}/g, (match, key) => {
    return templateVars[key] || match;
  });
  
  return processOperator(processedValue);
}
错误处理机制

健全的错误处理是高质量操作指令的关键:

class CustomOperator {
  constructor() {
    this.operations = new Map();
    this.registerOperations();
  }
  
  registerOperations() {
    this.operations.set('transform', this.handleTransform.bind(this));
    this.operations.set('redirect', this.handleRedirect.bind(this));
    this.operations.set('modify', this.handleModify.bind(this));
  }
  
  execute(ctx, operationString) {
    try {
      const operation = this.parseOperation(operationString);
      const handler = this.operations.get(operation.type);
      
      if (!handler) {
        throw new Error(`未知操作类型: ${operation.type}`);
      }
      
      return handler(ctx, operation.settings);
    } catch (error) {
      console.error('操作执行失败:', error.message);
      // 返回默认处理或抛出错误
      return this.handleDefault(ctx);
    }
  }
  
  parseOperation(operationString) {
    // 解析操作字符串的逻辑
    // 返回 { type: string, settings: object }
  }
}

性能优化建议

开发自定义操作指令时需要考虑性能因素:

  1. 缓存机制:对频繁使用的操作结果进行缓存
  2. 异步处理:使用异步操作避免阻塞主线程
  3. 内存管理:及时释放不再使用的资源
  4. 连接复用:重用HTTP连接减少开销
// 带缓存的操作处理器示例
const cache = new Map();

async function handleWithCache(ctx, operation) {
  const cacheKey = `${operation.type}:${JSON.stringify(operation.settings)}`;
  
  if (cache.has(cacheKey)) {
    return cache.get(cacheKey);
  }
  
  const result = await processOperation(ctx, operation);
  cache.set(cacheKey, result);
  
  // 设置缓存过期时间
  setTimeout(() => cache.delete(cacheKey), 30000);
  
  return result;
}

测试与调试

开发完成后需要进行充分的测试:

// 操作指令测试用例
describe('Custom Operator Tests', () => {
  test('should parse operation string correctly', () => {
    const operator = new CustomOperator();
    const result = operator.parseOperation('type=test&value=123');
    
    expect(result.type).toBe('test');
    expect(result.settings.value).toBe('123');
  });
  
  test('should handle template variables', () => {
    const ctx = { url: 'https://example.com', method: 'GET' };
    const result = processWithTemplates(ctx, 'redirect://{{url}}/new');
    
    expect(result).toBe('redirect://https://example.com/new');
  });
});

通过本文的指南,开发者可以掌握Whistle自定义规则操作指令的开发方法,创建出功能强大且性能优异的自定义操作指令,进一步提升网络调试的效率和灵活性。

界面扩展与自定义功能集成

Whistle作为一款功能强大的网络调试工具,其插件系统提供了丰富的界面扩展能力。通过自定义UI界面,开发者可以为Whistle添加全新的功能模块,提升调试效率和用户体验。

界面扩展架构

Whistle的界面扩展基于模块化的插件架构,通过Web UI与后端CGI接口的配合实现功能集成:

mermaid

自定义UI界面开发

Whistle插件可以通过webui配置项定义自定义界面,支持多种界面集成方式:

1. 独立页面集成

插件可以注册独立的HTML页面,通过特定的路由路径访问:

// 插件配置文件 whistle.js
module.exports = {
    // ... 其他配置
    webui: {
        // 注册自定义页面
        '/my-plugin': {
            title: '我的插件',
            content: '<div>自定义插件界面内容</div>'
        },
        // 或者指定外部HTML文件
        '/another-plugin': './path/to/template.html'
    }
};
2. 嵌入式组件集成

插件可以在Whistle主界面中嵌入自定义组件:

module.exports = {
    webui: {
        // 在Network页面添加标签页
        'network.tabs': {
            'my-tab': {
                title: '自定义标签',
                content: '<div>嵌入到Network页面的内容</div>'
            }
        },
        // 在Rules页面添加侧边栏
        'rules.sidebar': {
            'my-sidebar': {
                title: '自定义侧边栏',
                content: '<div>规则页面侧边栏内容</div>'
            }
        }
    }
};

后端接口开发

自定义界面通常需要与后端进行数据交互,Whistle提供了完整的CGI接口开发支持:

CGI接口定义
// plugins/my-plugin/cgi/my-api.js
module.exports = function(req, res) {
    // 获取请求参数
    const params = req.query;
    
    // 处理业务逻辑
    const result = {
        ec: 0, // 错误码,0表示成功
        data: {
            message: '处理成功',
            timestamp: Date.now()
        }
    };
    
    // 返回JSON响应
    res.json(result);
};
接口注册配置
// whistle.js 插件配置文件
module.exports = {
    // ... 其他配置
    cgi: {
        // 注册CGI接口
        '/cgi/my-plugin/api': './cgi/my-api.js',
        '/cgi/my-plugin/data': './cgi/data-handler.js'
    }
};

前后端数据交互

Whistle插件的前后端数据交互采用标准的HTTP请求方式:

// 前端JavaScript代码示例
class MyPluginUI {
    constructor() {
        this.baseURL = '/cgi/my-plugin';
    }
    
    // 获取数据方法
    async fetchData(params = {}) {
        try {
            const response = await fetch(`${this.baseURL}/api?${new URLSearchParams(params)}`);
            const result = await response.json();
            
            if (result.ec === 0) {
                return result.data;
            } else {
                throw new Error(result.em || '请求失败');
            }
        } catch (error) {
            console.error('数据获取失败:', error);
            throw error

【免费下载链接】whistle HTTP, HTTP2, HTTPS, Websocket debugging proxy 【免费下载链接】whistle 项目地址: https://gitcode.com/gh_mirrors/wh/whistle

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值