Skip to content

Commit

Permalink
feat: add new options
Browse files Browse the repository at this point in the history
  • Loading branch information
timi137137 committed Jan 10, 2024
1 parent 1cdd0a6 commit c9280d2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ First register the filter in `app.module.ts`, The sample code shows the IP addre

```typescript
IpFilterModule.register({
isGlobal: true,
mode: 'deny',
trustProxy: true,
ip: {
Expand All @@ -53,6 +54,7 @@ getHello(): string {

| Name | Type | Default | Description |
|------------|---------------------|-----------|----------------------------------------------------------------------|
| isGlobal | boolean | false | enabling this option will filter all routes |
| mode | string | deny | deny or allow |
| trustProxy | boolean or string[] | false | trust proxy headers |
| ip.list | string[] | undefined | list of IPs |
Expand All @@ -63,6 +65,7 @@ getHello(): string {

```txt
{
isGlobal: true,
mode: 'deny',
trustProxy: true,
ip: {
Expand Down
5 changes: 5 additions & 0 deletions lib/interfaces/ip-filter-options.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export type IpFilterModuleOptions = {
* If-select array, only the incoming proxy server is trusted, otherwise it is rejected.
*/
trustProxy?: boolean | string[];
/**
* Enabling this option will filter all routes\
* Default: false
*/
isGlobal?: boolean;
};

export type IpOptions = {
Expand Down
8 changes: 6 additions & 2 deletions lib/ip-filter.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ export class IpFilterGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): Promise<boolean> | Observable<boolean> | boolean {
const request = context.switchToHttp().getRequest();
const clientIp: string = getIp(request, this.ipFilterService.options);
// get guard
const guard = this.reflector.get<boolean>('ipFilter', context.getHandler());

// if not global and guard is not defined, then skip ip filter
if (!this.ipFilterService.isGlobal && guard === undefined) {
return true;
}
// if set guard to false, then skip ip filter
if (guard === false) {
return true;
}

const request = context.switchToHttp().getRequest();
const clientIp: string = getIp(request, this.ipFilterService.options);
const blockList = new BlockList();

if (this.ipFilterService.ipList) {
Expand Down
19 changes: 15 additions & 4 deletions lib/ip-filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ import { MODULE_OPTIONS_TOKEN } from './ip-filter.module-definition';
export class IpFilterService {
mode: IpFilterMode;
trustProxy: boolean | string[];
isGlobal: boolean;

private _ip: IpOptions;

constructor(
@Inject(MODULE_OPTIONS_TOKEN)
readonly options: IpFilterModuleOptions,
private readonly _options: IpFilterModuleOptions,
) {
this.mode = options.mode ?? 'deny';
this.trustProxy = options.trustProxy ?? false;
this.mode = _options.mode ?? 'deny';
this.trustProxy = _options.trustProxy ?? false;
this.isGlobal = _options.isGlobal ?? false;

this._ip = options.ip;
this._ip = _options.ip;
}

get options(): IpFilterModuleOptions {
return {
mode: this.mode,
trustProxy: this.trustProxy,
isGlobal: this.isGlobal,
ip: this._ip,
};
}

get ipList(): string[] {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@timi137/nestjs-ipfilter",
"version": "0.1.3",
"version": "0.1.4",
"description": "IP filter module for Nest framework (node.js) 🌎",
"author": "悠静 <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit c9280d2

Please sign in to comment.