Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ Usage: bitsrun status [OPTIONS]
Check current network login status.

Options:
--json / --no-json Output in JSON format.
--help Show this message and exit.
--json / --no-json Output in JSON format.
-a, --auth_url TEXT Authentication portal URL. (Optional)
--help Show this message and exit.
```

> **Note**: this is the output of `bitsrun status --help`.
Expand All @@ -69,6 +70,7 @@ Usage: bitsrun login/logout [OPTIONS]
Options:
-u, --username TEXT Your username.
-p, --password TEXT Your password.
-a, --auth_url TEXT Authentication portal URL. (Optional)
-v, --verbose Verbosely echo API response.
--help Show this message and exit.
```
Expand Down Expand Up @@ -112,6 +114,19 @@ To list all possible paths for your system (including those only for backward co
bitsrun config-paths
```

### Srun at other campuses

This script also works for other campuses using srun authentication. If your campus uses a different authentication portal, just add the `"auth_url"` field to your `bit-user.json` config file:

```json
{
"username": "xxxx",
"password": "xxxx",
"auth_url": "http(s)://your-portal-address"
}
```

Or use the `--auth_url` option in CLI commands. This makes bitsrun compatible with other schools or custom portals.
### Raycast script (macOS)

![raycast screenshot](https://user-images.githubusercontent.com/32114380/213919582-eff6d58f-1bd2-47b2-a5da-46dc6e2eaffa.png)
Expand Down
18 changes: 10 additions & 8 deletions src/bitsrun/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
_options = [
click.option('-u', '--username', help='Your username.', required=False),
click.option('-p', '--password', help='Your password.', required=False),
click.option('-a', '--auth_url', default='http://10.0.0.55', help='Authentication portal URL. (Optional)'),
click.option('-v', '--verbose', is_flag=True, help='Verbosely echo API response.'),
]

Expand Down Expand Up @@ -51,9 +52,10 @@ def config_paths():

@cli.command()
@click.option('--json/--no-json', default=False, help='Output in JSON format.')
def status(json: bool):
@click.option('-a', '--auth_url', default='http://10.0.0.55', help='Authentication portal URL. (Optional)')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@click.option('-a', '--auth_url', default='http://10.0.0.55', help='Authentication portal URL. (Optional)')
@click.option('-a', '--auth-url', default='http://10.0.0.55', help='Authentication portal URL. (Optional)')

上面--no-json-而非_,最好保持一致。(如果这个参数要保留的话)

def status(json: bool, auth_url: str):
"""Check current network login status."""
login_status = get_login_status()
login_status = get_login_status(auth_url)

# Output in JSON format if `--json` is passed, then exit
if json:
Expand All @@ -76,19 +78,19 @@ def status(json: bool):

@cli.command()
@add_options(_options)
def login(username, password, verbose):
def login(username, password, auth_url, verbose):
"""Log into the BIT network."""
do_action('login', username, password, verbose)
do_action('login', username, password, auth_url, verbose)


@cli.command()
@add_options(_options)
def logout(username, password, verbose):
def logout(username, password, auth_url, verbose):
"""Log out of the BIT network."""
do_action('logout', username, password, verbose)
do_action('logout', username, password, auth_url, verbose)


def do_action(action, username, password, verbose):
def do_action(action, username, password, auth_url, verbose):
# Support reading password from stdin when not passed via `--password`
if username and not password:
password = getpass(prompt='Please enter your password: ')
Expand All @@ -97,7 +99,7 @@ def do_action(action, username, password, verbose):
# Try to read username and password from args provided. If none, look for config
# files in possible paths. If none, fail and prompt user to provide one.
if username and password:
user = User(username, password)
user = User(username, password, auth_url)
elif conf := read_config():
if verbose:
click.echo(
Expand Down
12 changes: 6 additions & 6 deletions src/bitsrun/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
from bitsrun.models import LoginStatusRespType, UserResponseType
from bitsrun.utils import fkbase64, xencode

_API_BASE = 'http://10.0.0.55'
_TYPE_CONST = 1
_N_CONST = 200


def get_login_status(client: Optional[httpx.Client] = None) -> LoginStatusRespType:
def get_login_status(auth_url, client: Optional[httpx.Client] = None) -> LoginStatusRespType:
"""Get current login status of the device.

Note:
Expand All @@ -31,19 +30,20 @@ def get_login_status(client: Optional[httpx.Client] = None) -> LoginStatusRespTy
"""

if not client:
client = httpx.Client(base_url=_API_BASE)
client = httpx.Client(base_url=auth_url)

resp = client.get('/cgi-bin/rad_user_info', params={'callback': 'jsonp'})
return json.loads(resp.text[6:-1])


class User:
def __init__(self, username: str, password: str):
def __init__(self, username: str, password: str, auth_url: str='http://10.0.0.55'):
self.username = username
self.password = password
self.auth_url = auth_url

# Initialize reused httpx client
self.client = httpx.Client(base_url=_API_BASE)
self.client = httpx.Client(base_url=self.auth_url)

# Visit another site using HTTP, and let srun redirect to 10.0.0.55
# with url params (ac_id, theme, wlanuserip, etc.)
Expand All @@ -58,7 +58,7 @@ def __init__(self, username: str, password: str):
self.acid = resp_valid.url.params.get('ac_id')

# Check current login status and get device `online_ip`
login_status = get_login_status(client=self.client)
login_status = get_login_status(auth_url=self.auth_url, client=self.client)
self.ip = login_status.get('online_ip')
self.logged_in_user = login_status.get('user_name')

Expand Down
Loading