11import { NextFunction , Request , Response , Send } from 'express' ;
2- import { FirewallPolicy , AppGuardTcpInfo , AppGuardService , AuthHandler } from 'appguard-client-common' ;
2+ import { FirewallPolicy , AppGuardTcpInfo , AppGuardService , AuthHandler , CacheKey } from 'appguard-client-common' ;
33
44type ExpressMiddleware = (
55 req : Request ,
@@ -21,7 +21,8 @@ export const createAppGuardMiddleware = () => {
2121
2222 const attachResponseHandlers = (
2323 res : Response ,
24- tcp_info : AppGuardTcpInfo
24+ tcp_info : AppGuardTcpInfo ,
25+ cacheKey : CacheKey ,
2526 ) => {
2627 // Storing the original send function
2728 // @ts -ignore
@@ -49,10 +50,12 @@ export const createAppGuardMiddleware = () => {
4950 ) ) ;
5051
5152 if ( handleHTTPResponseResponse . policy === FirewallPolicy . DENY ) {
53+ appGuardService . insertToCache ( cacheKey , FirewallPolicy . DENY ) ;
5254 // Destroying the socket connection instead of sending the response
5355 // @ts -ignore
5456 res . socket ?. destroy ( ) ;
5557 } else {
58+ appGuardService . insertToCache ( cacheKey , FirewallPolicy . ALLOW ) ;
5659 // Intercepting the response.send() call
5760 // Calling the original send function
5861 //@ts -expect-error: This function is this context
@@ -72,6 +75,7 @@ export const createAppGuardMiddleware = () => {
7275 // @ts -ignore
7376 req . socket . remoteAddress ;
7477
78+
7579 // console.log(
7680 // // @ts -ignore
7781 // `Appguard Debug XRI:${req.headers['x-real-ip']} - XFF:${req.headers['x-forwarded-for']} TCP/PROXY:${req.socket.remoteAddress} SRC=${sourceIp}`
@@ -82,6 +86,32 @@ export const createAppGuardMiddleware = () => {
8286 // `Appguard Debug From - ${sourceIp} - ${req.method} ${req.originalUrl}`
8387 // );
8488
89+ let cacheKey : CacheKey = {
90+ // @ts -ignore
91+ originalUrl : req . originalUrl ,
92+ // @ts -ignore
93+ method : req . method ,
94+ // @ts -ignore
95+ body : req . body ,
96+ // @ts -ignore
97+ sourceIp : sourceIp ,
98+ // @ts -ignore
99+ headers : req . headers as Record < string , string > ,
100+ // @ts -ignore
101+ query : req . query as Record < string , string > ,
102+ } ;
103+
104+ let cached = appGuardService . getFromCache ( cacheKey ) ;
105+ if ( cached !== undefined ) {
106+ if ( cached === FirewallPolicy . DENY ) {
107+ // @ts -ignore
108+ res . socket ?. destroy ( ) ;
109+ } else {
110+ next ( ) ;
111+ }
112+ return ;
113+ }
114+
85115 const handleTCPConnectionResponse = await appGuardService . connectionPromise (
86116 {
87117 // @ts -ignore
@@ -122,6 +152,7 @@ export const createAppGuardMiddleware = () => {
122152
123153 const policy = handleHTTPRequestResponse . policy ;
124154 if ( policy === FirewallPolicy . DENY ) {
155+ appGuardService . insertToCache ( cacheKey , FirewallPolicy . DENY ) ;
125156 // Destroying the socket connection instead of sending the response
126157 // @ts -ignore
127158 res . socket ?. destroy ( ) ;
@@ -130,7 +161,8 @@ export const createAppGuardMiddleware = () => {
130161 // attach response handlers after we get the req.id
131162 attachResponseHandlers (
132163 res ,
133- handleTCPConnectionResponse . tcpInfo as AppGuardTcpInfo
164+ handleTCPConnectionResponse . tcpInfo as AppGuardTcpInfo ,
165+ cacheKey
134166 ) ;
135167 next ( ) ;
136168 }
0 commit comments