mr-cloud / test

0 stars 0 forks source link

Proxy servers #100

Open mr-cloud opened 2 years ago

mr-cloud commented 2 years ago

Does this library work behind a corporate proxy. I think the keywords in python are somehow verify="cert/location",proxies="proxy"... couldn't see anything in the docs so assuming it's not currently implemented. Will try and look but it's probably beyond my skills :-(.

Or is this solved by the open pull request? (he asks hopefully) ID: 56 Original Author: Quafadas link: Original Link

mr-cloud commented 2 years ago

@Quafadas I had the same issue and I couldn't find anything on the library. However, you can set your JVM to go through the proxy.

We used something like this:


case class Proxy(url: String, port: Int, user: Option[String] = None, pass: Option[String] = None, scheme: String = "http") {
  override def toString: String = s"PROXY (URL: $url; PORT: $port; SCHEME: $scheme)"
}

object Proxies extends LoggingSupport {
  def autoSetup(config: ConfigurationReader) = {
    val proxies = config.PROXIES

    if (proxies.nonEmpty) {
      logger.info("Setting up proxies.")
    } else {
      logger.info("No proxies found.")
    }

    proxies.foreach { proxy =>
      logger.debug(proxy.toString)

      System.setProperty(s"${proxy.scheme}.proxyHost", proxy.url)
      System.setProperty(s"${proxy.scheme}.proxyPort", proxy.port.toString)

      proxy.user.foreach(user => System.setProperty(s"${proxy.scheme}.proxyUser", user))
      proxy.pass.foreach(pass => System.setProperty(s"${proxy.scheme}.proxyPassword", pass))
    }

    config.PROXY_BYPASS.foreach(exp => System.setProperty("https.nonProxyHosts", exp))
    config.PROXY_BYPASS.foreach(exp => System.setProperty("http.nonProxyHosts", exp))
  }
}

Notice that this implementation is based on the Oracle documentation that can be found here.

https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html Original Author:anicolaspp

mr-cloud commented 2 years ago

Okay, thanks for the above. I'll let you know if I get any mileage. I had a dig into the code and there does appears to be support for proxies=(String, Int)... in the requests arguments, although it's not documented.

But I think our proxy is doing something weird. I can send a request to the service which gives me back a token, but I think the proxy is stripping the Authorization header of the subsequent requests. I can only ever get back unauthorised... but I can test in Postman etc, that the Authorization header I'm sending is correct... and Postman itself works.

I'm lost :-( ... but I'm not sure the problems are in this library... I have a hint that it may be to do with getting SSL correctly setup with client certs... but mostly... I'm just lost.

Original Author:Quafadas

mr-cloud commented 2 years ago

TO clarify this, I can never get beyond this stage;

play.api.UnexpectedException: Unexpected exception[IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"] at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:347) at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:267) at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:448) at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:446) at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:417) at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:41) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64) at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55) at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:92) at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) Caused by: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required" at java.base/sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2172) at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183) at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1362) at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1337) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:246) at requests.Requester$$anon$1.readBytesThrough(Requester.scala:250) at geny.Readable.writeBytesTo(Writable.scala:57) at geny.Readable.writeBytesTo$(Writable.scala:57) at requests.Requester$$anon$1.writeBytesTo(Requester.scala:157) at requests.Requester.apply(Requester.scala:108)

Original Author:Quafadas

mr-cloud commented 2 years ago

Hey, look into my closed issue #59 Maybe it help you. Original Author:mdrijwan123

mr-cloud commented 2 years ago

@mdrijwan123 thanks for that post! Unfortunately, providing username/password credentials in the URL didn't work for me (seems that the library is using java.net.Proxy and passing in an InetSocketAddress, which only accepts a host/ip). I also subsequently realized that proxy auth never worked, so I went down a rabbit hole attempting to fix this in #100. Original Author:half0wl