Fix(go): decode Windows host names with the system code page

The web console showed Chinese computer names as a run of U+FFFD.
ParseLoginInfo sent every string field through the UTF-8 decoder once a
client advertised CLIENT_CAP_UTF8, but szPCName and the OS string are not
covered by that bit: Windows fills them from gethostname(), a narrow API,
so they carry CP936/GBK even on modern clients. The MFC server decodes
these two fields with CP_ACP (HostJson.cpp::AnsiToUtf8), which is why the
Windows server and its web view were never affected.

A new DecodeLoginAnsiField picks by client type instead of capability:
LNX/MAC/macOS/APK are UTF-8 (they build the field from a UTF-8 host name
or, for Android, the device model), everything else is GBK. "macOS" is
what pre-1.0 macOS builds report; GetClientType sends "MAC" today and the
MFC WebService still accepts both. ASCII host names come out
byte-identical either way, and legacy clients keep the name they had.

Reserved keeps its capability-based decoding, so UTF-8 file paths and
locations are untouched; OS strings are ASCII in practice. The one case
that gets worse is a Windows machine running the "Beta: Use Unicode UTF-8"
locale, which will now mojibake there. Sniffing UTF-8 to cover it would
misfire on real GBK names -- 930 of 6763 GBK hanzi pass as a UTF-8
two-byte sequence, roughly 2% of two-character names -- so the
deterministic rule wins and the trade is written next to the function.

Tests cover GBK names from capable and legacy clients, UTF-8 names from
Linux, Android and legacy macOS, and Reserved still honoring the bit.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-14 11:42:32 +01:00
1 parent 9fadb58c76
commit 80c7d1ed53
2 files changed
+200 -9

No files matched your search

+46 -9
View File
@@ -541,15 +541,51 @@ type LoginInfo struct {
Reserved string // Contains additional info separated by |
}
// DecodeLoginAnsiField decodes a LOGIN_INFOR text field that is *not* covered
// by CLIENT_CAP_UTF8 (computer name, OS string). On Windows those are filled
// from narrow Win32/CRT APIs — gethostname() at client/LoginServer.cpp:380 —
// and carry the system code page (CP936/GBK on Chinese installs) even when the
// client advertises UTF-8 support; the capability bit only promises UTF-8 for
// the protocol string fields it lists (active window, window titles, file
// paths). The MFC server decodes these same fields with CP_ACP
// (HostJson.cpp::AnsiToUtf8), which is why a Chinese computer name renders
// correctly there and showed up as "����" in the Go server's web console.
// POSIX and Android clients build the field as UTF-8, so keep their bytes.
// "macOS" is the client-type string of pre-1.0 macOS builds (GetClientType
// returns "MAC" today; the MFC server still accepts both, see
// WebService.cpp's dblclick branch). Those builds also send UTF-8 names, so
// they must not fall into the GBK path or the same login decodes differently
// in the Go server than in the C++ one.
//
// GBK is hard-coded because the Go server has no equivalent of the server
// machine's CP_ACP; that matches the MFC deployment everyone runs (a Chinese
// server). Consequence worth knowing: a Windows machine with the "Beta: Use
// Unicode UTF-8" locale option sends UTF-8 here too and will show mojibake —
// the MFC server mangles that case as well, and sniffing UTF-8 instead would
// misfire on real GBK names (measured: 930 of 6763 GBK hanzi validate as a
// UTF-8 two-byte sequence, so ~2% of two-character host names would be
// misread), so the deterministic rule is the safer side of the trade.
func DecodeLoginAnsiField(data []byte, clientType string) string {
switch clientType {
case "LNX", "MAC", "macOS", "APK":
return Utf8CleanString(data)
}
return GbkToUTF8(data)
}
// ParseLoginInfo parses LOGIN_INFOR from data.
//
// Encoding: text fields are GBK on legacy Windows clients and UTF-8 on modern
// clients that set CLIENT_CAP_UTF8 (always on for LNX / MAC). Picking the
// wrong codec mangles non-ASCII characters — e.g. a German location string
// "Nürnberg" sent as UTF-8 (4E C3 BC 72 ...) and force-decoded as GBK turns
// into mojibake. The heartbeat path already honors this via DecodeClientString
// (see cmd/main.go handleHeartbeat); ParseLoginInfo previously did not, so
// every login string from a UTF-8 client was being misread.
// Encoding: Reserved (file path, location, ...) is GBK on legacy Windows
// clients and UTF-8 on modern clients that set CLIENT_CAP_UTF8 (always on for
// LNX / MAC). Picking the wrong codec mangles non-ASCII characters — e.g. a
// German location string "Nürnberg" sent as UTF-8 (4E C3 BC 72 ...) and
// force-decoded as GBK turns into mojibake. The heartbeat path already honors
// this via DecodeClientString (see cmd/main.go handleHeartbeat); ParseLoginInfo
// previously did not, so every login string from a UTF-8 client was misread.
//
// Computer name and OS use a different rule: they are ANSI on Windows
// regardless of the capability bit (see DecodeLoginAnsiField), so they are
// decoded by client type, not by capability.
//
// To get encoding right we have a chicken-and-egg problem: capability lives
// in ModuleVersion (offset 164) and clientType lives in Reserved field 0
@@ -600,11 +636,12 @@ func ParseLoginInfo(data []byte) (*LoginInfo, error) {
// Now decode every user-text field with the client's actual codec.
decode := func(b []byte) string { return DecodeClientString(b, capability, clientType) }
// Computer name / OS: ANSI on Windows whatever the capability says.
if len(data) >= OffsetOsVerInfoEx+156 {
info.OsVerInfo = decode(data[OffsetOsVerInfoEx : OffsetOsVerInfoEx+156])
info.OsVerInfo = DecodeLoginAnsiField(data[OffsetOsVerInfoEx:OffsetOsVerInfoEx+156], clientType)
}
if len(data) >= OffsetPCName+240 {
info.PCName = decode(data[OffsetPCName : OffsetPCName+240])
info.PCName = DecodeLoginAnsiField(data[OffsetPCName:OffsetPCName+240], clientType)
}
if len(data) >= OffsetMasterID+20 {
info.MasterID = decode(data[OffsetMasterID : OffsetMasterID+20])