Skip to content

Commit

Permalink
fix: options visibility (#13)
Browse files Browse the repository at this point in the history
* add options to useAgent

* Update README.md

options example

* Update src/request-filtering-agent.ts

Co-authored-by: azu <[email protected]>

Co-authored-by: azu <[email protected]>
  • Loading branch information
drudrum and azu authored Jul 4, 2022
1 parent 3c3e9ed commit 82efe06
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Install with [npm](https://www.npmjs.com/):

## Usage

`useAgent(url)` return an agent for the url.
`useAgent(url, options)` return an agent for the url.

The agent blocks the request to [Private network](https://en.wikipedia.org/wiki/Private_network) and [Reserved IP addresses](https://en.wikipedia.org/wiki/Reserved_IP_addresses) by default.

Expand All @@ -48,7 +48,7 @@ const { useAgent } = require("request-filtering-agent");
const url = 'http://127.0.0.1:8080/';
fetch(url, {
// use http or https agent for url
agent: useAgent(url)
agent: useAgent(url, { stopPortScanningByUrlRedirection: true })
}).catch(err => {
console.err(err); // DNS lookup 127.0.0.1(family:4, host:127.0.0.1.nip.io) is not allowed. Because, It is private IP address.
});
Expand All @@ -73,7 +73,7 @@ const fetch = require("node-fetch");
const { useAgent } = require("request-filtering-agent");
const url = 'http://127.0.0.1.nip.io:8080/';
fetch(url, {
agent: useAgent(url)
agent: useAgent(url, { stopPortScanningByUrlRedirection: true })
}).catch(err => {
console.err(err); // DNS lookup 127.0.0.1(family:4, host:127.0.0.1.nip.io) is not allowed. Because, It is private IP address.
});
Expand Down Expand Up @@ -133,7 +133,7 @@ export declare const globalHttpsAgent: RequestFilteringHttpsAgent;
* return http or https agent
* @param url
*/
export declare const useAgent: (url: string) => RequestFilteringHttpAgent | RequestFilteringHttpsAgent;
export declare const useAgent: (url: string, options?: https.AgentOptions & RequestFilteringAgentOptions) => RequestFilteringHttpAgent | RequestFilteringHttpsAgent;
```

### Example: Create an Agent with options
Expand Down
12 changes: 7 additions & 5 deletions src/request-filtering-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface RequestFilteringAgentOptions {
const validateIPAddress = (
{ address, host, family }: { address: string; host?: string; family?: string | number },
options: Required<RequestFilteringAgentOptions>
) => {
): null | undefined | Error => {
// if it is not IP address, skip it
if (net.isIP(address) === 0) {
return;
Expand Down Expand Up @@ -74,7 +74,7 @@ const validateIPAddress = (
);
}
} catch (error) {
return error; // if can not parsed IP address, throw error
return error as Error; // if can not parsed IP address, throw error
}
return;
};
Expand Down Expand Up @@ -131,7 +131,6 @@ export function applyRequestFilter<T extends http.Agent | https.Agent>(
if (host && net.isIP(host)) {
const addr = ipaddr.parse(host);
const range = addr.range();

if (range !== "unicast") {
throw new Error(
`DNS lookup ${host}(family:${family}, host:${host}) is not allowed. Because, It is private IP address.`
Expand Down Expand Up @@ -191,6 +190,9 @@ export const globalHttpsAgent = new RequestFilteringHttpsAgent();
* return http or https agent
* @param url
*/
export const useAgent = (url: string) => {
return url.startsWith("https") ? globalHttpsAgent : globalHttpAgent;
export const useAgent = (url: string, options?: https.AgentOptions & RequestFilteringAgentOptions) => {
if(!options) {
return url.startsWith("https") ? globalHttpsAgent : globalHttpAgent;
}
return url.startsWith("https") ? new RequestFilteringHttpsAgent(options) : new RequestFilteringHttpAgent(options);
};

0 comments on commit 82efe06

Please sign in to comment.