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
155 lines
4.9 KiB
Go
155 lines
4.9 KiB
Go
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)
|
|
}
|
|
}
|