Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Agoni-sudo opened this issue Nov 5, 2024 · 10 comments
Open

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

Agoni-sudo opened this issue Nov 5, 2024 · 10 comments

Comments

@Agoni-sudo
Copy link

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

@Yunustt
Copy link

Yunustt commented Nov 5, 2024

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

@Agoni-sudo
Copy link
Author

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

Thanks, but how do I get the connected channel

@Yunustt
Copy link

Yunustt commented Nov 18, 2024

There are channels in MQTTConnection

@Yunustt
Copy link

Yunustt commented Nov 18, 2024

NewNettyMQTTHandler uses ChannelHandlerContext to create an MQTTConnection
image

@Agoni-sudo
Copy link
Author

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);
}

@andsel
Copy link
Collaborator

andsel commented Nov 20, 2024

@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.

@Yunustt
Copy link

Yunustt commented Nov 22, 2024

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

@Yunustt
Copy link

Yunustt commented Nov 22, 2024

@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)));
    }
}

@andsel
Copy link
Collaborator

andsel commented Nov 22, 2024

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

@robinwu2008
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants