From 2f6f991e6ec35689025352885e5a7f8af6f50878 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 11 Mar 2017 12:33:27 +0800 Subject: [PATCH] Fix literal IPv6 address handling in URIs --- .../com/github/shadowsocks/database/Profile.scala | 3 ++- .../scala/com/github/shadowsocks/utils/Parser.scala | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/scala/com/github/shadowsocks/database/Profile.scala b/mobile/src/main/scala/com/github/shadowsocks/database/Profile.scala index 53581e85..fa347065 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/database/Profile.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/database/Profile.scala @@ -98,7 +98,8 @@ class Profile { .scheme("ss") .encodedAuthority("%s@%s:%d".formatLocal(Locale.ENGLISH, Base64.encodeToString("%s:%s".formatLocal(Locale.ENGLISH, method, password).getBytes, - Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE), host, remotePort)) + Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE), + if (host.contains(':')) '[' + host + ']' else host, remotePort)) val configuration = new PluginConfiguration(plugin) if (configuration.selected.nonEmpty) builder.appendQueryParameter(Key.plugin, configuration.selectedOptions.toString(false)) diff --git a/mobile/src/main/scala/com/github/shadowsocks/utils/Parser.scala b/mobile/src/main/scala/com/github/shadowsocks/utils/Parser.scala index 64d1ed88..3742af60 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/utils/Parser.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/utils/Parser.scala @@ -20,13 +20,15 @@ package com.github.shadowsocks.utils +import java.net.URI + import android.net.Uri import android.util.{Base64, Log} import com.github.shadowsocks.database.Profile object Parser { val TAG = "ShadowParser" - private val pattern = "(?i)ss://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]".r + private val pattern = "(?i)ss://[-a-zA-Z0-9+&@#/%?=~_|!:,.;\\[\\]]*[-a-zA-Z0-9+&@#/%=~_|\\[\\]]".r private val userInfoPattern = "^(.+?):(.*)$".r private val legacyPattern = "^(.+?):(.*)@(.+?):(\\d+?)$".r @@ -54,8 +56,12 @@ object Parser { val profile = new Profile profile.method = method profile.password = password - profile.host = uri.getHost - profile.remotePort = uri.getPort + // bug in Android: https://code.google.com/p/android/issues/detail?id=192855 + val javaURI = new URI(m.matched) + profile.host = javaURI.getHost + if (profile.host.headOption.contains('[') && profile.host.lastOption.contains(']')) + profile.host = profile.host.substring(1, profile.host.length - 1) + profile.remotePort = javaURI.getPort profile.plugin = uri.getQueryParameter(Key.plugin) profile.name = uri.getFragment profile