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:
1 parent
9fadb58c76
commit
80c7d1ed53
2 files changed
+200
-9
No files matched your search
@@ -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])
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// buildLoginInfo lays out a synthetic LOGIN_INFOR packet. The C++ struct is
|
||||
// fixed-width and NUL-padded; fields not touched here stay zero.
|
||||
func buildLoginInfo(moduleVersion, clientType string, pcName []byte) []byte {
|
||||
b := make([]byte, OffsetReserved+512)
|
||||
b[0] = TokenLogin
|
||||
copy(b[OffsetModuleVersion:], moduleVersion)
|
||||
copy(b[OffsetPCName:OffsetPCName+240], pcName)
|
||||
copy(b[OffsetReserved:], clientType)
|
||||
return b
|
||||
}
|
||||
|
||||
// A Chinese Windows host name arrives as ANSI (CP936/GBK) bytes: the client
|
||||
// fills LOGIN_INFOR.szPCName from gethostname(), a narrow Win32 API, whatever
|
||||
// CLIENT_CAP_UTF8 says (client/LoginServer.cpp:380). The MFC server converts
|
||||
// this field with CP_ACP, so it renders correctly; the Go server used to feed
|
||||
// it to the UTF-8 decoder for every modern client, turning each GBK byte into
|
||||
// U+FFFD ("����") in the web console.
|
||||
func TestLoginInfoGBKHostNameFromUTF8CapableClient(t *testing.T) {
|
||||
name := "我的电脑/研发组"
|
||||
raw := buildLoginInfo("Oct 20 2025-0006", "EXE", UTF8ToGBK(name))
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if info.PCName != name {
|
||||
t.Fatalf("PCName = %q, want %q", info.PCName, name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginInfoGBKHostNameFromLegacyClient(t *testing.T) {
|
||||
name := "张三的电脑"
|
||||
raw := buildLoginInfo("Dec 19 2024", "EXE", UTF8ToGBK(name))
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if info.PCName != name {
|
||||
t.Fatalf("PCName = %q, want %q", info.PCName, name)
|
||||
}
|
||||
}
|
||||
|
||||
// POSIX clients build szPCName from gethostname() on a UTF-8 system, so their
|
||||
// non-ASCII names must not be run through the GBK decoder.
|
||||
func TestLoginInfoUTF8HostNameFromLinuxClient(t *testing.T) {
|
||||
name := "Nürnberg-PC"
|
||||
raw := buildLoginInfo("Oct 20 2025-0006", "LNX", []byte(name))
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if info.PCName != name {
|
||||
t.Fatalf("PCName = %q, want %q", info.PCName, name)
|
||||
}
|
||||
}
|
||||
|
||||
// The APK client sends the Android device model as UTF-8.
|
||||
func TestLoginInfoUTF8HostNameFromAndroidClient(t *testing.T) {
|
||||
name := "小米14"
|
||||
raw := buildLoginInfo("Oct 20 2025-8007", "APK", []byte(name))
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if info.PCName != name {
|
||||
t.Fatalf("PCName = %q, want %q", info.PCName, name)
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-1.0 macOS builds report the client type as "macOS" (current builds send
|
||||
// "MAC"); their host names are UTF-8 too and must not be GBK-decoded.
|
||||
func TestLoginInfoUTF8HostNameFromLegacyMacClient(t *testing.T) {
|
||||
name := "München-Mac"
|
||||
raw := buildLoginInfo("Dec 19 2024", "macOS", []byte(name))
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if info.PCName != name {
|
||||
t.Fatalf("PCName = %q, want %q", info.PCName, name)
|
||||
}
|
||||
}
|
||||
|
||||
// Reserved must keep following CLIENT_CAP_UTF8: RES_FILE_PATH is UTF-8 on
|
||||
// modern clients (client/LoginServer.cpp:411) while legacy clients sent
|
||||
// CP_ACP, and the location field is UTF-8 for modern clients too.
|
||||
func TestLoginInfoReservedStillHonorsCapability(t *testing.T) {
|
||||
path := `C:\Users\张三\Desktop\样本.exe`
|
||||
reserved := strings.Join([]string{
|
||||
"EXE", // 0 client type
|
||||
"64", // 1 system bits
|
||||
"8", // 2 cpu
|
||||
"16", // 3 memory
|
||||
path, // 4 file path (UTF-8 on capable clients)
|
||||
"?", // 5
|
||||
"2025-01-02", // 6 install time
|
||||
"?", // 7
|
||||
"64", // 8 program bits
|
||||
"", // 9
|
||||
"北京/朝阳", // 10 location (UTF-8 on capable clients)
|
||||
"1.2.3.4", // 11 public ip
|
||||
"v1.0", // 12 version
|
||||
"user", // 13 username
|
||||
"0", // 14 admin
|
||||
}, "|")
|
||||
raw := buildLoginInfo("Oct 20 2025-0006", "", UTF8ToGBK("PC"))
|
||||
copy(raw[OffsetReserved:], reserved)
|
||||
// clientType itself lives in field 0 of the very same Reserved bytes.
|
||||
if !bytes.HasPrefix(raw[OffsetReserved:], []byte("EXE|")) {
|
||||
t.Fatal("test fixture broken")
|
||||
}
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if got := info.GetReservedField(ResFieldFilePath); got != path {
|
||||
t.Fatalf("file path = %q, want %q", got, path)
|
||||
}
|
||||
if got := info.GetReservedField(ResFieldClientLoc); got != "北京/朝阳" {
|
||||
t.Fatalf("location = %q, want %q", got, "北京/朝阳")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginInfoBinaryFieldsUnaffected(t *testing.T) {
|
||||
raw := buildLoginInfo("Oct 20 2025-0006", "EXE", UTF8ToGBK("PC"))
|
||||
binary.LittleEndian.PutUint32(raw[OffsetCPUMHz:], 3600)
|
||||
raw[OffsetWebCamExist] = 1
|
||||
binary.LittleEndian.PutUint32(raw[OffsetSpeed:], 123)
|
||||
|
||||
info, err := ParseLoginInfo(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginInfo: %v", err)
|
||||
}
|
||||
if info.CPUMHz != 3600 || !info.WebCamExist || info.Speed != 123 {
|
||||
t.Fatalf("binary fields = %d/%v/%d", info.CPUMHz, info.WebCamExist, info.Speed)
|
||||
}
|
||||
if info.PCName != "PC" {
|
||||
t.Fatalf("PCName = %q", info.PCName)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user