Skip to content

Commit

Permalink
improve connect speed
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Feb 11, 2024
1 parent b610738 commit 01fe69a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
35 changes: 32 additions & 3 deletions app/src/main/java/org/bepass/oblivion/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import com.suke.widget.SwitchButton;

import java.io.IOException;
import java.net.ServerSocket;

import tun2socks.StartOptions;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -207,14 +210,40 @@ private boolean isMyServiceRunning() {
return false;
}

private static int findFreePort() {
ServerSocket socket = null;
try {
socket = new ServerSocket(0);
socket.setReuseAddress(true);
int port = socket.getLocalPort();
try {
socket.close();
} catch (IOException e) {
// Ignore IOException on close()
}
return port;
} catch (IOException e) {
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on");
}

private String getBindAddress() {
String B = "";
String port = fileManager.getString("USERSETTING_port");
boolean enableLan = fileManager.getBoolean("USERSETTING_lan");
if(OblivionVpnService.isLocalPortInUse("127.0.0.1:" + port).equals("true")) {
port = findFreePort()+"";
}
String Bind = "";
Bind += B + "127.0.0.1:" + port;
Bind += "127.0.0.1:" + port;
if(enableLan) {
Bind = B + "0.0.0.0:" + port;
Bind = "0.0.0.0:" + port;
}
return Bind;
}
Expand Down
46 changes: 26 additions & 20 deletions app/src/main/java/org/bepass/oblivion/OblivionVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
import java.lang.ref.WeakReference;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -98,7 +102,7 @@ public static Map<String, Integer> splitHostAndPort(String hostPort) {
public static String pingOverHTTP(String bindAddress) {
Map<String, Integer> result = splitHostAndPort(bindAddress);
if (result == null) {
return "false";
return "exception";
}
String socksHost = result.keySet().iterator().next();
int socksPort = result.values().iterator().next();
Expand All @@ -115,7 +119,7 @@ public static String pingOverHTTP(String bindAddress) {

// Build the request
Request request = new Request.Builder()
.url("https://8.8.8.8") // Replace with actual URL
.url("https://1.1.1.1") // Replace with actual URL
.build();

// Execute the request
Expand All @@ -127,13 +131,30 @@ public static String pingOverHTTP(String bindAddress) {
}
}

public static String isLocalPortInUse(String bindAddress) {
Map<String, Integer> result = splitHostAndPort(bindAddress);
if (result == null) {
return "exception";
}
int socksPort = result.values().iterator().next();
try {
// ServerSocket try to open a LOCAL port
new ServerSocket(socksPort).close();
// local port can be opened, it's available
return "false";
} catch(IOException e) {
// local port cannot be opened, it's in use
return "true";
}
}

private static void performPingTask(Message msg, String bindAddress) {
new Thread(() -> {
long startTime = System.currentTimeMillis();
boolean isSuccessful = false;

while (System.currentTimeMillis() - startTime < 2 * 60 * 1000) { // 2 minutes
String result = pingOverHTTP(bindAddress);
String result = isLocalPortInUse(bindAddress);
if (result.contains("exception")) {
Message replyMsg = Message.obtain(null, MSG_TASK_FAILED);
try {
Expand Down Expand Up @@ -316,21 +337,6 @@ private void createNotification() {
.build();
}

private String getBindAddress(boolean addB) {
String B = " -b ";
if(!addB) {
B = "";
}
String port = fileManager.getString("USERSETTING_port", "");
boolean enableLan = fileManager.getBoolean("USERSETTING_lan", false);
String Bind = "";
Bind += B + "127.0.0.1:" + port;
if(enableLan) {
Bind = B + "0.0.0.0:" + port;
}
return Bind;
}

private StartOptions calculateArgs() {
StartOptions so = new StartOptions();
so.setPath(getApplicationContext().getFilesDir().getAbsolutePath());
Expand All @@ -350,7 +356,7 @@ private StartOptions calculateArgs() {
so.setScan(true);
}

so.setBindAddress(getBindAddress(false));
so.setBindAddress(bindAddress);

if(!license.trim().isEmpty()) {
so.setLicense(license.trim());
Expand All @@ -369,7 +375,7 @@ private StartOptions calculateArgs() {
so.setGool(true);
}

so.setRtt(1000);
so.setRtt(800);

return so;
}
Expand Down

0 comments on commit 01fe69a

Please sign in to comment.