Statica

Page last updated:

Statica allows a PWS application to route outbound traffic through a static IP. You can provide this IP address to an API partner for IP-based whitelisting and open your own firewall to access internal resources.

Statica is language and platform agnostic. There is native support for Ruby, Python, Node.js, Scala, Java and every other mainstream language.

What Are My IPs?

Statica provides you with two static IP addresses. You can view the addresses by clicking Manage on the Statica instance you created in your PWS space. Once logged in, you can view the IPs. The system provisions two IP addresses as part of our fault tolerant, load balanced service. Traffic may route through either IP address at any time.

Using Statica with Your Application

Once Statica is bound, it updates the VCAP_SERVICES environment variable with an entry for statica that contains the credentials as well as other information needed to access Statica.

The VCAP_SERVICES entry looks like the following:

{"statica":[{"name":"StaticaInstance","label":"statica","tags":[],"plan":"starter","credentials":{"STATICA_URL":STATICA_URL}}]}

To start using Statica in your application, set up your application to use the proxy URL available in the STATICA_URL parameter.

HTTP vs. SOCKS5 proxy

Statica supports both HTTP and SOCKS5 proxies. SOCKS is more versatile, as it handles TCP-level traffic, but the setup steps are more involved than the HTTP proxy. Our general rule of thumb is use HTTP if accessing a web service; otherwise, use SOCKS.

Example use cases:

Using with Ruby/Rails

Ruby has an excellent REST client that easily allows you to specify an HTTP proxy. You can run the below example in an IRb session and verify that the final IP returned is one of your two static IPs.


require "rest-client"
credentials = proxyURL = ''
if !ENV['VCAP_SERVICES'].blank?
  JSON.parse(ENV['VCAP_SERVICES']).each do |k,v|
    if !k.scan("statica").blank?
      credentials = v.first.select {|k1,v1| k1 == "credentials"}["credentials"]
      proxyURL = credentials["STATICA_URL"]
    end
  end
end

RestClient.proxy = proxyURL

res = RestClient.get("http://ip.jsontest.com")

puts "Your Static IP is: #{res.body}"

Using with Python/Django

Using with the Requests library

Requests is a great HTTP library for Python. It allows you to specify an authenticated proxy on a per-request basis, so you can pick and choose when to route through your static IP.

import requests
import os

statica_service = json.loads(os.environ['VCAP_SERVICES'])['statica'][0]
credentials = statica_service['credentials']

proxies = {
"http": credentials['STATICA_URL'],
"https": credentials['STATICA_URL']
}

res = requests.get("http://ip.jsontest.com/", proxies=proxies)
print res.text

Using with Node.js

Accessing an HTTP API with Node.js

To access an HTTP API, you can use the standard HTTP library in Node.js, but you must ensure you correctly set the “Host” header to your target hostname rather than the proxy hostname.

var http, options, proxy, url;

http = require("http");

url = require("url");

var vcap_services = JSON.parse(process.env.VCAP_SERVICES)
var proxyURL = vcap_services['statica'][0].credentials.STATICA_URL
proxy = url.parse(proxyURL);
target  = url.parse("http://ip.jsontest.com/");

options = {
  hostname: proxy.hostname,
  port: proxy.port || 80,
  path: target.href,
  headers: {
    "Proxy-Authorization": "Basic " + (new Buffer(proxy.auth).toString("base64")),
    "Host" : target.hostname
  }
};

http.get(options, function(res) {
  res.pipe(process.stdout);
  return console.log("status code", res.statusCode);
});

Accessing an HTTPS API with Node.js

The standard Node.js HTTPS module does not handle making requests through a proxy very well. If you need to access an HTTPS API, we recommend using the Request module (npm install request).

var request = require('request');

var vcap_services = JSON.parse(process.env.VCAP_SERVICES)
var proxyURL = vcap_services['statica'][0].credentials.STATICA_URL
var options = {
    proxy: proxyURL,
    url: 'https://api.github.com/repos/joyent/node',
    headers: {
        'User-Agent': 'node.js'
    }
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

Accessing a MySQL Database using SOCKS Proxy in Node.js

To use a SOCKS proxy for any reason in Node.js, we recommend socksjs, as this is one of the only Node.js SOCKS modules to support authentication.

npm install socksjs

This sample creates a connection to a SOCKS connection to our proxy and uses that for all MySQL requests.

var mysql = require('mysql2');
var url = require("url");
var SocksConnection = require('socksjs');
var remote_options = {
host:'mysql.db.hostname',
port: 3306
};
var vcap_services = JSON.parse(process.env.VCAP_SERVICES)
var proxyURL = vcap_services['statica'][0].credentials.STATICA_URL
var proxy = url.parse(proxyURL);
var auth = proxy.auth;
var username = auth.split(":")[0]
var pass = auth.split(":")[1]

var sock_options = {
host: proxy.hostname,
port: 1080,
user: username,
pass: pass
}
var sockConn = new SocksConnection(remote_options, sock_options)
var dbConnection = mysql.createConnection({
user: 'test',
database: 'test',
password: 'testpw',
stream: sockConn
});
dbConnection.query('SELECT 1+1 as test1;', function(err, rows, fields) {
if (err) throw err;

console.log('Result: ', rows);
sockConn.dispose();
});
dbConnection.end();

Using with PHP

PHP cURL is the easiest way to make HTTP requests via Statica. This example assumes that you have set the VCAP_SERVICES environment variable, which is automatically set for you when you provision the service.

The IP address printed on screen will be one of your two static IP addresses. If you run it a few times, you will probably see the other one too.


function lookup(){
  $vcap = json_decode(getenv("VCAP_SERVICES"),true);
  $statica_env = $vcap["statica"][0]["credentials"]["STATICA_URL"];

  $statica = parse_url($statica_env);

  $proxyUrl       = $statica['host'].":".$statica['port'];
  $proxyAuth       = $statica['user'].":".$statica['pass'];

  $url = "http://ip.jsontest.com/";

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_PROXY, $proxyUrl);
  curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
  $response = curl_exec($ch);
  return $response;
}

$res = lookup();
print_r($res);


FAQ

Is the HTTP proxy secure when accessing HTTPS services?

Yes. You can access HTTPS services via the HTTP proxy while still getting full SSL/TLS security. When you make a request via the proxy to an HTTPS endpoint, your client should transparently issue a CONNECT request rather than a basic GET request.

On receipt of this CONNECT request, the proxy opens a tunnel between your client and the endpoint, allowing your client to negotiate a standard SSL session with the endpoint. Once negotiated, all traffic sent between your client and the endpoint will be encrypted as if you had connected directly with them.

SOCKS proxy setup

Statica provides a wrapper script that transparently forwards all outbound TCP traffic through your static IP. This is language-independent, but there are known issues with certain Node.js connections hanging, so please contact us if you have any issues.

Do you offer dedicated Static IPs?

Yes: dedicated IP addresses are available on request for subscriptions on the Enterprise plan and above. As they require dedicated infrastructure, we only provision these on request, so please contact us if you want a dedicated static IP address.

What happens when I reach my usage limit?

To make sure we grow in harmony with your application, Statica operates initially with a soft limit. When you reach your plan’s monthly usage limit, your requests will continue going through, but we will reach out to you via e-mail to ask that you upgrade your plan. If you repeatedly exceed your limits without upgrading, then hard limits may be placed on your account, but this is a very last resort.

I’ve forgotten what my Static IPs are!

Both IPs are shown on your Dashboard, which you can get to either from your PWS services page

Why have you given me two Static IP addresses?

We believe all apps should be built for scalability and high availability. Our commitment to this means we only provide load balanced, high availability services. Load balancing our nodes allows one node to fail or be brought down for maintenance with no impact to your application. Each IP you are given represents one proxy node that is running behind a load balancer.

Can I access MySQL or Postgres through this?

Yes, we have many users doing this. The easiest way for most languages is to use our SOCKS proxy wrapper (installation details higher up the page). If you are using Node.js, you can also configure the SOCKS proxy in your Javascript code without using the wrapper (details also on this page).

Support

All Statica support and runtime issues should be submitted via the PWS Support channels. Any non-support related issues or product feedback is welcome at feedback@teachmatic.com or by visiting www.statica.io.

Create a pull request or raise an issue on the source for this page in GitHub