Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of -V flag #1742

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/src/main/java/com/github/shadowsocks/JniHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public static boolean waitForCompat(@NonNull Process process, long millis) throw
private static native Integer getExitValue(Process process);
private static native Object getExitValueMutex(Process process);
public static native int sendFd(int fd, @NonNull String path);
public static native void close(int fd);
@Nullable
public static native byte[] parseNumericAddress(@NonNull String str);
}
4 changes: 0 additions & 4 deletions core/src/main/jni/jni-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ JNIEXPORT jobject JNICALL
return env->GetObjectField(process, ProcessImpl_exitValueMutex);
}

JNIEXPORT void JNICALL Java_com_github_shadowsocks_JniHelper_close(JNIEnv *env, jobject thiz, jint fd) {
close(fd);
}

JNIEXPORT jint JNICALL
Java_com_github_shadowsocks_JniHelper_sendFd(JNIEnv *env, jobject thiz, jint tun_fd, jstring path) {
int fd;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/jni/shadowsocks-libev
7 changes: 1 addition & 6 deletions mobile/src/main/java/com/github/shadowsocks/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ import com.mikepenz.materialdrawer.model.PrimaryDrawerItem
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
import java.io.IOException
import java.net.HttpURLConnection
import java.net.InetSocketAddress
import java.net.Proxy
import java.net.URL
import java.util.*

Expand Down Expand Up @@ -166,10 +164,7 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Interface, Drawe
Acl.CHINALIST -> "www.qualcomm.cn"
else -> "www.google.com"
}, "/generate_204")
val conn = (if (BaseService.usingVpnMode) url.openConnection() else
url.openConnection(Proxy(Proxy.Type.SOCKS,
InetSocketAddress("127.0.0.1", DataStore.portProxy))))
as HttpURLConnection
val conn = url.openConnection(DataStore.proxy) as HttpURLConnection
conn.setRequestProperty("Connection", "close")
conn.instanceFollowRedirects = false
conn.useCaches = false
Expand Down
5 changes: 3 additions & 2 deletions mobile/src/main/java/com/github/shadowsocks/acl/AclSyncJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.util.Log
import com.evernote.android.job.Job
import com.evernote.android.job.JobCreator
import com.evernote.android.job.JobRequest
import com.github.shadowsocks.preference.DataStore
import java.io.IOException
import java.net.URL
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -54,8 +55,8 @@ class AclSyncJob(private val route: String) : Job() {
}

override fun onRunJob(params: Params): Result = try {
val acl = URL("https://shadowsocks.org/acl/android/v1/$route.acl").openStream().bufferedReader()
.use { it.readText() }
val acl = URL("https://shadowsocks.org/acl/android/v1/$route.acl").openConnection(DataStore.proxy)
.getInputStream().bufferedReader().use { it.readText() }
Acl.getFile(route).printWriter().use { it.write(acl) }
Result.SUCCESS
} catch (e: IOException) {
Expand Down
11 changes: 4 additions & 7 deletions mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,9 @@ object BaseService {
.put("method", profile.method)
val pluginPath = pluginPath
if (pluginPath != null) {
val pluginCmd = arrayListOf(pluginPath)
if (TcpFastOpen.sendEnabled) pluginCmd.add("--fast-open")
if (TcpFastOpen.sendEnabled) plugin["fast-open"] = null
config
.put("plugin", Commandline.toString(service.buildAdditionalArguments(pluginCmd)))
.put("plugin", pluginPath)
.put("plugin_opts", plugin.toString())
}
// sensitive Shadowsocks config is stored in
Expand Down Expand Up @@ -251,18 +250,16 @@ object BaseService {
}
}

fun buildAdditionalArguments(cmd: ArrayList<String>): ArrayList<String> = cmd

fun startNativeProcesses() {
val data = data
val profile = data.profile!!
val cmd = buildAdditionalArguments(arrayListOf(
val cmd = arrayListOf(
File((this as Context).applicationInfo.nativeLibraryDir, Executable.SS_LOCAL).absolutePath,
"-u",
"-b", "127.0.0.1",
"-l", DataStore.portProxy.toString(),
"-t", "600",
"-c", data.buildShadowsocksConfig().absolutePath))
"-c", data.buildShadowsocksConfig().absolutePath)

val acl = data.aclFile
if (acl != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ object LocalDnsService {
return file
}

if (!profile.udpdns) overtureProcess = GuardedProcess(buildAdditionalArguments(arrayListOf(
if (!profile.udpdns) overtureProcess = GuardedProcess(arrayListOf(
File(app.applicationInfo.nativeLibraryDir, Executable.OVERTURE).absolutePath,
"-c", buildOvertureConfig("overture.conf")
))).start()
)).start()
}

override fun killProcesses() {
Expand Down
68 changes: 7 additions & 61 deletions mobile/src/main/java/com/github/shadowsocks/bg/VpnService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.*
import android.os.Build
import android.os.IBinder
import android.os.ParcelFileDescriptor
import android.support.v4.os.BuildCompat
Expand All @@ -41,9 +40,7 @@ import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Subnet
import com.github.shadowsocks.utils.parseNumericAddress
import java.io.File
import java.io.FileDescriptor
import java.io.IOException
import java.lang.reflect.Method
import java.util.*
import android.net.VpnService as BaseVpnService

Expand All @@ -53,8 +50,6 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
private const val PRIVATE_VLAN = "26.26.26.%s"
private const val PRIVATE_VLAN6 = "fdfe:dcba:9876::%s"

private val getInt: Method = FileDescriptor::class.java.getDeclaredMethod("getInt$")

/**
* Unfortunately registerDefaultNetworkCallback is going to return VPN interface since Android P DP1:
* https://android.googlesource.com/platform/frameworks/base/+/dda156ab0c5d66ad82bdcf76cda07cbc0a9c8a2e
Expand All @@ -71,39 +66,6 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
.build()
}

private inner class ProtectWorker : LocalSocketListener("ShadowsocksVpnThread") {
override val socketFile: File = File(app.deviceContext.filesDir, "protect_path")

override fun accept(socket: LocalSocket) {
var success = false
try {
socket.inputStream.read()
val fd = socket.ancillaryFileDescriptors!!.single()!!
val fdInt = getInt.invoke(fd) as Int
try {
val network = underlyingNetwork
success = if (network != null && Build.VERSION.SDK_INT >= 23) {
network.bindSocket(fd)
true
} else protect(fdInt)
} catch (e: Exception) {
Log.e(tag, "Error when protect socket", e)
app.track(e)
} finally {
JniHelper.close(fdInt) // Trick to close file decriptor
}
} catch (e: Exception) {
Log.e(tag, "Error when receiving ancillary fd", e)
app.track(e)
}
try {
socket.outputStream.write(if (success) 0 else 1)
} catch (e: IOException) {
Log.e(tag, "Error when returning result in protect", e)
app.track(e)
}
}
}
class NullConnectionException : NullPointerException()

init {
Expand All @@ -115,27 +77,20 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
ServiceNotification(this, profileName, "service-vpn")

private var conn: ParcelFileDescriptor? = null
private var worker: ProtectWorker? = null
private var tun2socksProcess: GuardedProcess? = null
private var underlyingNetwork: Network? = null
@TargetApi(28)
set(value) {
setUnderlyingNetworks(if (value == null) null else arrayOf(value))
field = value
}

private val connectivity by lazy { getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager }
@TargetApi(28)
private val defaultNetworkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
underlyingNetwork = network
setUnderlyingNetworks(arrayOf(network))
}
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities?) {
// it's a good idea to refresh capabilities
underlyingNetwork = network
setUnderlyingNetworks(arrayOf(network))
}
override fun onLost(network: Network) {
underlyingNetwork = null
setUnderlyingNetworks(null)
}
}
private var listeningForDefaultNetwork = false
Expand All @@ -152,8 +107,6 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
connectivity.unregisterNetworkCallback(defaultNetworkCallback)
listeningForDefaultNetwork = false
}
worker?.stopThread()
worker = null
super.killProcesses()
tun2socksProcess?.destroy()
tun2socksProcess = null
Expand All @@ -172,21 +125,12 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
}

override fun startNativeProcesses() {
val worker = ProtectWorker()
worker.start()
this.worker = worker

super.startNativeProcesses()

val fd = startVpn()
if (!sendFd(fd)) throw IOException("sendFd failed")
}

override fun buildAdditionalArguments(cmd: ArrayList<String>): ArrayList<String> {
cmd += "-V"
return cmd
}

private fun startVpn(): Int {
val profile = data.profile!!
val builder = Builder()
Expand All @@ -202,8 +146,8 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
builder.addRoute("::", 0)
}

val me = packageName
if (profile.proxyApps) {
val me = packageName
profile.individual.split('\n')
.filter { it != me }
.forEach {
Expand All @@ -214,7 +158,9 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
Log.e(tag, "Invalid package name", ex)
}
}
if (!profile.bypass) builder.addAllowedApplication(me)
if (profile.bypass) builder.addDisallowedApplication(me)
} else {
builder.addDisallowedApplication(me)
}

when (profile.route) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import com.github.shadowsocks.database.PublicDatabase
import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.Key
import com.github.shadowsocks.utils.parsePort
import java.net.InetSocketAddress
import java.net.Proxy

object DataStore {
val publicStore = OrmLitePreferenceDataStore(PublicDatabase.kvPairDao)
Expand Down Expand Up @@ -64,6 +66,8 @@ object DataStore {
get() = getLocalPort(Key.portTransproxy, 8200)
set(value) = publicStore.putString(Key.portTransproxy, value.toString())

val proxy get() = Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", portProxy))

fun initGlobal() {
// temporary workaround for support lib bug
if (publicStore.getString(Key.serviceMode) == null) serviceMode = serviceMode
Expand Down