Пытаюсь создать VpnClient на Android. Создал сервис:
public class VPNService extends VpnService {
private ParcelFileDescriptor pfd;
Thread clientReaderThread, serverReaderThread;
private boolean stop;
private ServiceBinder binder;
private Socket vpnSocket;
@Override
public void onCreate() {
super.onCreate();
binder = new ServiceBinder();
}
@Override
public int onStartCommand(Intent arg0, int arg1, int arg2) {
VpnService.Builder vpnBuilder = new VpnService.Builder();
vpnBuilder.addAddress("192.168.35.1", 24);
vpnBuilder.addRoute("0.0.0.0", 0);
try {
pfd = vpnBuilder.establish();
} catch (Exception e) {
Log.e("MyVPN", e.toString());
}
new Thread(() -> {
try {
vpnSocket = new Socket();
vpnSocket.bind(new InetSocketAddress(0));
boolean bol = protect(vpnSocket);
vpnSocket.connect(new InetSocketAddress("178.68.14.91", 4410));
} catch (IOException e) {
Log.e("MyVPN", e.toString() + " ");
}
}).start();
ClientReader clientReader = new ClientReader(new FileInputStream(pfd.getFileDescriptor()));
ServerReader serverReader = new ServerReader(new FileOutputStream(pfd.getFileDescriptor()));
clientReaderThread = new Thread(clientReader);
serverReaderThread = new Thread(serverReader);
clientReaderThread.start();
//serverReaderThread.start();
return START_NOT_STICKY;
}
public void stop() {
if (pfd != null) {
try {
pfd.close();
} catch (IOException e) {
}
}
stop = true;
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onDestroy() {
stop();
super.onDestroy();
}
private class ClientReader implements Runnable {
FileInputStream in;
public ClientReader(FileInputStream in) {
this.in = in;
}
@Override
public void run() {
byte[] buffer = new byte[4096];
int read;
OutputStream outStream;
try {
outStream = vpnSocket.getOutputStream();
} catch (Exception e) {
Log.i("MyVPN", e.toString());
return;
}
while (!stop) {
try {
read = in.read(buffer);
outStream.write(buffer, 0, read);
} catch (IOException e) {
//Log.i("MyVPN", e.toString());
}
}
}
}
private class ServerReader implements Runnable {
FileOutputStream out;
public ServerReader(FileOutputStream out) {
this.out = out;
}
@Override
public void run() {
while (!stop) {
}
}
}
protected class ServiceBinder extends Binder {
protected VPNService getService() {
return VPNService.this;
}
}
При этом после включения VPN до IP 178.68.14.91 по сокету vpnSocket ничего не приходит. Нет даже соединения, то есть не срабатывает даже accept на ServerSocket. Хотя protect возвращает true. А если VPN убрать, то по этому сокету всё отлично доходит до получателя.