Skip to content

Instantly share code, notes, and snippets.

@null-dev
Last active January 8, 2019 00:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save null-dev/d1098a6e8918c288a189147aa0d9397e to your computer and use it in GitHub Desktop.
Save null-dev/d1098a6e8918c288a189147aa0d9397e to your computer and use it in GitHub Desktop.
1fichier.com direct link generator
package xyz.nulldev.csg
import okhttp3.OkHttpClient
import okhttp3.Request
import org.jetbrains.ktor.http.HttpStatusCode
import org.jetbrains.ktor.netty.embeddedNettyServer
import org.jetbrains.ktor.response.respondRedirect
import org.jetbrains.ktor.response.respondText
import org.jetbrains.ktor.routing.get
import org.jetbrains.ktor.routing.routing
import java.net.URLEncoder
fun main(args: Array<String>) {
//Start server
CSGServerDemo().start()
}
class CSGServerDemo {
val okhttp = OkHttpClient.Builder()
.followRedirects(false)
.followSslRedirects(false)
.build()!!
fun start() {
//Start HTTP server on port 8082
embeddedNettyServer(8082) {
routing {
//Handle all get requests (regardless of path)
get("{...}") {
//Get the file ID from the query string
var archive: String? = null
if (it.request.queryParameters.contains(ARCHIVE_PARAM_SHORT)) {
archive = it.request.queryParameters[ARCHIVE_PARAM_SHORT]
} else if (it.request.queryParameters.contains(ARCHIVE_PARAM)) {
archive = it.request.queryParameters[ARCHIVE_PARAM]
}
archive?.let { archive ->
//Create request for regular direct link
val request = Request.Builder()
.url("https://1fichier.com/?${URLEncoder.encode(archive, "UTF-8")}?auth=1&inline")
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36")
.header("Authorization", AUTH_HEADER) //Include login credentials
.build()
//Execute created request
okhttp.newCall(request)
.execute().use { resp ->
//Get resulting direct link and downgrade it to HTTP
//The HTTPS direct link is REALLY, REALLY slow for some wierd reason
resp.header("Location")?.let { loc ->
if (loc.startsWith("https")) {
"http${loc.removePrefix("https")}"
} else loc
}
}?.let { newLoc ->
//Create request used to upgrade the regular direct link -> premium
val request2 = Request.Builder()
.url(newLoc)
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36")
.build()
//Execute request, but close it immediately to avoid reading any data
okhttp.newCall(request2).execute().close()
//Send the resulting premium direct link to the browser in the 'Location' header
it.respondRedirect(newLoc, false)
return@get
}
//Redirect client
it.response.status(HttpStatusCode.InternalServerError)
it.respondText("Failed to unarchive file!")
return@get
}
}
}
}.start(wait = true)
}
val ARCHIVE_PARAM_SHORT = "a"
val ARCHIVE_PARAM = "archive"
//Archive auth
val AUTH_HEADER = "Basic " + "[BASE64 encoded login credentials. Format: 'user:pass' (without quotes)]"
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.nulldev</groupId>
<artifactId>CSG</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.1.1</kotlin.version>
</properties>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-kotlin-ktor</id>
<name>bintray</name>
<url>http://dl.bintray.com/kotlin/ktor</url>
</repository>
<repository>
<id>jcenter</id>
<name>jcenter</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jetbrains.ktor</groupId>
<artifactId>ktor-core</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.ktor</groupId>
<artifactId>ktor-features</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.ktor</groupId>
<artifactId>ktor-netty</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment