Skip to content

Commit 2f433bd

Browse files
committed
fix CacheKey and implement caching for nextjs
1 parent a493a83 commit 2f433bd

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

client_common/src/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class CacheKey {
55
method: string;
66
body: string;
77
sourceIp: string;
8-
headers: Record<string, string>;
8+
userAgent: string;
99
query: Record<string, string>;
1010
}
1111

clients/express/src/express-middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const createAppGuardMiddleware = () => {
9696
// @ts-ignore
9797
sourceIp: sourceIp,
9898
// @ts-ignore
99-
headers: req.headers as Record<string, string>,
99+
userAgent: req.headers["user-agent"],
100100
// @ts-ignore
101101
query: req.query as Record<string, string>,
102102
};

clients/nextjs/src/nextjs-middleware.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {NextRequest, NextResponse} from 'next/server';
2-
import { FirewallPolicy, AppGuardTcpInfo, AppGuardService, AuthHandler } from 'appguard-client-common';
2+
import { FirewallPolicy, AppGuardTcpInfo, AppGuardService, AuthHandler, CacheKey } from 'appguard-client-common';
33

44
type NextjsMiddleware = (req: NextRequest) => Promise<NextResponse>;
55

@@ -15,7 +15,7 @@ export const createAppGuardMiddleware = async () => {
1515
}
1616
await initialize();
1717

18-
const handleOutgoingResponse = async (tcp_info: AppGuardTcpInfo): Promise<NextResponse> => {
18+
const handleOutgoingResponse = async (tcp_info: AppGuardTcpInfo, cacheKey: CacheKey): Promise<NextResponse> => {
1919
let res = NextResponse.next();
2020
const response_headers = res.headers;
2121

@@ -30,11 +30,13 @@ export const createAppGuardMiddleware = async () => {
3030
));
3131

3232
if (handleHTTPResponseResponse.policy === FirewallPolicy.DENY) {
33+
appGuardService.insertToCache(cacheKey, FirewallPolicy.DENY);
3334
return NextResponse.json(
3435
{success: false, message: 'Unauthorized'},
3536
{status: 401}
3637
);
3738
} else {
39+
appGuardService.insertToCache(cacheKey, FirewallPolicy.ALLOW);
3840
return NextResponse.next();
3941
}
4042
};
@@ -49,6 +51,31 @@ export const createAppGuardMiddleware = async () => {
4951
parseInt(req.headers.get('x-forwarded-port') as string, 10) :
5052
undefined;
5153

54+
let cacheKey: CacheKey = {
55+
originalUrl: req.nextUrl.pathname,
56+
method: req.method,
57+
// @ts-ignore
58+
body: req.body,
59+
// @ts-ignore
60+
sourceIp: sourceIp,
61+
// @ts-ignore
62+
userAgent: req.headers["user-agent"],
63+
// @ts-ignore
64+
query: req.nextUrl.searchParams as Record<string, string>,
65+
};
66+
67+
let cached = appGuardService.getFromCache(cacheKey);
68+
if (cached !== undefined) {
69+
if (cached === FirewallPolicy.DENY) {
70+
return NextResponse.json(
71+
{success: false, message: 'Unauthorized'},
72+
{status: 401}
73+
);
74+
} else {
75+
return NextResponse.next();
76+
}
77+
}
78+
5279
const handleTCPConnectionResponse = await appGuardService.connectionPromise(
5380
{
5481
sourceIp: sourceIp,
@@ -80,12 +107,13 @@ export const createAppGuardMiddleware = async () => {
80107

81108
const policy = handleHTTPRequestResponse.policy;
82109
if (policy === FirewallPolicy.DENY) {
110+
appGuardService.insertToCache(cacheKey, FirewallPolicy.DENY);
83111
return NextResponse.json(
84112
{success: false, message: 'Unauthorized'},
85113
{status: 401}
86114
);
87115
} else {
88-
return await handleOutgoingResponse(handleTCPConnectionResponse.tcpInfo as AppGuardTcpInfo);
116+
return await handleOutgoingResponse(handleTCPConnectionResponse.tcpInfo as AppGuardTcpInfo, cacheKey);
89117
}
90118
} catch (error) {
91119
console.error(error);

0 commit comments

Comments
 (0)