A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
chore(gateway): session handling updates (#3853)
Hampus1e5a6112f0bb
diff --git a/fluxer_docs/src/content/docs/operator/environment.mdx b/fluxer_docs/src/content/docs/operator/environment.mdxindex b71bb2205..b4f2c7ef0 100644--- a/fluxer_docs/src/content/docs/operator/environment.mdx+++ b/fluxer_docs/src/content/docs/operator/environment.mdx@@ -1072,6 +1072,10 @@ How many NATS RPC requests the gateway works on at once. When it's empty, the gaThe base 2 logarithm of the zstd window the gateway compresses websocket traffic with, from 10 to 21. Values outside that range count as the nearest end. Larger values compress better and use more memory per connection. When it's empty, the gateway uses `17`. With rexctl, use `rexctl config set FLUXER_GATEWAY_ZSTD_WINDOW_LOG`.+### `FLUXER_GATEWAY_IDENTIFY_TOKEN_LIMIT_PER_MINUTE`++The most times one token can identify on one gateway node in a minute. The gateway does not answer identifies over the limit. A value of `0` turns off this limit. When it's empty, the gateway uses `0`. With rexctl, use `rexctl config set FLUXER_GATEWAY_IDENTIFY_TOKEN_LIMIT_PER_MINUTE`.+### `FLUXER_GATEWAY_HTTP_FAILURE_THRESHOLD`How many failed requests from the gateway to the API in 10 seconds open its circuit breaker, when at least 80 percent of the requests in that time failed. The breaker then stops further requests for a while. When it's empty, the gateway uses `6`. With rexctl, use `rexctl config set FLUXER_GATEWAY_HTTP_FAILURE_THRESHOLD`.diff --git a/fluxer_gateway/src/gateway/fluxer_gateway_config.erl b/fluxer_gateway/src/gateway/fluxer_gateway_config.erlindex 71295320e..b3f190a2e 100644--- a/fluxer_gateway/src/gateway/fluxer_gateway_config.erl+++ b/fluxer_gateway/src/gateway/fluxer_gateway_config.erl@@ -75,6 +75,9 @@ env_services_config() ->env_gateway_config() ->maps:merge(env_gateway_base_config(), #{<<"zstd_window_log">> => env_int("FLUXER_GATEWAY_ZSTD_WINDOW_LOG", 17),+ <<"identify_token_limit_per_minute">> => env_int(+ "FLUXER_GATEWAY_IDENTIFY_TOKEN_LIMIT_PER_MINUTE", 0+ ),<<"guild_visitor_sessions_max">> => env_int("FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_MAX",visitor_limit_default(guild_visitor_sessions_max)@@ -350,6 +353,8 @@ build_misc_config(Service) ->voice_state_counts_sync_interval_ms =>get_int(Service, <<"voice_state_counts_sync_interval_ms">>, 30000),zstd_window_log => get_int(Service, <<"zstd_window_log">>, 17),+ identify_token_limit_per_minute =>+ get_int(Service, <<"identify_token_limit_per_minute">>, 0),logger_level => get_log_level(Service, <<"logger_level">>, info)}.diff --git a/fluxer_gateway/src/gateway/gateway_handler_heartbeat.erl b/fluxer_gateway/src/gateway/gateway_handler_heartbeat.erlindex 1d0f4f28e..8666689f0 100644--- a/fluxer_gateway/src/gateway/gateway_handler_heartbeat.erl+++ b/fluxer_gateway/src/gateway/gateway_handler_heartbeat.erl@@ -94,6 +94,7 @@ verify_heartbeat_ack(SeqNum, Pid) when is_integer(SeqNum), is_pid(Pid) ->ok -> true;_ -> falsecatch+ exit:{timeout, _} -> true;exit:_ -> falseend;verify_heartbeat_ack(_, _) ->diff --git a/fluxer_gateway/src/gateway/gateway_handler_identify.erl b/fluxer_gateway/src/gateway/gateway_handler_identify.erlindex f3099a0ff..6d0e83125 100644--- a/fluxer_gateway/src/gateway/gateway_handler_identify.erl+++ b/fluxer_gateway/src/gateway/gateway_handler_identify.erl@@ -48,7 +48,7 @@ handle_identify_validated(Data, PeerIP, State) ->),case ValidateResult of{ok, Token, Properties, Presence, IgnoredEvents, Flags, InitialGuildId, Shard} ->- do_identify(+ identify_within_token_limit(Token,Properties,Presence,@@ -74,6 +74,48 @@ handle_identify_validated(Data, PeerIP, State) ->)end.+-spec identify_within_token_limit(+ binary(),+ map(),+ term(),+ list(),+ non_neg_integer(),+ integer() | undefined,+ gateway_sharding:shard() | undefined,+ binary(),+ gateway_timings:recorder(),+ state()+) -> ws_result().+identify_within_token_limit(+ Token,+ Properties,+ Presence,+ IgnoredEvents,+ Flags,+ InitialGuildId,+ Shard,+ PeerIP,+ GwTimings,+ State+) ->+ case session_abuse_protection:check_token_identify_rate(Token) of+ {error, identify_rate_limited} ->+ hold_identify(<<"Identify rate limited">>, State);+ ok ->+ do_identify(+ Token,+ Properties,+ Presence,+ IgnoredEvents,+ Flags,+ InitialGuildId,+ Shard,+ PeerIP,+ GwTimings,+ State+ )+ end.+-spec do_identify(binary(),map(),diff --git a/fluxer_gateway/src/gateway/session_abuse_protection.erl b/fluxer_gateway/src/gateway/session_abuse_protection.erlindex 4b71d54d6..c330a37b9 100644--- a/fluxer_gateway/src/gateway/session_abuse_protection.erl+++ b/fluxer_gateway/src/gateway/session_abuse_protection.erl@@ -7,7 +7,8 @@check_user_session_limit/1,increment_user_sessions/1,decrement_user_sessions/1,- check_identify_rate/1+ check_identify_rate/1,+ check_token_identify_rate/1]).-define(IDENTIFY_TABLE, gateway_identify_rate).@@ -81,6 +82,48 @@ check_identify_rate(PeerIP) when is_binary(PeerIP) ->check_identify_rate(_) ->ok.+-spec check_token_identify_rate(term()) -> ok | {error, identify_rate_limited}.+check_token_identify_rate(Token) when is_binary(Token) ->+ check_token_identify_rate(Token, token_identify_limit());+check_token_identify_rate(_) ->+ ok.++-spec check_token_identify_rate(binary(), non_neg_integer()) ->+ ok | {error, identify_rate_limited}.+check_token_identify_rate(_Token, 0) ->+ ok;+check_token_identify_rate(Token, Limit) ->+ case gateway_handler_rate_limit:rate_limits_disabled() of+ true ->+ ok;+ false ->+ ok = ensure_identify_table(),+ do_check_token_identify_rate(Token, Limit)+ end.++-spec token_identify_limit() -> non_neg_integer().+token_identify_limit() ->+ case fluxer_gateway_env:get_optional(identify_token_limit_per_minute) of+ Limit when is_integer(Limit), Limit > 0 -> Limit;+ _ -> 0+ end.++-spec do_check_token_identify_rate(binary(), pos_integer()) ->+ ok | {error, identify_rate_limited}.+do_check_token_identify_rate(Token, Limit) ->+ Hash = binary:part(crypto:hash(sha256, Token), 0, 16),+ Bucket = erlang:system_time(second) div ?IDENTIFY_WINDOW_SECS,+ Key = {{identify_token, Hash}, Bucket},+ try ets:update_counter(?IDENTIFY_TABLE, Key, {2, 1}, {Key, 0}) of+ Count when Count > Limit ->+ {error, identify_rate_limited};+ _ ->+ ok+ catch+ error:badarg ->+ ok+ end.+-spec do_check_identify_rate(binary()) -> ok | {error, identify_rate_limited}.do_check_identify_rate(PeerIP) ->Now = erlang:system_time(second),diff --git a/fluxer_rexctl/internal/instance/envvars.go b/fluxer_rexctl/internal/instance/envvars.goindex 3b080549e..8350e29ba 100644--- a/fluxer_rexctl/internal/instance/envvars.go+++ b/fluxer_rexctl/internal/instance/envvars.go@@ -285,6 +285,7 @@ var envVars = []envVar{{"FLUXER_GATEWAY_HTTP_RPC_MAX_CONCURRENCY", "gateway", "How many HTTP requests the gateway can have open to the API at once. When it's empty, the gateway uses `512`."},{"FLUXER_GATEWAY_NATS_RPC_MAX_HANDLERS", "gateway", "How many NATS RPC requests the gateway works on at once. When it's empty, the gateway uses `512`."},{"FLUXER_GATEWAY_ZSTD_WINDOW_LOG", "gateway", "The base 2 logarithm of the zstd window the gateway compresses websocket traffic with, from 10 to 21. Values outside that range count as the nearest end. Larger values compress better and use more memory per connection. When it's empty, the gateway uses `17`."},+ {"FLUXER_GATEWAY_IDENTIFY_TOKEN_LIMIT_PER_MINUTE", "gateway", "The most times one token can identify on one gateway node in a minute. The gateway does not answer identifies over the limit. A value of `0` turns off this limit. When it's empty, the gateway uses `0`."},{"FLUXER_GATEWAY_HTTP_FAILURE_THRESHOLD", "gateway", "How many failed requests from the gateway to the API in 10 seconds open its circuit breaker, when at least 80 percent of the requests in that time failed. The breaker then stops further requests for a while. When it's empty, the gateway uses `6`."},{"FLUXER_GATEWAY_HTTP_RECOVERY_TIMEOUT_MS", "gateway", "How long the circuit breaker for gateway requests to the API stays open before it tries again, in milliseconds. When it's empty, the gateway uses `15000`."},{"FLUXER_GATEWAY_GUILD_VISITOR_SESSIONS_MAX", "gateway", "The most visitor sessions one community allows, however many members it has. A visitor views a community without joining it. When it's empty, the gateway uses `10000`."},diff --git a/fluxer_rexctl/internal/stack/files/.env.example b/fluxer_rexctl/internal/stack/files/.env.exampleindex b200f48f9..3a71251d0 100644--- a/fluxer_rexctl/internal/stack/files/.env.example+++ b/fluxer_rexctl/internal/stack/files/.env.example@@ -1551,6 +1551,12 @@ FLUXER_VAPID_PRIVATE_KEY=# rexctl: rexctl config set FLUXER_GATEWAY_ZSTD_WINDOW_LOG# FLUXER_GATEWAY_ZSTD_WINDOW_LOG=+# The most times one token can identify on one gateway node in a minute. The+# gateway does not answer identifies over the limit. A value of 0 turns off+# this limit. When it's empty, the gateway uses 0.+# rexctl: rexctl config set FLUXER_GATEWAY_IDENTIFY_TOKEN_LIMIT_PER_MINUTE+# FLUXER_GATEWAY_IDENTIFY_TOKEN_LIMIT_PER_MINUTE=+# How many failed requests from the gateway to the API in 10 seconds open its# circuit breaker, when at least 80 percent of the requests in that time# failed. The breaker then stops further requests for a while. When it's