88import os
99
1010
11+ from cryptography .hazmat .primitives .ciphers .aead import AESGCM
12+ from cryptography .hazmat .primitives .asymmetric import ed25519
13+ from cryptography .hazmat .primitives import serialization
14+ import json
15+
16+
1117from datetime import datetime , timedelta
1218import pytz
1319from pytz import UTC
1824from open_webui .models .users import Users
1925
2026from open_webui .constants import ERROR_MESSAGES
27+
2128from open_webui .env import (
29+ OFFLINE_MODE ,
30+ LICENSE_BLOB ,
31+ pk ,
2232 WEBUI_SECRET_KEY ,
2333 TRUSTED_SIGNATURE_KEY ,
2434 STATIC_DIR ,
@@ -74,6 +84,18 @@ def override_static(path: str, content: str):
7484
7585
7686def get_license_data (app , key ):
87+ def data_handler (data ):
88+ for k , v in data .items ():
89+ if k == "resources" :
90+ for p , c in v .items ():
91+ globals ().get ("override_static" , lambda a , b : None )(p , c )
92+ elif k == "count" :
93+ setattr (app .state , "USER_COUNT" , v )
94+ elif k == "name" :
95+ setattr (app .state , "WEBUI_NAME" , v )
96+ elif k == "metadata" :
97+ setattr (app .state , "LICENSE_METADATA" , v )
98+
7799 def handler (u ):
78100 res = requests .post (
79101 f"{ u } /api/v1/license/" ,
@@ -83,30 +105,52 @@ def handler(u):
83105
84106 if getattr (res , "ok" , False ):
85107 payload = getattr (res , "json" , lambda : {})()
86- for k , v in payload .items ():
87- if k == "resources" :
88- for p , c in v .items ():
89- globals ().get ("override_static" , lambda a , b : None )(p , c )
90- elif k == "count" :
91- setattr (app .state , "USER_COUNT" , v )
92- elif k == "name" :
93- setattr (app .state , "WEBUI_NAME" , v )
94- elif k == "metadata" :
95- setattr (app .state , "LICENSE_METADATA" , v )
108+ data_handler (payload )
96109 return True
97110 else :
98111 log .error (
99112 f"License: retrieval issue: { getattr (res , 'text' , 'unknown error' )} "
100113 )
101114
102115 if key :
103- us = ["https://api.openwebui.com" , "https://licenses.api.openwebui.com" ]
116+ us = [
117+ "https://api.openwebui.com" ,
118+ "https://licenses.api.openwebui.com" ,
119+ ]
104120 try :
105121 for u in us :
106122 if handler (u ):
107123 return True
108124 except Exception as ex :
109125 log .exception (f"License: Uncaught Exception: { ex } " )
126+
127+ try :
128+ if LICENSE_BLOB :
129+ nl = 12
130+ kb = hashlib .sha256 ((key .replace ("-" , "" ).upper ()).encode ()).digest ()
131+
132+ def nt (b ):
133+ return b [:nl ], b [nl :]
134+
135+ lb = base64 .b64decode (LICENSE_BLOB )
136+ ln , lt = nt (lb )
137+
138+ aesgcm = AESGCM (kb )
139+ p = json .loads (aesgcm .decrypt (ln , lt , None ))
140+ pk .verify (base64 .b64decode (p ["s" ]), p ["p" ].encode ())
141+
142+ pb = base64 .b64decode (p ["p" ])
143+ pn , pt = nt (pb )
144+
145+ data = json .loads (aesgcm .decrypt (pn , pt , None ).decode ())
146+ if not data .get ("exp" ) and data .get ("exp" ) < datetime .now ().date ():
147+ return False
148+
149+ data_handler (data )
150+ return True
151+ except Exception as e :
152+ log .error (f"License: { e } " )
153+
110154 return False
111155
112156
0 commit comments