Archivos por Etiqueta: proxy

Apache: PROXY protocol for Amazon ELB (Elastic Load Balancing)

If you want to use Apache 2.2 / 2.4 behind a Amazon ELB (Elastic Load Balancing), HAproxy, Stunnel, Stud o anything similar, you will have seen that in the logs appears IP of ELB instead of the remote client, and filter by IP is a double problem.

The module “remoteip” does not solve the problem. Amazon ELB, HAProxy and others support “PROXY protocol”, that helps to fix this issues working behind proxys.

The protocol look similar to:

PROXY TCP4 10.1.2.3 192.168.0.11 56324 443\r\n
GET / HTTP/1.1\r\n
Host: www.acme.com\r\n
Accept: */*\r\n
Connection: close\r\n
\r\n

But Apache currently does not support out-of-the-box. So following the philosophy of Do-It-Yourself, here is the source code (beta):

mod_myfixip.c in Github (for Apache 2.4)
mod_myfixip.c in Github (for Apache 2.2)

UPDATE (2015.01.17): Seems that AWS-ELB don’t always send PROXY header in only one packet / TCP-frame (in some cases data is received fragmented, now the module handle this behavior).

References:
ElasticLoadBalancing: Enable Proxy Protocol
PROXY protocol

Java: Agnostic Cache with Dynamic Proxies and Reflection

Hace tiempo usé un modulo de Perl muy interesante, Memoize. La idea era muy sencilla, tienes una funcion X, si para una entrada A, hay una salida B constantes y ese cálculo es lento, puede usar un caché.

La idea es esa, calcular una vez, usar muchas; sin tener que tener una implementación con cache de cada implementación original.

Aquí está la implementación en Java usando Reflexión:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ReflectMemoizer implements InvocationHandler {
  private final Object object;
  private final HashMap<Method, ConcurrentHashMap<List<Object>, Object>> caches;

  /**
   * Memoize object
   * @param object source
   * @return proxied object
   */
  public static Object memoize(final Object object) 
        throws InstantiationException, IllegalAccessException {
    final Class<?> clazz = object.getClass();
    final ReflectMemoizer memoizer = new ReflectMemoizer(object);
    return Proxy.newProxyInstance(clazz.getClassLoader(), 
        clazz.getInterfaces(), memoizer);
  }

  private ReflectMemoizer(final Object object) {
    this.object = object;
    this.caches = new HashMap<Method, ConcurrentHashMap<List<Object>, Object>>();
  }

  public Object invoke(final Object proxy, final Method method, 
        final Object[] args) throws Throwable {
    if (method.getReturnType().equals(Void.TYPE)) {
      // Don't cache void methods
      return invoke(method, args);
    } else {
      final Map<List<Object>, Object> cache = getCache(method);
      final List<Object> key = Arrays.asList(args);
      Object value = cache.get(key);
      if ((value == null) && !cache.containsKey(key)) {
        value = invoke(method, args);
        cache.put(key, value);
      }
      return value;
    }
  }

  private synchronized Map<List<Object>, Object> getCache(final Method m) {
    ConcurrentHashMap<List<Object>, Object> cache = caches.get(m);
    if (cache == null) {
      cache = new ConcurrentHashMap<List<Object>, Object>();
      caches.put(m, cache);
    }
    return cache;
  }

  private Object invoke(final Method method, final Object[] args) 
        throws Throwable {
    try {
      return method.invoke(object, args);
    } catch (InvocationTargetException e) {
      throw e.getTargetException();
    }
  }
}

Leer más de esta entrada

A %d blogueros les gusta esto: