@@ -84,6 +84,11 @@ pub(crate) struct Opts {
84
84
#[ clap( long) ]
85
85
debug : bool ,
86
86
87
+ /// Whether or not to fetch the root key from the replica back end. Do not use this when
88
+ /// talking to the Internet Computer blockchain mainnet as it is unsecure.
89
+ #[ clap( long) ]
90
+ fetch_root_key : bool ,
91
+
87
92
/// A map of domain names to canister IDs.
88
93
/// Format: domain.name:canister-id
89
94
#[ clap( long) ]
@@ -419,13 +424,21 @@ fn not_found() -> Result<Response<Body>, Box<dyn Error>> {
419
424
. body ( "Not found" . into ( ) ) ?)
420
425
}
421
426
427
+ fn unable_to_fetch_root_key ( ) -> Result < Response < Body > , Box < dyn Error > > {
428
+ Ok ( Response :: builder ( )
429
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
430
+ . body ( "Unable to fetch root key" . into ( ) ) ?)
431
+ }
432
+
433
+ #[ allow( clippy:: too_many_arguments) ]
422
434
async fn handle_request (
423
435
ip_addr : IpAddr ,
424
436
request : Request < Body > ,
425
437
replica_url : String ,
426
438
proxy_url : Option < String > ,
427
439
dns_canister_config : Arc < DnsCanisterConfig > ,
428
440
logger : slog:: Logger ,
441
+ fetch_root_key : bool ,
429
442
debug : bool ,
430
443
) -> Result < Response < Body > , Infallible > {
431
444
let request_uri_path = request. uri ( ) . path ( ) ;
@@ -459,8 +472,11 @@ async fn handle_request(
459
472
. build ( )
460
473
. expect ( "Could not create agent..." ) ,
461
474
) ;
462
-
463
- forward_request ( request, agent, dns_canister_config. as_ref ( ) , logger. clone ( ) ) . await
475
+ if fetch_root_key && agent. fetch_root_key ( ) . await . is_err ( ) {
476
+ unable_to_fetch_root_key ( )
477
+ } else {
478
+ forward_request ( request, agent, dns_canister_config. as_ref ( ) , logger. clone ( ) ) . await
479
+ }
464
480
} {
465
481
Err ( err) => {
466
482
slog:: warn!( logger, "Internal Error during request:\n {:#?}" , err) ;
@@ -491,6 +507,7 @@ fn main() -> Result<(), Box<dyn Error>> {
491
507
let counter = AtomicUsize :: new ( 0 ) ;
492
508
let debug = opts. debug ;
493
509
let proxy_url = opts. proxy . clone ( ) ;
510
+ let fetch_root_key = opts. fetch_root_key ;
494
511
495
512
let service = make_service_fn ( |socket : & hyper:: server:: conn:: AddrStream | {
496
513
let ip_addr = socket. remote_addr ( ) ;
@@ -520,6 +537,7 @@ fn main() -> Result<(), Box<dyn Error>> {
520
537
proxy_url. clone ( ) ,
521
538
dns_canister_config,
522
539
logger,
540
+ fetch_root_key,
523
541
debug,
524
542
)
525
543
} ) )
0 commit comments