@@ -21,6 +21,7 @@ import CtxRQNamespace from "./helpers/ctx_rq_namespace";
2121import { bodyParser , getContentType } from "./helpers/http_helpers" ;
2222import { RQ_INTERCEPTED_CONTENT_TYPES } from "./constants" ;
2323import { CONSTANTS as GLOBAL_CONSTANTS } from "@requestly/requestly-core" ;
24+ import { dataToServeUnreachablePage , isAddressUnreachableError } from "./helpers/handleUnreachableAddress" ;
2425// import SSLProxyingConfigFetcher from "renderer/lib/fetcher/ssl-proxying-config-fetcher";
2526// import SSLProxyingManager from "../ssl-proxying/ssl-proxying-manager";
2627
@@ -142,7 +143,7 @@ class ProxyMiddlewareManager {
142143 // Only modify response if any modify_response action is applied
143144 const modifyResponseActionExist = action_result_objs . some ( ( action_result_obj ) => action_result_obj ?. action ?. action === "modify_response" )
144145
145- if ( modifyResponseActionExist ) {
146+ if ( modifyResponseActionExist ) {
146147 const statusCode = ctx . rq_response_status_code || 404 ;
147148 const responseHeaders = getResponseHeaders ( ctx ) || { }
148149 ctx . proxyToClientResponse . writeHead (
@@ -163,9 +164,39 @@ class ProxyMiddlewareManager {
163164 rules_middleware . action_result_objs ,
164165 GLOBAL_CONSTANTS . REQUEST_STATE . COMPLETE
165166 )
166- } else {
167- console . log ( "Expected Error after early termination of request: " , err ) ;
168- }
167+ }
168+ } else if ( kind === "PROXY_TO_SERVER_REQUEST_ERROR" ) {
169+ try {
170+ const host = get_request_url ( ctx ) ;
171+ const isAddressUnreachable = await isAddressUnreachableError ( host ) ;
172+ if ( isAddressUnreachable ) {
173+ const { status, contentType, body } = dataToServeUnreachablePage ( host ) ;
174+ ctx . proxyToClientResponse . writeHead (
175+ status ,
176+ http . STATUS_CODES [ status ] ,
177+ {
178+ "Content-Type" : contentType ,
179+ "x-rq-error" : "ERR_NAME_NOT_RESOLVED"
180+ }
181+ ) ;
182+ ctx . proxyToClientResponse . end ( body ) ;
183+ ctx . rq . set_final_response ( {
184+ status_code : status ,
185+ headers : { "Content-Type" : contentType } ,
186+ body : body ,
187+ } ) ;
188+ logger_middleware . send_network_log (
189+ ctx ,
190+ rules_middleware . action_result_objs ,
191+ GLOBAL_CONSTANTS . REQUEST_STATE . COMPLETE
192+ ) ;
193+ return ;
194+ }
195+ } catch ( error ) {
196+ console . error ( "Error checking address:" , error ) ;
197+ }
198+ } else {
199+ console . log ( "Expected Error after early termination of request: " , err ) ;
169200 }
170201
171202 return callback ( ) ;
0 commit comments