Beispiel #1
0
 def __sendLS(self, address):
     packet = struct.pack("!BIH", 2, utils.ip2int(self.address[0]),
                          self.address[1])
     for addr, cost in self.neighbour.items():
         packet += struct.pack("!IHH", utils.ip2int(addr[0]), addr[1], cost)
     # for addr, cost in self.neighbour.items():
     # print(addr, cost)
     self.__sendPacket(packet, address)
Beispiel #2
0
 def __sendForwardTable(self):
     # send forwarding tables to all the neighbours
     for client in self.map:
         packet = struct.pack('!B', 3)
         for item in self.nextHopForRouters[client].items():
             packet += struct.pack("!IHIH",
                                   utils.ip2int(item[0][0]), item[0][1],
                                   utils.ip2int(item[1][0]), item[1][1])
         self.transport.write(packet, client)
Beispiel #3
0
 def __sendLSU(self, address, packet=None):
     if packet == None:
         packet = struct.pack("!BIHIH", 2, utils.ip2int(self.address[0]),
                              self.address[1], utils.ip2int(
                                  self.address[0]),
                              self.address[1])
         for item in self.neighbour.keys():
             packet += struct.pack("!IHH", utils.ip2int(item[0]), item[1],
                                   self.neighbour[item])
     else:
         packet = packet[0:7] + struct.pack("!IH", utils.ip2int(self.address[0]),
                                            self.address[1]) + packet[13:]
     self.__sendPacket(packet, address)
Beispiel #4
0
def _load_data():
    logging.info('loading database in memory from: %s' % settings.CSV_PATH)
    with open(settings.CSV_PATH, 'rb') as csvfile:
        ip_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        #"0.0.0.0","0.255.255.255","US","California","Los Angeles"
        for row in ip_reader:
            if row[0] == '::':
                break
            start_address = utils.ip2int(row[0])
            stop_address = utils.ip2int(row[1])

            _ADDRESSES.append(start_address)
            _DETAILS[start_address] = (stop_address, row[2], row[3], row[4])
Beispiel #5
0
def _load_data():
    logging.info('loading database in memory from: %s' % settings.CSV_PATH)
    with open(settings.CSV_PATH, 'rb') as csvfile:
        ip_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        #"0.0.0.0","0.255.255.255","US","California","Los Angeles"
        for row in ip_reader:
            if row[0] == '::':
                break
            start_address = utils.ip2int(row[0])
            stop_address = utils.ip2int(row[1])
            
            _ADDRESSES.append(start_address)
            _DETAILS[start_address] = (stop_address, row[2], row[3], row[4])
Beispiel #6
0
    def traceroute(self, address):
        self.traceRouteResult[address] = 0
        self.path[address] = []

        for i in range(1, 32 + 1):
            packet = struct.pack("!BIHIHB", 3, utils.ip2int(self.address[0]),
                                 self.address[1], utils.ip2int(address[0]),
                                 address[1], i)
            self.__sendPacket(packet, address)
        threading.Timer(self.TraceRouteInterval,
                        self.__traceRouteExceed, args=[address]).start()
        while(self.traceRouteResult[address] == 0):
            pass
        return self.traceRouteResult[address] == 1
Beispiel #7
0
 def __tracerouteReceived(self, packet):
     (ip, port) = struct.unpack("!IH", packet[1:7])
     sourceAddress = (utils.int2ip(ip), port)
     (ip, port, count) = struct.unpack("!IHB", packet[7:14])
     destAddress = (utils.int2ip(ip), port)
     count = count - 1
     if count > 0 and destAddress != self.address:
         packet = packet[0:-1] + struct.pack("!B", count)
         self.__sendPacket(packet, destAddress)
     elif count == 0:  # send Echo packet
         EchoPacket = struct.pack("!BIHIHIH", 4, utils.ip2int(sourceAddress[0]),
                                  sourceAddress[1], utils.ip2int(
                                      destAddress[0]),
                                  destAddress[1], utils.ip2int(
                                      self.address[0]),
                                  self.address[1])
         self.__sendPacket(EchoPacket, sourceAddress)
Beispiel #8
0
 def get(self):
     ip = self.get_argument('ip_addr', None)
     if not ip:
         return self.response('error', 'please input an IPv4 address')
     try:
         ip = utils.ip2int(ip)
     except:
         return self.response('error', 'please input a valid IPv4 address')
     
     if not (0 <= ip <= 4294967295):
         return self.response('error', 'please input a valid IPv4 address')
     
     response = None
     resolver = self.get_argument('resolver', 'mem')
     
     start_addr = memback.find_ip(ip)
     if not start_addr:
         return self.response('error', 'could not find address')
         
     if resolver == 'postgres':
         cursor = self.pgconn.cursor()
         #cursor.execute('select x, y, country, county, city from addresses where x <= %s and y >= %s', (ip, ip))
         cursor.execute('select x, y, country, county, city from addresses where x = %s', (start_addr, ))
         records = cursor.fetchall()
         if records:
             record = records[0]
             response =  {'range': (utils.int2ip(record[0]), utils.int2ip(record[1])),
                 'country': record[2],
                 'county': record[3],
                 'city': record[4]
             }
             cursor.close()
         
     elif resolver == 'mongo':
         #records = self.mgconn.ipdb.addresses.find({'x': {'$lte': ip}, 'y': {'$gte': ip}})
         records = self.mgconn.ipdb.addresses.find({'x': start_addr})
         if records:
             record = records[0]
             response =  {'range': (utils.int2ip(record['x']), utils.int2ip(record['y'])),
                 'country': record['country'],
                 'county': record['county'],
                 'city': record['city']
             }
             records.close()
         
     else:
         #details = self.rdconn.get(start_addr)
         #response = utils.sh2rec(start_addr, json.loads(details))
         response = utils.sh2rec(start_addr, memback.get_address_details(start_addr))
     
     if response:    
         return self.response(data=response)
     
     self.response('error', 'could not find address')
Beispiel #9
0
    def __init__(self, name, **data):
        ip_range = data['range']
        self.ip1, ip2_sz = ip_range.split('-')
        self.ip2, self.sz = ip2_sz.split('/')
        self.ip1 = self.ip1.strip()
        self.ip2 = self.ip2.strip()
        self.sz = int(self.sz)
        self.url = data.get('url', 'qemu:///system')

        self.name = name
        self.ip = int2ip(ip2int(self.ip1) + 1)
        self.bridge = data['bridge'].strip()
        self.netmask = netsz2netmask(self.sz)
Beispiel #10
0
    def get_by_ip(ip):
        try:
            query = (Proxy
                     .select_query()
                     .where(Proxy.ip == ip2int(ip))
                     .dicts())
            if len(query) > 0:
                return query[0]

        except OperationalError as e:
            log.exception('Failed to get proxy by IP from database: %s', e)

        return None
Beispiel #11
0
    def __init__(self, name, **data):
        ip_range = data['range']
        self.ip1, ip2_sz = ip_range.split('-')
        self.ip2, self.sz = ip2_sz.split('/')
        self.ip1 = self.ip1.strip()
        self.ip2 = self.ip2.strip()
        self.sz = int(self.sz)
        self.url = data.get('url', 'qemu:///system')

        self.name = name
        self.ip = int2ip(ip2int(self.ip1) + 1)
        self.bridge = data['bridge'].strip()
        self.netmask = netsz2netmask(self.sz)
Beispiel #12
0
 def db_format(proxy):
     return {
         'hash': proxy['hash'],
         'ip': ip2int(proxy['ip']),
         'port': proxy['port'],
         'protocol': proxy['protocol'],
         'username': proxy['username'],
         'password': proxy['password'],
         'insert_date': proxy.get('insert_date', datetime.utcnow()),
         'scan_date': proxy.get('scan_date', None),
         'latency': proxy.get('latency', None),
         'fail_count': proxy.get('fail_count', 0),
         'anonymous': proxy.get('anonymous', ProxyStatus.UNKNOWN),
         'niantic': proxy.get('niantic', ProxyStatus.UNKNOWN),
         'ptc_login': proxy.get('ptc_login', ProxyStatus.UNKNOWN),
         'ptc_signup': proxy.get('ptc_signup', ProxyStatus.UNKNOWN)}
Beispiel #13
0
    def get(self):
        ip = self.get_argument('ip', '')
        if ip == '':
            return self.return_error(constants.ERR_IP_NULL)

        try:
            int_ip = utils.ip2int(ip)
        except:
            return self.return_error(constants.ERR_IP_TYPE)

        ip_info = ipregion.get_info(self._get_id(int_ip))
        result = {
            "ipstart": ip_info['start'],
            "ipend": ip_info['end'],
            "country": ip_info['country'],
            "province": ip_info['region'],
            "area": ip_info['area'],
            "city": ip_info['city'],
            "county": ip_info['county'],
            "isp": ip_info['isp'],
            "total": ip_info['total'],
        }
        self.return_result(result)
Beispiel #14
0
    def get(self):
        ip = self.get_argument("ip", "")
        if ip == "":
            return self.return_error(constants.ERR_IP_NULL)

        try:
            int_ip = utils.ip2int(ip)
        except:
            return self.return_error(constants.ERR_IP_TYPE)

        ip_info = ipregion.get_info(self._get_id(int_ip))
        result = {
            "ipstart": ip_info["start"],
            "ipend": ip_info["end"],
            "country": ip_info["country"],
            "province": ip_info["region"],
            "area": ip_info["area"],
            "city": ip_info["city"],
            "county": ip_info["county"],
            "isp": ip_info["isp"],
            "total": ip_info["total"],
        }
        self.return_result(result)
Beispiel #15
0
def prepare_guest_debian(disk_path, hostname, passwords, eth_devs, format=None, apt_proxy_ip=None):

    logger.info("Prepare image for " + hostname)
    if format == 'lxc':
        gfs = LocalGuestFS(disk_path)
        gfs.rm('/etc/init/udev.conf')

        interfaces = []
        for dev, (hw, ip, sz, gw) in eth_devs.items():
            if ip == 'dhcp':
                interfaces.append("dhclient {0}".format(dev))
            else:
                interfaces.append("ifconfig {0} {1}/{2} up".format(dev, ip, sz))
        gfs.write('/etc/init/lxc_lan.conf', ifconfig_script.format("\n".join(interfaces)))
    else:
        gfs = guestfs.GuestFS()
        gfs.add_drive_opts(disk_path, format=format)
        logger.debug("Launch libguestfs vm")
        gfs.launch()
        logger.debug("ok")

        os_devs = gfs.inspect_os()
        if len(os_devs) > 1:
            msg = "Two or more bootable partitions - disk prepare impossible " + disk_path
            logger.error(msg)
            raise CloudError(msg)

        # for dev, fs_type in  gfs.list_filesystems():
        #     logger.debug("Fount partition {0} with fs type {1}".format(dev, fs_type))

        #     # TODO: add lvm support
        #     if fs_type in 'ext2 ext3 reiserfs3 reiserfs4 xfs jfs btrfs':
        #         gfs.mount(dev, '/')
        #         if gfs.exists('/etc'):
        #             logger.debug("Fount /etc on partition {0} - will work on it".format(dev))
        #             break
        #         gfs.umount(dev)
        #         logger.debug("No /etc dir found - continue")

        if 0 == len(os_devs):
            mounts = sorted(gfs.inspect_get_mountpoints(os_devs[0]))

            for mpoint, dev in mounts:
                gfs.mount(dev, mpoint)

                if not gfs.exists('/etc'):
                    msg = "Can't fount /etc dir in image " + disk_path
                    logger.error(msg)
                    raise CloudError(msg)
        else:
            gfs.mount(os_devs[0], '/')
            #gfs.mount('/dev/vda1', '/')

            if not gfs.exists('/etc'):
                msg = "Can't fount /etc dir in image " + disk_path
                logger.error(msg)
                raise CloudError(msg)

    logger.debug("Launch ok. Set hostname")
    #hostname
    gfs.write('/etc/hostname', hostname)

    #set device names
    logger.debug("Set device names and network imterfaces")
    templ = 'SUBSYSTEM=="net", DRIVERS=="?*", ATTR{{address}}=="{hw}", NAME="{name}"'
    rules_fc = []
    interfaces = ["auto lo\niface lo inet loopback"]

    for dev, (hw, ip, sz, gw) in eth_devs.items():
        rules_fc.append(templ.format(hw=hw, name=dev))
        interfaces.append("auto " + dev)

        if ip == 'dhcp':
            interfaces.append("iface {0} inet dhcp".format(dev))
        else:
            interfaces.append("iface {0} inet static".format(dev))
            interfaces.append("    address " + ip)
            network = int2ip(ip2int(ip) & ip2int(netsz2netmask(sz)))
            interfaces.append("    network " + network)
            interfaces.append("    netmask " + netsz2netmask(sz))

    gfs.write('/etc/udev/rules.d/70-persistent-net.rules', "\n".join(rules_fc))
    # gfs.write('/etc/network/interfaces', "\n".join(interfaces))
    gfs.write('/etc/network/interfaces.d/eth0', "\n".join(interfaces))

    # update passwords
    logger.debug("Update passwords")

    chars = "".join(chr(i) for i in range(ord('a'), ord('z') + 1))
    chars += "".join(chr(i) for i in range(ord('A'), ord('Z') + 1))
    chars += "".join(chr(i) for i in range(ord('0'), ord('9') + 1))

    hashes = {}
    for login, passwd in passwords.items():
        salt = "".join(random.choice(chars) for _ in range(8))
        hashes[login] = crypt.crypt(passwd, "$6$" + salt)

    new_shadow = []
    need_logins = set(hashes)

    for ln in gfs.read_file('/etc/shadow').split('\n'):
        ln = ln.strip()
        if ln != '' and ln[0] != '#':
            login = ln.split(':', 1)[0]
            if login in hashes:
                sh_templ = "{login}:{hash}:{rest}"
                sh_line = sh_templ.format(login=login,
                                          hash=hashes[login],
                                          rest=ln.split(':', 2)[2])
                new_shadow.append(sh_line)
                need_logins.remove(login)
        else:
            new_shadow.append(ln)

    for login in need_logins:
        new_sh_templ = "{login}:{hash}:{rest}"
        new_sh_line = new_sh_templ.format(login=login,
                                          hash=hashes[login],
                                          rest="0:0:99999:7:::")
        new_shadow.append(new_sh_line)

    gfs.write('/etc/shadow', "\n".join(new_shadow))

    # add new users to passwd
    ids = []
    logins = []
    passwd = gfs.read_file('/etc/passwd')
    for ln in passwd.split('\n'):
        ln = ln.strip()
        if ln != '' and ln[0] != '#':
            logins.append(ln.split(':', 1)[0])
            ids.append(ln.split(':')[2])
            ids.append(ln.split(':')[3])

    add_lines = []
    try:
        mid = max(i for i in ids if i < 65000)
    except ValueError:
        mid = 0
    mid += 1024

    for login in set(hashes) - set(logins):
        home = '/home/' + login
        add_lines.append(":".join([login, 'x', str(mid), str(mid), "", home, '/bin/bash']))
        if not gfs.exists(home):
            gfs.mkdir_p(home)
        mid += 1

    if add_lines != []:
        gfs.write('/etc/passwd', passwd.rstrip() + "\n" + "\n".join(add_lines))

    # if apt_proxy_ip is not None:
    #     logger.debug("Set apt-proxy to http://{0}:3142".format(apt_proxy_ip))
    #     fc = 'Acquire::http {{ Proxy "http://{0}:3142"; }};'.format(apt_proxy_ip)
    #     gfs.write('/etc/apt/apt.conf.d/02proxy', fc)

    logger.debug("Update hosts")

    hosts = gfs.read_file('/etc/hosts')

    new_hosts = ["127.0.0.1 localhost\n127.0.0.1 " + hostname]
    for ln in hosts.split('#'):
        if not ln.strip().startswith('127.0.0.1'):
            new_hosts.append(ln)

    gfs.write('/etc/hosts', "\n".join(new_hosts))

    # allow ssh passwd auth
    if gfs.is_file('/etc/ssh/ssh_config'):
        name = '/etc/ssh/ssh_config'
    elif gfs.is_file('/etc/ssh/sshd_config'):
        name = '/etc/ssh/sshd_config'
    else:
        logger.warning("Both '/etc/ssh/sshd_config' and '/etc/ssh/ssh_config' are absent. Skip ssh config patching")
        name = None

    if name is not None:
        sshd_conf = gfs.read_file('/etc/ssh/ssh_config')
        sshd_conf_lines = sshd_conf.split("\n")
        for pos, ln in enumerate(sshd_conf_lines):
            if "PasswordAuthentication" in ln:
                sshd_conf_lines[pos] = "PasswordAuthentication yes"
                break
        else:
            sshd_conf_lines.append("PasswordAuthentication yes")
        gfs.write('/etc/ssh/ssh_config', "\n".join(sshd_conf_lines))
Beispiel #16
0
dbconn.autocommit = True
cursor = dbconn.cursor()

print('creating PostgreSQL table')
cursor.execute(create_query)

print('importing CSV file')
with open(settings.CSV_PATH, 'rb') as csvfile:
    ip_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    k = 0
    for row in ip_reader:
        k += 1
        if k % 200000 == 0:
            print('loaded %d addresses' % k)
        
        if row[0] == '::':
            break
        start_address = utils.ip2int(row[0])
        stop_address = utils.ip2int(row[1])
        data = (
            start_address,
            stop_address,
            row[2],
            row[3],
            row[4]
        )
        cursor.execute("INSERT INTO addresses(x, y, country, county, city) VALUES(%s, %s, %s, %s, %s)", data)

print('creating indexes')
cursor.execute(index_query)
print('finished')
Beispiel #17
0
 def getBytesIP(self): 
     """ retorna o ip como inteiro nao-negativo de 4 bytes """
     if  not self.bip:
         bip = ip2int(self.ip)
         self.bip = bip
     return self.bip
Beispiel #18
0
def prepare_guest_debian(disk_path,
                         hostname,
                         passwords,
                         eth_devs,
                         format=None,
                         apt_proxy_ip=None):

    logger.info("Prepare image for " + hostname)
    if format == 'lxc':
        gfs = LocalGuestFS(disk_path)
        gfs.rm('/etc/init/udev.conf')

        interfaces = []
        for dev, (hw, ip, sz, gw) in eth_devs.items():
            if ip == 'dhcp':
                interfaces.append("dhclient {0}".format(dev))
            else:
                interfaces.append("ifconfig {0} {1}/{2} up".format(
                    dev, ip, sz))
        gfs.write('/etc/init/lxc_lan.conf',
                  ifconfig_script.format("\n".join(interfaces)))
    else:
        gfs = guestfs.GuestFS()
        gfs.add_drive_opts(disk_path, format=format)
        logger.debug("Launch libguestfs vm")
        gfs.launch()
        logger.debug("ok")

        os_devs = gfs.inspect_os()
        if len(os_devs) > 1:
            msg = "Two or more bootable partitions - disk prepare impossible " + disk_path
            logger.error(msg)
            raise CloudError(msg)

        # for dev, fs_type in  gfs.list_filesystems():
        #     logger.debug("Fount partition {0} with fs type {1}".format(dev, fs_type))

        #     # TODO: add lvm support
        #     if fs_type in 'ext2 ext3 reiserfs3 reiserfs4 xfs jfs btrfs':
        #         gfs.mount(dev, '/')
        #         if gfs.exists('/etc'):
        #             logger.debug("Fount /etc on partition {0} - will work on it".format(dev))
        #             break
        #         gfs.umount(dev)
        #         logger.debug("No /etc dir found - continue")

        if 0 == len(os_devs):
            mounts = sorted(gfs.inspect_get_mountpoints(os_devs[0]))

            for mpoint, dev in mounts:
                gfs.mount(dev, mpoint)

                if not gfs.exists('/etc'):
                    msg = "Can't fount /etc dir in image " + disk_path
                    logger.error(msg)
                    raise CloudError(msg)
        else:
            gfs.mount(os_devs[0], '/')
            #gfs.mount('/dev/vda1', '/')

            if not gfs.exists('/etc'):
                msg = "Can't fount /etc dir in image " + disk_path
                logger.error(msg)
                raise CloudError(msg)

    logger.debug("Launch ok. Set hostname")
    #hostname
    gfs.write('/etc/hostname', hostname)

    #set device names
    logger.debug("Set device names and network imterfaces")
    templ = 'SUBSYSTEM=="net", DRIVERS=="?*", ATTR{{address}}=="{hw}", NAME="{name}"'
    rules_fc = []
    interfaces = ["auto lo\niface lo inet loopback"]

    for dev, (hw, ip, sz, gw) in eth_devs.items():
        rules_fc.append(templ.format(hw=hw, name=dev))
        interfaces.append("auto " + dev)

        if ip == 'dhcp':
            interfaces.append("iface {0} inet dhcp".format(dev))
        else:
            interfaces.append("iface {0} inet static".format(dev))
            interfaces.append("    address " + ip)
            network = int2ip(ip2int(ip) & ip2int(netsz2netmask(sz)))
            interfaces.append("    network " + network)
            interfaces.append("    netmask " + netsz2netmask(sz))

    gfs.write('/etc/udev/rules.d/70-persistent-net.rules', "\n".join(rules_fc))
    # gfs.write('/etc/network/interfaces', "\n".join(interfaces))
    gfs.write('/etc/network/interfaces.d/eth0', "\n".join(interfaces))

    # update passwords
    logger.debug("Update passwords")

    chars = "".join(chr(i) for i in range(ord('a'), ord('z') + 1))
    chars += "".join(chr(i) for i in range(ord('A'), ord('Z') + 1))
    chars += "".join(chr(i) for i in range(ord('0'), ord('9') + 1))

    hashes = {}
    for login, passwd in passwords.items():
        salt = "".join(random.choice(chars) for _ in range(8))
        hashes[login] = crypt.crypt(passwd, "$6$" + salt)

    new_shadow = []
    need_logins = set(hashes)

    for ln in gfs.read_file('/etc/shadow').split('\n'):
        ln = ln.strip()
        if ln != '' and ln[0] != '#':
            login = ln.split(':', 1)[0]
            if login in hashes:
                sh_templ = "{login}:{hash}:{rest}"
                sh_line = sh_templ.format(login=login,
                                          hash=hashes[login],
                                          rest=ln.split(':', 2)[2])
                new_shadow.append(sh_line)
                need_logins.remove(login)
        else:
            new_shadow.append(ln)

    for login in need_logins:
        new_sh_templ = "{login}:{hash}:{rest}"
        new_sh_line = new_sh_templ.format(login=login,
                                          hash=hashes[login],
                                          rest="0:0:99999:7:::")
        new_shadow.append(new_sh_line)

    gfs.write('/etc/shadow', "\n".join(new_shadow))

    # add new users to passwd
    ids = []
    logins = []
    passwd = gfs.read_file('/etc/passwd')
    for ln in passwd.split('\n'):
        ln = ln.strip()
        if ln != '' and ln[0] != '#':
            logins.append(ln.split(':', 1)[0])
            ids.append(ln.split(':')[2])
            ids.append(ln.split(':')[3])

    add_lines = []
    try:
        mid = max(i for i in ids if i < 65000)
    except ValueError:
        mid = 0
    mid += 1024

    for login in set(hashes) - set(logins):
        home = '/home/' + login
        add_lines.append(":".join(
            [login, 'x', str(mid),
             str(mid), "", home, '/bin/bash']))
        if not gfs.exists(home):
            gfs.mkdir_p(home)
        mid += 1

    if add_lines != []:
        gfs.write('/etc/passwd', passwd.rstrip() + "\n" + "\n".join(add_lines))

    # if apt_proxy_ip is not None:
    #     logger.debug("Set apt-proxy to http://{0}:3142".format(apt_proxy_ip))
    #     fc = 'Acquire::http {{ Proxy "http://{0}:3142"; }};'.format(apt_proxy_ip)
    #     gfs.write('/etc/apt/apt.conf.d/02proxy', fc)

    logger.debug("Update hosts")

    hosts = gfs.read_file('/etc/hosts')

    new_hosts = ["127.0.0.1 localhost\n127.0.0.1 " + hostname]
    for ln in hosts.split('#'):
        if not ln.strip().startswith('127.0.0.1'):
            new_hosts.append(ln)

    gfs.write('/etc/hosts', "\n".join(new_hosts))

    # allow ssh passwd auth
    if gfs.is_file('/etc/ssh/ssh_config'):
        name = '/etc/ssh/ssh_config'
    elif gfs.is_file('/etc/ssh/sshd_config'):
        name = '/etc/ssh/sshd_config'
    else:
        logger.warning(
            "Both '/etc/ssh/sshd_config' and '/etc/ssh/ssh_config' are absent. Skip ssh config patching"
        )
        name = None

    if name is not None:
        sshd_conf = gfs.read_file('/etc/ssh/ssh_config')
        sshd_conf_lines = sshd_conf.split("\n")
        for pos, ln in enumerate(sshd_conf_lines):
            if "PasswordAuthentication" in ln:
                sshd_conf_lines[pos] = "PasswordAuthentication yes"
                break
        else:
            sshd_conf_lines.append("PasswordAuthentication yes")
        gfs.write('/etc/ssh/ssh_config', "\n".join(sshd_conf_lines))
Beispiel #19
0
def register():
    print("request.headers", request.headers)
    try:
        X_APP_ID = request.headers["X-App-Id"]
    except:
        error = json.dumps({"error": "Missing X-APP-ID!"})
        return json_response(error, 403)

    try:
        X_DEVICE_ID = request.headers["X-Device-Id"]
    except:
        error = json.dumps({"error": "Missing X-DEVICE-ID!"})
        return json_response(error, 403)

    data = request.json
    print("data", data)
    try:
        b = all([
            data.get("username"),
            data.get("password"),
            data.get("phone"),
            data.get("email"),
            data.get("WeChatID")
        ])
    except Exception as e:
        # print(e)
        error = json.dumps({"error": "HTTPS request body imcomplete!"})
        return json_response(error, 400)

    params = {
        "username": data["username"],
        "password": data["password"],
        "phone": data["phone"],
        "email": data["email"],
        "WeChatID": data["WeChatID"]
    }

    IP_addr = request.remote_addr
    IP_addr_int = ip2int(IP_addr)

    try:
        HOSTNAME = "rm-uf6ktwa39f10394a7no.mysql.rds.aliyuncs.com"
        PORT = "3306"
        DATABASE = "pigfarmdb"
        USERNAME = "******"
        PASSWORD = "******"

        DB_URI = "mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8".\
        format(username=USERNAME,password=PASSWORD,host=HOSTNAME,port=PORT,db=DATABASE)

        engine = create_engine(DB_URI)
        conn = engine.connect()

        conn.execute("insert into user(username, password, email, phone, WeChatID, registered_IP) VALUES(\"{username}\", \"{password}\", \"{email}\", {phone}, \"{WeChatID}\", {registered_IP});"\
            .format(username=params["username"], password=params["password"], email=params["email"], phone=params["phone"], WeChatID=params["WeChatID"], registered_IP=IP_addr_int))

        # result = conn.execute("select user_id from user where username=\"{var1}\";".format(var1=params["username"]))
        # user_id = result.fetchone()[0]

        # user_id_hash = get_sha256_hash(str(user_id))

        # conn.execute("update user set user_id_hash = \"{var2}\" where username = \"{var3}\";".format(var2=user_id_hash, var3=params["username"]))

        day_theme_list = generate_day_theme_list()
        query = build_queries_from_dict_username(params["username"],
                                                 day_theme_list, "UPDATE")
        conn.execute(query)

        conn.close()
        return json_response()
    except Exception as e:
        conn.close()
        error = json.dumps({"error": e})
        return json_response(error, 403)
Beispiel #20
0
 def __sendHello(self, address):
     packet = struct.pack("!BIHH", 1, utils.ip2int(self.address[0]),
                          self.address[1], self.neighbour[address])
     self.__sendPacket(packet, address)
Beispiel #21
0
import settings
import utils

client = pymongo.MongoClient(settings.MONGO_CONN)

print('importing CSV file')
with open(settings.CSV_PATH, 'rb') as csvfile:
    ip_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    k = 0
    for row in ip_reader:
        k += 1
        if k % 200000 == 0:
            print('loaded %d addresses' % k)
        
        if row[0] == '::':
            break
        start_address = utils.ip2int(row[0])
        stop_address = utils.ip2int(row[1])
        doc = {
            'x': start_address,
            'y': stop_address,
            'country': row[2],
            'county': row[3],
            'city': row[4]
        }
        client.ipdb.addresses.insert_one(doc)

print('creating indexes')
client.ipdb.addresses.ensure_index([('x', 1)])
client.ipdb.addresses.ensure_index([('y', 1)])
print('finished')
Beispiel #22
0
 def send(self, data, address):
     packet = struct.pack("!BIHIH%ds" % len(data), 0,
                          utils.ip2int(self.address[0]), self.address[1],
                          utils.ip2int(address[0]), address[1], data)
     self.__sendPacket(packet, address)