diff --git a/config/sys.config.src b/config/sys.config.src index be47df743..68ef969a8 100644 --- a/config/sys.config.src +++ b/config/sys.config.src @@ -68,7 +68,8 @@ {devaddr_allocate_resolution, "${ROUTER_DEVADDR_ALLOCATE_RESOLUTION}"}, {metrics_port, "${ROUTER_METRICS_PORT}"}, {denylist_keys, ["1SbEYKju337P6aYsRd9DT2k4qgK5ZK62kXbSvnJgqeaxK3hqQrYURZjL"]}, - {denylist_url, "https://api.github.com/repos/helium/denylist/releases/latest"} + {denylist_url, "https://api.github.com/repos/helium/denylist/releases/latest"}, + {charge_joins, "${ROUTER_CHARGE_JOINS}"} ]}, {grpcbox, [ {client, #{ diff --git a/src/device/router_device_worker.erl b/src/device/router_device_worker.erl index a8c413f64..1021138b0 100644 --- a/src/device/router_device_worker.erl +++ b/src/device/router_device_worker.erl @@ -1265,30 +1265,50 @@ validate_join( #packet_pb{ payload = <> + DevNonce:2/binary, _MIC:4/binary>> = Payload } = Packet, PubKeyBin, Region, APIDevice, AppKey, Device, - _OfferCache + OfferCache ) when MType == ?JOIN_REQ -> case lists:member(DevNonce, router_device:dev_nonces(Device)) of true -> {error, bad_nonce}; false -> - OrgID = maps:get(organization_id, router_device:metadata(Device), undefined), - {Balance, Nonce} = router_console_dc_tracker:current_balance(OrgID), - {ok, UpdatedDevice, JoinAcceptArgs} = handle_join( - Packet, - PubKeyBin, - Region, - APIDevice, - AppKey, - Device - ), - {ok, UpdatedDevice, DevNonce, JoinAcceptArgs, {Balance, Nonce}} + case router_utils:get_env_bool(charge_joins, true) of + true -> + PayloadSize = erlang:byte_size(Payload), + PHash = blockchain_helium_packet_v1:packet_hash(Packet), + case maybe_charge(Device, PayloadSize, PubKeyBin, PHash, OfferCache) of + {error, _} = Error -> + Error; + {ok, Balance, Nonce} -> + {ok, UpdatedDevice, JoinAcceptArgs} = handle_join( + Packet, + PubKeyBin, + Region, + APIDevice, + AppKey, + Device + ), + {ok, UpdatedDevice, DevNonce, JoinAcceptArgs, {Balance, Nonce}} + end; + false -> + OrgID = maps:get(organization_id, router_device:metadata(Device), undefined), + {Balance, Nonce} = router_console_dc_tracker:current_balance(OrgID), + {ok, UpdatedDevice, JoinAcceptArgs} = handle_join( + Packet, + PubKeyBin, + Region, + APIDevice, + AppKey, + Device + ), + {ok, UpdatedDevice, DevNonce, JoinAcceptArgs, {Balance, Nonce}} + end end; validate_join( _Packet,