
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
@smock/interceptor
Advanced tools
安装:
npm i -g @smock/interceptor
#或
yarn add global @smock/interceptor
-p , --port, 指定端口。示例: sproxy -port 8000-i, --init,生成一个默认配置文件。-V, --version 查看版本-h 显示帮助信息。在启动命令的目录创建.smockrc.js文件, 加入如下形式的配置:
可以运行
sproxy --init创建一个默认配置文件。 该文件也会在首次运行时自动创建。
(下面包含所有可配置项)
module.exports = {
/* 代理目标设置(真实的接口地址)*/
target: "http://xxxxx.com",
/* 资源缓存路径 */
workDir: ".smock",
/* 拦截服务运行的端口 */
workPort: 10011,
/* 正则匹配到的路径将会被缓存 */
matchRegexp: /.*/,
/* 是否缓存静态文件 */
cacheStatic: true,
/* 加入pathIgnore的路径,仍然会被缓存,但缓存的时候将不会以query或body作为区分
* 这样做的原因是有些缓存的时候是根据method参数、query参数和body参数为基础计算出来的哈希值
* 如果请求数据中含有随机数,那么每次请求都会记录一个新值,造成开启拦截后找不到数据的情况
* 此时可以将这些路径加入忽略路径中。
*/
pathIgnore: {
query: [
'/oauth/logout'
],
body: [
'/oauth/login'
]
},
plugins: []
}
module.exports = {
cwd: process.cwd(),
target: "http://localhost:4000",
workDir: ".smock",
workPort: 10011,
matchRegexp: /.*/,
cacheStatic: true,
pathIgnore: {
query: [],
body: []
},
plugins: []
};
启动代理服务:
sproxy # 不指定端口(以默认配置运行在10011端口)
sproxy -p 10011 # 指定运行端口, 会覆盖.smockrc.js里的定义
开发环境下,将api host设置成 http://localhost:10011即可。
GUI操作界面:打开http://localhost:10011/__interceptor即可查看可视化操作页面。
安装umi-插件:
yarn add @smock/umi-plugin-sproxy -D
再次启动命令即可。
注意:工具默认运行端口为10011.首次执行会自动生成配置文件,可以更改其中的配置。更改完成后需再次重新启动开发服务。
工具支持插件系统,使用插件可以为工具添加一些额外的中间件,和生命周期处理。这些中间件会在被插入拦截中间件之前,基础中间件(bodyParser等)之后。生命周期支持:created, beforeRequest, afterSend。
module.exports = (api) => {
api.registerMiddleware((ctx) => {
return function (req, res, next) {
console.log('请求:', req.url);
next();
}
});
api.transformResponse((data) => {
return {
...data,
prop1: 'a example value'
}
});
api.on('created', (ctx, scope) => {
console.log('Created called. logged in log-middleware');
});
api.on('beforeRequest', (ctx, scope) => {
console.log('BeforeRequest called. logged in log-middleware');
});
api.on('afterSend', (ctx, scope) => {
console.log('AfterSend called. logged in log-middleware');
});
}
然后在.smockrc.js中添加如下代码:
module.exports {
// ...
plugins: [require('path/to/log-middleware')]
}
on(hook: string): void; 注册一个生命周期处理函数。 hook = 'created' | 'beforeRequest' | 'afterSend'
registerMiddleware(creator: (ctx: GlobalContext) => RequestHandler) 注册一个中间件
transformResponse(transformer: (data: any) => any) 返回数据处理器
interface GlobalContextInterface {
app: Application;
db: DbUtil;
file: FileUtil;
config: ProxyConfig;
cwd: string;
pluginApi: PluginApi;
}
export interface ScopedContextInterface {
request: Request;
response: Response;
}
注意: 余下方法为app内部调用准备。在中间件内调用可能会引起一些意想不到的结果。请勿随意使用。
FAQs
Api interceptor, based on proxy.
We found that @smock/interceptor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 12 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.