Jump to content

MongoDB Connection Timeout Forge 1.8.9


vromM

Recommended Posts

I have developed a Minecraft mod that utilizes MongoDB Atlas. The code works fine when I run it in the Intellij IDE, and the connection to the database is established successfully. However, when I try to launch the mod using the Minecraft launcher, the connection to the database times out, and I get an error message. I have checked the connection settings that any ip can access the database, verified that the MongoDB driver is included in the mod, and confirmed that the MongoDB server is running and accessible. Despite these efforts, the issue persists, and I am unable to connect to the database when launching the mod in the Minecraft launcher.

Code:

import co.rmacro.components.ChatListener;
import co.rmacro.components.Mod;
import co.rmacro.components.NoKey;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Projections;
import org.bson.Document;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;


import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;

@Mod(modid = "myMod", name = "myMod", version = "1.0.0", acceptedMinecraftVersions = "1.8.9", dependencies = "required-after:[email protected]")
public class rMacro {
    private Mod mod;

    @EventHandler
    public void preinit(FMLInitializationEvent event) throws SocketException {
        boolean chooseMod = checkKey();
        if (chooseMod) {
            mod = new Mod();
            ChatListener chatListener = new ChatListener(macro);
            MinecraftForge.EVENT_BUS.register(chatListener);
        } else {
            System.out.println("NoKey");
            NoKey noKey = new NoKey();

            MinecraftForge.EVENT_BUS.register(noKey);
        }
    }

    private boolean checkKey() throws SocketException {
        System.out.println("CheckKey");
        // get the MAC address
        String macAddress = getMacAddress();
        String uri = "mongodb+srv://customer:[email protected]/?retryWrites=true&w=majority";
        MongoClient mongoClient = MongoClients.create(uri);
        boolean macAddressFound = false;
        // connect to the MongoDB and get the collection
        try {

            MongoDatabase database = mongoClient.getDatabase("Mod-Keys");
            MongoCollection<Document> collection = database.getCollection("MacAddress");

            // check if the MAC address is in the collection
            List<Document> doc = collection.find().projection(Projections.include("macA")).into(new ArrayList());
            System.out.println(doc);
            for (Document document : doc) {
                String mac = document.getString("macA");
                System.out.println(mac);
                if (macAddress.equals(mac)) {
                    System.out.println("MAC address found");
                    System.out.println(macAddress);
                    macAddressFound = true;
                    break;
                }
                else {
                    System.out.println("MAC address: " + mac);
                    System.out.println("macAddress: " + macAddress);
                }
            }
        } catch (Exception e) {
            System.out.println("Error connecting to MongoDB: " + e.getMessage());

        }
        return macAddressFound;
    }
 
    private String getMacAddress() throws SocketException {
        System.out.println("getMacAddress");
        String macAddress = "";
        try {
            // use Java Network Interface API to get the MAC address
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            byte[] mac = networkInterface.getHardwareAddress();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            macAddress = sb.toString();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return macAddress;
    }
}

 

The Error I get when I launch the mod in the Minecraft Launcher:
 

Error connecting to MongoDB: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@4fe9a720. \n Client view of cluster state is {type=REPLICA_SET, servers=[{address=ac-cpyryjh-shard-00-00.cnvpg2s.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names matching IP address 18.197.37.129 found}, caused by {java.security.cert.CertificateException: No subject alternative names matching IP address 18.197.37.129 found}}, {address=ac-cpyryjh-shard-00-01.cnvpg2s.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names matching IP address 18.195.134.220 found}, caused by {java.security.cert.CertificateException: No subject alternative names matching IP address 18.195.134.220 found}}, {address=ac-cpyryjh-shard-00-02.cnvpg2s.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names matching IP address 52.28.222.2 found}, caused by {java.security.cert.CertificateException: No subject alternative names matching IP address 52.28.222.2 found}}

 

Edited by vromM
Clarification
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.