-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c0152e
commit 188431a
Showing
5 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/src/main/java/com/lizongying/mytv/api/Tls12SocketFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.lizongying.mytv.api | ||
|
||
import java.io.IOException | ||
import java.net.InetAddress | ||
import java.net.Socket | ||
import java.net.UnknownHostException | ||
import javax.net.ssl.SSLSocket | ||
import javax.net.ssl.SSLSocketFactory | ||
|
||
|
||
/** | ||
* Enables TLS v1.2 when creating SSLSockets. | ||
* | ||
* | ||
* For some reason, android supports TLS v1.2 from API 16, but enables it by | ||
* default only from API 20. | ||
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html | ||
* @see SSLSocketFactory | ||
*/ | ||
class Tls12SocketFactory(val delegate: SSLSocketFactory) : SSLSocketFactory() { | ||
override fun getDefaultCipherSuites(): Array<String> { | ||
return delegate.defaultCipherSuites | ||
} | ||
|
||
override fun getSupportedCipherSuites(): Array<String> { | ||
return delegate.supportedCipherSuites | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun createSocket(s: Socket, host: String, port: Int, autoClose: Boolean): Socket { | ||
return patch(delegate.createSocket(s, host, port, autoClose)) | ||
} | ||
|
||
@Throws(IOException::class, UnknownHostException::class) | ||
override fun createSocket(host: String, port: Int): Socket { | ||
return patch(delegate.createSocket(host, port)) | ||
} | ||
|
||
@Throws(IOException::class, UnknownHostException::class) | ||
override fun createSocket( | ||
host: String, | ||
port: Int, | ||
localHost: InetAddress, | ||
localPort: Int | ||
): Socket { | ||
return patch(delegate.createSocket(host, port, localHost, localPort)) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun createSocket(host: InetAddress, port: Int): Socket { | ||
return patch(delegate.createSocket(host, port)) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun createSocket( | ||
address: InetAddress, | ||
port: Int, | ||
localAddress: InetAddress, | ||
localPort: Int | ||
): Socket { | ||
return patch(delegate.createSocket(address, port, localAddress, localPort)) | ||
} | ||
|
||
private fun patch(s: Socket): Socket { | ||
if (s is SSLSocket) { | ||
s.enabledProtocols = TLS_V12_ONLY | ||
} | ||
return s | ||
} | ||
|
||
companion object { | ||
private val TLS_V12_ONLY = arrayOf("TLSv1.2") | ||
} | ||
} |