diff --git a/build.nims b/build.nims index 6318e459..2f46a6ab 100644 --- a/build.nims +++ b/build.nims @@ -72,7 +72,9 @@ task testStorage, "Build & run Logos Storage tests": task testIntegration, "Run integration tests": buildBinary "storage", outName = "storage", - params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE" + params = + "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE " & + "-d:storage_enable_nat_simulation=true" test "testIntegration" # use params to enable logging from the integration test executable # test "testIntegration", params = "-d:chronicles_sinks=textlines[notimestamps,stdout],textlines[dynamic] " & diff --git a/storage/conf.nim b/storage/conf.nim index 5af6c497..af23bd0d 100644 --- a/storage/conf.nim +++ b/storage/conf.nim @@ -73,6 +73,7 @@ proc defaultDataDir*(): string = const storage_enable_api_debug_peers* {.booldefine.} = false storage_enable_log_counter* {.booldefine.} = false + storage_enable_nat_simulation* {.booldefine.} = false DefaultThreadCount* = ThreadCount(0) diff --git a/storage/rest/api.nim b/storage/rest/api.nim index e9a14007..91edef58 100644 --- a/storage/rest/api.nim +++ b/storage/rest/api.nim @@ -629,26 +629,27 @@ proc initDebugApi( trace "Excepting processing request", exc = exc.msg return RestApiResponse.error(Http500, headers = headers) - router.api(MethodPost, "/api/storage/v1/debug/nat/filtering") do( - filtering: Option[string] - ) -> RestApiResponse: - var headers = buildCorsHeaders("POST", allowedOrigin) + when storage_enable_nat_simulation: + router.api(MethodPost, "/api/storage/v1/debug/nat/filtering") do( + filtering: Option[string] + ) -> RestApiResponse: + var headers = buildCorsHeaders("POST", allowedOrigin) - without natSimulation =? natRouter: - return RestApiResponse.error( - Http400, "NAT simulation not active on this node", headers = headers - ) + without natSimulation =? natRouter: + return RestApiResponse.error( + Http400, "NAT simulation not active on this node", headers = headers + ) - without res =? filtering and filtering =? res: - return - RestApiResponse.error(Http400, "Missing filtering value", headers = headers) + without res =? filtering and filtering =? res: + return + RestApiResponse.error(Http400, "Missing filtering value", headers = headers) - let behavior = FilteringBehavior.fromString(filtering).valueOr: - return - RestApiResponse.error(Http400, "Invalid filtering value", headers = headers) + let behavior = FilteringBehavior.fromString(filtering).valueOr: + return + RestApiResponse.error(Http400, "Invalid filtering value", headers = headers) - natSimulation.setFiltering(behavior) - return RestApiResponse.response("", headers = headers) + natSimulation.setFiltering(behavior) + return RestApiResponse.response("", headers = headers) when storage_enable_api_debug_peers: router.api(MethodGet, "/api/storage/v1/debug/peer/{peerId}") do( diff --git a/storage/storage.nim b/storage/storage.nim index 08d3f71f..ac32f0f2 100644 --- a/storage/storage.nim +++ b/storage/storage.nim @@ -317,17 +317,27 @@ proc new*( var natRouter: Option[NatRouter] let switch = - if config.natSimulation.isSome: - # Provide a NAT simulation useful for testing NAT Traversal - let filtering = FilteringBehavior.fromString(config.natSimulation.get).valueOr( - AddressAndPortDependent - ) - let router = NatRouter.new(filtering) - natRouter = some(router) - switchBuilder - .withNatTransport(router, {ServerFlags.ReuseAddr, ServerFlags.TcpNoDelay}) - .build() + when storage_enable_nat_simulation: + if config.natSimulation.isSome: + # Provide a NAT simulation useful for testing NAT Traversal + let filtering = FilteringBehavior.fromString(config.natSimulation.get).valueOr( + AddressAndPortDependent + ) + let router = NatRouter.new(filtering) + natRouter = some(router) + switchBuilder + .withNatTransport(router, {ServerFlags.ReuseAddr, ServerFlags.TcpNoDelay}) + .build() + else: + switchBuilder + .withTcpTransport({ServerFlags.ReuseAddr, ServerFlags.TcpNoDelay}) + .build() else: + if config.natSimulation.isSome: + raise newException( + StorageError, + "--nat-simulation requires a build with -d:storage_enable_nat_simulation=true", + ) switchBuilder .withTcpTransport({ServerFlags.ReuseAddr, ServerFlags.TcpNoDelay}) .build()