PDA

View Full Version : این v p n رو چطور اجرا کنم؟



Amin-rz
یک شنبه 05 مهر 1394, 13:29 عصر
سلام.یه پروژه v p n دانلود کردم که مشکلی نداره.یه vp n service داره که از طریق اکتیویتی فعال میشه.
میخوام دوتا دکمه شناور رو صفحه بذارم که با اونا v p n رو فعال و غیر فعال کنم که نمیشه.دکمه شناورم بلدم بسازم ولی نمی تونم از طریق اون فعالش کنم.
یه راهنمایی بکنید.
پروژه رو ضمیمه می کنم.14 کیلوبایته.
اینم کد vp n service :


/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.toyvpn;

import android.app.PendingIntent;
import android.app.Service;
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;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class ToyVpnService extends VpnService {
private static final String TAG = "MyVpn";

private PendingIntent mConfigureIntent;
private ParcelFileDescriptor mInterface;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand called");
connect();
return START_NOT_STICKY;
}

public void connect() {
Log.i(TAG, "connect");

Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.0.2", 32);

mInterface = builder.setSession("172.17.9.88")
.setConfigureIntent(mConfigureIntent)
.establish();

mConnected = true;
updateActivity();
}

public void disconnect() {
Log.i(TAG, "Disconnect in service");
try {
mInterface.close();
} catch (java.io.IOException e) {
Log.i(TAG, e.getMessage());
}

mConnected = false;
updateActivity();
stopSelf(0);
}

public boolean isConnected() {
return mConnected;
}

@Override
public void onDestroy() {
Log.i(TAG, "onDestroy called");
mConnected = false;
}

@Override
public void onRevoke() {
Log.i(TAG, "onRevoke called");
disconnect();
}

@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind called");
return mBinder;
}

public class LocalBinder extends Binder {
ToyVpnService getService() {
// Return this instance of LocalService so clients can call public methods
return ToyVpnService.this;
}

@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
if (code == IBinder.LAST_CALL_TRANSACTION) {
onRevoke();
return true;
}
return false;
}
}

private void updateActivity() {
Intent intent = new Intent();
intent.setAction("MY_ACTION");
intent.putExtra("state", mConnected == true);
sendBroadcast(intent);
}

private final IBinder mBinder = new LocalBinder();
boolean mConnected = false;
}











اینم کد اکتیویتی :









/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.toyvpn;

import android.app.Activity;
import android.content.Intent;
import android.net.VpnService;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.os.IBinder;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;

public class ToyVpnClient extends Activity {
private TextView mServerAddress;
private TextView mServerPort;
private TextView mSharedSecret;

private Button connectBtn;
private Button disconnectBtn;

ToyVpnService mService;
boolean mBound = false;

MyReceiver myReceiver;

private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i("MyVpn", "onServiceConnected");
// We've bound to LocalService, cast the IBinder and get LocalService instance
ToyVpnService.LocalBinder binder = (ToyVpnService.LocalBinder) service;
mService = binder.getService();
mBound = true;
updateState();
}

@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.i("MyVpn", "onServiceDisconnected");
mBound = false;
updateState();
}
};

@Override
protected void onStart() {
Log.i("MyVpn", "onStart");
super.onStart();

myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("MY_ACTION");
registerReceiver(myReceiver, intentFilter);

bindService(new Intent(this, ToyVpnService.class), mConnection, 0);
}

@Override
protected void onStop() {
Log.i("MyVpn", "onStop");
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}

unregisterReceiver(myReceiver);
}

@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("MyVpn", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.form);

mServerAddress = (TextView) findViewById(R.id.address);
mServerPort = (TextView) findViewById(R.id.port);
mSharedSecret = (TextView) findViewById(R.id.secret);

mServerAddress.setText("172.17.9.88");
mServerPort.setText("8000");

connectBtn = (Button) findViewById(R.id.connect);
disconnectBtn = (Button) findViewById(R.id.disconnect);

connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = VpnService.prepare(ToyVpnClient.this);
if (intent != null) {
startActivityForResult(intent, 0);
} else {
onActivityResult(0, RESULT_OK, null);
}
}
});

disconnectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToyVpnClient.this.mService.disconnect();
}
});

updateState();
}

private void updateState() {
if (mBound) {
// service started
boolean connected = mService.isConnected();
connectBtn.setEnabled(!connected);
disconnectBtn.setEnabled(connected);
} else {
// service stopped
connectBtn.setEnabled(true);
disconnectBtn.setEnabled(false);
}
}

@Override
protected void onActivityResult(int request, int result, Intent data) {
Log.i("MyVpn", "onActivityResult");
if (result == RESULT_OK) {
String prefix = getPackageName();
Intent intent = new Intent(this, ToyVpnService.class)
.putExtra(prefix + ".ADDRESS", mServerAddress.getText().toString())
.putExtra(prefix + ".PORT", mServerPort.getText().toString())
.putExtra(prefix + ".SECRET", mSharedSecret.getText().toString());
startService(intent);
}
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
//int connected = arg1.getIntExtra("state", 0);
updateState();
}
}
}