PDA

View Full Version : مشکل این وی پی ان سرویس چیه ؟



Amin-rz
چهارشنبه 13 خرداد 1394, 09:20 صبح
سلام دوستان من هر کاری کردم نتونستم یه وی پی ان بسازم.
این کلاس وی پی ان :

package com.kpbird.globaltouchevent;

import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.Enumeration;

import android.content.Intent;
import android.net.VpnService;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.widget.Toast;

public class ToyVpnService extends VpnService implements Handler.Callback, Runnable {
private static final String TAG = "ToyVpnService";

private Handler mHandler;
private Thread mThread;

private ParcelFileDescriptor mInterface;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The handler is only used to show messages.
if (mHandler == null) {
mHandler = new Handler(this);
}

// Stop the previous session by interrupting the thread.
if (mThread != null) {
mThread.interrupt();
}

// Start a new session by creating a new thread.
mThread = new Thread(this, "ToyVpnThread");
mThread.start();
return START_STICKY;
}

@Override
public void onDestroy() {
if (mThread != null) {
mThread.interrupt();
}
}

public boolean handleMessage(Message message) {
if (message != null) {
Toast.makeText(this, message.what, Toast.LENGTH_SHORT).show();
}
return true;
}

@Override
public synchronized void run() {
Log.i(TAG,"running vpnService");
try {
runVpnConnection();
} catch (Exception e) {
e.printStackTrace();
//Log.e(TAG, "Got " + e.toString());
} finally {
try {
mInterface.close();
} catch (Exception e) {
// ignore
}
mInterface = null;

mHandler.sendEmptyMessage(R.string.disconnected);
Log.i(TAG, "Exiting");
}
}

private boolean runVpnConnection() throws Exception {

configure();

FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());

// Allocate the buffer for a single packet.
ByteBuffer packet = ByteBuffer.allocate(32767);

// We keep forwarding packets till something goes wrong.
while (true) {
// Assume that we did not make any progress in this iteration.
boolean idle = true;

// Read the outgoing packet from the input stream.
int length = in.read(packet.array());
if (length > 0) {

Log.i(TAG,"************new packet");
System.exit(-1);
while (packet.hasRemaining()) {
Log.i(TAG,""+packet.get());
//System.out.print((char) packet.get());
}
packet.limit(length);
// tunnel.write(packet);
packet.clear();

// There might be more outgoing packets.
idle = false;
}
Thread.sleep(50);
}
}

public String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
Log.i(TAG,"****** INET ADDRESS ******");
Log.i(TAG,"address: "+inetAddress.getHostAddress());
Log.i(TAG,"hostname: "+inetAddress.getHostName());
Log.i(TAG,"address.toString(): "+inetAddress.getHostAddress().toString());
if (!inetAddress.isLoopbackAddress()) {
//IPAddresses.setText(inetAddress.getHostAddress().t oString());
Log.i(TAG,"IS NOT LOOPBACK ADDRESS: "+inetAddress.getHostAddress().toString());
return inetAddress.getHostAddress().toString();
} else{
Log.i(TAG,"It is a loopback address");
}
}
}
} catch (SocketException ex) {
String LOG_TAG = null;
Log.e(LOG_TAG, ex.toString());
}

return null;
}

private void configure() throws Exception {
// If the old interface has exactly the same parameters, use it!
if (mInterface != null) {
Log.i(TAG, "Using the previous interface");
return;
}

// Configure a builder while parsing the parameters.
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.2.0", 24);
builder.addRoute("0.0.0.0",0);

try {
mInterface.close();
} catch (Exception e) {
// ignore
}

mInterface = builder.establish();
}
}




با این کد هم شروعش می کنم :

Intent ToyVpnService = new Intent(this,ToyVpnService.class);
startService(ToyVpnService);



اینم مانیفست :

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_TASKS" />

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<service
android:name="ToyVpnService"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
</service>


</application>

Amin-rz
چهارشنبه 13 خرداد 1394, 09:21 صبح
از اینجا هم گرفتمش :
http://stackoverflow.com/questions/17766405/android-***service-to-capture-packets-wont-capture-packets


*** هم وی پی ان هست

pbm_soy
پنج شنبه 14 خرداد 1394, 02:41 صبح
خطا میده؟ در چه مرحله ای خطا میده؟ لاگ خطاها را میذاشتید تا شاید دوستان بهتر بتونن کمک کنند!

داخل کد لاگهای زیادی گذاشته شده است وضعیت این لاگها چیست؟