Skip to content

Commit

Permalink
feat: new auto mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandaft committed Mar 22, 2023
1 parent 2e2e2c3 commit 60d8d2a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Usage: ESurfingPy-CLI.exe auto [OPTIONS]
多种模式触发重登校园网

Options:
-m, --mode [uls|dls|ult|dlt|itv|mul]
-m, --mode [net|uls|dls|ult|dlt|itv|mul]
触发模式
-t, --threshold FLOAT 触发网速(MB/s)或流量(MB)或时间(s)
-s, --auto-stop BOOLEAN 自动停止(仅对网速模式有效) [default: True]
Expand All @@ -187,6 +187,7 @@ Options:

|| 值备注 | 说明 |
|:---:|--------------------------------------:|------------------------------------------|
| net | <u>net</u>work | 每间隔指定的时间(s)检测网络,断网时自动重登校园网 |
| uls | <u>u</u>p<u>l</u>oad <u>s</u>peed | 实时监控**上传**速率(MB/s),连续 10s 低于指定值时自动重登校园网。 |
| dls | <u>d</u>own<u>l</u>oad <u>s</u>peed | 实时监控**下载**速率(MB/s),连续 10s 低于指定值时自动重登校园网。 |
| ult | <u>u</u>p<u>l</u>oad <u>t</u>raffic | 实时监控**上传**流量(MB),达到指定值时自动重登校园网。 |
Expand All @@ -196,11 +197,15 @@ Options:

示例:

- 间隔 5 秒检测网络,当网络断开时自动登录:

`./ESurfingPy-CLI.exe auto -m net -t 5 -u 125.88.59.131:10001 -c 123.123.123.123 -r 234.234.234.234 -a 15012341234 -p 12345678 -v true`

- 实时监控上传速率,连续 10s 低于 3MB/s 时自动重登:

`./ESurfingPy-CLI.exe auto -m uls -t 3 -u 125.88.59.131:10001 -c 123.123.123.123 -r 234.234.234.234 -a 15012341234 -p 12345678 -v true`

- 实时监控下载流量,达到 600MB 时自动重登:
- 实时监控下载流量,达到 600MB 时自动重登:

`./ESurfingPy-CLI.exe auto -m dlt -t 600 -u 125.88.59.131:10001 -c 123.123.123.123 -r 234.234.234.234 -a 15012341234 -p 12345678 -v true`

Expand Down
4 changes: 2 additions & 2 deletions esurfingpy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.5.1"
__date__ = "2023/03/21"
__version__ = "0.6.0"
__date__ = "2023/03/22"
__url__ = "https://github.com/Pandaft/ESurfingPy-CLI/"
37 changes: 35 additions & 2 deletions esurfingpy/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,35 @@

from .esurfing import ESurfing
from .log import log
from .net import Net
from .net import Net, is_networked


def network(esf: ESurfing, interval: int, threshold: int):
"""断网时自动重登校园网"""

# 最小间隔时间为 1 秒
if int(interval) < 1:
interval = 1

count = 0
while True:
time.sleep(interval)

# 检测网络
if is_networked():
count = 0
log.info("当前网络正常")
continue

# 网络断开
count += 1
log.warning(f"当前网络断开({count} / {threshold})")

# 重登校园网
if count >= threshold:
if not esf.login():
return log.error("登录失败")
count = 0


def speed_mode(esf: ESurfing, mode: str, value: float, auto_stop: bool):
Expand Down Expand Up @@ -139,6 +167,7 @@ def manual_mode(esf: ESurfing):

def relogin(esf: ESurfing, mode: str, value: float, auto_stop: bool):
"""
net, network - 当网络断开时自动登录校园网
uls, upload_speed - 上行速率低于指定值时自动重登校园网
dls, download_speed - 下载速率低于指定值时自动重登校园网
ult, upload_traffic - 上传流量达到指定值时自动重登校园网
Expand All @@ -147,8 +176,12 @@ def relogin(esf: ESurfing, mode: str, value: float, auto_stop: bool):
mul, manual - 手动按回车后自动重登校园网
"""

# 网络模式
if mode == "net":
return network(esf, int(value), 5)

# 速率模式
if mode in ["uls", "dls"]:
elif mode in ["uls", "dls"]:
return speed_mode(esf, mode, value, auto_stop)

# 流量模式
Expand Down
3 changes: 2 additions & 1 deletion esurfingpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def cli_logout(esurfing_url, wlan_acip, wlan_userip, account, password, signatur


@cli.command(name="auto")
@click.option('-m', '--mode', prompt='触发模式', help='触发模式', type=click.Choice(["uls", "dls", "ult", "dlt", "itv", "mul"], case_sensitive=False))
@click.option('-m', '--mode', prompt='触发模式', help='触发模式', type=click.Choice(["net", "uls", "dls", "ult", "dlt", "itv", "mul"], case_sensitive=False))
@click.option('-t', '--threshold', prompt='触发阈值', type=float, help='触发网速(MB/s)或流量(MB)或时间(s)')
@click.option('-s', '--auto-stop', prompt='自动停止', default=True, show_default=True, type=bool, help='自动停止(仅对网速模式有效)')
@click.option('-u', '--esurfing-url', default=DEFAULT_ESURFING_URL, show_default=True, help='校园网登录网址')
Expand All @@ -72,6 +72,7 @@ def cli_logout(esurfing_url, wlan_acip, wlan_userip, account, password, signatur
def cli_auto(mode, threshold, auto_stop, esurfing_url, wlan_acip, wlan_userip, account, password, verbose):
"""多种模式触发重登校园网"""
# mode:
# net, network - 断网时自动重登校园网
# uls, upload_speed - 上行速率低于指定值时自动重登校园网
# dls, download_speed - 下载速率低于指定值时自动重登校园网
# ult, upload_traffic - 上传流量达到指定值时自动重登校园网
Expand Down
11 changes: 11 additions & 0 deletions esurfingpy/net.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import socket
from time import sleep

from psutil import net_io_counters
Expand Down Expand Up @@ -28,3 +29,13 @@ def traffic(self) -> float:
if self.download:
t += net_io_counter.bytes_recv
return round(t / 1024 / 1024, 2)


def is_networked():
"""判断是否已联网"""
try:
socket.create_connection(("114.114.114.114", 53), timeout=2)
return True
except TimeoutError:
pass
return False

0 comments on commit 60d8d2a

Please sign in to comment.