summaryrefslogtreecommitdiff
path: root/app/java/src/DNSProxyRunner.java
blob: 0c136e80c4327aead1e707ad78dc5c51241ccf32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.pihole.dnsproxy;

import android.os.ParcelFileDescriptor;

import android.util.Log;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import java.nio.channels.DatagramChannel;

public class DNSProxyRunner implements Runnable {
    public interface OnEstablishListener {
        void onEstablish(ParcelFileDescriptor networkInterface);
    }
    private OnEstablishListener onEstablishListener;

    private DNSProxyService service;

    DNSProxyRunner(DNSProxyService service) {
        this.service = service;
    }

    @Override
    public void run() {
        try {
            DatagramChannel tunnel = DatagramChannel.open();

            if(!this.service.protect(tunnel.socket())) {
                throw new IllegalStateException("Cannot protect tunnel");
            }

            // SocketAddress server = new InetSocketAddress("servername", "serverport");
            //
            // tunnel.connect(server);
            // tunnel.configureBlocking(false);
            //
            // ParcelFileDescriptor networkInterface = this.handshake(tunnel);
            // FileInputStream in = new FileInputStream(networkInterface.getFileDescriptor());
            // FileOutputStream out = new FileOutputStream(networkInterface.getFileDescriptor());
        } catch (IOException exception) {
            Log.e(DNSProxyService.LOG_TAG, "Cannot use socket", exception);
        }
    }

    public void setOnEstablishListener(OnEstablishListener listener) {
        this.onEstablishListener = listener;
    }
}