Commit e05abd7c authored by Mygod's avatar Mygod Committed by Max Lv

Support one-time authentication in ss urls for sharing (#644)

This commit implements the new ss protocol proposed here: https://github.com/shadowsocks/shadowsocks-org/issues/16
parent c8bb8fc9
...@@ -99,6 +99,6 @@ class Profile { ...@@ -99,6 +99,6 @@ class Profile {
@DatabaseField @DatabaseField
var userOrder: Long = _ var userOrder: Long = _
override def toString = "ss://" + Base64.encodeToString("%s:%s@%s:%d".formatLocal(Locale.ENGLISH, override def toString = "ss://" + Base64.encodeToString("%s%s:%s@%s:%d".formatLocal(Locale.ENGLISH,
method, password, host, remotePort).getBytes, Base64.NO_PADDING | Base64.NO_WRAP) method, if (auth) "-auth" else "", password, host, remotePort).getBytes, Base64.NO_PADDING | Base64.NO_WRAP)
} }
...@@ -45,22 +45,22 @@ import com.github.shadowsocks.database.Profile ...@@ -45,22 +45,22 @@ import com.github.shadowsocks.database.Profile
object Parser { object Parser {
val TAG = "ShadowParser" val TAG = "ShadowParser"
private val pattern = "(?i)ss://([A-Za-z0-9+-/=_]+)".r private val pattern = "(?i)ss://([A-Za-z0-9+-/=_]+)".r
private val decodedPattern = "(?i)^((.+?)(-auth)??:(.*)@(.+?):(\\d+?))$".r
def findAll(data: CharSequence) = pattern.findAllMatchIn(data).map(m => try { def findAll(data: CharSequence) = pattern.findAllMatchIn(data).map(m => try
val content = new String(Base64.decode(m.group(1), Base64.NO_PADDING), "UTF-8") decodedPattern.findFirstMatchIn(new String(Base64.decode(m.group(1), Base64.NO_PADDING), "UTF-8")) match {
val methodIdx = content.indexOf(':') case Some(ss) =>
val passwordIdx = content.lastIndexOf('@') val profile = new Profile
val hostIdx = content.lastIndexOf(':') profile.method = ss.group(2).toLowerCase
val host = content.substring(passwordIdx + 1, hostIdx) if (ss.group(3) != null) profile.auth = true
profile.password = ss.group(4)
val profile = new Profile profile.name = ss.group(5)
profile.name = host profile.host = profile.name
profile.host = host profile.remotePort = ss.group(6).toInt
profile.remotePort = content.substring(hostIdx + 1).toInt profile
profile.method = content.substring(0, methodIdx).toLowerCase case _ => null
profile.password = content.substring(methodIdx + 1, passwordIdx) }
profile catch {
} catch {
case ex: Exception => case ex: Exception =>
Log.e(TAG, "parser error: " + m.source, ex)// Ignore Log.e(TAG, "parser error: " + m.source, ex)// Ignore
null null
......
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