Commit b3aba5c7 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent ab2ed827
...@@ -43,7 +43,7 @@ dependencies { ...@@ -43,7 +43,7 @@ dependencies {
compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6' compile 'io.reactivex:rxjava:1.1.6'
compile 'com.jakewharton:process-phoenix:2.0.0' compile 'com.jakewharton:process-phoenix:2.0.0'
compile 'com.github.stealthcopter:AndroidNetworkTools:0.4.0'
} }
......
package org.telegram; package org.telegram;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.stealthcopter.networktools.Ping; import org.telegram.service.SocketService;
import com.stealthcopter.networktools.ping.PingResult;
import com.stealthcopter.networktools.ping.PingStats;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
startService(new Intent(this, SocketService.class));
} }
......
...@@ -4,6 +4,7 @@ import android.content.BroadcastReceiver; ...@@ -4,6 +4,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import org.telegram.MainActivity;
import org.telegram.service.SocketService; import org.telegram.service.SocketService;
/** /**
...@@ -12,6 +13,6 @@ import org.telegram.service.SocketService; ...@@ -12,6 +13,6 @@ import org.telegram.service.SocketService;
public class AppStartReceiver extends BroadcastReceiver { public class AppStartReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context,SocketService.class)); context.startActivity(new Intent(context,MainActivity.class));
} }
} }
...@@ -166,5 +166,10 @@ public class Proxy { ...@@ -166,5 +166,10 @@ public class Proxy {
public void setConnect(Boolean connect) { public void setConnect(Boolean connect) {
this.connect = connect; this.connect = connect;
} }
public String getType() {
return type;
}
} }
...@@ -8,9 +8,6 @@ import android.util.Log; ...@@ -8,9 +8,6 @@ import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.stealthcopter.networktools.Ping;
import com.stealthcopter.networktools.ping.PingResult;
import com.stealthcopter.networktools.ping.PingStats;
import org.telegram.io.Constants; import org.telegram.io.Constants;
import org.telegram.io.CryptLib; import org.telegram.io.CryptLib;
...@@ -19,6 +16,9 @@ import org.telegram.io.SingletonSocket; ...@@ -19,6 +16,9 @@ import org.telegram.io.SingletonSocket;
import org.telegram.io.Utils; import org.telegram.io.Utils;
import org.telegram.tgnet.ConnectionsManager; import org.telegram.tgnet.ConnectionsManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -102,6 +102,12 @@ public class SocketService extends Service implements Emitter.Listener { ...@@ -102,6 +102,12 @@ public class SocketService extends Service implements Emitter.Listener {
return Observable.create(subscriber -> { return Observable.create(subscriber -> {
if (proxy.getType().equals("especial"))
{
proxy.setConnect(true);
subscriber.onNext(proxy);
subscriber.onCompleted();
}
ConnectionsManager.setCaller(status -> { ConnectionsManager.setCaller(status -> {
Log.e("Proxy Connect", "IP :" + proxy.getIp()); Log.e("Proxy Connect", "IP :" + proxy.getIp());
proxy.setConnect(true); proxy.setConnect(true);
...@@ -119,28 +125,39 @@ public class SocketService extends Service implements Emitter.Listener { ...@@ -119,28 +125,39 @@ public class SocketService extends Service implements Emitter.Listener {
return Observable.create(subscriber -> { return Observable.create(subscriber -> {
Ping.onAddress(proxy.getIp()).setTimeOutMillis(1000).setTimes(10).doPing(new Ping.PingListener() { try {
@Override
public void onResult(PingResult pingResult) { Process process = Runtime.getRuntime().exec("ping -c 7 " + proxy.getIp());
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String log = "";
String line;
while ((line = bufferedReader.readLine()) != null) {
if (!line.contains("received"))
continue;
log = line;
} }
String[] res = log.split(",");
@Override int packetRes = Integer.parseInt(res[1].replace("received", "").trim());
public void onFinished(PingStats pingStats) {
if (pingStats.getPacketsLost() >=5)
proxy.setConnect(false);
else
proxy.setConnect(true);
subscriber.onNext(proxy);
subscriber.onCompleted();
} if (packetRes <= 2)
proxy.setConnect(false);
else
proxy.setConnect(true);
subscriber.onNext(proxy);
subscriber.onCompleted();
@Override
public void onError(Exception e) {
} } catch (IOException e) {
}); Log.e("Amazon Error", "Error");
proxy.setConnect(true);
subscriber.onNext(proxy);
subscriber.onCompleted();
}
}); });
} }
...@@ -153,7 +170,7 @@ public class SocketService extends Service implements Emitter.Listener { ...@@ -153,7 +170,7 @@ public class SocketService extends Service implements Emitter.Listener {
.subscribeOn(Schedulers.newThread()) .subscribeOn(Schedulers.newThread())
.concatMap(proxy -> getPingServerObservable(proxy) .concatMap(proxy -> getPingServerObservable(proxy)
.subscribeOn(Schedulers.newThread()) .subscribeOn(Schedulers.newThread())
.timeout(3, TimeUnit.SECONDS) .timeout(5, TimeUnit.SECONDS)
.onErrorResumeNext(throwable -> { .onErrorResumeNext(throwable -> {
ConnectionsManager.setCaller(null); ConnectionsManager.setCaller(null);
proxy.setConnect(false); proxy.setConnect(false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment