Filter IP Addresses Based on Subnet

노션으로 옮김·2023년 12월 8일
0
                for wl in whitelist_ip:
                    cdn, description = wl.split(';;')
                    if '/' in cdn:
                        cdn_net, nmask = cdn.split('/')
                        nmask = int(nmask)
                        bin_a = ''.join([ bin(int(x))[2:].rjust(8, '0') for x in ip.split('.')])
                        bin_b = '1'*(nmask) + '0'*(32-nmask)
                        a = int(bin_a, 2)
                        b = int(bin_b, 2)
                        c = bin(a&b)[2:].rjust(32, '0')

                        res = []
                        for x in range(0, 32, 8):
                            res.append(int(c[x:x+8], 2))

                        ip_net = '.'.join([ str(x) for x in res])
                        if ip_net == cdn_net:
                            censor_msg = cdn + f' ({description})'
                            op = "● " + op + ' --> [' + censor_msg + ']'
                            censored = 1
                            break

0개의 댓글