Skip to content

Commit

Permalink
feat: 增加 请求代理支持
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Oct 7, 2023
1 parent b83e224 commit fc84fa6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ updates:
open-pull-requests-limit: 20
schedule:
interval: "daily"
time: "19:00"
time: "05:00"
timezone: "Asia/Shanghai"
ignore:
- dependency-name: "conventional-changelog-cli"
- dependency-name: "semantic-release"
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
>
> 温馨提示:出于安全考虑, **所有** 推送方式请在 **服务端** 使用!请勿在 **客户端(网页端)** 使用!网页端使用还将额外产生跨域问题。
### 🏠 [主页](https://github.com/CaoMeiYouRen/push-all-in-one#readme)
## 🏠 [主页](https://github.com/CaoMeiYouRen/push-all-in-one#readme)

[https://github.com/CaoMeiYouRen/push-all-in-one#readme](https://github.com/CaoMeiYouRen/push-all-in-one#readme)


### [Demo](https://github.com/CaoMeiYouRen/push-all-in-one#readme)
## [Demo](https://github.com/CaoMeiYouRen/push-all-in-one#readme)

[https://github.com/CaoMeiYouRen/push-all-in-one#readme](https://github.com/CaoMeiYouRen/push-all-in-one#readme)

Expand Down Expand Up @@ -120,6 +120,33 @@ const discord = new Discord(DISCORD_WEBHOOK, DISCORD_USERNAME)
discord.send('你好,我很可爱 - Discord')
```

**代理支持**

| 环境变量 | 作用 | 例子 |
| ----------- | ----------------------------------- | ---------------------- |
| NO_PROXY | 设置是否禁用代理 | true |
| HTTP_PROXY | 设置 http 代理 | http://127.0.0.1:8101 |
| HTTPS_PROXY | 设置 https 代理 | http://127.0.0.1:8101 |
| SOCKS_PROXY | 通过 socks 协议设置 http/https 代理 | socks://127.0.0.1:8100 |

本项目通过环境变量来支持请求代理

```ts
// 在 nodejs 项目中可通过直接设置环境变量来设置代理
process.env.HTTP_PROXY = 'http://127.0.0.1:8101' // 当请求是 http 的时候走 HTTP_PROXY
process.env.HTTPS_PROXY = 'http://127.0.0.1:8101' // 当请求是 https 的时候走 HTTPS_PROXY
process.env.SOCKS_PROXY = 'socks://127.0.0.1:8100' // 当 HTTP_PROXY 和 HTTPS_PROXY 均为设置时走 SOCKS_PROXY
// process.env.NO_PROXY = true // 设置 NO_PROXY 可禁用代理
```

在命令行中可手动设置环境变量

```sh
set HTTPS_PROXY='http://127.0.0.1:8101' # Windows
export HTTPS_PROXY='http://127.0.0.1:8101' # Linux
cross-env HTTPS_PROXY='http://127.0.0.1:8101' # 通过 cross-env 这个包来跨平台
```

## 🛠️ 开发

本项目采用 TypeScript 开发,使用 rollup 打包,可以完美实现类型提示和摇树优化,对于未使用到的模块,会在编译阶段去除。
Expand Down
3 changes: 3 additions & 0 deletions api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
"ae-missing-release-tag": {
"logLevel": "none"
},
"ae-wrong-input-file-type": {
"logLevel": "warning"
}
},
"tsdocMessageReporting": {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"start": "node ./dist/index",
"release": "semantic-release",
"commit": "git add . && git cz",
"test": "npm run test:e2e",
"test:e2e": "cross-env DEBUG=push:* NODE_ENV=development mocha -r ts-node/register test/register.ts test/**/*.test.ts -R nyan -t 30000",
"test:debug": "cross-env DEBUG=push:* NODE_ENV=development ts-node-dev test/index.test.ts",
"prepare": "husky install"
Expand Down Expand Up @@ -110,8 +111,10 @@
"axios": "^1.2.1",
"crypto-js": "^4.0.0",
"debug": "^4.3.1",
"https-proxy-agent": "^7.0.2",
"nodemailer": "^6.9.1",
"qs": "^6.10.3"
"qs": "^6.10.3",
"socks-proxy-agent": "^8.0.2"
},
"config": {
"commitizen": {
Expand All @@ -136,4 +139,4 @@
"git add"
]
}
}
}
4 changes: 0 additions & 4 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
export const __PROD__ = process.env.NODE_ENV === 'production'
export const __DEV__ = process.env.NODE_ENV === 'development'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// export const __BROWSER__ = typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs
2 changes: 1 addition & 1 deletion src/push/dingtalk.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AxiosResponse } from 'axios'
import debug from 'debug'
import CryptoJS from 'crypto-js'
import { Send } from '../interfaces/send'
import { MessageTemplateAbs } from './dingtalk/template'
import { Text } from './dingtalk/Text'
import { Markdown } from './dingtalk/Markdown'
import { Send } from '@/interfaces/send'
import { warn } from '@/utils/helper'
import { ajax } from '@/utils/ajax'

Expand Down
22 changes: 22 additions & 0 deletions src/utils/ajax.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import axios, { AxiosResponse, Method, AxiosRequestHeaders } from 'axios'
import qs from 'qs'
import debug from 'debug'
import { HttpsProxyAgent } from 'https-proxy-agent'
import { SocksProxyAgent } from 'socks-proxy-agent'

const Debugger = debug('push:ajax')

Expand Down Expand Up @@ -33,13 +35,33 @@ export async function ajax(config: AjaxConfig): Promise<AxiosResponse<any>> {
data = qs.stringify(data)
}

let httpAgent = null
let httpsAgent = null
if (!process.env.NO_PROXY) {
if (url?.startsWith('http://') || baseURL?.startsWith('http://')) {
if (process.env.HTTP_PROXY) {
httpAgent = new HttpsProxyAgent(process.env.HTTP_PROXY)
} else if (process.env.SOCKS_PROXY) {
httpAgent = new SocksProxyAgent(process.env.SOCKS_PROXY)
}
} else if (url?.startsWith('https://') || baseURL?.startsWith('https://')) {
if (process.env.HTTPS_PROXY) {
httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY)
} else if (process.env.SOCKS_PROXY) {
httpsAgent = new SocksProxyAgent(process.env.SOCKS_PROXY)
}
}
}
const response = await axios(url, {
baseURL,
method,
headers,
params: query,
data,
timeout: 10000,
httpAgent,
httpsAgent,
proxy: false,
})
Debugger('response data: %O', response.data)
return response
Expand Down

0 comments on commit fc84fa6

Please sign in to comment.