Going further
Accounts and proxies
This page covers who your bot logs in as and how its connection reaches the server: offline
accounts, Microsoft accounts for online-mode servers, and SOCKS5 proxies with a routing policy.
It all lives in ghast-auth, re-exported as ghast::auth and ghast::connect.
You are responsible for following the rules of the servers you connect to, including whether they allow bots at all.
Offline accounts
An offline account is just a name. Account::offline checks it the way vanilla does (1 to 16
characters of [A-Za-z0-9_]) and derives the UUID that vanilla and Bukkit / Paper servers
assign to an offline player:
let account = offline?;
println!;
An offline account works on servers with online-mode=false. If the server asks for
authentication, the login fails with ClientError::OnlineModeRequired. A 26.x encryption
request that does not ask for a session-server join is answered with an offline account too,
as vanilla does.
Microsoft accounts
Online-mode servers need a Microsoft account. Turn on the microsoft feature, which pulls in
an HTTPS client:
= { = "0.5", = ["microsoft"] }
MicrosoftAuth::authenticate runs the same chain the vanilla launcher does:
- Microsoft OAuth with the device-code flow: the user opens a URL, types a short code and signs in, while the client polls for an access token and a refresh token.
- Xbox Live user authentication, then XSTS authorization for Minecraft services.
- Minecraft services
login_with_xbox, which returns the Minecraft access token. - The Minecraft profile (UUID and name). An account that does not own the game has none.
use Arc;
use ;
use ;
let auth = new?;
let cache = new;
let session = auth
.authenticate
.await?;
let account = Microsoft ;
let client = new;
ghast ships no OAuth client id, so you supply one. MicrosoftConfig::azure_ad takes your own
Azure app registration; Mojang must approve new registrations for the Minecraft API, or
login_with_xbox fails with MicrosoftError::AppNotPermitted. MicrosoftConfig::live takes a
client id registered with the older login.live.com endpoint.
The token cache
TokenCache is one JSON file per account, at a path you choose. authenticate reuses the
cached Minecraft token if it is valid for at least five more minutes, otherwise refreshes the
Microsoft token, and only runs the device-code flow if that fails. The result is written back.
A rejected refresh token falls back to a new sign-in; network errors and rate limits are
returned instead, so a short outage does not ask the user to sign in again.
Writes are atomic. On Unix the file is created with mode 0600 and a missing parent directory
with 0700; on other platforms it inherits the directory's permissions, so pick a private
directory. Tokens and proxy passwords are SecretStrings, which Debug and Display never
print.
What happens at login
On an online-mode server's encryption request, the client makes a shared secret, encrypts it
with the server's RSA key, calls the session server's join with the account's token, and turns
on AES/CFB8 encryption both ways. On 1.19.3+ protocols (26.x here), the client also fetches the account's profile key
pair and signs chat messages and signable command arguments; Client::signed_chat(false)
turns that off. The key exchange and chat signing are checked byte for byte against the real
26.1 client. The online path is lab-tested against a local stand-in of the account services,
not with a real account; see Accuracy.
SOCKS5 proxies
A Connector opens the TCP connection, directly or through a SOCKS5 proxy, and you hand it to
the client with Client::connector. Without one, Client::new connects directly.
use ;
let proxy: Socks5Proxy = "socks5h://user:[email protected]:1080".parse?;
let connector = new
.with_proxy
.with_policy;
let target: TargetAddr = "mc.example.net:25565".parse?;
let client = new.connector;
The proxy client speaks RFC 1928 with no authentication or RFC 1929 username / password. The URL may use socks5h:// or socks5://; both behave the same, because
ghast always sends domain names to the proxy and lets it resolve them. A proxied target never
causes a local DNS query. Only the proxy's own host name and targets of direct connections are
resolved locally. Percent-encoding in the user name and password is decoded, and the port
defaults to 1080.
ghast does not look up _minecraft._tcp SRV records: they cannot be resolved through SOCKS5,
and resolving them locally would leak the name. Give the real host and port.
Default timeouts are 10 s for local DNS, 10 s per TCP connect and 15 s for the SOCKS5
handshake (with_timeouts). A connector is cheap to clone; one can serve many bots.
Proxy policy
A ProxyPolicy decides per target whether the proxy must, may or must not be used:
| policy | what it does |
|---|---|
PreferProxy | Uses the proxy if one is set, otherwise connects directly. The default of Connector::new. |
RequireProxy | Every connection must use the proxy. With no proxy set, it fails with ConnectError::ProxyRequired and nothing is sent. |
RequireProxyExceptLocal | Local targets connect directly, every other target must use the proxy. |
Custom | Your closure returns a Route (Proxy, PreferProxy or Direct) per target. |
A target counts as local for loopback, private IPv4 ranges, link-local and IPv6 unique-local
addresses, and the name localhost. Other domain names are never treated as local, because that
would need a local DNS lookup. CGNAT addresses (100.64.0.0/10) are not local either.
The examples' --policy flag maps onto these: except-local (the default) is
RequireProxyExceptLocal, require is RequireProxy, prefer is PreferProxy, and direct
is a custom policy that always returns Route::Direct, ignoring any --proxy:
Connector::plan(&target) tells you what connect would do without connecting: Some(true)
through the proxy, Some(false) directly, None if the policy refuses.
The Microsoft sign-in makes HTTPS requests, which do not go through the Connector. To send
them through the same proxy, build a reqwest::Client with
reqwest::Proxy::all("socks5h://...") (this needs reqwest's socks feature in your
application) and pass it to MicrosoftAuth::with_http_client.