Skip to content

Commit

Permalink
feat(logging): include username in log
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed Jun 27, 2020
1 parent cda1300 commit 0685709
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
48 changes: 41 additions & 7 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,53 @@ def test_port(mpv_instance, v, expected_8080, expected_8000):


@pytest.mark.parametrize(
"mpv_instance", [get_script_opts({"logging": "yes"})], indirect=["mpv_instance"],
"mpv_instance,use_auth,username,password,status_code",
[
(get_script_opts({"logging": "yes"}), False, None, None, 200),
(
get_script_opts({"logging": "yes", "htpasswd_path": "/tmp/.htpasswd"}),
True,
None,
None,
401,
),
(
get_script_opts({"logging": "yes", "htpasswd_path": "/tmp/.htpasswd"}),
True,
"user",
"secret",
200,
),
(
get_script_opts({"logging": "yes", "htpasswd_path": "/tmp/.htpasswd"}),
True,
"user",
None,
401,
),
(get_script_opts({"logging": "yes"}), False, "user", "secret", 200,),
],
indirect=["mpv_instance"],
)
def test_logging(mpv_instance):
resp = requests.get(get_uri("api/status"), headers={"Referer": "https://referer"})
assert resp.status_code == 200
def test_logging(htpasswd, mpv_instance, use_auth, username, password, status_code):
auth = None
if use_auth and username:
auth = HTTPBasicAuth(username, password)
resp = requests.get(
get_uri("api/status"), auth=auth, headers={"Referer": "https://referer"}
)
assert resp.status_code == status_code

# example log line
# ::1 - - [17/Apr/2020:13:18:22 +0000] "GET /api/status HTTP/1.1" 200 1253 "https://referer" "python-requests/2.23.0"
# ::1 - user [17/Apr/2020:13:18:22 +0000] "GET /api/status HTTP/1.1" 200 1253 "https://referer" "python-requests/2.23.0"
user = "-"
if username and auth:
user = "user"

assert (
mpv_instance.expect(
r"\[webui\] ::1 - - \[\d\d?/[A-Z][a-z][a-z][a-z]?/?/\d{4}:\d{2}:\d{2}:\d{2} \+0{4}\] "
r'"GET /api/status HTTP/1.1" 200 \d* "https://referer" "python-requests/',
fr"\[webui\] ::1 - {user} \[\d\d?/[A-Z][a-z][a-z][a-z]?/?/\d{{4}}:\d{{2}}:\d{{2}}:\d{{2}} \+0{{4}}\] "
fr'"GET /api/status HTTP/1.1" {status_code} \d* "https://referer" "python-requests/',
timeout=1,
)
== 0
Expand Down
6 changes: 5 additions & 1 deletion webui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,13 @@ local function log_line(request, code, length)
end

local clientip = request.clientip or '-'
local user = request.user or '-'
local path = request.request or '-'
local referer = request.referer or '-'
local agent = request.agent or '-'
local time = os.date('%d/%b/%Y:%H:%M:%S %z', os.time())
mp.msg.info(
clientip..' - - ['..time..'] "'..path..'" '..code..' '..length..' "'..referer..'" "'..agent..'"')
clientip..' - '..user..' ['..time..'] "'..path..'" '..code..' '..length..' "'..referer..'" "'..agent..'"')
end

local function build_status_response()
Expand Down Expand Up @@ -476,6 +477,9 @@ local function handle_request(request, passwd)
if not is_authenticated(request, passwd) then
return 401, get_content_type('plain'), "Authentication required."
end
else
request.user = nil
request.password = nil
end
if request.method == "POST" then
return handle_post(request['path'])
Expand Down

0 comments on commit 0685709

Please sign in to comment.