Commit e19e6294 authored by Mygod's avatar Mygod

Move catch blocks to child classes

This reverts commit 07a48bc2 and f870093c.
parent c50ef11b
...@@ -70,5 +70,4 @@ public class JniHelper { ...@@ -70,5 +70,4 @@ public class JniHelper {
private static native Object getExitValueMutex(Process process); private static native Object getExitValueMutex(Process process);
public static native int sendFd(int fd, String path); public static native int sendFd(int fd, String path);
public static native void close(int fd); public static native void close(int fd);
public static native void unlink(String path);
} }
...@@ -72,12 +72,6 @@ JNIEXPORT void JNICALL Java_com_github_shadowsocks_JniHelper_close(JNIEnv *env, ...@@ -72,12 +72,6 @@ JNIEXPORT void JNICALL Java_com_github_shadowsocks_JniHelper_close(JNIEnv *env,
close(fd); close(fd);
} }
JNIEXPORT void JNICALL Java_com_github_shadowsocks_JniHelper_unlink(JNIEnv *env, jobject thiz, jstring path) {
const char *sock_str = env->GetStringUTFChars(path, 0);
unlink(sock_str);
env->ReleaseStringUTFChars(path, sock_str);
}
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_com_github_shadowsocks_JniHelper_sendFd(JNIEnv *env, jobject thiz, jint tun_fd, jstring path) { Java_com_github_shadowsocks_JniHelper_sendFd(JNIEnv *env, jobject thiz, jint tun_fd, jstring path) {
int fd; int fd;
......
...@@ -23,8 +23,6 @@ ...@@ -23,8 +23,6 @@
-keepattributes *DatabaseField* -keepattributes *DatabaseField*
-keepattributes *DatabaseTable* -keepattributes *DatabaseTable*
-keepattributes *SerializedName* -keepattributes *SerializedName*
-keepattributes Exceptions, InnerClasses
-keep class com.j256.** -keep class com.j256.**
-keepclassmembers class com.j256.** { *; } -keepclassmembers class com.j256.** { *; }
-keep enum com.j256.** -keep enum com.j256.**
......
...@@ -25,75 +25,46 @@ import android.net.LocalSocket ...@@ -25,75 +25,46 @@ import android.net.LocalSocket
import android.net.LocalSocketAddress import android.net.LocalSocketAddress
import android.util.Log import android.util.Log
import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.App.Companion.app
import com.github.shadowsocks.JniHelper
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.atomic.AtomicReference
abstract class LocalSocketListener(private val tag: String) : Thread() { abstract class LocalSocketListener(protected val tag: String) : Thread() {
init { init {
setUncaughtExceptionHandler(app::track) setUncaughtExceptionHandler(app::track)
} }
protected abstract val socketFile: File protected abstract val socketFile: File
private val localSocket = AtomicReference<LocalSocket?>() private val serverSocket = AtomicReference<LocalServerSocket?>()
/** /**
* Inherited class do not need to close input/output streams as they will be closed automatically. * Inherited class do not need to close input/output streams as they will be closed automatically.
*/ */
protected abstract fun accept(socket: LocalSocket) protected abstract fun accept(socket: LocalSocket)
override fun run() { override fun run() {
socketFile.delete()
JniHelper.unlink(socketFile.absolutePath) try {
Thread.sleep(1000) // trick to close the previous local socket safely val localSocket = LocalSocket()
localSocket.bind(LocalSocketAddress(socketFile.absolutePath, LocalSocketAddress.Namespace.FILESYSTEM))
val serverSocket: LocalServerSocket? = try { serverSocket.set(LocalServerSocket(localSocket.fileDescriptor))
val ls = LocalSocket()
localSocket.set(ls)
ls.bind(LocalSocketAddress(socketFile.absolutePath, LocalSocketAddress.Namespace.FILESYSTEM))
LocalServerSocket(ls.fileDescriptor)
} catch (e: IOException) { } catch (e: IOException) {
Log.e(tag, "unable to bind", e) Log.e(tag, "unable to bind", e)
null return
} }
while (true) { while (true) {
val serverSocket = serverSocket.get() ?: return
try { try {
val socket = serverSocket?.accept() ?: break serverSocket.accept()
try {
accept(socket)
} catch (e: Exception) {
Log.e(tag, "Error when recv traffic stat", e)
app.track(e)
}
try {
socket.close()
} catch (e: IOException) {
Log.e(tag, "Error when closing the local socket", e)
app.track(e)
}
} catch (e: IOException) { } catch (e: IOException) {
Log.e(tag, "Error when accept socket", e) Log.e(tag, "Error when accept socket", e)
app.track(e) app.track(e)
break null
} }?.use(this::accept)
}
try {
serverSocket?.close()
} catch (e: IOException) {
Log.e(tag, "Error when closing socket", e)
app.track(e)
} }
Log.d(tag, "thread exit")
} }
fun stopThread() { fun stopThread() {
val old = localSocket.getAndSet(null) ?: return val old = serverSocket.getAndSet(null) ?: return
try { try {
old.close() old.close()
} catch (_: Exception) { } // ignore } catch (_: Exception) { } // ignore
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package com.github.shadowsocks.bg package com.github.shadowsocks.bg
import android.net.LocalSocket import android.net.LocalSocket
import android.util.Log
import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.App.Companion.app
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
...@@ -31,10 +32,15 @@ class TrafficMonitorThread : LocalSocketListener("TrafficMonitorThread") { ...@@ -31,10 +32,15 @@ class TrafficMonitorThread : LocalSocketListener("TrafficMonitorThread") {
override val socketFile = File(app.filesDir, "/stat_path") override val socketFile = File(app.filesDir, "/stat_path")
override fun accept(socket: LocalSocket) { override fun accept(socket: LocalSocket) {
try {
val buffer = ByteArray(16) val buffer = ByteArray(16)
if (socket.inputStream.read(buffer) != 16) throw IOException("Unexpected traffic stat length") if (socket.inputStream.read(buffer) != 16) throw IOException("Unexpected traffic stat length")
val stat = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN) val stat = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN)
TrafficMonitor.update(stat.getLong(0), stat.getLong(8)) TrafficMonitor.update(stat.getLong(0), stat.getLong(8))
socket.outputStream.write(0) socket.outputStream.write(0)
} catch (e: Exception) {
Log.e(tag, "Error when recv traffic stat", e)
app.track(e)
}
} }
} }
...@@ -55,6 +55,7 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface { ...@@ -55,6 +55,7 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
override val socketFile: File = File(filesDir, "protect_path") override val socketFile: File = File(filesDir, "protect_path")
override fun accept(socket: LocalSocket) { override fun accept(socket: LocalSocket) {
try {
socket.inputStream.read() socket.inputStream.read()
val fds = socket.ancillaryFileDescriptors val fds = socket.ancillaryFileDescriptors
if (fds.isEmpty()) return if (fds.isEmpty()) return
...@@ -62,6 +63,10 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface { ...@@ -62,6 +63,10 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
val ret = protect(fd) val ret = protect(fd)
JniHelper.close(fd) // Trick to close file decriptor JniHelper.close(fd) // Trick to close file decriptor
socket.outputStream.write(if (ret) 0 else 1) socket.outputStream.write(if (ret) 0 else 1)
} catch (e: Exception) {
Log.e(tag, "Error when protect socket", e)
app.track(e)
}
} }
} }
class NullConnectionException : NullPointerException() class NullConnectionException : NullPointerException()
......
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