9
9
from http .server import BaseHTTPRequestHandler , ThreadingHTTPServer
10
10
from typing import Any , Optional , TypeVar , overload
11
11
from urllib .parse import quote as _url_quote , urlparse as _urlparse
12
- import functools , hmac , html .parser , itertools , json , math , re , time , uuid
12
+ import functools , hmac , html .parser , itertools , json , math , os , re , time , uuid
13
13
import miniirc , requests , threading , traceback # type: ignore
14
14
15
15
16
- ver = (0 , 0 , 12 )
16
+ ver = (0 , 0 , 13 )
17
17
__version__ = '.' .join (map (str , ver ))
18
18
19
19
@@ -440,6 +440,7 @@ def __init__(
440
440
token : Optional [str ] = None ,
441
441
media_proxy_port : Optional [int ] = None ,
442
442
media_proxy_url : Optional [str ] = None ,
443
+ media_proxy_key : Optional [bytes ] = None ,
443
444
** kwargs
444
445
) -> None :
445
446
# Cache _get_room_url
@@ -463,9 +464,11 @@ def __init__(
463
464
464
465
self ._media_proxy : Optional [ThreadingHTTPServer ] = None
465
466
self ._media_proxy_port = media_proxy_port
466
- if media_proxy_port and not media_proxy_port :
467
+ if media_proxy_port and not media_proxy_url :
467
468
media_proxy_url = f'http://127.0.0.1:{ media_proxy_port } '
468
469
self ._media_proxy_url = media_proxy_url and media_proxy_url .rstrip ('/' )
470
+ if media_proxy_port is not None :
471
+ self ._media_proxy_key = media_proxy_key or os .urandom (32 )
469
472
470
473
# Stop miniirc from trying to access the (non-existent) socket
471
474
kwargs ['ping_interval' ] = kwargs ['ping_timeout' ] = None
@@ -541,11 +544,8 @@ def _get_room_url_no_cache(self, room_id: str) -> str:
541
544
return f'rooms/{ _url_quote (room_id )} '
542
545
543
546
def __make_url_digest (self , path : str ) -> str :
544
- return hmac .digest (
545
- b'miniirc_matrix hmac v1 ' + self .token .encode ('ascii' ),
546
- path .encode ('ascii' ),
547
- 'sha256'
548
- ).hex ()
547
+ return hmac .digest (self ._media_proxy_key , path .encode ('ascii' ),
548
+ 'sha256' ).hex ()
549
549
550
550
def _download_media (self , url : str ) -> requests .Response :
551
551
url_base , _ , key = url .partition ('?key=' )
0 commit comments