def GetZoneRecords(request): if request.method == 'GET': return HttpResponse('You get nothing!') elif request.method == 'POST': if request.META.has_key('HTTP_X_FORWARDED_FOR'): clientip = request.META['HTTP_X_FORWARDED_FOR'] else: clientip = request.META['REMOTE_ADDR'] logger.info('[POST]%s is requesting. %s' % (clientip, request.get_full_path())) data = json.loads(request.body)['postdata'] logger.info(data) record_list = [] for zone in data: cf_acc = cf_account.objects.filter(name=zone['product']).first() cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) result = cfapi.GetZoneRecords(zone['zone_id']) if len(result['result']) == 0: continue for record in result['result']: tmp_dict = {} tmp_dict['product'] = zone['product'] tmp_dict['zone'] = record['zone_name'] tmp_dict['name'] = record['name'] tmp_dict['type'] = record['type'] tmp_dict['content'] = record['content'] tmp_dict['proxied'] = record['proxied'] tmp_dict['record_id'] = record['id'] tmp_dict['zone_id'] = record['zone_id'] record_list.append(tmp_dict) return HttpResponse(json.dumps(record_list)) else: return HttpResponse('nothing!')
def receive(self, text=None, bytes=None, **kwargs): """ Called when a message is received with either text or bytes filled out. """ #self.close() return_info = {} return_info['permission'] = True data = json.loads(self.message['text']) self.clientip = '127.0.0.1' self.username = self.message.user.username try: self.role = self.message.user.userprofile.role except: self.role = 'none' #判断是否有权限 if not HasDnsPermission(self.message, "cf", data['product'], "add"): return_info['permission'] = False return_info['result'] = False self.message.reply_channel.send({'text': json.dumps(return_info)}) self.close() return False step = 0 for sub_domain in data['sub_domain']: step += 1 return_info['domain'] = sub_domain + '.' + data[ 'zone'] if sub_domain != "@" else data['zone'] return_info['step'] = step cf_acc = cf_account.objects.get(name=data['product']) try: cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) except Exception as e: logger.error("新增 %s 域名失败!" % return_info['domain']) return_info['result'] = False else: result = cfapi.CreateZoneRecord( zone_id=data['zone_id'], record_name=return_info['domain'], record_type=data['type'], record_content=data['content'], proxied=True if data['proxied'].lower() == 'true' else False, ) return_info['result'] = result['success'] if return_info['result']: insert_ah( self.clientip, self.username, "null", "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" % (data['type'], sub_domain + '.' + data['zone'], data['content'], '1'), return_info['result'], 'add') self.message.reply_channel.send({'text': json.dumps(return_info)})
def GetProductRecords(request): if request.method == 'GET': return HttpResponse('You get nothing!') elif request.method == 'POST': clientip = getIp(request) username = request.user.username try: role = request.user.userprofile.role except: role = 'none' if not username: request.websocket.send('userNone') logger.info('user: 用户名未知 | [POST]%s is requesting. %s' % (clientip, request.get_full_path())) return HttpResponseServerError("用户名未知!") logger.info('[POST]%s is requesting. %s' % (clientip, request.get_full_path())) if request.user.is_superuser: products = cf_account.objects.all() else: products = [ dns.cf_account for dns in request.user.userprofile.dns.filter( permission='read').all() if dns.cf_account ] zone_name_list = [] for product in products: cf_acc = cf_account.objects.get(name=product.name) cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) page = 1 result = cfapi.GetDnsLists(page=page) total_pages = result['result_info']['total_pages'] tmp_dict = { 'product': product.name, 'product_py': lazy_pinyin(product.name), 'domain': [], } if len(result['result']) == 0: continue while page <= total_pages: for record in result['result']: tmp_dict['domain'].append({ 'name': record['name'], 'id': record['id'], 'status': 'enable', }) page += 1 result = cfapi.GetDnsLists(page=page) zone_name_list.append(tmp_dict) #logger.info(zone_name_list) zone_name_list.sort(key=takeId) #以product 拼音排序 return HttpResponse(json.dumps(zone_name_list)) else: return HttpResponse('nothing!')
def purgeCfDomain(request): if request.method == 'POST': email = "*****@*****.**" key = "29697a07614e03226eb4d32e08b0c2e337cf2" data = json.loads(request.body) cfapi = CfApi(CF_URL, email, key) result = cfapi.purge(data["zone_id"]) logger.info(result) return HttpResponse(json.dumps(result))
def DeleteRecords(request): if request.method == 'GET': return HttpResponse('You get nothing!') elif request.method == 'POST': clientip = getIp(request) username = request.user.username manage = request.user.userprofile.manage try: role = request.user.userprofile.role except: role = 'none' if not username: logger.info('user: 用户名未知 | [POST]%s is requesting. %s' % (clientip, request.get_full_path())) return HttpResponseServerError("用户名未知,请登陆有效账号!") logger.info('[POST]%s is requesting. %s' % (clientip, request.get_full_path())) data = json.loads(request.body) record_list = [] for zone in data: if not HasDnsPermission(request, "cf", zone['product'], "delete"): return HttpResponseServerError("抱歉,您没有删除账号[%s]解析的权限!" % zone['product']) cf_acc = cf_account.objects.get(name=zone['product']) try: cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) except Exception as e: logger.error("删除 %s 域名失败!%s" % (zone['name'], str(e))) return HttpResponseServerError("删除 %s 域名失败!" % zone['name']) else: result = cfapi.DeleteZoneRecord(zone['zone_id'], zone['record_id']) if not result['success']: logger.error("删除 %s 域名失败!%s" % (zone['name'], str(result))) return HttpResponseServerError("删除 %s 域名失败!" % zone['name']) else: logger.info("删除 %s 域名成功!%s" % (zone['name'], str(result))) insert_ah( clientip, username, "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" % (zone['type'], zone['name'], zone['content'], zone['proxied']), "null", result['success'], 'delete') return HttpResponse("删除 %s 域名成功!" % zone['name']) else: return HttpResponse('nothing!')
def receive(self, text=None, bytes=None, **kwargs): """ Called when a message is received with either text or bytes filled out. """ #self.close() self.clientip = '127.0.0.1' self.username = self.message.user.username try: self.role = self.message.user.userprofile.role except: self.role = 'none' data = json.loads(self.message['text']) step = 0 for record in data['records']: step += 1 return_info = {} return_info['record'] = record return_info['step'] = step return_info['permission'] = True #判断是否有权限 if not HasDnsPermission(self.message, "cf", record['product'], "change"): return_info['permission'] = False return_info['result'] = False self.message.reply_channel.send({'text': json.dumps(return_info)}) continue cf_acc = cf_account.objects.filter(name=record['product']).first() cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) if data['proxied'] == 'true': proxied = True else: proxied = False result = cfapi.UpdateZoneRecord(record['zone_id'], data['type'], record['name'], data['content'], proxied=proxied, record_id=record['record_id']) if not result['success']: return_info['result'] = False else: return_info['result'] = True logger.info("req_ip: %s | user: %s | updaterecord: { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s } ---> { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s }" %(self.clientip, self.username, record['type'], record['name'], record['content'], record['proxied'], data['type'], record['name'], data['content'], proxied)) insert_ah(self.clientip, self.username, "'type':%s, 'name': %s, 'content': %s, 'proxied':%s" %(record['type'], record['name'], record['content'], record['proxied']), "'type':%s, 'name': %s, 'content': %s, 'proxied':%s" %(data['type'], record['name'], data['content'], proxied), return_info['result']) self.message.reply_channel.send({'text': json.dumps(return_info)})
def UpdateRecord(request): if request.method == 'POST': if request.META.has_key('HTTP_X_FORWARDED_FOR'): clientip = request.META['HTTP_X_FORWARDED_FOR'] else: clientip = request.META['REMOTE_ADDR'] data = json.loads(request.body) logger.info('%s is requesting. %s data: %s' % (clientip, request.get_full_path(), data)) api = CfApi('https://api.cloudflare.com/client/v4/zones/', '*****@*****.**', '763d1ef590cea5ec364ddd6e676eee0e72f3d') result = api.UpdateDnsRecords(data['zone_id'], data['record_type'], data['record_name'], data['record_content'], data['proxied']) if result['result'] == 'id null': logger.info('%s doesn\'t exist. pls check!' % data['record_name']) return HttpResponse('%s doesn\'t exist. pls check!' % data['record_name']) elif result['result'] == 'id id more than one': logger.info('%s has more than one id. pls check!' % data['record_name']) return HttpResponse('%s has more than one id. pls check!' % data['record_name']) elif result['result'] == None or result['result'] == 'bad arguments': logger.info('wrong arguments. pls check!') return HttpResponse('wrong arguments. pls check!') elif result['result'] == {}: logger.info( 'request https://api.cloudflare.com raise a exception. pls check!' ) return HttpResponse( 'request https://api.cloudflare.com raise a exception. pls check!' ) else: logger.info('update successfully!') return HttpResponse(json.dumps(result['result'])) elif request.method == 'GET': return HttpResponse('You get nothing!') else: return HttpResponse('nothing!')
def UpdateApiRoute(request): if request.method == 'POST': clientip = request.META['REMOTE_ADDR'] data = json.loads(request.body) logger.info('%s is requesting. %s data: %s' % (clientip, request.get_full_path(), data)) domain_l = domain_info.objects.filter(domain=data['domain']).all() zone_id = domain_l[0].zone_id record_id = domain_l[0].record_id cf_account_name = domain_l[0].cf_account_name return_info = {} r_type = 'CNAME' cf_acc = cf_account.objects.filter(name=cf_account_name).first() cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) if data['route'] == 'cloudflare': proxied = True else: proxied = False if data['route'] == 'nginx': content = [ domain_i.content for domain_i in domain_l if domain_i.route == 'nginx' ] elif data['route'] == 'cloudflare': content = [ domain_i.content for domain_i in domain_l if domain_i.route == 'cloudflare' ] elif data['route'] == 'aegins': content = [ domain_i.content for domain_i in domain_l if domain_i.route == 'aegins' ] elif data['route'] == 'wangsu': content = [ domain_i.content for domain_i in domain_l if domain_i.route == 'wangsu' ] result = cfapi.UpdateZoneRecord(zone_id, r_type, data['domain'], content[0], proxied=proxied, record_id=record_id) #logger.info(result) if not result['success']: return_info['result'] = False logger.error(result) else: return_info['result'] = True #api_list = domain_info.objects.filter(domain=data['domain'], status=1).all() api_list = domain_info.objects.filter(domain=data['domain']).all() for info in api_list: if info.route == data['route']: info.route_status = 1 info.save() else: info.route_status = 0 info.save() #logger.info(return_info) return HttpResponse(json.dumps(return_info))
def UpdateRecords(request): if request.is_websocket(): global username, role, clientip if request.META.has_key('HTTP_X_FORWARDED_FOR'): clientip = request.META['HTTP_X_FORWARDED_FOR'] else: clientip = request.META['REMOTE_ADDR'] username = request.user.username try: role = request.user.userprofile.role except: role = 'none' if not username: request.websocket.send('userNone') logger.info('user: 用户名未知 | [POST]%s is requesting. %s' % (clientip, request.get_full_path())) ### close websocket ### request.websocket.close() logger.info('user:%s | [POST]%s is requesting. %s' % (username, clientip, request.get_full_path())) for postdata in request.websocket: #logger.info(type(postdata)) if not postdata: logger.info('this is test!') break data = json.loads(postdata) step = 0 for record in data['records']: step += 1 return_info = {} return_info['record'] = record return_info['step'] = step return_info['permission'] = True #判断是否有权限 if not HasDnsPermission(request, "cf", record['product'], "change"): return_info['permission'] = False return_info['result'] = False request.websocket.send(json.dumps(return_info)) continue cf_acc = cf_account.objects.filter( name=record['product']).first() cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) if data['proxied'] == 'true': proxied = True else: proxied = False result = cfapi.UpdateZoneRecord(record['zone_id'], data['type'], record['name'], data['content'], proxied=proxied, record_id=record['record_id']) if not result['success']: return_info['result'] = False else: return_info['result'] = True logger.info( "req_ip: %s | user: %s | updaterecord: { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s } ---> { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s }" % (clientip, username, record['type'], record['name'], record['content'], record['proxied'], data['type'], record['name'], data['content'], proxied)) insert_h = alter_history( time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), req_ip=clientip, user=username, pre_rec="'type':%s, 'name': %s, 'content': %s, 'proxied':%s" % (record['type'], record['name'], record['content'], record['proxied']), now_rec="'type':%s, 'name': %s, 'content': %s, 'proxied':%s" % (data['type'], record['name'], data['content'], proxied)) insert_h.save() request.websocket.send(json.dumps(return_info)) ### close websocket ### request.websocket.close()
def CreateRecords(request): if request.method == 'POST': clientip = getIp(request) username = request.user.username try: role = request.user.userprofile.role except: role = 'none' #if not username: # logger.info('user: 用户名未知 | [POST]%s is requesting. %s' %(clientip, request.get_full_path())) # return HttpResponseServerError("用户名未知!") logger.info('user:%s | [POST]%s is requesting. %s' % (username, clientip, request.get_full_path())) data = json.loads(request.body) #判断是否有权限 #if not HasDnsPermission(request, "cf", data['product'], "add"): # return HttpResponseServerError("抱歉,您没有新增账号[%s]解析的权限!" %data['product']) result_list = [] for sub_domain in data['sub_domain']: cf_acc = cf_account.objects.get(name=data['product']) record_name = sub_domain + '.' + data[ 'zone'] if sub_domain != '@' else data['zone'] try: cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) except Exception as e: info = "新增 %s 域名失败: %s" % (record_name, str(e)) logger.error(info) result = {'result': None, 'errors': str(e), 'success': False} else: result = cfapi.CreateZoneRecord( zone_id=data['zone_id'], record_name=record_name, record_type=data['type'], record_content=data['content'], proxied=True if data['proxied'].lower() == 'true' else False, ) result_list.append(result) insert_ah( clientip, username, "null", "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" % (data['type'], record_name, data['content'], '1'), result['success'], 'add') if not result['success']: return HttpResponseServerError(result_list) return HttpResponse(json.dumps(result_list)) elif request.is_websocket(): clientip = getIp(request) username = request.user.username try: role = request.user.userprofile.role except: role = 'none' if not username: request.websocket.send('userNone') logger.info('user: 用户名未知 | [WS]%s is requesting. %s' % (clientip, request.get_full_path())) ### close websocket ### request.websocket.close() logger.info('user:%s | [WS]%s is requesting. %s' % (username, clientip, request.get_full_path())) for postdata in request.websocket: if not postdata: ### close websocket ### request.websocket.close() break data = json.loads(postdata) #判断是否有权限 if not HasDnsPermission(request, "cf", data['product'], "add"): request.websocket.send('noPermission') ### close websocket ### request.websocket.close() break step = 0 for sub_domain in data['sub_domain']: step += 1 return_info = {} return_info['domain'] = sub_domain + '.' + data[ 'zone'] if sub_domain != "@" else data['zone'] return_info['step'] = step cf_acc = cf_account.objects.get(name=data['product']) try: cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key) except Exception as e: logger.error("新增 %s 域名失败!" % return_info['domain']) return_info['result'] = False else: result = cfapi.CreateZoneRecord( zone_id=data['zone_id'], record_name=return_info['domain'], record_type=data['type'], record_content=data['content'], proxied=True if data['proxied'].lower() == 'true' else False, ) return_info['result'] = result['success'] insert_ah( clientip, username, "null", "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" % (data['type'], sub_domain + '.' + data['zone'], data['content'], '1'), return_info['result'], 'add') request.websocket.send(json.dumps(return_info)) ### close websocket ### request.websocket.close() elif request.method == 'GET': return HttpResponse('You get nothing!') else: return HttpResponse('nothing!')
from phxweb.settings import DATABASES as databases from dns.cf_api import CfApi from phxweb.settings import CF_URL #获取当前目录 current_dir = os.path.abspath(os.path.dirname(__file__)) #print basedir cursor = django.db.connection.cursor() with open('%s/api_info.txt' % current_dir, 'r') as f: contents = f.readlines() for info in contents: if not '#' in info and info.replace(' ', '') != '\n': cf_acc = cf_account.objects.filter(name=info.split()[3]).first() api = CfApi(CF_URL, cf_acc.email, cf_acc.key) zone_id = api.GetZoneId('.'.join( info.split()[2].split('.')[-2:]))['zone_id'] record_id = api.GetDnsRecordId(zone_id, info.split()[2]) try: #update = domain_info.objects.filter(product=info.split()[0], client=info.split()[1], domain=info.split()[2], route=info.split()[4]).first() #update = domain_info.objects.filter(product=info.split()[0], client=info.split()[1], domain=info.split()[2], content=info.split()[5]).first() #update.zone_id = zone_id #update.route = info.split()[4] #update.record_id = record_id #update.status = info.split()[6] #update.route_status = info.split()[7] #update.save() #print info.split()[0], info.split()[2], info.split()[4], 'update success.'
def receive(self, text=None, bytes=None, **kwargs): """ Called when a message is received with either text or bytes filled out. """ #self.close() data = json.loads(self.message['text']) self.clientip = '127.0.0.1' self.username = self.message.user.username try: self.role = self.message.user.userprofile.role except: self.role = 'none' ### step one ## info = {} info['result'] = [] info['step'] = 'one' self.message.reply_channel.send({'text': json.dumps(info)}) #time.sleep(2) ### two step ### info['step'] = 'two' cdn_d = {} cdns = cdn_t.objects.all() for cdn in cdns: cdn_d[cdn.get_name_display() + "_" + cdn.account] = { 'name': cdn.get_name_display(), 'domain': [], 'secretid': str(cdn.secretid), 'secretkey': str(cdn.secretkey), 'failed': [], 'success': [], } cf_d = {} cfs = cf_t.objects.all() for cf in cfs: cf_d[cf.name] = { 'name': cf.name, 'domain': [], 'email': str(cf.email), 'key': str(cf.key), 'failed': [], 'success': [], } domain_l = domains.objects.filter(id__in=data['domain']).all() logger.info(domain_l) for domain in domain_l: for cdn in domain.cdn.all(): cdn_d[cdn.get_name_display() + "_" + cdn.account]['domain'].append( urlparse.urlsplit(domain.name).scheme + "://" + urlparse.urlsplit(domain.name).netloc) for cf in domain.cf.all(): cf_d[cf.name]['domain'].append( urlparse.urlsplit(domain.name).scheme + "://" + urlparse.urlsplit(domain.name).netloc) #logger.info(cdn_d) for cdn in cdn_d: info['cdn'] = cdn if cdn_d[cdn]['domain']: #开始清缓存,判断CDN接口是否存在 if cdn_d[cdn]['name'] == "tencent": req = tcApi(cdn_d[cdn]['secretid'], cdn_d[cdn]['secretkey']) elif cdn_d[cdn]['name'] == "wangsu": req = wsApi(cdn_d[cdn]['secretid'], cdn_d[cdn]['secretkey']) else: info['result'] = ["CDN 接口不存在!"] cdn_d[cdn]['failed'].append("%s: 接口不存在!" % cdn) self.message.reply_channel.send({'text': json.dumps(info)}) continue while len(cdn_d[cdn]['domain']) != 0: domains_c = cdn_d[cdn]['domain'][:10] cdn_d[cdn]['domain'] = cdn_d[cdn]['domain'][10:] for uri in data['uri']: result, status = req.purge(domains_c, uri) if status: info['result'] = [ cdn + ": " + domain + uri + ": 清缓存成功。" for domain in domains_c ] cdn_d[cdn]['success'] += [ domain + uri for domain in domains_c ] else: info['result'] = [ cdn + ": " + domain + uri + ": 清缓存失败!" for domain in domains_c ] cdn_d[cdn]['failed'] += [ domain + uri for domain in domains_c ] self.message.reply_channel.send( {'text': json.dumps(info)}) for cf in cf_d: info['cf'] = cf if cf_d[cf]['domain']: #开始清CF缓存 req = CfApi(CF_URL, cf_d[cf]['email'], cf_d[cf]['key']) for domain in cf_d[cf]['domain']: zone = ".".join(domain.split(".")[-2:]) zone_id = req.GetZoneId(zone)['zone_id'] if not zone_id: info['result'] = [ "CloudFlare_" + cf + ": " + domain + ": 清缓存失败!" ] cf_d[cf]['failed'] += [domain] result = req.purge(zone_id) if result['success']: info['result'] = [ "CloudFlare_" + cf + ": " + domain + ": 清缓存成功。" ] cf_d[cf]['success'] += [domain] else: info['result'] = [ "CloudFlare_" + cf + ": " + domain + ": 清缓存失败!" ] cf_d[cf]['failed'] += [domain] self.message.reply_channel.send({'text': json.dumps(info)}) info['step'] = 'final' self.message.reply_channel.send({'text': json.dumps(info)}) for cdn in cdn_d: if cdn_d[cdn]['failed']: message["text"] = cdn_d[cdn]['failed'] message['caption'] = cdn + ': 域名缓存清理失败!' sendTelegramRe(message) if cdn_d[cdn]['success']: message["text"] = cdn_d[cdn]['success'] message['caption'] = cdn + ': 域名缓存清理成功。' sendTelegramRe(message) for cf in cf_d: if cf_d[cf]['failed']: message["text"] = cf_d[cf]['failed'] message['caption'] = "CloudFlare_" + cf + ': 域名缓存清理失败!' sendTelegramRe(message) if cf_d[cf]['success']: message["text"] = cf_d[cf]['success'] message['caption'] = "CloudFlare_" + cf + ': 域名缓存清理成功。' sendTelegramRe(message) self.close()