Skip to content

Commit a3acfbe

Browse files
committedMay 6, 2013
resolve the IP using dnsjava
1 parent 7c4a6c4 commit a3acfbe

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed
 

‎build.sbt

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ libraryDependencies ++= Seq(
1111
"com.actionbarsherlock" % "actionbarsherlock" % "4.2.0" artifacts(Artifact("actionbarsherlock", "apklib", "apklib")),
1212
"net.saik0.android.unifiedpreference" % "unifiedpreference" % "0.0.2" artifacts(Artifact("unifiedpreference", "apklib", "apklib")),
1313
"org.jraf" % "android-switch-backport" % "1.0" artifacts(Artifact("android-switch-backport", "apklib", "apklib")),
14-
"de.keyboardsurfer.android.widget" % "crouton" % "1.7"
14+
"de.keyboardsurfer.android.widget" % "crouton" % "1.7",
15+
"dnsjava" % "dnsjava" % "2.1.1"
1516
)
1617

‎src/main/scala/com/github/shadowsocks/ShadowVpnService.scala

+36-25
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ import android.util.Log
5252
import com.google.analytics.tracking.android.EasyTracker
5353
import java.io._
5454
import java.lang.ref.WeakReference
55-
import java.net.Inet6Address
56-
import java.net.InetAddress
57-
import java.net.UnknownHostException
5855
import android.net.VpnService
5956
import org.apache.http.conn.util.InetAddressUtils
57+
import org.xbill.DNS._
58+
import android.os.Message
59+
import scala.Some
6060

6161
object ShadowVpnService {
6262
def isServiceStarted: Boolean = {
@@ -118,6 +118,23 @@ class ShadowVpnService extends VpnService {
118118
}.start()
119119
}
120120

121+
def resolve(host: String, addrType: Int): Option[String] = {
122+
val lookup = new Lookup(host, addrType)
123+
val resolver = new SimpleResolver("8.8.8.8")
124+
resolver.setTimeout(5)
125+
lookup.setResolver(resolver)
126+
val records = lookup.run()
127+
for (r <- records) {
128+
addrType match {
129+
case org.xbill.DNS.Type.A =>
130+
return Some(r.asInstanceOf[ARecord].getAddress.getHostAddress)
131+
case org.xbill.DNS.Type.AAAA =>
132+
return Some(r.asInstanceOf[AAAARecord].getAddress.getHostAddress)
133+
}
134+
}
135+
None
136+
}
137+
121138
def getVersionName: String = {
122139
var version: String = null
123140
try {
@@ -168,35 +185,30 @@ class ShadowVpnService extends VpnService {
168185
def run() {
169186
handler.sendEmptyMessage(MSG_CONNECT_START)
170187
var resolved: Boolean = false
171-
if (appHost != null) {
172-
var addr: InetAddress = null
173-
val isIPv6Support: Boolean = Utils.isIPv6Support
174-
175-
try {
176-
val addrs = InetAddress.getAllByName(appHost)
177-
for (a <- addrs) {
178-
if (isIPv6Support && addr == null && a.isInstanceOf[Inet6Address]) {
179-
addr = a
188+
if (!InetAddressUtils.isIPv4Address(appHost) && !InetAddressUtils.isIPv6Address(appHost)) {
189+
if (Utils.isIPv6Support) {
190+
resolve(appHost, Type.AAAA) match {
191+
case Some(host) => {
192+
appHost = host
193+
resolved = true
180194
}
181195
}
182-
if (addr == null) addr = addrs(0)
183-
} catch {
184-
case ignored: UnknownHostException => {
185-
addr = null
186-
}
187196
}
188-
if (addr != null) {
189-
appHost = addr.getHostAddress
190-
resolved = true
197+
if (!resolved) {
198+
resolve(appHost, Type.A) match {
199+
case Some(host) => {
200+
appHost = host
201+
resolved = true
202+
}
203+
}
191204
}
205+
} else {
206+
resolved = true
192207
}
193-
Log.d(TAG, "IPTABLES: " + Utils.getIptables)
194-
hasRedirectSupport = Utils.getHasRedirectSupport
195208
if (resolved && handleConnection) {
196209
notifyAlert(getString(R.string.forward_success), getString(R.string.service_running))
197210
handler.sendEmptyMessageDelayed(MSG_CONNECT_SUCCESS, 500)
198-
}
199-
else {
211+
} else {
200212
notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed))
201213
stopSelf()
202214
handler.sendEmptyMessageDelayed(MSG_CONNECT_FAIL, 500)
@@ -391,7 +403,6 @@ class ShadowVpnService extends VpnService {
391403
var localPort: Int = 0
392404
var sitekey: String = null
393405
var settings: SharedPreferences = null
394-
var hasRedirectSupport: Boolean = true
395406
var isGlobalProxy: Boolean = false
396407
var isGFWList: Boolean = false
397408
var isBypassApps: Boolean = false

0 commit comments

Comments
 (0)
Please sign in to comment.