集群HTTP代理

背景

目前扬子科创集群均无法访问互联网,在未联网的情况下,软件安装,维护效率低下,为了解决该问题,将部署二级代理!

登录节点明细

HOSTNAME IP
njlogin01.yzgroup.local 20.0.0.10/192.168.89.214
njgate01.yzgroup.local 192.168.89.212
扬子科创集群网段

经检查,集群计算机点网段分布如下:

192.168.88.0/23
10.2.150.0/23

gost软件介绍

https://docs.ginuerzh.xyz/gost/getting-started/

代理部署架构

upload successful
upload successful
upload successful
upload successful

查看详情
upload successful

使用
# 终端下临时配置代理
export http_proxy=http://192.168.89.214:8090
export https_proxy=http://192.168.89.214:8090

# 取消设置
unset http_proxy
unset https_proxy
# 写入配置文件,永久生效
cat << EOF >> ~/.bash_profile
export http_proxy=http://192.168.89.214:8090
export https_proxy=http://192.168.89.214:8090
EOF
Ansible Hosts
[proxy-jumpbox]
10.105.42.23 ansible_ssh_user=jianhu.yong ansible_ssh_private_key_file='/opt/keys/id_rsa_2048_jianhu.yong' ansible_become=true ansible_become_user=root ansible_become_method=sudo

[proxy-yzkc]
20.0.0.10 ansible_ssh_user=genewiznj ansible_ssh_private_key_file='/opt/keys/id_rsa_genewiznj' ansible_remote_tmp='/tmp/.ansible/tmp'
CODE
class ProxyIndexView(View):
def get(self, request):
current_year = datetime.now().year
proxy_ip = ['10.105.42.23','20.0.0.10']
proxy_status = {}
for ip in proxy_ip:
job = ANSRunner()
job.run_model(host_list='{0}'.format(ip), module_name='shell',
module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
proxy_status[ip] = 'Running'
else:
proxy_status[ip] = 'Stopped'
return render(request, "proxy-index.html", {
'current_year': current_year,
'level1': proxy_status['10.105.42.23'],
'level2': proxy_status['20.0.0.10']
})

def post(self, request):
proxy_ip = request.POST.get('proxy_ip','')
action = request.POST.get('action','')
level = int(request.POST.get('level', ''))

if level == 1:
if action == 'start':
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
res.update({'status': 'Has Running', 'msg': '代理服务器已运行,操作无效!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='cd /opt/genewiznj/gost/ && nohup ./gost -C gost.json &> /dev/null &')
res = job.get_model_result()
if res['success']:
res.update({'status': 'Running', 'msg': '代理服务启动成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
elif action == 'stop':
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell', module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args="ps -ef|grep gost|grep -v grep|awk '{print $2}'|xargs kill -9")
res = job.get_model_result()
if res['success']:
res.update({'status': 'Stopped', 'msg': '代理服务停止成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
res.update({'status': 'Has Stopped', 'msg': '代理服务已停止,操作无效!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
elif action == 'restart':
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell', module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args="ps -ef|grep gost|grep -v grep|awk '{print $2}'|xargs kill -9 && cd /opt/genewiznj/gost/ && nohup ./gost -C gost.json &> /dev/null &")
res = job.get_model_result()
if res['success']:
res.update({'status': 'Restarted', 'msg': '代理服务重启成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='cd /opt/genewiznj/gost/ && nohup ./gost -C gost.json &> /dev/null &')
res = job.get_model_result()
if res['success']:
res.update({'status': 'Restarted', 'msg': '代理服务重启成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='curl -I -x {0}:8090 http://www.baidu.com'.format(proxy_ip))
res = job.get_model_result()
if res['success']:
res.update({'status': 'curl-success', 'msg': '代理服务测试成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
res.update({'status': 'curl-failer', 'msg': '代理服务测试失败!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
elif level == 2:
if action == 'start':
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
res.update({'status': 'Has Running', 'msg': '代理服务器已运行,操作无效!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='cd /YZGROUP/genewiz/public/app/gost-2.4/ && nohup ./gost -C gost.json &> /dev/null &')
res = job.get_model_result()
if res['success']:
res.update({'status': 'Running', 'msg': '代理服务启动成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
elif action == 'stop':
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell', module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args="ps -ef|grep gost|grep -v grep|awk '{print $2}'|xargs kill -9")
res = job.get_model_result()
if res['success']:
res.update({'status': 'Stopped', 'msg': '代理服务停止成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
res.update({'status': 'Has Stopped', 'msg': '代理服务已停止,操作无效!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
elif action == 'restart':
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell', module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
if res['success']:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args="ps -ef|grep gost|grep -v grep|awk '{print $2}'|xargs kill -9 && cd /YZGROUP/genewiz/public/app/gost-2.4/ && nohup ./gost -C gost.json &> /dev/null &")
res = job.get_model_result()
if res['success']:
res.update({'status': 'Restarted', 'msg': '代理服务重启成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='cd /YZGROUP/genewiz/public/app/gost-2.4/ && nohup ./gost -C gost.json &> /dev/null &')
res = job.get_model_result()
if res['success']:
res.update({'status': 'Restarted', 'msg': '代理服务重启成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell',
module_args='curl -I -x {0}:8090 http://www.baidu.com'.format(proxy_ip))
res = job.get_model_result()
if res['success']:
res.update({'status': 'curl-success', 'msg': '代理服务测试成功!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
else:
res.update({'status': 'curl-failer', 'msg': '代理服务测试失败!'})
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')


class ProxyStatusView(View):
def get(self, request):
current_year = datetime.now().year
proxy_ip = request.GET.get('proxy_ip','')
job = ANSRunner()
job.run_model(host_list='{0}'.format(proxy_ip), module_name='shell', module_args='netstat -anptul|grep 8090')
res = job.get_model_result()
return HttpResponse(json.dumps(res, indent=4), content_type='application/json')
Ajax
<script>
$(function () {
// initHighlightingOnLoad
$('pre code').each(function(i, block) {
$(this).parent().addClass('hljs')
hljs.highlightBlock(block);
});

function proxy(current_elem, proxy_ip, action,level){
$.ajax({
cache: false,
type: "POST",
url:"{% url 'proxy:proxy_index' %}",
data:{'proxy_ip':proxy_ip,'action':action,'level':level},
async: true,
beforeSend:function(xhr, settings){
xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
},
success: function(data) {
if(data.status === 'Has Running'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 5
})
}else if(data.status === 'Running'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 1
});
console.log(current_elem.parents('tr').children('td:eq(5)').children('span').text('Running').removeClass().addClass('label label-success'))
}else if(data.status === 'Has Stopped'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 5
})
}else if(data.status === 'Stopped'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 1
});
console.log(current_elem.parents('tr').children('td:eq(5)').children('span').text('Stopped').removeClass().addClass('label label-danger'))
}else if(data.status === 'Restarted'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 1
});
window.location.reload()
}else if(data.status === 'curl-success'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 1
})
}else if(data.status ==='curl-failer'){
layer.alert(data['msg'],{
skin: 'layui-layer-lan',
closeBtn: 0,
icon: 5
});
}
}
})
}


// 启动一级代理服务
$('.start-proxy-one').click(function(){
var action = 'start';
var level = 1;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)
});

// 停止一级代理服务
$('.stop-proxy-one').click(function(){
layer.msg('正在停止代理服务,请稍候!', {icon: 6});
var action = 'stop';
var level = 1;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)

});

// 重启一级代理服务
$('.restart-proxy-one').click(function(){
var action = 'restart';
var level = 1;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)

});

// 测试一级代理服务
$('.test-proxy-one').click(function(){
var action = 'test';
var level = 1;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)

});


// 启动二级代理服务
$('.start-proxy-two').click(function(){
var action = 'start';
var level = 2;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)
});

// 停止二级代理服务
$('.stop-proxy-two').click(function(){
layer.msg('正在停止代理服务,请稍候!', {icon: 6});
var action = 'stop';
var level = 2;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)

});

// 重启二级代理服务
$('.restart-proxy-two').click(function(){
var action = 'restart';
var level = 2;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)

});

// 测试二级代理服务
$('.test-proxy-two').click(function(){
var action = 'test';
var level = 2;
var proxy_ip = $(this).parents('tr').children('td:eq(2)').text();
console.log(proxy_ip);
console.log(this);
proxy($(this),proxy_ip,action,level)

});

})
</script>
0%