def route(source,gp,args):
    dest = ""
    ibuf = ""
    p = args.output_port
    ns = None

    while True:
        d = source.recv(32384)
        if d == '':
            break
        if dest == "":
            h = ""
            ibuf = ibuf + d
            #print >> sys.stderr, "result: %s" % ibuf
            #log(syslog.LOG_INFO,"result: %s" % ibuf)
            #CONNECT https://don.don-vpn.vpn.sandvine.rocks:9999:443 HTTP/1.1
            result_api = re.match("^GET /api/", ibuf, re.MULTILINE)
            result_connect = re.match("^CONNECT (.*):",ibuf)
            result_sra = re.match("^SSTP_DUPLEX_POST (.*sra_)", ibuf)
            result_host = re.search("^Host: ([^\r\n]+)", ibuf, re.MULTILINE)
            # 
            if result_api != None:
                do_api(source,ibuf,args);
                return
            if result_host != None:
                host = result_host.groups()[0]
            if result_sra != None or result_host != None or result_connect != None:
                if result_connect != None:
                    ibuf = ""
                    tenant,instance = result_instance_tenant(result_connect.groups()[0])
                    h, ns,floating = find_ns.find_host(  args.admin_user,
                                                tenant,
                                                args.admin_pass,
                                                instance,
                                                args.keystone_url)

                if (h == "" and result_host != None and len(host.split('.')) > 3):
                    tenant,instance = result_instance_tenant(result_host.groups()[0])
                    h, ns,floating = find_ns.find_host(  args.admin_user,
                                                tenant,
                                                args.admin_pass,
                                                instance,
                                                args.keystone_url)

                if (h == "" and result_sra != None):
                    tenant,instance = result_instance_tenant(result_sra.groups()[0])
                    h, ns,floating = find_ns.find_host(  args.admin_user,
                                                tenant,
                                                args.admin_pass,
                                                instance,
                                                args.keystone_url)

                ibuf = re.sub("^SSTP_DUPLEX_POST.*/sra_","SSTP_DUPLEX_POST /sra_", ibuf)
                ibuf = re.sub("(Host: .*)(:[0-9]+)",r'\1',ibuf,flags=re.MULTILINE)
                if (h != "" and ns != ""):
                    d = ibuf
                    log(syslog.LOG_INFO,"Connect proxy to %s:%d (ns=%s)" % (h,p,ns))
                    _ns = find_ns.NS(ns)
                    if result_connect != None:
                        log(syslog.LOG_INFO,"to send 200OK")
                        dest = eventlet.connect((h,p))
                        dest.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                        source.sendall("HTTP/1.0 200 Connection established\r\n\r\n")
                    else:
                        try:
                            if (args.output_tls):
                                dest = eventlet.wrap_ssl(eventlet.connect((h,p)),
                                                       cert_reqs=ssl.CERT_NONE
                                                      )
                            else:
                                dest = eventlet.connect((h,p))
                            dest.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                            dest.sendall(d)
                        except:
                            find_ns.uncache_host(tenant,instance)
                            log(syslog.LOG_ERR,"Error on connect (%s,%s) get %s" % (h,p,traceback.format_exc()))
                    if dest != None:
                        # now proxy dest<>source
                        gp.spawn(forward, dest, source)
                        return forward(source,dest)
                    else:
                        log(syslog.LOG_ERR,"Give up on connection-2 h:%s,ns:%s (ibuf=%s)" % (h,ns,ibuf))
                        source.close()
                        break
                else:
                    log(syslog.LOG_ERR,"Give up on connection-3 h:%s,ns:%s (ibuf=%s)" % (h,ns,ibuf))
                    source.close()
                    break
            else:
                # dunno what we got, but lets not keep looking
                if len(ibuf) > 10:
                    log(syslog.LOG_ERR,"Give up on connection-3 (ibuf=%s)" % ibuf)
                    source.close()
                    break
Example #2
0
    global ppp_user, namespace_id, vpn_ip
    tenant = ""
    instance = ""
    syslog.syslog(syslog.LOG_INFO, "get_secret_for_user(%s,%s)" % (user,ipparam))
    try:
        # ppp_user is required for ip-up and ip-down.
        ppp_user, tenant, instance = parse_user(user)
    except AssertionError, e:
        syslog.syslog(syslog.LOG_INFO, "wrong format for user '%s': %s" % user, e)
        return str(random.random())

    syslog.syslog(syslog.LOG_INFO, "connecting user %s with ipparam %s" % (user, ipparam))

    try:
        args = find_ns.do_args()
        vpn_ip, ns, h = find_ns.find_host(args.user, tenant, args.password, instance, args.auth_url, args.shared_subnet_id, args.shared_router_id)
        #syslog.syslog(syslog.LOG_INFO, "namespace for %s %s is %s" % (tenant, instance, ns))
        assert ns != "", "empty namespace"
        namespace_id = ns
    except:
        syslog.syslog(syslog.LOG_INFO, "could not find namespace for %s %s" % (tenant, instance))
        return str(random.random())

    return "cl0ud"

def allowed_address_hook(ip):
    syslog.syslog(syslog.LOG_INFO, "allowed_address_hook %s" % ip)
    return True

def chap_check_hook():
    syslog.syslog(syslog.LOG_INFO, "chap_check_hook")
Example #3
0
    syslog.syslog(syslog.LOG_INFO,
                  "get_secret_for_user(%s,%s)" % (user, ipparam))
    try:
        # ppp_user is required for ip-up and ip-down.
        ppp_user, tenant, instance = parse_user(user)
    except AssertionError, e:
        syslog.syslog(syslog.LOG_INFO, "wrong format for user '%s': %s" % user,
                      e)
        return str(random.random())

    syslog.syslog(syslog.LOG_INFO,
                  "connecting user %s with ipparam %s" % (user, ipparam))

    try:
        args = find_ns.do_args()
        vpn_ip, ns, h = find_ns.find_host(args.user, tenant, args.password,
                                          instance, args.auth_url)
        #syslog.syslog(syslog.LOG_INFO, "namespace for %s %s is %s" % (tenant, instance, ns))
        assert ns != "", "empty namespace"
        namespace_id = ns
    except:
        syslog.syslog(
            syslog.LOG_INFO,
            "could not find namespace for %s %s" % (tenant, instance))
        return str(random.random())

    return "cl0ud"


def allowed_address_hook(ip):
    syslog.syslog(syslog.LOG_INFO, "allowed_address_hook %s" % ip)
    return True
Example #4
0
def route(source,gp,args):
    dest = ""
    ibuf = ""
    p = args.output_port
    ns = None

    while True:
        d = source.recv(32384)
        if d == '':
            break
        if dest == "":
            h = ""
            ibuf = ibuf + d
            #print >> sys.stderr, "result: %s" % ibuf
            #log(syslog.LOG_INFO,"result: %s" % ibuf)
            #CONNECT https://don.don-vpn.vpn.sandvine.rocks:9999:443 HTTP/1.1
            result_api = re.match("^GET /api/", ibuf, re.MULTILINE)
            result_connect = re.match("^CONNECT (.*):",ibuf)
            result_sra = re.match("^SSTP_DUPLEX_POST (.*sra_)", ibuf)
            result_host = re.search("^Host: ([^\r\n]+)", ibuf, re.MULTILINE)
            if result_api != None:
                if (do_api(source,ibuf,args)):
                    return
            if result_host != None:
                host = result_host.groups()[0]
            if result_sra != None or result_host != None or result_connect != None:
                if result_connect != None:
                    ibuf = ""
                    tenant,instance = result_instance_tenant(result_connect.groups()[0])
                    h, ns,floating = find_ns.find_host(  args.admin_user,
                                                tenant,
                                                args.admin_pass,
                                                instance,
                                                args.keystone_url)

                if (h == "" and result_host != None and len(host.split('.')) > 3):
                    tenant,instance = result_instance_tenant(result_host.groups()[0])
                    h, ns,floating = find_ns.find_host(  args.admin_user,
                                                tenant,
                                                args.admin_pass,
                                                instance,
                                                args.keystone_url)

                if (h == "" and result_sra != None):
                    tenant,instance = result_instance_tenant(result_sra.groups()[0])
                    h, ns,floating = find_ns.find_host(  args.admin_user,
                                                tenant,
                                                args.admin_pass,
                                                instance,
                                                args.keystone_url)

                ibuf = re.sub("^SSTP_DUPLEX_POST.*/sra_","SSTP_DUPLEX_POST /sra_", ibuf)
                ibuf = re.sub("(Host: .*)(:[0-9]+)",r'\1',ibuf,flags=re.MULTILINE)
                if (h != "" and ns != ""):
                    d = ibuf
                    log(syslog.LOG_INFO,"Connect proxy to %s:%d (ns=%s)" % (h,p,ns))
                    _ns = find_ns.NS(ns)
                    if result_connect != None:
                        log(syslog.LOG_INFO,"to send 200OK")
                        dest = eventlet.connect((h,p))
                        dest.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                        source.sendall("HTTP/1.0 200 Connection established\r\n\r\n")
                    else:
                        try:
                            if (args.output_tls):
                                dest = eventlet.wrap_ssl(eventlet.connect((h,p)),
                                                       cert_reqs=ssl.CERT_NONE
                                                      )
                            else:
                                dest = eventlet.connect((h,p))
                            dest.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                            d = re.sub("(Host: .*\r\n)", r"\1X-Forwarded-For: %s\r\n" % source.getpeername()[0], d, flags=re.MULTILINE)
                            dest.sendall(d)
                        except:
                            find_ns.uncache_host(tenant,instance)
                            log(syslog.LOG_ERR,"Error on connect (%s,%s) get %s" % (h,p,traceback.format_exc()))
                    if dest != None:
                        # now proxy dest<>source
                        gp.spawn(forward, dest, source)
                        return forward(source,dest)
                    else:
                        log(syslog.LOG_ERR,"Give up on connection-2 h:%s,ns:%s (ibuf=%s)" % (h,ns,ibuf))
                        source.close()
                        break
                else:
                    log(syslog.LOG_ERR,"Give up on connection-3 h:%s,ns:%s (ibuf=%s)" % (h,ns,ibuf))
                    source.close()
                    break
            else:
                # dunno what we got, but lets not keep looking
                if len(ibuf) > 10:
                    log(syslog.LOG_ERR,"Give up on connection-3 (ibuf=%s)" % ibuf)
                    source.close()
                    break