11import { 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
44type 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