Browse Source

add workaround for ssl errors

pull/18/head
Jannik 2 years ago
parent
commit
36acf1a00a
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 4
      Dockerfile
  2. 2
      build.gradle
  3. 2
      src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt
  4. 39
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt
  5. 41
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

4
Dockerfile

@ -0,0 +1,4 @@
FROM openjdk:11
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} thecitadelofricks.jar
ENTRYPOINT ["java","-jar","/thecitadelofricks.jar"]

2
build.gradle

@ -6,7 +6,7 @@ plugins {
}
group 'org.mosad'
version '1.2.3'
version '1.2.4'
repositories {
jcenter()

2
src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt

@ -48,7 +48,7 @@ class APIController {
companion object {
const val apiVersion = "1.2.0"
const val softwareVersion = "1.2.3"
const val softwareVersion = "1.2.4"
val startTime = System.currentTimeMillis() / 1000
}

39
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt

@ -26,6 +26,13 @@ import org.jsoup.Jsoup
import org.mosad.thecitadelofricks.Course
import org.slf4j.LoggerFactory
import java.net.SocketTimeoutException
import java.security.KeyManagementException
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
class CourseListParser {
@ -39,7 +46,7 @@ class CourseListParser {
fun getCourseLinks(courseListURL: String): HashMap<String, Course>? {
val courseLinkList = HashMap<String, Course>()
try {
val courseHTML = Jsoup.connect(courseListURL).get()
val courseHTML = Jsoup.connect(courseListURL).sslSocketFactory(socketFactory()).get()
courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element ->
courseLinkList[element.text()] = Course(
@ -57,4 +64,34 @@ class CourseListParser {
return courseLinkList
}
// Hack to ignore ssl errors while reading
private fun socketFactory(): SSLSocketFactory {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
@Throws(CertificateException::class)
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
}
@Throws(CertificateException::class)
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
}
override fun getAcceptedIssuers(): Array<X509Certificate> {
return arrayOf()
}
})
try {
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, trustAllCerts, java.security.SecureRandom())
return sslContext.socketFactory
} catch (e: Exception) {
when (e) {
is RuntimeException, is KeyManagementException -> {
throw RuntimeException("Failed to create a SSL socket factory", e)
}
else -> throw e
}
}
}
}

41
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

@ -27,6 +27,13 @@ import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson
import org.mosad.thecitadelofricks.TimetableWeek
import org.slf4j.LoggerFactory
import java.security.KeyManagementException
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
class TimetableParser {
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(TimetableParser::class.java)
@ -40,7 +47,7 @@ class TimetableParser {
*/
fun getTimeTable(timetableURL: String): TimetableWeek {
return try {
parseTimeTable(Jsoup.connect(timetableURL).get())
parseTimeTable(Jsoup.connect(timetableURL).sslSocketFactory(socketFactory()).get())
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
TimetableWeek()
@ -122,7 +129,7 @@ class TimetableParser {
*/
fun getWeekNumberYear(timetableURL: String): Int {
return try {
parseWeekNumberYear(Jsoup.connect(timetableURL).get())
parseWeekNumberYear(Jsoup.connect(timetableURL).sslSocketFactory(socketFactory()).get())
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
0
@ -181,4 +188,34 @@ class TimetableParser {
println(" \n")
}
// Hack to ignore ssl errors while reading
private fun socketFactory(): SSLSocketFactory {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
@Throws(CertificateException::class)
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
}
@Throws(CertificateException::class)
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
}
override fun getAcceptedIssuers(): Array<X509Certificate> {
return arrayOf()
}
})
try {
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, trustAllCerts, java.security.SecureRandom())
return sslContext.socketFactory
} catch (e: Exception) {
when (e) {
is RuntimeException, is KeyManagementException -> {
throw RuntimeException("Failed to create a SSL socket factory", e)
}
else -> throw e
}
}
}
}
Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close