// Copyright 2012-2026 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Net;
using ZB.MOM.NatsNet.Server.Protocol;
namespace ZB.MOM.NatsNet.Server;
///
/// Client-side PROXY protocol compatibility surface for Batch 3 mappings.
///
public sealed partial class ClientConnection
{
private IPEndPoint? _proxyRemoteEndPoint;
///
/// Returns the proxied remote endpoint when available, otherwise socket remote endpoint.
/// Mirrors Go proxyConn.RemoteAddr().
///
public EndPoint? RemoteAddr()
{
lock (_mu) { return _proxyRemoteEndPoint ?? GetRemoteEndPoint(); }
}
internal void SetProxyRemoteAddress(ProxyProtocolAddress? address)
{
lock (_mu)
{
_proxyRemoteEndPoint = address is null
? null
: new IPEndPoint(address.SrcIp, address.SrcPort);
}
}
internal static (int version, byte[] header) DetectProxyProtoVersion(Stream conn) =>
ProxyProtocolParser.DetectVersion(conn);
internal static ProxyProtocolAddress? ReadProxyProtoV1Header(Stream conn) =>
ProxyProtocolParser.ReadV1Header(conn);
internal static ProxyProtocolAddress? ReadProxyProtoHeader(Stream conn) =>
ProxyProtocolParser.ReadProxyProtoHeader(conn);
internal static ProxyProtocolAddress? ReadProxyProtoV2Header(Stream conn) =>
ProxyProtocolParser.ReadProxyProtoV2Header(conn);
internal static ProxyProtocolAddress? ParseProxyProtoV2Header(Stream conn, byte[] header) =>
ProxyProtocolParser.ParseV2Header(conn, header.AsSpan());
internal static ProxyProtocolAddress ParseIPv4Addr(Stream conn, ushort addrLen) =>
ProxyProtocolParser.ParseIPv4Addr(conn, addrLen);
internal static ProxyProtocolAddress ParseIPv6Addr(Stream conn, ushort addrLen) =>
ProxyProtocolParser.ParseIPv6Addr(conn, addrLen);
}