Skip to content

Commit

Permalink
Revert "Migrate Internet Card to Apache HTTPClient, fix #3364"
Browse files Browse the repository at this point in the history
This reverts commit 9f1d521.
  • Loading branch information
asiekierka committed May 30, 2023
1 parent 4cbe57d commit 555ae0e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 67 deletions.
26 changes: 9 additions & 17 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -959,19 +959,11 @@ opencomputers {
# Whether to allow adding custom headers to HTTP requests.
enableHttpHeaders: true

# This is a list of allowed HTTP methods. If empty, any HTTP method
# can be used.
enableHttpMethods: []

# This is a number describing how many HTTP redirects are allowed
# while processing an HTTP request call. If 0, no redirects are allowed.
enableHttpRedirects: 50

# Whether to allow TCP connections via internet cards. When enabled,
# the `connect` method on internet card components becomes available.
enableTcp: true

# This is a list of forbidden domain names. If an HTTP request is made
# This is a list of blacklisted domain names. If an HTTP request is made
# or a socket connection is opened the target address will be compared
# to the addresses / address ranges in this list. It it is present in this
# list, the request will be denied.
Expand All @@ -993,9 +985,9 @@ opencomputers {
"172.16.0.0/12"
]

# This is a list of allowed domain names. Requests may only be made
# This is a list of whitelisted domain names. Requests may only be made
# to addresses that are present in this list. If this list is empty,
# requests may be made to all addresses not forbidden. Note that the
# requests may be made to all addresses not blacklisted. Note that the
# blacklist is always applied, so if an entry is present in both the
# whitelist and the blacklist, the blacklist will win.
# Entries are of the same format as in the blacklist. Examples:
Expand Down Expand Up @@ -1108,7 +1100,7 @@ opencomputers {
# Radius in blocks of the disintegration behavior for each active input.
disintegrationRange: 1

# Allowed potions, i.e. potions that will be used for the potion
# Whitelisted potions, i.e. potions that will be used for the potion
# behaviors nanomachines may trigger. This can contain strings or numbers.
# In the case of strings, it has to be the internal name of the potion,
# in case of a number it has to be the potion ID. Add any potion effects
Expand Down Expand Up @@ -1320,7 +1312,7 @@ opencomputers {
geolyzerNoise: 2

# By default the disassembler can only be used to disassemble items from
# OpenComputers itself (or objects allowed via the API). If you'd
# OpenComputers itself (or objects whitelisted via the API). If you'd
# like to allow the disassembler to work on all kinds of items, even from
# other mods, set this to true.
disassembleAllTheThings: false
Expand All @@ -1331,7 +1323,7 @@ opencomputers {
# this chance of breaking in the process.
disassemblerBreakChance: 0.05

# Names of items / blocks that are forbidden. Recipes containing these
# Names of items / blocks that are blacklisted. Recipes containing these
# as inputs will be ignored by the disassembler.
disassemblerInputBlacklist: [
"minecraft:fire"
Expand Down Expand Up @@ -1605,15 +1597,15 @@ opencomputers {

# Chunkloader Upgrade settings.
chunkloader {
# This is a list of forbidden dimensions in the form of numeric IDs.
# This is a list of blacklisted dimensions in the form of numeric IDs.
# Chunkloaders may only be activated in dimensions that aren't present in
# this list.
dimBlacklist: []

# This is a list of allowed dimensions in the form of numeric IDs.
# This is a list of whitelisted dimensions in the form of numeric IDs.
# Chunkloaders may only be activated in dimensions that are present in
# this list. If this list is empty, chunkloaders may be activated to all
# dimensions not forbidden. Note that the blacklist is always applied,
# dimensions not blacklisted. Note that the blacklist is always applied,
# so if an entry is present in both the whitelist and the blacklist, the
# blacklist will win.
dimWhitelist: []
Expand Down
2 changes: 0 additions & 2 deletions src/main/scala/li/cil/oc/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ class Settings(val config: Config) {
// internet
val httpEnabled = config.getBoolean("internet.enableHttp")
val httpHeadersEnabled = config.getBoolean("internet.enableHttpHeaders")
val httpMethodsEnabled = config.getStringList("internet.enableHttpMethods")
val httpRedirectsEnabled = config.getInt("internet.enableHttpRedirects") max -1
val tcpEnabled = config.getBoolean("internet.enableTcp")
val httpHostBlacklist = Array(config.getStringList("internet.blacklist").map(new Settings.AddressValidator(_)): _*)
val httpHostWhitelist = Array(config.getStringList("internet.whitelist").map(new Settings.AddressValidator(_)): _*)
Expand Down
76 changes: 28 additions & 48 deletions src/main/scala/li/cil/oc/server/component/InternetCard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import java.nio.channels.SelectionKey
import java.nio.channels.Selector
import java.nio.channels.SocketChannel
import java.util
import java.util.{Locale, UUID}
import java.util.UUID
import java.util.concurrent._

import li.cil.oc.Constants
import li.cil.oc.OpenComputers
import li.cil.oc.Settings
Expand All @@ -28,11 +29,6 @@ import li.cil.oc.api.prefab
import li.cil.oc.api.prefab.AbstractValue
import li.cil.oc.util.ThreadPoolFactory
import net.minecraft.server.MinecraftServer
import org.apache.http.HttpHost
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.RequestBuilder
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients

import scala.collection.convert.WrapAsJava._
import scala.collection.convert.WrapAsScala._
Expand Down Expand Up @@ -474,50 +470,34 @@ object InternetCard {
private class RequestSender(val url: URL, val post: Option[String], val headers: Map[String, String], val method: Option[String]) extends Callable[InputStream] {
override def call() = try {
checkLists(InetAddress.getByName(url.getHost), url.getHost)
val proxy = MinecraftServer.getServer.getServerProxy

val methodStr = if (method.isDefined) method.get.toUpperCase(Locale.ROOT) else if (post.isDefined) "POST" else "GET"
if (Settings.get.httpMethodsEnabled.nonEmpty && !Settings.get.httpMethodsEnabled.contains(methodStr)) {
throw new IOException("method not allowed: " + methodStr)
}

val requestBuilder = RequestBuilder.create(methodStr)
headers.foreach(Function.tupled(requestBuilder.addHeader))
requestBuilder.setUri(url.toURI)

val httpRequestConfig = RequestConfig.custom()
.setConnectTimeout(Settings.get.httpTimeout)
.setConnectionRequestTimeout(Settings.get.httpTimeout)
.setSocketTimeout(Settings.get.httpTimeout)

val maxRedirects = Settings.get.httpRedirectsEnabled
if (maxRedirects > 0) {
httpRequestConfig.setMaxRedirects(maxRedirects)
} else if (maxRedirects == 0) {
httpRequestConfig
.setRedirectsEnabled(false)
.setRelativeRedirectsAllowed(false)
}

if (proxy != null && proxy != Proxy.NO_PROXY) proxy.address() match {
case inetProxyAddress: InetSocketAddress => httpRequestConfig.setProxy(new HttpHost(inetProxyAddress.getAddress, inetProxyAddress.getPort))
}

val clientBuilder = HttpClients.custom()
clientBuilder.setDefaultRequestConfig(httpRequestConfig.build())

if (post.isDefined) {
requestBuilder.setEntity(new StringEntity(post.get))
}

val r = clientBuilder.build().execute(requestBuilder.build())
val proxy = Option(MinecraftServer.getServer.getServerProxy).getOrElse(java.net.Proxy.NO_PROXY)
url.openConnection(proxy) match {
case http: HttpURLConnection => try {
http.setDoInput(true)
http.setDoOutput(post.isDefined)
http.setRequestMethod(if (method.isDefined) method.get else if (post.isDefined) "POST" else "GET")
headers.foreach(Function.tupled(http.setRequestProperty))
if (post.isDefined) {
http.setReadTimeout(Settings.get.httpTimeout)

val out = new BufferedWriter(new OutputStreamWriter(http.getOutputStream))
out.write(post.get)
out.close()
}

val input = r.getEntity.getContent
HTTPRequest.this.synchronized {
import collection.JavaConverters._
response = Some((r.getStatusLine.getStatusCode, r.getStatusLine.getReasonPhrase, r.getAllHeaders.groupBy(h => h.getName).map(i => i._1 -> i._2.toList.asJava).asJava))
val input = http.getInputStream
HTTPRequest.this.synchronized {
response = Some((http.getResponseCode, http.getResponseMessage, http.getHeaderFields))
}
input
}
catch {
case t: Throwable =>
http.disconnect()
throw t
}
case other => throw new IOException("unexpected connection type")
}
input
}
catch {
case e: UnknownHostException =>
Expand Down

0 comments on commit 555ae0e

Please sign in to comment.