Skip to content

Commit 2227ea3

Browse files
authored
Merge pull request #129 from galvasis193/feature-ProxySharing
Add proxy sharing function. Fix potential security vulnerbility.
2 parents 2d8ed22 + 4d0e9e2 commit 2227ea3

File tree

11 files changed

+141
-31
lines changed

11 files changed

+141
-31
lines changed

V2rayNG/app/src/main/kotlin/com/v2ray/ang/AngApplication.kt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AngApplication : Application() {
1111
const val PREF_LAST_VERSION = "pref_last_version"
1212
}
1313

14+
var curIndex = -1 //Current proxy that is opened. (Used to implement restart feature)
1415
var firstRun = false
1516
private set
1617

V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class V2RayVpnService : VpnService() {
6464
private var mNotificationManager: NotificationManager? = null
6565

6666

67-
6867
/**
6968
* Unfortunately registerDefaultNetworkCallback is going to return our VPN interface: https://android.googlesource.com/platform/frameworks/base/+/dda156ab0c5d66ad82bdcf76cda07cbc0a9c8a2e
7069
*
@@ -282,12 +281,19 @@ class V2RayVpnService : VpnService() {
282281
unregisterReceiver(mMsgReceive)
283282
} catch (e: Exception) {
284283
}
284+
285+
//stopSelf has to be called ahead of mInterface.close(). otherwise v2ray core cannot be stooped
286+
//It's strage but true.
287+
//This can be verified by putting stopself() behind and call stopLoop and startLoop
288+
//in a row for several times. You will find that later created v2ray core report port in use
289+
//which means the first v2ray core somehow failed to stop and release the port.
290+
stopSelf()
291+
285292
try {
286293
mInterface.close()
287294
} catch (ignored: Exception) {
288295
}
289296

290-
stopSelf()
291297
}
292298
}
293299

V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
232232

233233
R.id.export_all -> {
234234
if (AngConfigManager.shareAll2Clipboard() == 0) {
235-
toast(R.string.toast_success)
235+
//remove toast, otherwise it will block previous warning message
236236
} else {
237237
toast(R.string.toast_failure)
238238
}

V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt

+95-25
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import android.content.Intent
44
import android.content.SharedPreferences
55
import android.net.Uri
66
import android.os.Bundle
7-
import android.preference.CheckBoxPreference
8-
import android.preference.EditTextPreference
9-
import android.preference.Preference
10-
import android.preference.PreferenceFragment
7+
import android.preference.*
8+
import com.v2ray.ang.AngApplication
119
import com.v2ray.ang.BuildConfig
1210
//import com.v2ray.ang.InappBuyActivity
1311
import com.v2ray.ang.R
@@ -29,6 +27,7 @@ class SettingsActivity : BaseActivity() {
2927
// const val PREF_MUX_ENAimport libv2ray.Libv2rayBLED = "pref_mux_enabled"
3028
const val PREF_SPEED_ENABLED = "pref_speed_enabled"
3129
const val PREF_SNIFFING_ENABLED = "pref_sniffing_enabled"
30+
const val PREF_PROXY_SHARING = "pref_proxy_sharing_enabled"
3231
const val PREF_LOCAL_DNS_ENABLED = "pref_local_dns_enabled"
3332
const val PREF_REMOTE_DNS = "pref_remote_dns"
3433
const val PREF_DOMESTIC_DNS = "pref_domestic_dns"
@@ -59,12 +58,19 @@ class SettingsActivity : BaseActivity() {
5958

6059
class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
6160
val perAppProxy by lazy { findPreference(PREF_PER_APP_PROXY) as CheckBoxPreference }
62-
// val autoRestart by lazy { findPreference(PREF_AUTO_RESTART) as CheckBoxPreference }
63-
val remoteDns by lazy { findPreference(PREF_REMOTE_DNS) as EditTextPreference }
64-
val domesticDns by lazy { findPreference(PREF_DOMESTIC_DNS) as EditTextPreference }
61+
val sppedEnabled by lazy { findPreference(PREF_SPEED_ENABLED) as CheckBoxPreference }
62+
val sniffingEnabled by lazy { findPreference(PREF_SNIFFING_ENABLED) as CheckBoxPreference }
63+
val proxySharing by lazy { findPreference(PREF_PROXY_SHARING) as CheckBoxPreference }
64+
val domainStrategy by lazy { findPreference(PREF_ROUTING_DOMAIN_STRATEGY) as ListPreference }
65+
val routingMode by lazy { findPreference(PREF_ROUTING_MODE) as ListPreference }
6566

66-
val enableLocalDns by lazy { findPreference(PREF_LOCAL_DNS_ENABLED) as CheckBoxPreference }
6767
val forwardIpv6 by lazy { findPreference(PREF_FORWARD_IPV6) as CheckBoxPreference }
68+
val enableLocalDns by lazy { findPreference(PREF_LOCAL_DNS_ENABLED) as CheckBoxPreference }
69+
val domesticDns by lazy { findPreference(PREF_DOMESTIC_DNS) as EditTextPreference }
70+
val remoteDns by lazy { findPreference(PREF_REMOTE_DNS) as EditTextPreference }
71+
72+
// val autoRestart by lazy { findPreference(PREF_AUTO_RESTART) as CheckBoxPreference }
73+
6874

6975
// val socksPort by lazy { findPreference(PREF_SOCKS_PORT) as EditTextPreference }
7076
// val httpPort by lazy { findPreference(PREF_HTTP_PORT) as EditTextPreference }
@@ -76,14 +82,95 @@ class SettingsActivity : BaseActivity() {
7682
// val tgGroup: Preference by lazy { findPreference(PREF_TG_GROUP) }
7783
val version: Preference by lazy { findPreference(PREF_VERSION) }
7884

85+
private fun restartProxy() {
86+
Utils.stopVService(activity)
87+
Utils.startVService(activity)
88+
}
89+
90+
private fun isRunning(): Boolean {
91+
return Utils.isServiceRun(activity, "com.v2ray.ang.service.V2RayVpnService")
92+
}
93+
7994
override fun onCreate(savedInstanceState: Bundle?) {
8095
super.onCreate(savedInstanceState)
8196
addPreferencesFromResource(R.xml.pref_settings)
97+
var app = activity.application as AngApplication
98+
99+
perAppProxy.setOnPreferenceClickListener {
100+
if (isRunning()) {
101+
Utils.stopVService(activity)
102+
}
103+
startActivity<PerAppProxyActivity>()
104+
perAppProxy.isChecked = true
105+
true
106+
}
107+
sppedEnabled.setOnPreferenceClickListener {
108+
if (isRunning())
109+
restartProxy()
110+
true
111+
}
112+
sniffingEnabled.setOnPreferenceClickListener {
113+
if (isRunning())
114+
restartProxy()
115+
true
116+
}
117+
118+
proxySharing.setOnPreferenceClickListener {
119+
if (proxySharing.isChecked)
120+
toast(R.string.toast_warning_pref_proxysharing)
121+
if (isRunning())
122+
restartProxy()
123+
true
124+
}
125+
126+
domainStrategy.setOnPreferenceChangeListener { _, _ ->
127+
if (isRunning())
128+
restartProxy()
129+
true
130+
}
131+
routingMode.setOnPreferenceChangeListener { _, _ ->
132+
if (isRunning())
133+
restartProxy()
134+
true
135+
}
82136

83137
routingCustom.onClick {
138+
if (isRunning())
139+
Utils.stopVService(activity)
84140
startActivity<RoutingSettingsActivity>()
85141
}
86142

143+
forwardIpv6.setOnPreferenceClickListener {
144+
if (isRunning())
145+
restartProxy()
146+
true
147+
}
148+
149+
enableLocalDns.setOnPreferenceClickListener {
150+
if (isRunning())
151+
restartProxy()
152+
true
153+
}
154+
155+
156+
domesticDns.setOnPreferenceChangeListener { preference, any ->
157+
// domesticDns.summary = any as String
158+
val nval = any as String
159+
domesticDns.summary = if (nval == "") AppConfig.DNS_DIRECT else nval
160+
if (isRunning())
161+
restartProxy()
162+
true
163+
}
164+
165+
remoteDns.setOnPreferenceChangeListener { preference, any ->
166+
// remoteDns.summary = any as String
167+
val nval = any as String
168+
remoteDns.summary = if (nval == "") AppConfig.DNS_AGENT else nval
169+
if (isRunning())
170+
restartProxy()
171+
true
172+
}
173+
87174
// donate.onClick {
88175
// startActivity<InappBuyActivity>()
89176
// }
@@ -110,24 +197,7 @@ class SettingsActivity : BaseActivity() {
110197
// }
111198
// }
112199

113-
perAppProxy.setOnPreferenceClickListener {
114-
startActivity<PerAppProxyActivity>()
115-
perAppProxy.isChecked = true
116-
false
117-
}
118200

119-
remoteDns.setOnPreferenceChangeListener { preference, any ->
120-
// remoteDns.summary = any as String
121-
val nval = any as String
122-
remoteDns.summary = if (nval == "") AppConfig.DNS_AGENT else nval
123-
true
124-
}
125-
domesticDns.setOnPreferenceChangeListener { preference, any ->
126-
// domesticDns.summary = any as String
127-
val nval = any as String
128-
domesticDns.summary = if (nval == "") AppConfig.DNS_DIRECT else nval
129-
true
130-
}
131201
// socksPort.setOnPreferenceChangeListener { preference, any ->
132202
// socksPort.summary = any as String
133203
// true

V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ object AngConfigManager {
147147
fun setActiveServer(index: Int): Int {
148148
try {
149149
if (index < 0 || index > angConfig.vmess.count() - 1) {
150+
app.curIndex = -1
150151
return -1
151152
}
152153
angConfig.index = index
153-
154+
app.curIndex = index
154155
storeConfigFile()
155156
} catch (e: Exception) {
156157
e.printStackTrace()
158+
app.curIndex = -1
157159
return -1
158160
}
159161
return 0

V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.v2ray.ang.AngApplication
2727
import com.v2ray.ang.AppConfig
2828
import com.v2ray.ang.R
2929
import com.v2ray.ang.extension.responseLength
30+
import com.v2ray.ang.extension.v2RayApplication
3031
import com.v2ray.ang.service.V2RayVpnService
3132
import com.v2ray.ang.ui.SettingsActivity
3233
import kotlinx.android.synthetic.main.activity_logcat.*
@@ -311,7 +312,11 @@ object Utils {
311312
* startVService
312313
*/
313314
fun startVService(context: Context): Boolean {
314-
context.toast(R.string.toast_services_start)
315+
if (context.v2RayApplication.defaultDPreference.getPrefBoolean(SettingsActivity.PREF_PROXY_SHARING, false)) {
316+
context.toast(R.string.toast_warning_pref_proxysharing_short)
317+
}else{
318+
context.toast(R.string.toast_services_start)
319+
}
315320
if (AngConfigManager.genStoreV2rayConfig(-1)) {
316321
val configContent = AngConfigManager.currGeneratedV2rayConfig()
317322
val configType = AngConfigManager.currConfigType()
@@ -335,6 +340,7 @@ object Utils {
335340
*/
336341
fun startVService(context: Context, guid: String): Boolean {
337342
val index = AngConfigManager.getIndexViaGuid(guid)
343+
context.v2RayApplication.curIndex=index
338344
return startVService(context, index)
339345
}
340346

V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ object V2rayConfigUtil {
129129
*/
130130
private fun inbounds(vmess: VmessBean, v2rayConfig: V2rayConfig, app: AngApplication): Boolean {
131131
try {
132+
v2rayConfig.inbounds.forEach { curInbound ->
133+
if (!app.defaultDPreference.getPrefBoolean(SettingsActivity.PREF_PROXY_SHARING, false)) {
134+
//bind all inbounds to localhost if the user requests
135+
curInbound.listen = "127.0.0.1"
136+
}
137+
}
132138
v2rayConfig.inbounds[0].port = 10808
133139
// val socksPort = Utils.parseInt(app.defaultDPreference.getPrefString(SettingsActivity.PREF_SOCKS_PORT, "10808"))
134140
// val lanconnPort = Utils.parseInt(app.defaultDPreference.getPrefString(SettingsActivity.PREF_HTTP_PORT, ""))
@@ -547,7 +553,7 @@ object V2rayConfigUtil {
547553
mux = null))
548554
}
549555

550-
// DNS routing
556+
// DNS routing
551557
v2rayConfig.routing.rules.add(0, V2rayConfig.RoutingBean.RulesBean(
552558
type = "field",
553559
outboundTag = AppConfig.TAG_DIRECT,

V2rayNG/app/src/main/res/values-zh-rCN/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,9 @@
180180
<item>绕过大陆地址</item>
181181
<item>绕过局域网及大陆地址</item>
182182
</string-array>
183+
<string name="title_pref_proxy_sharing_enabled">代理共享</string>
184+
<string name="summary_pref_proxy_sharing_enabled">绑定代理入口ip到0.0.0.0</string>
185+
<string name="toast_warning_pref_proxysharing">其他设备可以使用socks/http协议通过您的IP地址连接到代理\nHttp 代理: http://您的ip:10809\nSocks 代理: socks(4/5)://您的ip:10808\n仅在受信任的网络中启用以避免未经授权的连接</string>
186+
<string name="toast_warning_pref_proxysharing_short">代理共享已启用,请确保处于受信网络</string>
183187

184188
</resources>

V2rayNG/app/src/main/res/values-zh-rTW/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,8 @@
182182
<item>略過中國大陸</item>
183183
<item>略過局域網及中國大陸</item>
184184
</string-array>
185+
<string name="title_pref_proxy_sharing_enabled">代理共享</string>
186+
<string name="summary_pref_proxy_sharing_enabled">綁定代理入口ip到0.0.0.0</string>
187+
<string name="toast_warning_pref_proxysharing">其他設備可以使用socks/http協議通過您的IP地址連接到代理\nHttp 代理: http://您的ip:10809\nSocks 代理: socks(4/5)://您的ip:10808\n僅在受信任的網絡中啟用以避免未經授權的連接</string>
188+
<string name="toast_warning_pref_proxysharing_short">代理共享已啟用,請確保處於受信網絡</string>
185189
</resources>

V2rayNG/app/src/main/res/values/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,9 @@
181181
<item>Bypass mainland address</item>
182182
<item>Bypassing LAN and mainland address</item>
183183
</string-array>
184+
<string name="title_pref_proxy_sharing_enabled">Proxy sharing</string>
185+
<string name="summary_pref_proxy_sharing_enabled">Bind inbound to 0.0.0.0</string>
186+
<string name="toast_warning_pref_proxysharing">Other devices can connect to proxy by your ip address through socks/http protocol\nHttp Proxy: http://yourIP:10809\nSocks Proxy: socks(4/5)://yourIP:10808\nOnly enable in trusted network to avoid unauthorized connection</string>
187+
<string name="toast_warning_pref_proxysharing_short">Proxy sharing enabled\nMake sure you are in a trusted network</string>
184188

185189
</resources>

V2rayNG/app/src/main/res/xml/pref_settings.xml

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
android:summary="@string/summary_pref_sniffing_enabled"
2323
android:title="@string/title_pref_sniffing_enabled" />
2424

25+
<CheckBoxPreference
26+
android:defaultValue="false"
27+
android:key="pref_proxy_sharing_enabled"
28+
android:onClick="proxySharingOnClick"
29+
android:summary="@string/summary_pref_proxy_sharing_enabled"
30+
android:title="@string/title_pref_proxy_sharing_enabled" />
31+
2532
</PreferenceCategory>
2633

2734
<PreferenceCategory android:title="@string/title_pref_routing">

0 commit comments

Comments
 (0)