Skip to content

Commit 6df1ad5

Browse files
committed
httphook: Allow specifying requires_login info field
1 parent 3d110b9 commit 6df1ad5

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

httphook-ldpreload/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ registration process:
4545
- `LIBTW2_HTTPHOOK_LOG` (default: `info`): Specify log level of the library.
4646
Examples: `debug`, `error`. [Documentation of the
4747
syntax](https://docs.rs/env_logger/0.3.5/env_logger/#enabling-logging).
48+
- `LIBTW2_HTTPHOOK_OVERRIDE_REQUIRES_LOGIN` (unset by default): Populate the
49+
`"requires_login"` field in the server info with this. Possible values:
50+
`true`, `false`.
4851
- `LIBTW2_HTTPHOOK_REGISTER_URL` (default:
4952
`https://master1.ddnet.org/ddnet/15/register`): Contact the mastersrv given
5053
by this URL. Example: `http://localhost:8080/ddnet/15/register` for local testing.

httphook/src/json.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@ use serde_derive::Serialize;
33

44
#[derive(Serialize)]
55
pub struct Server {
6-
max_clients: i32,
7-
max_players: i32,
8-
passworded: bool,
9-
game_type: String,
10-
name: String,
11-
map: Map,
12-
version: String,
13-
clients: Vec<Client>,
6+
pub max_clients: i32,
7+
pub max_players: i32,
8+
pub passworded: bool,
9+
pub game_type: String,
10+
pub name: String,
11+
pub map: Map,
12+
pub version: String,
13+
#[serde(skip_serializing_if = "Option::is_none")]
14+
pub requires_login: Option<bool>,
15+
pub clients: Vec<Client>,
1416
}
1517

1618
#[derive(Serialize)]
1719
pub struct Map {
18-
name: String,
20+
pub name: String,
1921
#[serde(skip_serializing_if = "Option::is_none")]
20-
tw_crc: Option<String>,
22+
pub tw_crc: Option<String>,
2123
#[serde(skip_serializing_if = "Option::is_none")]
22-
size: Option<u32>,
24+
pub size: Option<u32>,
2325
}
2426

2527
#[derive(Serialize)]
2628
pub struct Client {
27-
name: String,
28-
clan: String,
29-
country: i32,
30-
score: i32,
31-
is_player: bool,
29+
pub name: String,
30+
pub clan: String,
31+
pub country: i32,
32+
pub score: i32,
33+
pub is_player: bool,
3234
}
3335

3436
impl From<&browse_protocol::ServerInfo> for Server {
@@ -45,6 +47,7 @@ impl From<&browse_protocol::ServerInfo> for Server {
4547
size: info.map_size,
4648
},
4749
version: (&*info.version).into(),
50+
requires_login: None,
4851
clients: info.clients.iter().map(Client::from).collect(),
4952
}
5053
}

httphook/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod runtime;
3131
struct Config {
3232
community_token: Option<Box<str>>,
3333
log: Option<Box<str>>,
34+
override_requires_login: Option<bool>,
3435
register_url: Option<Box<str>>,
3536
}
3637

@@ -177,6 +178,12 @@ fn build_register(port: u16, info: Arc<str>) -> Register {
177178
builder.build(port, info.into())
178179
}
179180

181+
fn apply_overrides(mut info: json::Server) -> json::Server {
182+
let config = &config();
183+
info.requires_login = config.override_requires_login.or(info.requires_login);
184+
info
185+
}
186+
180187
async fn register_server_6_impl(port: u16, register: Arc<OnceLock<Register>>) {
181188
let socket = UdpSocket::bind("0.0.0.0:0").await.unwrap();
182189
const LOCALHOST: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
@@ -197,7 +204,7 @@ async fn register_server_6_impl(port: u16, register: Arc<OnceLock<Register>>) {
197204
Ok(info) => info,
198205
Err(_) => continue,
199206
};
200-
let info = serde_json::to_string(&json::Server::from(&info)).unwrap();
207+
let info = serde_json::to_string(&apply_overrides(json::Server::from(&info))).unwrap();
201208

202209
if let Some(register) = register.get() {
203210
register.on_new_info(info.into());

0 commit comments

Comments
 (0)