Skip to content

Commit

Permalink
Add ban related APIs
Browse files Browse the repository at this point in the history
Add ban related APIs. To get the ID to ban a player we use the
`GetPlayers` API in the net service.

Although these ban APIs are `net` APIs they are not available in the
MSE, only the Hook environment, hence their being placed in the
corresponding namespace.
  • Loading branch information
rurounijones committed Apr 19, 2022
1 parent 5afd243 commit 90c9293
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `IsMultiplayer` API
- `IsServer` API
- `GetMissionDescription` API
- `BanPlayer` API
- `GetBannedPlayers` API
- `UnbanPlayer` API

### Changed
- Replaced `groupName` field in the `GroupCommand` event with all the group details as exposed by the group exporter
Expand Down
5 changes: 2 additions & 3 deletions STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ should use their independent logging and tracing functions.
- [ ] <del>`trace`</del> (client applications should use their own logging)

#### GameGUI API
- [ ] `load_mission`
- [ ] `load_next_mission`

- [ ] `load_mission`
- [ ] `load_next_mission`

### Timer Service
- [x] `getTime`
Expand Down
35 changes: 35 additions & 0 deletions lua/DCS-gRPC/methods/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,39 @@ end

GRPC.methods.isServer = function()
return GRPC.success({server = DCS.isServer()})
end

GRPC.methods.banPlayer = function(params)
if params.id == 1 then
return GRPC.errorInvalidArgument("Cannot ban the server user")
end

local player_id = net.get_player_info(params.id, "id")

if not player_id then
return GRPC.errorNotFound("Could not find player with the ID of " .. params.id)
end

return GRPC.success({banned = net.banlist_add(params.id, params.period, params.reason)})
end

GRPC.methods.unbanPlayer = function(params)
return GRPC.success({unbanned = net.banlist_remove(params.ucid)})
end

GRPC.methods.getBannedPlayers = function()
local result = {}

for i, detail in ipairs(net.banlist_get()) do
result[i] = {
ucid = detail.ucid,
ipAddress = detail.ipaddr,
playerName = detail.name,
reason = detail.reason,
bannedFrom = detail.banned_from,
bannedUntil = detail.banned_until
}
end

return GRPC.success({bans = result})
end
56 changes: 56 additions & 0 deletions protos/dcs/hook/v0/hook.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ service HookService {

// https://wiki.hoggitworld.com/view/DCS_func_isServer
rpc IsServer(IsServerRequest) returns (IsServerResponse) {}

// Bans a player that is currently connected to the server
rpc BanPlayer(BanPlayerRequest) returns (BanPlayerResponse) {}

// Unbans a player via their globally unique ID
rpc UnbanPlayer(UnbanPlayerRequest) returns (UnbanPlayerResponse) {}

// Get a list of all the banned players
rpc GetBannedPlayers(GetBannedPlayersRequest)
returns (GetBannedPlayersResponse) {}
}

message GetMissionNameRequest {
Expand Down Expand Up @@ -107,3 +117,49 @@ message IsServerRequest {
message IsServerResponse {
bool server = 1;
}

message BanPlayerRequest {
// The session ID of the player
uint32 id = 1;
// The period of the ban in seconds
uint32 period = 2;
// The reason for the ban
string reason = 3;
}

message BanPlayerResponse {
// Was the player successfully banned
bool banned = 1;
}

message UnbanPlayerRequest {
// The globally unique ID of the player
string ucid = 1;
}

message UnbanPlayerResponse {
// Was the player successfully unbanned
bool unbanned = 1;
}

message GetBannedPlayersRequest {
}

message GetBannedPlayersResponse {
repeated BanDetails bans = 1;
}

message BanDetails {
// The globally unique ID of the player
string ucid = 1;
// The IP address the user had when they were banned
string ip_address = 2;
// The Name of the player at the time of the ban
string player_name = 3;
// The reason given for the ban
string reason = 4;
// When the ban was issued in unixtime
uint64 banned_from = 5;
// When the ban will expire in unixtime
uint64 banned_until = 6;
}
25 changes: 25 additions & 0 deletions src/rpc/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,29 @@ impl HookService for HookRpc {
let res: hook::v0::IsServerResponse = self.request("isServer", request).await?;
Ok(Response::new(res))
}

async fn ban_player(
&self,
request: Request<hook::v0::BanPlayerRequest>,
) -> Result<Response<hook::v0::BanPlayerResponse>, Status> {
let res: hook::v0::BanPlayerResponse = self.request("banPlayer", request).await?;
Ok(Response::new(res))
}

async fn unban_player(
&self,
request: Request<hook::v0::UnbanPlayerRequest>,
) -> Result<Response<hook::v0::UnbanPlayerResponse>, Status> {
let res: hook::v0::UnbanPlayerResponse = self.request("unbanPlayer", request).await?;
Ok(Response::new(res))
}

async fn get_banned_players(
&self,
request: Request<hook::v0::GetBannedPlayersRequest>,
) -> Result<Response<hook::v0::GetBannedPlayersResponse>, Status> {
let res: hook::v0::GetBannedPlayersResponse =
self.request("getBannedPlayers", request).await?;
Ok(Response::new(res))
}
}

0 comments on commit 90c9293

Please sign in to comment.