Ejemplo n.º 1
0
def connect_to_server(data, relay, bind_all, test=False):

    assert relay in ['SSH', 'OSSH']

    server = Psiphon3Server(data.servers(), data.propagation_channel_id(),
                            data.sponsor_id(), CLIENT_VERSION, CLIENT_PLATFORM)

    if server.relay_not_supported(relay):
        raise Exception('Server does not support %s' % relay)

    handshake_performed = False
    if not server.can_attempt_relay_before_handshake(relay):
        handshake_response = do_handshake(server, data, relay)
        handshake_performed = True

    ssh_connection = make_ssh_connection(server, relay, bind_all)
    ssh_connection.test_connection()

    server.set_socks_proxy(SOCKS_PORT)

    if not handshake_performed:
        try:
            handshake_response = do_handshake(server, data, relay)
            handshake_performed = True
        except Exception as e:
            print 'DEBUG: handshake request: ' + str(e)

    connected_performed = False
    if handshake_performed:
        print_sponsor_message(handshake_response)
        try:
            server.connected(relay)
            connected_performed = True
        except Exception as e:
            print 'DEBUG: connected request: ' + str(e)

    if test:
        print 'Testing connection to ip %s' % server.ip_address
        ssh_connection.disconnect_on_success(test_site=test)
    else:
        print 'Press Ctrl-C to terminate.'
        try:
            ssh_connection.wait_for_disconnect()
        except KeyboardInterrupt as e:
            if connected_performed:
                try:
                    server.disconnected(relay)
                except Exception as e:
                    print 'DEBUG: disconnected request: ' + str(e)
            ssh_connection.disconnect()
Ejemplo n.º 2
0
def connect(bind_all, copy="", test=False):
    if test:
        data = copy
    else:
        data = Data.load()
    while True:
        top = data.servers()[0]
        #This line is also broken, same as the above
        """loc = top.find('{"webServerCertificate":'.encode('hex'))
        ob=json.loads(top[loc:].decode('hex'))"""
        ob = json.loads(top.decode('hex').split()[-1])

        print "Trying to connect to " + ob["region"] + " : " + ob["ipAddress"]

        try:
            relay = 'SSH'
            # NOTE that this path is also hard-coded in psi_ssh_connection
            ossh_path = './ssh'
            if _test_executable(ossh_path):
                relay = 'OSSH'
            else:
                print '%s is not a valid executable. Using standard ssh.' % (
                    ossh_path, )

            connect_to_server(data, relay, bind_all, test)
            break
        except Exception as error:
            print 'DEBUG: %s connection: %s' % (relay, str(error))
            server = Psiphon3Server(data.servers(),
                                    data.propagation_channel_id(),
                                    data.sponsor_id(), CLIENT_VERSION,
                                    CLIENT_PLATFORM)

            if test:
                break
            if not data.move_first_server_entry_to_bottom():
                print 'DEBUG: could not reorder servers'

            #data.save()
            print 'Trying next server...'
Ejemplo n.º 3
0
def connect_to_server(data, relay, bind_all, test=False):

    assert relay in ['SSH', 'OSSH']

    server = Psiphon3Server(data.servers(), data.propagation_channel_id(),
                            data.sponsor_id(), CLIENT_VERSION, CLIENT_PLATFORM)

    if server.relay_not_supported(relay):
        raise Exception('Server does not support %s' % relay)
        print(server.ip_address)
        print(server.extended_config)
        server
    handshake_performed = False
    if not server.can_attempt_relay_before_handshake(relay):
        handshake_response = do_handshake(server, data, relay)
        handshake_performed = True

    ssh_connection = make_ssh_connection(server, relay, bind_all)
    ssh_connection.test_connection()

    server.set_socks_proxy(SOCKS_PORT)

    if not handshake_performed:
        try:
            handshake_response = do_handshake(server, data, relay)
            handshake_performed = True
        except Exception as e:
            print 'DEBUG: handshake request: ' + str(e)

    connected_performed = False
    if handshake_performed:
        print_sponsor_message(handshake_response)
        try:
            server.connected(relay)
            connected_performed = True
        except Exception as e:
            print 'DEBUG: connected request: ' + str(e)
    with open('servers.dat', 'r') as serv_file:
        tempdata = json.loads(serv_file.read())
        top_index = tempdata["servers"].index(data.servers()[0])
        tempdata["servers"].insert(0, tempdata["servers"].pop(top_index))
    with open('servers.new', 'w') as data_file:
        data_file.write(json.dumps(tempdata))
    os.rename('servers.new', 'servers.dat')
    if test:
        print 'Testing connection to ip %s' % server.ip_address
        ssh_connection.disconnect_on_success(test_site=test)
    else:
        print("SERVER CONNECTED :: " + str(server.extended_config['region']))
        print "IP Address :" + str(server.ip_address)
        #For redsocks
        curr = dict()
        if (SOCKS_PORT == 1080):
            with open("connected_server", "w") as cc:
                curr["ip"] = str(server.ip_address)
                curr["port"] = SOCKS_PORT
                cc.write(json.dumps(curr))
        #For redsocks
        print 'Press Ctrl-C to terminate.'
        try:
            ssh_connection.wait_for_disconnect()
        except KeyboardInterrupt as e:
            if connected_performed:
                try:
                    server.disconnected(relay)
                except Exception as e:
                    print 'DEBUG: disconnected request: ' + str(e)
            ssh_connection.disconnect()