Scala makes it easy to build networked applications thanks to its concise syntax and strong libraries. Here is an HTTP proxy server in Scala in just 20 lines of code:
First import the libraries we need:
import akka.actor.{Actor, ActorSystem, Props}
import akka.io.{IO, Tcp}
import akka.util.ByteString
Akka provides the actor model for concurrency and socket handling.
Define an actor to handle each client connection:
class ProxyActor extends Actor {
def receive = {
case (client: ActorRef, data: ByteString) =>
// handle request
}
}
To handle the request we:
val url = extractUrl(data.utf8String)
val response = Http().singleRequest(Get(url))
client ! response.entity.dataBytes
Start the proxy actor when system starts:
system.actorOf(Props[ProxyActor], "proxy")
This shows the basic structure of an HTTP proxy in Scala using Akka.
here is the full code:
import akka.actor.{Actor, ActorSystem, Props}
import akka.io.{IO, Tcp}
import akka.util.ByteString
class ProxyActor extends Actor {
def receive = {
case (client: ActorRef, data: ByteString) =>
val url = extractUrl(data.utf8String)
val response = Http().singleRequest(Get(url))
client ! response.entity.dataBytes
}
}
object Main extends App {
val system = ActorSystem("ProxySystem")
system.actorOf(Props[ProxyActor], "proxy")
IO(Tcp) ! Bind(system.actorOf(Props[PlaintextServer]), "localhost", 8080)
}
This implements a simple HTTP proxy actor in Scala using Akka in around 20 lines of code.
This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.
Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.
Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.
Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.
The whole thing can be accessed by a simple API like below in any programming language.
In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:
curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
We have a running offer of 1000 API calls completely free. Register and get your free API Key.