Commit b3aba5c7 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent ab2ed827
......@@ -43,7 +43,7 @@ dependencies {
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
compile 'com.jakewharton:process-phoenix:2.0.0'
compile 'com.github.stealthcopter:AndroidNetworkTools:0.4.0'
}
......
package org.telegram;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.stealthcopter.networktools.Ping;
import com.stealthcopter.networktools.ping.PingResult;
import com.stealthcopter.networktools.ping.PingStats;
import org.telegram.service.SocketService;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, SocketService.class));
}
......
......@@ -4,6 +4,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.telegram.MainActivity;
import org.telegram.service.SocketService;
/**
......@@ -12,6 +13,6 @@ import org.telegram.service.SocketService;
public class AppStartReceiver extends BroadcastReceiver {
@Override
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 {
public void setConnect(Boolean connect) {
this.connect = connect;
}
public String getType() {
return type;
}
}
......@@ -8,9 +8,6 @@ import android.util.Log;
import com.google.gson.Gson;
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.CryptLib;
......@@ -19,6 +16,9 @@ import org.telegram.io.SingletonSocket;
import org.telegram.io.Utils;
import org.telegram.tgnet.ConnectionsManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.TimeUnit;
......@@ -102,6 +102,12 @@ public class SocketService extends Service implements Emitter.Listener {
return Observable.create(subscriber -> {
if (proxy.getType().equals("especial"))
{
proxy.setConnect(true);
subscriber.onNext(proxy);
subscriber.onCompleted();
}
ConnectionsManager.setCaller(status -> {
Log.e("Proxy Connect", "IP :" + proxy.getIp());
proxy.setConnect(true);
......@@ -119,28 +125,39 @@ public class SocketService extends Service implements Emitter.Listener {
return Observable.create(subscriber -> {
Ping.onAddress(proxy.getIp()).setTimeOutMillis(1000).setTimes(10).doPing(new Ping.PingListener() {
@Override
public void onResult(PingResult pingResult) {
try {
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
public void onFinished(PingStats pingStats) {
if (pingStats.getPacketsLost() >=5)
proxy.setConnect(false);
else
proxy.setConnect(true);
subscriber.onNext(proxy);
subscriber.onCompleted();
int packetRes = Integer.parseInt(res[1].replace("received", "").trim());
}
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 {
.subscribeOn(Schedulers.newThread())
.concatMap(proxy -> getPingServerObservable(proxy)
.subscribeOn(Schedulers.newThread())
.timeout(3, TimeUnit.SECONDS)
.timeout(5, TimeUnit.SECONDS)
.onErrorResumeNext(throwable -> {
ConnectionsManager.setCaller(null);
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