Created
March 31, 2018 01:01
-
-
Save jviveiros/6d7d44164f3c68289070721c2ba22d18 to your computer and use it in GitHub Desktop.
Scala Sampe
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
package example | |
import java.net.{InetSocketAddress, Proxy} | |
import com.softwaremill.sttp.okhttp.{OkHttpSyncBackend} | |
import okhttp3.{Authenticator, OkHttpClient} | |
import okhttp3.Credentials | |
import okhttp3.Route | |
import okhttp3.Response | |
import okhttp3.Request | |
import java.io.IOException | |
import com.softwaremill.sttp._ | |
import org.json4s._ | |
import org.json4s.native.JsonMethods._ | |
object Example extends App { | |
//Get Env variables | |
val username = sys.env("FORWARD_HTTP_PROXY_USERNAME") | |
val password = sys.env("FORWARD_HTTP_PROXY_PASSWORD") | |
val reverseProxy = sys.env("REVERSE_HTTP_PROXY_HOST") | |
val forwardProxy = sys.env("FORWARD_HTTP_PROXY_HOST") | |
def revealViaForwardProxy(redactedSecret: String): String = { | |
System.setProperty("javax.net.ssl.trustStore", "src/main/resources/cacerts") | |
System.setProperty("javax.net.ssl.trustStorePassword", "password") | |
System.setProperty("javax.net.ssl.trustStoreType", "JKS") | |
val proxyAuthenticator = new Authenticator() { | |
@throws[IOException] | |
def authenticate(route: Route, response: Response): Request = { | |
val credential = Credentials.basic(username, password) | |
response.request.newBuilder.header("Proxy-Authorization", credential).build | |
} | |
} | |
val hostport: Array[String] = forwardProxy.split(":") | |
val client = new OkHttpClient.Builder() | |
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostport(0), hostport(1).toInt))) | |
.proxyAuthenticator(proxyAuthenticator) | |
.build() | |
implicit val backend: SttpBackend[Id, _] = OkHttpSyncBackend.usingClient(client) | |
val request = sttp | |
.body(redactedSecret) | |
.post(uri"https://httpbin.verygoodsecurity.io/post") | |
val json = parse(request.send.unsafeBody) | |
implicit val formats: DefaultFormats = DefaultFormats | |
(json \ "data").extract[String] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment