Skip to content

Commit 324b414

Browse files
feat: Added server ip check
1 parent 11f86c4 commit 324b414

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Minecraft OAuth Provider
22

33
## 🚀 Description
4-
MC-oAuth-rs is an all-in-one solution that allows users to log in to a website using their Minecraft account without entering their username or password.
4+
MC-OAuth-rs is an all-in-one solution that allows users to log in to a website using their Minecraft account without entering their username or password.
55

66
It’s secure, simple, and user-friendly, enabling logins only for users with a licensed Minecraft account.
77
The service supports Minecraft versions above 1.8.
@@ -67,26 +67,38 @@ For the server to work, create a `config.toml` file in the same directory as the
6767
[api]
6868
# API address
6969
addr = "0.0.0.0"
70+
7071
# API port
7172
port = 8008
73+
7274
# Life time of assigned code
7375
code_life_time = 300
7476

7577
[server]
7678
# Minecraft server address
7779
addr = "0.0.0.0"
80+
7881
# Minecraft server port
7982
port = 25565
83+
8084
# Server connection timeout
85+
# Sets the maximum time a client can stay connected to the server. Used to prevent idle or junk connections.
8186
timeout = 10
8287

88+
# [Optional] This setting defines the server IP to prevent proxy bypass or spoofing.
89+
server_ip = "localhost"
90+
8391
[server.config]
8492
# Minecraft server name
8593
server_name = "mc-oauth-rs"
94+
8695
# Protocol version (`0` for auto)
96+
# Used only during the server ping and is ignored when trying to connect. If set to 0, the protocol version that the client uses will be applied.
8797
protocol = 0
98+
8899
# Minecraft version string
89100
version = "1.21"
101+
90102
# Session Auth URL
91103
# `{{NAME}}` in string will be replaced by the client nickname
92104
# `{{HASH}}` will be replaced by the generated client hash
@@ -95,11 +107,15 @@ auth_url = "https://sessionserver.mojang.com/session/minecraft/hasJoined?usernam
95107
[server.status]
96108
# Server description (you can use MOTD)
97109
description = "§6mc-oauth.andcool.ru"
110+
98111
# Max players count, displayed in server list
99112
players_max = 0
113+
100114
# Online players count, displayed in server list
101115
players_online = 0
116+
102117
# Path to the server icon (can be empty)
118+
# Should be in .png format and 64x64 pixels in size
103119
icon_path = "server_icon.png"
104120

105121
[messages]
@@ -108,14 +124,14 @@ icon_path = "server_icon.png"
108124
# `{{UUID}}` will be replaced by the client UUID
109125
# `{{CODE}}` will be replaced by the generated code
110126
success = "Hello, §6{{NAME}}§r! Your code is: §a{{CODE}}"
127+
111128
# Message for Mojang API error
112129
bad_session = "§cFailed to login: Invalid session (Try restarting your game and the launcher)"
130+
131+
# Message for bad server address (`server_ip` setting)
132+
using_proxy = "§cYou are using a proxy!"
113133
```
114134

115-
> [!NOTE]
116-
> The server icon should be in `.png` format and 64x64 pixels in size.
117-
> `timeout` in the config sets the **maximum** time that the client can be connected before the server disconnects.
118-
> `protocol` is used only during the server ping and is ignored when trying to connect. If set to `0`, the protocol version that the client uses will be applied.
119135

120136
### 🚀 Running
121137
After configuring, run the compiled binary file through the console.

config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ code_life_time = 300
77
addr = "0.0.0.0"
88
port = 25566
99
timeout = 20
10+
server_ip = "localhost"
1011

1112
[server.config]
1213
server_name = "mc-oauth-rs"
@@ -23,3 +24,4 @@ icon_path = "server_icon.png"
2324
[messages]
2425
success = "Hello, §6{{NAME}}§r! Your code is: §a{{CODE}}"
2526
bad_session = "§cFailed to login: Invalid session (Try restarting your game and the launcher)"
27+
using_proxy = "§cYou are using a proxy!"

src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl MinecraftClient {
133133
handle_login_start(&mut self.session, &mut self.buffer)?;
134134
send_encryption(&mut self.stream, self.keys.clone(), &mut self.session).await?;
135135
}
136-
NextStateEnum::Unknown => handle_handshake(&mut self.session, &mut self.buffer)?, // Handle handshake
136+
NextStateEnum::Unknown => handle_handshake(self).await?, // Handle handshake
137137
}
138138

139139
Ok(())

src/config/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub struct Server {
4444

4545
/// Server list ping config
4646
pub status: ServerStatus,
47+
48+
/// Proxy address (optional)
49+
pub server_ip: Option<String>,
4750
}
4851

4952
#[derive(Deserialize, Debug)]
@@ -88,4 +91,7 @@ pub struct Messages {
8891

8992
/// Message for Mojang API error
9093
pub bad_session: String,
94+
95+
/// Using a proxy
96+
pub using_proxy: String,
9197
}

src/handlers/handshake.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1+
use anyhow::{Error, Result};
2+
13
use crate::{
2-
client::{NextStateEnum, Session},
4+
client::{MinecraftClient, NextStateEnum},
35
packets::handshake::HandshakePacket,
6+
responses::disconnect::send_disconnect,
47
};
5-
use bytes::BytesMut;
6-
use std::io;
78

8-
pub fn handle_handshake(session: &mut Session, buff: &mut BytesMut) -> Result<(), io::Error> {
9-
let handshake = HandshakePacket::parse(buff)?;
9+
pub async fn handle_handshake(client: &mut MinecraftClient) -> Result<()> {
10+
let handshake = HandshakePacket::parse(&mut client.buffer)?;
1011

11-
session.next_state = match handshake.next_state {
12+
client.session.next_state = match handshake.next_state {
1213
1 => NextStateEnum::Status,
1314
2 => NextStateEnum::Login,
1415
_ => NextStateEnum::Unknown,
1516
};
1617

17-
session.proto_ver = Some(handshake.proto_ver);
18+
client.session.proto_ver = Some(handshake.proto_ver);
19+
if let Some(server_ip) = &client.config.server.server_ip {
20+
if server_ip.ne(&handshake.server_addr) {
21+
send_disconnect(
22+
&mut client.stream,
23+
&mut client.session,
24+
client.config.messages.using_proxy.clone(),
25+
)
26+
.await?;
27+
return Err(Error::msg("Client using a proxy!"));
28+
}
29+
}
1830

1931
Ok(())
2032
}

0 commit comments

Comments
 (0)