Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ class ProfileTest {
Assert.assertEquals("ss://YmYtY2ZiOnRlc3Q@192.168.100.1:8888#example-server".toUri(),
results.single().toUri())
}

@Test
fun parsingPlainUserInfo() {
val results = Profile.findAllUrls(
"ss://2022-blake3-chacha20-poly1305:F6BY4IwbXB5Juobo2aofoJ6P86G%2FyBYJO3pJzcMIUOU%3D@1.2.3.4:8400")
.toList()
Assert.assertEquals(1, results.size)
results.single().also {
Assert.assertEquals("2022-blake3-chacha20-poly1305", it.method)
Assert.assertEquals("F6BY4IwbXB5Juobo2aofoJ6P86G/yBYJO3pJzcMIUOU=", it.password)
Assert.assertEquals("1.2.3.4", it.host)
Assert.assertEquals(8400, it.remotePort)
}
}
}
12 changes: 10 additions & 2 deletions core/src/main/java/com/github/shadowsocks/database/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ data class Profile(
private val userInfoPattern = "^(.+?):(.*)$".toRegex()
private val legacyPattern = "^(.+?):(.*)@(.+?):(\\d+?)$".toRegex()

private fun userInfoCandidates(uri: Uri) = sequence {
uri.userInfo?.also { userInfo ->
try {
yield(String(Base64.decode(userInfo, Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE)))
} catch (_: IllegalArgumentException) { }
yield(userInfo)
}
}

fun findAllUrls(data: CharSequence?, feature: Profile? = null) = pattern.findAll(data ?: "").map {
val uri = it.value.toUri()
try {
Expand All @@ -129,8 +138,7 @@ data class Profile(
null
}
} else {
val match = userInfoPattern.matchEntire(String(Base64.decode(uri.userInfo,
Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE)))
val match = userInfoCandidates(uri).mapNotNull(userInfoPattern::matchEntire).firstOrNull()
if (match != null) {
val profile = Profile()
feature?.copyFeatureSettingsTo(profile)
Expand Down