mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-22 07:51:00 +00:00
Add --api-keys-remote
This commit is contained in:
parent
647379aea5
commit
8f8087f8ae
6 changed files with 47 additions and 3 deletions
|
@ -190,6 +190,7 @@ docker-compose -f docker-compose.cuda.yml up -d --build
|
||||||
| --frontend-language-target | Set frontend default language - target | `es` | LT_FRONTEND_LANGUAGE_TARGET |
|
| --frontend-language-target | Set frontend default language - target | `es` | LT_FRONTEND_LANGUAGE_TARGET |
|
||||||
| --frontend-timeout | Set frontend translation timeout | `500` | LT_FRONTEND_TIMEOUT |
|
| --frontend-timeout | Set frontend translation timeout | `500` | LT_FRONTEND_TIMEOUT |
|
||||||
| --api-keys | Enable API keys database for per-user rate limits lookup | `Don't use API keys` | LT_API_KEYS |
|
| --api-keys | Enable API keys database for per-user rate limits lookup | `Don't use API keys` | LT_API_KEYS |
|
||||||
|
| --api-keys-remote | Use this remote endpoint to query for valid API keys instead of using the local database | `Use local API key database` | LT_API_KEYS_REMOTE |
|
||||||
| --require-api-key-origin | Require use of an API key for programmatic access to the API, unless the request origin matches this domain | `No restrictions on domain origin` | LT_REQUIRE_API_KEY_ORIGIN |
|
| --require-api-key-origin | Require use of an API key for programmatic access to the API, unless the request origin matches this domain | `No restrictions on domain origin` | LT_REQUIRE_API_KEY_ORIGIN |
|
||||||
| --load-only | Set available languages | `all from argostranslate` | LT_LOAD_ONLY |
|
| --load-only | Set available languages | `all from argostranslate` | LT_LOAD_ONLY |
|
||||||
| --suggestions | Allow user suggestions | `false` | LT_SUGGESTIONS |
|
| --suggestions | Allow user suggestions | `false` | LT_SUGGESTIONS |
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import uuid
|
import uuid
|
||||||
|
import requests
|
||||||
from expiringdict import ExpiringDict
|
from expiringdict import ExpiringDict
|
||||||
|
|
||||||
DEFAULT_DB_PATH = "api_keys.db"
|
DEFAULT_DB_PATH = "api_keys.db"
|
||||||
|
@ -61,3 +61,28 @@ class Database:
|
||||||
def all(self):
|
def all(self):
|
||||||
row = self.c.execute("SELECT api_key, req_limit FROM api_keys")
|
row = self.c.execute("SELECT api_key, req_limit FROM api_keys")
|
||||||
return row.fetchall()
|
return row.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteDatabase:
|
||||||
|
def __init__(self, url, max_cache_len=1000, max_cache_age=600):
|
||||||
|
self.url = url
|
||||||
|
self.cache = ExpiringDict(max_len=max_cache_len, max_age_seconds=max_cache_age)
|
||||||
|
|
||||||
|
def lookup(self, api_key):
|
||||||
|
req_limit = self.cache.get(api_key)
|
||||||
|
if req_limit is None:
|
||||||
|
try:
|
||||||
|
r = requests.post(self.url, data={'api_key': api_key})
|
||||||
|
res = r.json()
|
||||||
|
except Exception as e:
|
||||||
|
print("Cannot authenticate API key: " + str(e))
|
||||||
|
return False
|
||||||
|
|
||||||
|
if res.get('error', None) is None:
|
||||||
|
req_limit = res.get('req_limit', None)
|
||||||
|
self.cache[api_key] = req_limit
|
||||||
|
else:
|
||||||
|
req_limit = False
|
||||||
|
self.cache[api_key] = False
|
||||||
|
|
||||||
|
return req_limit
|
||||||
|
|
|
@ -17,7 +17,7 @@ from werkzeug.utils import secure_filename
|
||||||
from app import flood, remove_translated_files, security
|
from app import flood, remove_translated_files, security
|
||||||
from app.language import detect_languages, transliterate
|
from app.language import detect_languages, transliterate
|
||||||
|
|
||||||
from .api_keys import Database
|
from .api_keys import Database, RemoteDatabase
|
||||||
from .suggestions import Database as SuggestionsDatabase
|
from .suggestions import Database as SuggestionsDatabase
|
||||||
|
|
||||||
|
|
||||||
|
@ -146,7 +146,12 @@ def create_app(args):
|
||||||
api_keys_db = None
|
api_keys_db = None
|
||||||
|
|
||||||
if args.req_limit > 0 or args.api_keys or args.daily_req_limit > 0:
|
if args.req_limit > 0 or args.api_keys or args.daily_req_limit > 0:
|
||||||
api_keys_db = Database() if args.api_keys else None
|
api_keys_db = None
|
||||||
|
if args.api_keys:
|
||||||
|
if args.api_keys_remote:
|
||||||
|
api_keys_db = RemoteDatabase(args.api_keys_remote)
|
||||||
|
else:
|
||||||
|
api_keys_db = Database()
|
||||||
|
|
||||||
from flask_limiter import Limiter
|
from flask_limiter import Limiter
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,11 @@ _default_options_objects = [
|
||||||
'name': 'API_KEYS',
|
'name': 'API_KEYS',
|
||||||
'default_value': False,
|
'default_value': False,
|
||||||
'value_type': 'bool'
|
'value_type': 'bool'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'name': 'API_KEYS_REMOTE',
|
||||||
|
'default_value': '',
|
||||||
|
'value_type': 'str'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'name': 'REQUIRE_API_KEY_ORIGIN',
|
'name': 'REQUIRE_API_KEY_ORIGIN',
|
||||||
|
|
|
@ -89,6 +89,12 @@ def get_args():
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Enable API keys database for per-user rate limits lookup",
|
help="Enable API keys database for per-user rate limits lookup",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--api-keys-remote",
|
||||||
|
default=DEFARGS['API_KEYS_REMOTE'],
|
||||||
|
type=str,
|
||||||
|
help="Use this remote endpoint to query for valid API keys instead of using the local database",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--require-api-key-origin",
|
"--require-api-key-origin",
|
||||||
type=str,
|
type=str,
|
||||||
|
|
|
@ -15,3 +15,5 @@ translatehtml==1.5.2
|
||||||
argos-translate-files==1.1.0
|
argos-translate-files==1.1.0
|
||||||
itsdangerous==2.1.2
|
itsdangerous==2.1.2
|
||||||
Werkzeug==2.1.2
|
Werkzeug==2.1.2
|
||||||
|
requests==2.28.0
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue