Hello.
I'm having a problem for a quite long time. I need to write VPN on android which actually doesn't create an actual connections, just imitates/simulates it. So in that way i could see all internet data and make some filter. The problems is that at the moment i make a VPN connection and a key appears on telephone where i can see how much data was sent and receive, but it atually doesnt receive any data. And because of that automatically internet stops working (Can't load anything because doesn't get any data).
What i have know is a code from another forum and i want to extend it and make it work, but to do that i want to know, what do i need to do to start receiving data. Should i imitate somehow a seperated server or what?
I would really appreciate any help.
I'm having a problem for a quite long time. I need to write VPN on android which actually doesn't create an actual connections, just imitates/simulates it. So in that way i could see all internet data and make some filter. The problems is that at the moment i make a VPN connection and a key appears on telephone where i can see how much data was sent and receive, but it atually doesnt receive any data. And because of that automatically internet stops working (Can't load anything because doesn't get any data).
What i have know is a code from another forum and i want to extend it and make it work, but to do that i want to know, what do i need to do to start receiving data. Should i imitate somehow a seperated server or what?
I would really appreciate any help.
Code:
public class VPN extends VpnService implements Handler.Callback, Runnable {
private static final String TAG = "VpnService";
private String mServerAddress = "127.0.0.1";
private int mServerPort = 55555;
private Handler mHandler;
private Thread mThread;
private ParcelFileDescriptor mInterface;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mHandler == null) {
mHandler = new Handler(this);
}
if (mThread != null) {
mThread.interrupt();
}
mThread = new Thread(this, "VpnThread");
mThread.start();
return START_STICKY;
}
@Override
public void onDestroy() {
if (mThread != null) {
mThread.interrupt();
}
}
@Override
public boolean handleMessage(Message message) {
if (message != null) {
Toast.makeText(this, (String) message.obj, Toast.LENGTH_SHORT).show();
}
return true;
}
@Override
public synchronized void run() {
try {
Log.i(TAG, "Starting");
InetSocketAddress server = new InetSocketAddress(mServerAddress, mServerPort);
run(server);
} catch (Exception e) {
Log.e(TAG, "Got " + e.toString());
try {
mInterface.close();
} catch (Exception e2) { }
Message msgObj = mHandler.obtainMessage();
msgObj.obj = "Disconnected";
mHandler.sendMessage(msgObj);
} finally {
}
}
DatagramChannel mTunnel = null;
private boolean run(InetSocketAddress server) throws Exception {
boolean connected = false;
mTunnel = DatagramChannel.open();
if (!protect(mTunnel.socket())) {
throw new IllegalStateException("Cannot protect the tunnel");
}
mTunnel.connect(server);
mTunnel.configureBlocking(false);
handshake();
connected = true;
Message msgObj = mHandler.obtainMessage();
msgObj.obj = "Connected";
mHandler.sendMessage(msgObj);
new Thread ()
{
public void run ()
{
FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
ByteBuffer packet = ByteBuffer.allocate(32767);
int length;
try
{
while (true)
{
while ((length = in.read(packet.array())) > 0) {
packet.limit(length);
Log.d(TAG, "Total Length:" + mTunnel.socket().getInetAddress());
mTunnel.write(packet);
packet.clear();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
new Thread ()
{
public void run ()
{
DatagramChannel tunnel = mTunnel;
ByteBuffer packet = ByteBuffer.allocate(8096);
FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());
while (true)
{
try
{
int length;
while ((length = tunnel.read(packet)) > 0)
{
out.write(packet.array(), 0, length);
packet.clear();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}.start();
return connected;
}
private void handshake() throws Exception {
if (mInterface == null)
{
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.2.0", 32);
builder.addRoute("0.0.0.0", 0);
try {
mInterface.close();
} catch (Exception e) {
// ignore
}
mInterface = builder.setSession("VPN'as").establish();
}
}
}