Perform a dns srv request in react native with JAVA on android

Viewed 465

I'm trying to resolve a DNS SRV entry via a reactive-native app for android. Unfortunately, after some research on npm and google, I could not find a suitable library and tried to implement a native JAVA implementation myself. Unfortunately without success.

Therefore, I followed the Official Guide of Facebook (https://facebook.github.io/react-native/docs/native-modules-android) to create a native module. But before that, I did a general test in JAVA with success. Example code can be found below.

Native Java implementation. I have created a new maven project with dnsjava as dependency (https://github.com/dnsjava/dnsjava/blob/master/EXAMPLES.md)

package test.jsc;
import org.xbill.DNS.*;

public class App 
{
    public static void main( String[] args )
    {

        try {
            Record[] records = new Lookup("_2ndlock._tcp.symlink.de", Type.SRV).run();

            if(records != null && records.length > 0) {
                System.out.println(records[0]);
            }
        } catch (TextParseException e) {
            e.printStackTrace();
            System.out.println("no service found");
        }

    }
}

That works perfect and printed out the record in my terminal.

_2ndlock._tcp.symlink.de.   600 IN  SRV 10 5 443 jarvis.symlink.de.

So far, everything is fine.

Then I created the native implementation for the react-native app. That is, in my react-native project in the folder, the android/app/src/java/com/secondlock/SymlinkDnsSrv.java. The class provide the method "resolveDnsSrvRecord" to resolve the dns entry.

package com.secondlock;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import org.xbill.DNS.*;
import com.facebook.react.bridge.Promise;

public class SymlinkDnsSrv extends ReactContextBaseJavaModule {

    public SymlinkDnsSrv(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "SymlinkDnsSrv";
    }

    @ReactMethod
    public void resolveDnsSrvRecord(String domain, Promise promise) {

        try {
            Record[] records = new Lookup(domain, Type.SRV).run();
            promise.resolve(records);

        } catch (Exception e) {
            promise.reject(e);
        }

    }
}

And that is the package java class in my react-native project in the folder android/app/src/java/com/secondlock/SymlinkDnsSrvPackage.java

package com.secondlock;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class SymlinkDnsSrvPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext){
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new SymlinkDnsSrv(reactContext));
        return modules;
    }
}

In my react-native project if have create a module load wrapper (as facebook recommend) to load the native module

import {NativeModules} from 'react-native';
export default NativeModules.SymlinkDnsSrv;

And use the implementation in my bootstrapper class

private async load(): Promise<void> {
        console.log(SymlinkDnsSrv);
        const r = await SymlinkDnsSrv.resolveDnsSrvRecord("_2ndlock._tcp.symlink.de");

        console.log(r);
    }

Then i run the app in the android simulator (Pixel Api 28, Android 9.0) and get always this a output from the debugger:

{resolveDnsSrvRecord: ƒ, getConstants: ƒ}getConstants: ƒ ()resolveDnsSrvRecord: ƒ fn()__proto__: Object


Bootstrapper.ts:22 null

Can´t resolve the DNS SRV record. Return always null.

Maybe someone has a tip here, what am I doing wrong? Or maybe already made such an implementation? I look forward to every help and say thank you.

+++ Update I have played around with different android versions on my virtual device and it turned out that with android 7 everything's work fine. The problem occurs at version 8 and higher.

0 Answers
Related