Skip to content

Commit

Permalink
fix(proxy): support proxy authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Vacxe committed May 31, 2024
1 parent 1d7da41 commit b3ca551
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 30 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Master Build

on:
push:
branches:
- master

jobs:
build-tar:
name: Build Tar
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
name: Set up JDK 11
with:
distribution: "temurin"
java-version: 11
cache: gradle
- name: Build release tar
run: ./gradlew distTar
- name: Tar Checksum
run: shasum -a 256 build/distributions/google-play-cli.tar
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: google-play-cli
path: build/distributions/google-play-cli.tar
2 changes: 1 addition & 1 deletion github-action/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/vacxe/google-play-cli:0.4.6
FROM ghcr.io/vacxe/google-play-cli:0.4.7

COPY entrypoint.sh /entrypoint.sh
COPY templates /templates
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/com/github/vacxe/googleplaycli/PlayStoreApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.vacxe.googleplaycli

import com.github.vacxe.googleplaycli.actions.*
import com.github.vacxe.googleplaycli.environments.Env
import com.github.vacxe.googleplaycli.environments.ProxyEnvironment
import com.github.vacxe.googleplaycli.environments.Proxy
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.HttpRequestInitializer
import com.google.api.client.json.gson.GsonFactory
Expand Down Expand Up @@ -38,13 +38,15 @@ class PlayStoreApi(serviceAccountInputStream: InputStream, appName: String) :
.fromStream(serviceAccountInputStream)
.createScoped(listOf(AndroidPublisherScopes.ANDROIDPUBLISHER))

ProxyEnvironment.apply()
val connectionTimeout = Env.connectionTimeout
Proxy.apply()

androidPublisher = AndroidPublisher.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
TransportFactory.buildTransport(),
GsonFactory.getDefaultInstance(),
setHttpTimeout(HttpCredentialsAdapter(accountCredentials), connectionTimeout)
setHttpTimeout(
HttpCredentialsAdapter(accountCredentials),
Env.connectionTimeout
)
)
.setApplicationName(appName)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fun main(args: Array<String>) {
addCmd {
object : CliktCommand(name = "version", help = "Library version code") {
override fun run() {
println("0.4.6")
println("0.4.7")
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions src/main/kotlin/com/github/vacxe/googleplaycli/TransportFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.vacxe.googleplaycli

import com.github.vacxe.googleplaycli.environments.Proxy
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.HttpTransport
import com.google.api.client.http.apache.v2.ApacheHttpTransport
import com.google.api.client.http.javanet.NetHttpTransport
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.client.ProxyAuthenticationStrategy
import java.io.FileInputStream
import java.security.KeyStore

object TransportFactory {
private val host = Proxy.Environment.host
private val port = Proxy.Environment.port
private val username = Proxy.Environment.username
private val password = Proxy.Environment.password
private val trustStore = Proxy.Environment.trustStore
private val trustStorePassword = Proxy.Environment.trustStorePassword

fun buildTransport(): HttpTransport = when {
host != null && port != null -> createHttpTransportProxy(
host,
port,
username,
password
)

trustStore != null && trustStorePassword != null -> createHttpTransportTrustStore(
trustStore,
trustStorePassword
)

else -> GoogleNetHttpTransport.newTrustedTransport()
}

private fun createHttpTransportProxy(
host: String,
port: String,
username: String?,
password: String?
): HttpTransport {
val httpClient = ApacheHttpTransport.newDefaultHttpClientBuilder()
.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE)
.apply {
if (username != null && password != null) {
val credentials = BasicCredentialsProvider().apply { }
credentials.setCredentials(
AuthScope(host, port.toInt()),
UsernamePasswordCredentials(username, password)
)
setDefaultCredentialsProvider(credentials)
}
}
.build()
return ApacheHttpTransport(httpClient)

}

private fun createHttpTransportTrustStore(
trustStore: String,
trustStorePassword: String
): HttpTransport {
val ks = KeyStore.getInstance(KeyStore.getDefaultType())
FileInputStream(trustStore).use { fis ->
ks.load(fis, trustStorePassword.toCharArray())
}
return NetHttpTransport.Builder().trustCertificates(ks).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.vacxe.googleplaycli.environments

object Proxy {
object Environment {
val host: String?
get() = System.getenv("PLAYSTORE_PROXY_HOST")

val port: String?
get() = System.getenv("PLAYSTORE_PROXY_PORT")

val username: String?
get() = System.getenv("PLAYSTORE_PROXY_USERNAME")

val password: String?
get() = System.getenv("PLAYSTORE_PROXY_USERNAME")

val trustStore: String?
get() = System.getenv("PLAYSTORE_PROXY_TRUST_STORE")

val trustStorePassword: String?
get() = System.getenv("PLAYSTORE_PROXY_TRUST_STORE_PASSWORD")
}

fun apply() {
val host = Proxy.Environment.host
val port = Proxy.Environment.port
val username = Proxy.Environment.username
val password = Proxy.Environment.password
val trustStore = Proxy.Environment.trustStore
val trustStorePassword = Proxy.Environment.trustStorePassword

if (host != null && port != null) {
val protocols = arrayOf("http", "https")
protocols.forEach { protocol ->
System.setProperty("$protocol.proxySet", "true");
System.setProperty("$protocol.proxyHost", host)
System.setProperty("$protocol.proxyPort", port)
username?.run { System.setProperty("$protocol.proxyUser", this) }
password?.run { System.setProperty("$protocol.proxyPassword", this) }
}
}

if(trustStore != null && trustStorePassword != null) {
System.setProperty("javax.net.ssl.trustStore", trustStore)
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword)
}
}
}

This file was deleted.

0 comments on commit b3ca551

Please sign in to comment.