Просмотр исходного кода

Fix direct connections for ICQ v2 clients (#232)

aaronk6 1 неделя назад
Родитель
Сommit
e745ff99aa

+ 1 - 1
server/icq_legacy/handler.go

@@ -97,7 +97,7 @@ func (d *ProtocolDispatcher) SendUserOnline(toSession *LegacySession, onlineUIN
 	case ICQLegacyVersionV1:
 		return d.v1Handler.sendUserOnline(toSession, onlineUIN, downgradeStatusForV2(status), nil, 0)
 	case ICQLegacyVersionV2:
-		return d.v2Handler.sendUserOnline(toSession, onlineUIN, downgradeStatusForV2(status), nil, 0)
+		return d.v2Handler.sendV2UserOnline(toSession, onlineUIN, status)
 	case ICQLegacyVersionV3:
 		return d.v3Handler.sendUserOnline(toSession, onlineUIN, status)
 	case ICQLegacyVersionV4:

+ 40 - 0
server/icq_legacy/handler_test.go

@@ -1,7 +1,9 @@
 package icq_legacy
 
 import (
+	"encoding/binary"
 	"log/slog"
+	"net"
 	"testing"
 
 	"github.com/mk6i/open-oscar-server/config"
@@ -72,6 +74,44 @@ func TestProtocolDispatcher_SendUserOnline(t *testing.T) {
 	}
 }
 
+func TestProtocolDispatcher_SendUserOnlineV2DirectConnection(t *testing.T) {
+	for _, tt := range []struct {
+		name        string
+		enabled     bool
+		peerVersion uint16
+		wantIP      []byte
+		wantPort    uint16
+	}{
+		{"enabled", true, ICQLegacyVersionV2, []byte{192, 0, 2, 10}, 12345},
+		{"disabled", false, ICQLegacyVersionV2, []byte{0, 0, 0, 0}, 0},
+		{"different protocol", true, ICQLegacyVersionV5, []byte{0, 0, 0, 0}, 0},
+	} {
+		t.Run(tt.name, func(t *testing.T) {
+			sender := newMockPacketSender(t)
+			dispatcher := newTestDispatcher(t, sender)
+			if tt.enabled {
+				dispatcher.v2Handler.sessions.config.DirectConnections = []int{int(ICQLegacyVersionV2)}
+			}
+			recipient := newTestLegacySession(10001, legacySessionOptVersion(ICQLegacyVersionV2))
+			peer := &LegacySession{
+				UIN:     67890,
+				Version: tt.peerVersion,
+				Addr:    &net.UDPAddr{IP: net.IPv4(192, 0, 2, 10), Port: 4001},
+			}
+			peer.SetDirectConnectionInfo(12345, 0, 0, 0)
+			registerSession(dispatcher.v2Handler.sessions, peer)
+
+			sender.EXPECT().SendToSession(recipient, mock.AnythingOfType("[]uint8")).
+				Run(func(_ *LegacySession, packet []byte) {
+					assert.Equal(t, ICQLegacySrvUserOnline, v2ServerCommand(packet))
+					assert.Equal(t, tt.wantIP, packet[10:14])
+					assert.Equal(t, tt.wantPort, binary.LittleEndian.Uint16(packet[14:16]))
+				}).Return(nil).Once()
+			assert.NoError(t, dispatcher.SendUserOnline(recipient, peer.UIN, 0))
+		})
+	}
+}
+
 func TestProtocolDispatcher_SendUserOnline_NilSession(t *testing.T) {
 	sender := newMockPacketSender(t)
 	dispatcher := newTestDispatcher(t, sender)

+ 23 - 0
server/icq_legacy/session.go

@@ -174,6 +174,29 @@ func (m *LegacySessionManager) GetSession(uin uint32) *LegacySession {
 	return m.sessions[uin]
 }
 
+// v2DirectConnection returns the peer's IPv4 address and TCP listening port.
+func (m *LegacySessionManager) v2DirectConnection(uin uint32) (net.IP, uint16) {
+	if !m.config.DirectConnectionEnabled(int(ICQLegacyVersionV2)) {
+		return nil, 0
+	}
+
+	m.mu.RLock()
+	defer m.mu.RUnlock()
+
+	peer := m.sessions[uin]
+	if peer == nil || peer.Version != ICQLegacyVersionV2 || peer.Addr == nil {
+		return nil, 0
+	}
+
+	ip := peer.Addr.IP.To4()
+	port := peer.GetTCPPort()
+	if ip == nil || port == 0 || port > 65535 {
+		return nil, 0
+	}
+
+	return ip, uint16(port)
+}
+
 // GetSessionByAddr retrieves a session by UDP address
 func (m *LegacySessionManager) GetSessionByAddr(addr *net.UDPAddr) *LegacySession {
 	m.mu.RLock()

+ 15 - 12
server/icq_legacy/v2_handler.go

@@ -224,6 +224,7 @@ func (h *V2Handler) handleLogin(session *LegacySession, addr *net.UDPAddr, pkt *
 	}
 
 	newSession.SetStatus(loginData.Status)
+	newSession.SetDirectConnectionInfo(uint32(loginData.Port), 0, 0, 0)
 	newSession.Password = loginData.Password
 
 	// 4. Send ACK first, then login reply
@@ -344,20 +345,10 @@ func (h *V2Handler) handleContactList(session *LegacySession, pkt *V2ClientPacke
 		return h.sender.SendToSession(session, h.packetBuilder.BuildContactListDone(session.NextServerSeqNum(), session.UIN))
 	}
 
-	// 3. Build and send responses using packet builder
-	// Send online status for each contact that is online.
-	// Apply downgradeStatusForV2 so V2 clients see correct icons
-	// for statuses they don't natively support (N/A→Away, Occupied→DND, FFC→Online).
+	// 3. Send online status for each contact that is online.
 	for _, contact := range contactResult.OnlineContacts {
 		if contact.Online {
-			onlinePkt := h.packetBuilder.BuildUserOnline(
-				session.NextServerSeqNum(),
-				contact.UIN,
-				downgradeStatusForV2(contact.Status),
-				nil, // IP not available from service layer
-				0,   // Port not available from service layer
-			)
-			_ = h.sender.SendToSession(session, onlinePkt)
+			_ = h.sendV2UserOnline(session, contact.UIN, contact.Status)
 		}
 	}
 
@@ -365,6 +356,18 @@ func (h *V2Handler) handleContactList(session *LegacySession, pkt *V2ClientPacke
 	return h.sender.SendToSession(session, h.packetBuilder.BuildContactListDone(session.NextServerSeqNum(), session.UIN))
 }
 
+// sendV2UserOnline sends the initial presence packet for an online contact.
+func (h *V2Handler) sendV2UserOnline(session *LegacySession, uin uint32, status uint32) error {
+	var ip net.IP
+	var port uint16
+	if session.Version == ICQLegacyVersionV2 {
+		ip, port = h.sessions.v2DirectConnection(uin)
+	}
+
+	packet := h.packetBuilder.BuildUserOnline(session.NextServerSeqNum(), uin, downgradeStatusForV2(status), ip, port)
+	return h.sender.SendToSession(session, packet)
+}
+
 // handleSendMessage processes a message send request
 // Refactored to use service layer (ProcessMessage) and packet builder.
 // Following the OSCAR pattern: unmarshal -> call service -> build response