BITNP/bitsrun

Duplicate functions used in `utils` and `user`

spencerwooo opened this issue · 0 comments

bitsrun/bitsrun/utils.py

Lines 10 to 52 in cb26cc1

def parse_homepage(api_base: str) -> Tuple[str, str]:
"""Parse homepage of 10.0.0.55 and get the acid + ip of current session.
Raises:
Exception: Throw exception if acid not present in the redirected URL.
Exception: Throw exception if response text does not contain IP.
Returns:
A tuple of (ip, acid) of the current session.
"""
res = requests.get(api_base)
# ac_id appears in the url query parameter of the redirected URL
query = parse_qs(urlparse(res.url).query)
ac_id = query.get("ac_id")
if not ac_id:
raise Exception("failed to get acid")
# ip appears in the response HTML
class IPParser(HTMLParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ip = None
def handle_starttag(self, tag, attrs):
if tag == "input":
attr_dict = dict(attrs)
if attr_dict.get("name") == "user_ip":
self.ip = attr_dict["value"]
def feed(self, *args, **kwargs):
super().feed(*args, **kwargs)
return self.ip
parser = IPParser()
ip = parser.feed(res.text)
if not ip:
raise Exception("failed to get ip")
return ip, ac_id[0]

bitsrun/bitsrun/user.py

Lines 63 to 76 in cb26cc1

def _get_user_info(self) -> Optional[str]:
"""Get current logged in user info if exists.
Returns:
The username of the current logged in user if exists.
"""
resp = self.session.get(_API_BASE + "/cgi-bin/rad_user_info")
data = resp.text
if data == "not_online_error":
return None
return data.split(",")[0]

The latter API seems to have covered the former functionality, as in when calling:

GET /cgi-bin/rad_user_info?callback=jsonp HTTP/1.1
Host: 10.0.0.55

When logged in, the API responds with:

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 03 Feb 2023 06:29:43 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 510
Connection: close
Srun-Server: SRunCGIAuthIntfSvr V1.18 B20220802
X-Frame-Options: SAMEORIGIN

{"ServerFlag":4294967040,"add_time":"<REDACTED>","all_bytes":"<REDACTED>","bytes_in":"<REDACTED>","bytes_out":"<REDACTED>","checkout_date":0,"domain":"","error":"ok","group_id":"7","keepalive_time":"<REDACTED>","online_ip":"10.108.11.185","products_name":"学生-10元含200GB不限速流量-2022","real_name":"","remain_bytes":"<REDACTED>","remain_seconds":0,"sum_bytes":"<REDACTED>","sum_seconds":10754,"sysver":"1.01.20220802","user_balance":10,"user_charge":0,"user_mac":"","user_name":"<REDACTED>","wallet_balance":0}

Outer wrap jsonp( and ) omitted for better formatting in GitHub.

TODO:

  • Check what the endpoint responds when not logged in.
  • Check whether ac_id is really necessary when calling the login or logout endpoint.