0

I'm going to create an Android vpn application. I have extended a VpnService class. Below is my class:


public class MyVpnService extends VpnService {
    private String TAG = "mjh";
    private Thread thread;
    private ServerConnection serverConnection;
    private ParcelFileDescriptor pfd;
    private ArrayList<Client> clients;
    private boolean CONNECTED = false;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //line bellow creates a socket.io connection to my server and when connected successfully, it runs the onConnected code block:
    serverConnection = new ServerConnection(
                //onConnected
                sc -> {
                    ascender = new Thread(() -> {
                        try {
                            pfd = new VpnService.Builder()
                                    .addAddress("10.0.0.1", 24)
                                    .setSession("MyVpnService")
                                    .addRoute("0.0.0.0", 0)
                                    .setBlocking(false)
                                    .setUnderlyingNetworks(null)
                                    .establish();
                        } catch (Exception e) {
                            Log.d(TAG, "Error : in creating localhost : " + e);
                        }

                        CONNECTED = true;
                        ByteBuffer buffer = ByteBuffer.allocate(10000);
                        FileInputStream is = new FileInputStream(pfd.getFileDescriptor());
                        while (CONNECTED) {
                            try {
                                try {
                                    buffer.clear();
                                    int length = is.read(buffer.array());
                                    if (length > 0) {
                                        byte[] data = new byte[length];
                                        buffer.get(data, 0, length);

                                        Log.d(TAG,Arrays.toString(data));
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, "Error in receiving data from device : " + e);
                                }
                            } catch (Exception e) {
                                Log.d(TAG, "Error in receiving data from device : " + e);
                            }

                        }
                    });
                    ascender.start();
                },
                //onDisconnected
                sc -> {
                    CONNECTED = false;
                }
        );
        return START_STICKY;
    }
}

When I run my code, the line:

Log.d(TAG,Arrays.toString(data));

logs the bellow data:

[69, 0, 0, 52, 55, -121, 64, 0, 64, 6, -59, -82, -64, -88, 94, 15, -64, -88, 94, 46, -72, 60, 11, -72, 104, -121, -27, 113, -75, 54, 10, 97, -128, 16, 0, 87, 111, -7, 0, 0, 1, 1 , .... ]

or a similar array.

and if I change the line to below:

Log.d(TAG , new String(data, "UTF-8"));

I get a messy string as bellow:

E??��S@??@�0��^��^.���=x|ܝ�]W�??W�a????
                 �^��=���S�U3�US�U�!US�Uj�$��9��US�US�US�Ui�PS�Tܡ��S�UW�U��US�US�U��UW�U��US�US�U���9W�U��US�US�US�U��2�ު��iȳ�A���W������S���]���@���@���������o��Cb�P#b�PCj����PCb�Pz+]U4T2q�`�PCb�PCb�PCb�R�b��Cb�P��z]�/Nh�!�J   .�H�TX�C>�G1�J�[(�X3�BXg�R'���[i��[i��i�(�i��[i�����m�)Yi��[i��[i��[i��[l��[h�Y[�Z�[i��[i�)Yi��[i��[i�)[i��[i�)Yi��[i��[i�)m��[i�)Yi��[i��[i��[i���_���k������,Б�:�ܞ}���>���0ϔ�-�ʑ-֕�}ͅ�}������[�i��h��)��h��7ʛ7��ob}f��F4�ۯi�ƪi�Z�aϘ�lB3T�C:d�Zd�:(����:d�-�MR���f�:d�:d�:d�??d�:d��d�y:d�>d��f�:d�:d��d�>d��f�:d�:d��R��>d��f�:d�:d�:d��hi]�o]h�B2�
                 .��K�>�1�
                 �(�CK3�Kg�45ن99�99�19�x�9�99�p�<��y;9�99�99�9;9^��99D��4vt�GY��G�Z�k�W�X�L??�AX�Z�F    ��1��??I��??I��??Hsl??I��??I���L�چhr�??I��??I��??I��??_��K��I�ş��??J��??Ir�??I��??I��??Hr�??I��??Ir�??I��??I��??Hrچh��??Ir�??I��??I��??I��??K���É*������O��M��Q���F���B���O��^�����G���W˂���D���Dͳ�D�MqDͳ�D͊�������L�Dͳ�Dͳ�Dͳ�Dۉ�Aϳ�E�<���Dη�D�L�Dͳ�Dͳ�D�L�Dͷ�D�L�Dͳ�Dͳ�D�L����D�L�Dͳ�Dͳ�Dͳ�Dρ�R�ݚf��   ���!��7���pȭ�3���=��� ��� ⸶p���p������G�K�G�T��N�C�G��A^ܰF{?����G���?�G��Bt
                 �Oʷ惁�G��D�

while it is supposed to give me http requests like GET , CONNECT , POST or etc.

I also have tried all other decoding methods except UTF-8 it didn't work.

Any suggestion to get to my ideal request string?

I also have tried all other decoding methods except UTF-8 it didn't work. while it is supposed to give me http requests like GET , CONNECT , POST or etc.

  • What you create with `VpnService.Builder` is a TUN device, which provides and expects raw IP packets. To get to the HTTP requests, you'd first have to parse the IP and TCP headers (if the browser uses QUIC it will be UDP). However, there will usually be a TLS layer as well, so you'll probably never see the actual HTTP requests (unless you break up that TLS connection, although that will not work if the browser uses certificate pinning or similar). – ecdsa Feb 24 '23 at 08:50
  • Thank you, then how should I handle it in my vpn server? – mohammad javad haji Feb 24 '23 at 10:12
  • Like an IP packet. Or what do you mean? – ecdsa Feb 24 '23 at 14:39
  • yes,if I tunnel the data to my vpn server, then how could parse it in my server. – mohammad javad haji Feb 26 '23 at 11:40
  • As I said, you treat it like an IP packet, either parse it or forward it (e.g. via another TUN device). – ecdsa Feb 27 '23 at 09:48

0 Answers0