moquette icon indicating copy to clipboard operation
moquette copied to clipboard

Is there any way to get the ip of the client?

Open Agoni-sudo opened this issue 1 year ago • 10 comments

Is there any way to get the ip of the client?

Agoni-sudo avatar Nov 05 '24 09:11 Agoni-sudo

((InetSocketAddress)channel.remoteAddress()).getHostString()

Yunustt avatar Nov 05 '24 11:11 Yunustt

((InetSocketAddress)channel.remoteAddress()).getHostString()

Thanks, but how do I get the connected channel

Agoni-sudo avatar Nov 16 '24 03:11 Agoni-sudo

There are channels in MQTTConnection

Yunustt avatar Nov 18 '24 03:11 Yunustt

NewNettyMQTTHandler uses ChannelHandlerContext to create an MQTTConnection image

Yunustt avatar Nov 18 '24 03:11 Yunustt

NewNettyMQTTHandler uses ChannelHandlerContext to create an MQTTConnection image

Thank you. Is this part of the moquette source code? I want to get the ip in my moquette interceptor. The first time I use moquette, I don't know if it's the wrong way. The way you said to get the ip through the channel ((InetSocketAddress)channel.remoteAddress()).getHostString() should be fine, but this channel seems to be private and I can't call it @Override public void onConnect( InterceptConnectMessage msg) {

    Connect connect = new Connect();
    connect.setClient_id(msg.getClientID());
    connect.setUsername(msg.getUsername() != null ? msg.getUsername() : "unset");
    connect.setStatus(true);  
    connect.setKeeplive(msg.getKeepAlive());
    connect.setCleansession(msg.isCleanSession());
    connect.setDatetime(new Timestamp(System.currentTimeMillis()));

    log.info("client connect:{}", connect);

    connectService.handleClientConnection(connect);
}

Agoni-sudo avatar Nov 18 '24 08:11 Agoni-sudo

@Agoni-sudo in the interceptor interface you can grab all the information present in InterceptConnectMessage, and actually it doesn't contains such information. Please create an issue describing the desired behavior and label it with enhancement 🙏 and close this.

andsel avatar Nov 20 '24 13:11 andsel

Use connection callbacks as an example,You can construct an ip property in an InterceptConnectMessage

Yunustt avatar Nov 22 '24 03:11 Yunustt

@Override
public void notifyClientConnected(final MqttConnectMessage msg, String remoteAddress) {
    for (final InterceptHandler handler : this.handlers.get(InterceptConnectMessage.class)) {
        LOG.debug("Sending MQTT CONNECT message to interceptor. CId={}, interceptorId={}",
                msg.payload().clientIdentifier(), handler.getID());
        executor.execute(() -> handler.onConnect(new InterceptConnectMessage(msg, remoteAddress)));
    }
}

Yunustt avatar Nov 22 '24 04:11 Yunustt

Yep, @Yunustt if you or @Agoni-sudo desire to create a PR for this aspect I'll be happy to review :-)

andsel avatar Nov 22 '24 14:11 andsel

My requirement is to determine the source of the client based on the IP address, so if I can get the IP address in IAuthenticator, it would be a perfect solution.

robinwu2008 avatar Jan 04 '25 06:01 robinwu2008