点 赞
3
热 度
73
评 论
0

CQU校园网负载均衡的改进

  1. 首页
  2. CQU校园网
  3. CQU校园网负载均衡的改进

首先得感谢前人智慧:

https://github.com/cyyself/drcom-http-multidial

但这个已过时,登录方法在如今已经无法使用。

我针对A区网络环境稍微进行抓包修改了一下。同时我参考了这篇文章的登录命令,非常感谢!

https://blog.krytro.com/blogs/daily/240322.html

代码如下:

import os
from time import *
conn = [
	{
		"username":"20252025", #学号
		"password":"cqucqucqu", #密码
        "ua": "Mozilla%2F5.0%20(Macintosh%3B%20Intel%20Mac%20OS%20X%2010_15_7)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F139.0.0.0%20Safari%2F537.36",
		"night_discon":False, #周一到周五0:00-6:00晚上是否不连接
		"type":"1", #电脑1手机2
		"callback":"dr1004",#电脑dr1004手机dr1005
		"mwan_interface":"WANA",#在mwan显示的名称
		"device_interface":"wan_A",#在接口显示的名称
		"v": "4180"
	},
	{
		"username": "20252025",
		"password": "cqucqucqu",
		"ua": "Mozilla%2F5.0%20(Macintosh%3B%20Intel%20Mac%20OS%20X%2010_15_7)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F139.0.0.0%20Safari%2F537.36",
		"night_discon": False,
		"type":"1",
		"callback":"dr1004",
		"mwan_interface": "WANB",
		"device_interface": "wan_B",
		"v": "4180"
	},
	{
        #目前发现无法稳定连接3台设备,会频繁掉线,最好两台设备。
		"username": "20252025",
		"password": "cqucqucqu",
		"ua": "Mozilla%2F5.0%20(iPhone%3B%20CPU%20iPhone%20OS%2018_5%20like%20Mac%20OS%20X)%20AppleWebKit%2F605.1.15%20(KHTML%2C%20like%20Gecko)%20Version%2F18.5%20Mobile%2F15E148%20Safari%2F604.1",
		"night_discon": False,
		"type":"2",
		"callback":"dr1005",
		"mwan_interface": "WANC",
		"device_interface": "wan_C",
		"v": "9367"
	}
]
tmp_file = "/tmp/drcom_result.txt"
#CONFIG END

def check_status(interface):# If you don't use mwan3, you should manually change this function.
	os.system("/usr/sbin/mwan3 status > {}".format(tmp_file))
	try:
		with open(tmp_file,'r',encoding='ascii',errors='ignore') as file:
			result = file.read()
			if result.find("interface {} is offline".format(interface)) != -1:
				return False
			else:
				return True
	except:
		return True


def do_login(username,password,device_interface,mwan_interface,ua,type,callback,v):
	ip = "$(ifconfig " + device_interface + " | grep 'inet addr:' | grep -oE '([0-9]{1,3}.){3}.[0-9]{1,3}' | head -n 1)"
	if callback == "dr1004":
		mac = "$(cat /sys/class/net/"+device_interface+"/address | sed 's/://g')"
	else:
		mac = "000000000000"
	cmd = "mwan3 use {} curl -s \"https://login.cqu.edu.cn:802/eportal/portal/login?callback={}&login_method=1&user_account=%2C0%2C{}&user_password={}&wlan_user_ip={}&wlan_user_ipv6=&wlan_user_mac={}&wlan_ac_ip=&wlan_ac_name=&term_ua={}&term_type={}&jsVersion=4.2.2&terminal_type={}&lang=zh-cn&v={}&lang=zh\" ".format(mwan_interface,callback,username,password,ip,mac,ua,type,type,v)
	print(cmd)
	os.system(cmd)

def write_pid():
	with open('/var/run/drcom.pid','w') as file:
		file.write(str(os.getpid()))

def watchdog():
	cold_start = True
	while True:
		for x in conn:
			if x['night_discon'] and (time()+8*60*60) % (24*60*60) < (6*60*60) and (strftime("%a",gmtime(time()+8*60*60)) not in ['Sat','Sun']):
				continue
			if not check_status(x['mwan_interface']):
				print("[INFO] {} attempt login {}".format(asctime(localtime(time())),x['username']))
				do_login(x['username'],x['password'],x['device_interface'],x['mwan_interface'],x['ua'],x["type"],x['callback'],x['v'])
			else:
				if cold_start:
					print("[INFO] {} already logged in {}".format(asctime(localtime(time())),x['username']))
		cold_start = False
		sleep(30)

def addiprule():
	# Avoid error caused by mwan3
	os.system("/sbin/ip rule del pref 100 to {} lookup main".format(authserver))
	os.system("/sbin/ip rule add pref 100 to {} lookup main".format(authserver))

addiprule()
write_pid()
watchdog()

目前在A区不能实现3台设备模拟,会频繁掉线,2台设备较为稳定,欢迎大家留言问题进行修改。(可以在mwan中禁用第三条接口)


一个卑微的CQUer,发点自己觉得还不错的分享。

特呆萌的徒弟

intj 建筑师

站长

具有版权性

请您在转载、复制时注明本文 作者、链接及内容来源信息。 若涉及转载第三方内容,还需一同注明。

具有时效性

导航动态

9 文章数
3 分类数
0 评论数
11标签数
最近评论