Merge branch 'dev' of https://github.com/oxen-io/session-android into fix-open-group-spam

This commit is contained in:
Ryan ZHAO 2021-05-20 11:08:10 +10:00
commit 1729764f1d
2 changed files with 15 additions and 10 deletions

View File

@ -15,8 +15,10 @@ import org.session.libsignal.utilities.Log
object FileServerAPIV2 {
private const val SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69"
const val SERVER = "http://88.99.175.227"
private const val OLD_SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69"
const val OLD_SERVER = "http://88.99.175.227"
private const val SERVER_PUBLIC_KEY = "da21e1d886c6fbaea313f75298bd64aab03a97ce985b46bb2dad9f2089c8ee59"
const val SERVER = "http://filev2.getsession.org"
sealed class Error(message: String) : Exception(message) {
object ParsingFailed : Error("Invalid response.")
@ -42,8 +44,10 @@ object FileServerAPIV2 {
return RequestBody.create(MediaType.get("application/json"), parametersAsJSON)
}
private fun send(request: Request): Promise<Map<*, *>, Exception> {
val url = HttpUrl.parse(SERVER) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL)
private fun send(request: Request, useOldServer: Boolean): Promise<Map<*, *>, Exception> {
val server = if (useOldServer) OLD_SERVER else SERVER
val serverPublicKey = if (useOldServer) OLD_SERVER_PUBLIC_KEY else SERVER_PUBLIC_KEY
val url = HttpUrl.parse(server) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL)
val urlBuilder = HttpUrl.Builder()
.scheme(url.scheme())
.host(url.host())
@ -64,7 +68,7 @@ object FileServerAPIV2 {
HTTP.Verb.DELETE -> requestBuilder.delete(createBody(request.parameters))
}
if (request.useOnionRouting) {
return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), SERVER, SERVER_PUBLIC_KEY).fail { e ->
return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), server, serverPublicKey).fail { e ->
Log.e("Loki", "File server request failed.", e)
}
} else {
@ -76,14 +80,14 @@ object FileServerAPIV2 {
val base64EncodedFile = Base64.encodeBytes(file)
val parameters = mapOf( "file" to base64EncodedFile )
val request = Request(verb = HTTP.Verb.POST, endpoint = "files", parameters = parameters)
return send(request).map { json ->
return send(request, false).map { json ->
json["result"] as? Long ?: throw OpenGroupAPIV2.Error.ParsingFailed
}
}
fun download(file: Long): Promise<ByteArray, Exception> {
fun download(file: Long, useOldServer: Boolean): Promise<ByteArray, Exception> {
val request = Request(verb = HTTP.Verb.GET, endpoint = "files/$file")
return send(request).map { json ->
return send(request, useOldServer).map { json ->
val base64EncodedFile = json["result"] as? String ?: throw Error.ParsingFailed
Base64.decode(base64EncodedFile) ?: throw Error.ParsingFailed
}

View File

@ -41,11 +41,12 @@ object DownloadUtilities {
@JvmStatic
fun downloadFile(outputStream: OutputStream, url: String, maxSize: Int, listener: SignalServiceAttachment.ProgressListener?) {
if (url.contains(FileServerAPIV2.SERVER)) {
if (url.contains(FileServerAPIV2.SERVER) || url.contains(FileServerAPIV2.OLD_SERVER)) {
val httpUrl = HttpUrl.parse(url)!!
val fileId = httpUrl.pathSegments().last()
val useOldServer = url.contains(FileServerAPIV2.OLD_SERVER)
try {
FileServerAPIV2.download(fileId.toLong()).get().let {
FileServerAPIV2.download(fileId.toLong(), useOldServer).get().let {
outputStream.write(it)
}
} catch (e: Exception) {