mirror of
https://github.com/Eugeny/tabby-web.git
synced 2026-08-16 22:36:02 +01:00
init
This commit is contained in:
1
backend/.dockerignore
Normal file
1
backend/.dockerignore
Normal file
@@ -0,0 +1 @@
|
||||
__pycache__
|
||||
7
backend/.flake8
Normal file
7
backend/.flake8
Normal file
@@ -0,0 +1,7 @@
|
||||
[flake8]
|
||||
ignore=E501,D103,C901,D203,W504,S607,S603,S404,S606,S322,S410,S320,B010
|
||||
exclude = .git,__pycache__,help,static,misc,locale,templates,tests,deployment,migrations,elements/ai/scripts
|
||||
max-complexity = 40
|
||||
builtins = _
|
||||
per-file-ignores = scripts/*:T001,E402
|
||||
select = C,E,F,W,B,B902
|
||||
3
backend/.gitignore
vendored
Normal file
3
backend/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
__pycache__
|
||||
db.sqlite3
|
||||
public
|
||||
22
backend/Dockerfile
Normal file
22
backend/Dockerfile
Normal file
@@ -0,0 +1,22 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
FROM python:3.7-alpine AS build
|
||||
ARG EXTRA_DEPS
|
||||
RUN apk add build-base musl-dev libffi-dev openssl-dev mariadb-dev
|
||||
WORKDIR /app
|
||||
RUN pip install -U setuptools 'cryptography>=3.0,<3.1' poetry==1.1.7 $EXTRA_DEPS
|
||||
COPY pyproject.toml poetry.lock ./
|
||||
RUN poetry config virtualenvs.create false
|
||||
RUN poetry install --no-dev --no-ansi --no-interaction
|
||||
|
||||
FROM python:3.7-alpine AS package
|
||||
WORKDIR /app
|
||||
COPY --from=0 /usr /usr
|
||||
COPY manage.py gunicorn.conf.py ./
|
||||
COPY tabby tabby
|
||||
|
||||
COPY start.sh /start.sh
|
||||
RUN ["chmod", "+x", "/start.sh"]
|
||||
|
||||
RUN ./manage.py collectstatic --noinput
|
||||
|
||||
CMD ["/start.sh"]
|
||||
14
backend/cloudbuild.yaml
Normal file
14
backend/cloudbuild.yaml
Normal file
@@ -0,0 +1,14 @@
|
||||
steps:
|
||||
- name: 'gcr.io/cloud-builders/docker'
|
||||
dir: 'backend'
|
||||
args:
|
||||
- build
|
||||
- '-t'
|
||||
- '${_DOCKER_TAG}'
|
||||
- '--cache-from'
|
||||
- '${_DOCKER_TAG}'
|
||||
- '--build-arg'
|
||||
- 'EXTRA_DEPS=${_EXTRA_DEPS}'
|
||||
- '.'
|
||||
|
||||
images: ['${_DOCKER_TAG}']
|
||||
7
backend/gunicorn.conf.py
Normal file
7
backend/gunicorn.conf.py
Normal file
@@ -0,0 +1,7 @@
|
||||
wsgi_app = "tabby.wsgi:application"
|
||||
workers = 4
|
||||
preload_app = True
|
||||
sendfile = True
|
||||
|
||||
max_requests = 1000
|
||||
max_requests_jitter = 100
|
||||
22
backend/manage.py
Executable file
22
backend/manage.py
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tabby.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
1023
backend/poetry.lock
generated
Normal file
1023
backend/poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
33
backend/pyproject.toml
Normal file
33
backend/pyproject.toml
Normal file
@@ -0,0 +1,33 @@
|
||||
[tool.poetry]
|
||||
name = "tabby-web"
|
||||
version = "1.0.0"
|
||||
description = ""
|
||||
authors = ["Eugeny <e@ajenti.org>"]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.7"
|
||||
Django = "^3.2.3"
|
||||
django-rest-framework = "^0.1.0"
|
||||
djangorestframework-dataclasses = "^0.9"
|
||||
social-auth-app-django = "^4.0.0"
|
||||
python-dotenv = "^0.17.1"
|
||||
websockets = "^9.1"
|
||||
gql = "^2.0.0"
|
||||
dj-database-url = "^0.5.0"
|
||||
mysqlclient = "^2.0.3"
|
||||
gunicorn = "^20.1.0"
|
||||
Twisted = "20.3.0"
|
||||
semver = "^2.13.0"
|
||||
requests = "^2.25.1"
|
||||
pyga = "^2.6.2"
|
||||
django-cors-headers = "^3.7.0"
|
||||
cryptography = "3.0"
|
||||
fsspec = "^2021.7.0"
|
||||
whitenoise = "^5.3.0"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
flake8 = "^3.9.2"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core>=1.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
4
backend/start.sh
Executable file
4
backend/start.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
cd /app
|
||||
./manage.py migrate
|
||||
gunicorn
|
||||
0
backend/tabby/__init__.py
Normal file
0
backend/tabby/__init__.py
Normal file
0
backend/tabby/app/__init__.py
Normal file
0
backend/tabby/app/__init__.py
Normal file
14
backend/tabby/app/admin.py
Normal file
14
backend/tabby/app/admin.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from .models import Gateway, User, Config
|
||||
|
||||
|
||||
class CustomUserAdmin(UserAdmin):
|
||||
fieldsets = UserAdmin.fieldsets + (
|
||||
(None, {'fields': ('custom_connection_gateway', 'custom_connection_gateway_token')}),
|
||||
)
|
||||
|
||||
|
||||
admin.site.register(User, CustomUserAdmin)
|
||||
admin.site.register(Config)
|
||||
admin.site.register(Gateway)
|
||||
18
backend/tabby/app/api/__init__.py
Normal file
18
backend/tabby/app/api/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework import routers
|
||||
from . import app_version, auth, config, gateway, info, user
|
||||
|
||||
|
||||
router = routers.DefaultRouter(trailing_slash=False)
|
||||
router.register('api/1/configs', config.ConfigViewSet)
|
||||
router.register('api/1/versions', app_version.AppVersionViewSet, basename='app-versions')
|
||||
|
||||
urlpatterns = [
|
||||
path('api/1/auth/logout', auth.LogoutView.as_view()),
|
||||
path('api/1/user', user.UserViewSet.as_view({'get': 'retrieve', 'put': 'update'})),
|
||||
path('api/1/instance-info', info.InstanceInfoViewSet.as_view({'get': 'retrieve'})),
|
||||
path('api/1/gateways/choose', gateway.ChooseGatewayViewSet.as_view({'post': 'retrieve'})),
|
||||
|
||||
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
64
backend/tabby/app/api/app_version.py
Normal file
64
backend/tabby/app/api/app_version.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import fsspec
|
||||
import os
|
||||
from django.conf import settings
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.cache import cache_page
|
||||
from dataclasses import dataclass
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.mixins import ListModelMixin
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework_dataclasses.serializers import DataclassSerializer
|
||||
from typing import List
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppVersion:
|
||||
version: str
|
||||
plugins: List[str]
|
||||
|
||||
|
||||
class AppVersionSerializer(DataclassSerializer):
|
||||
class Meta:
|
||||
dataclass = AppVersion
|
||||
|
||||
|
||||
class AppVersionViewSet(ListModelMixin, GenericViewSet):
|
||||
serializer_class = AppVersionSerializer
|
||||
lookup_field = 'id'
|
||||
lookup_value_regex = r'[\w\d.-]+'
|
||||
queryset = ''
|
||||
|
||||
def _get_versions(self):
|
||||
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
|
||||
return [
|
||||
self._get_version(x['name'])
|
||||
for x in fs.listdir(settings.APP_DIST_STORAGE)
|
||||
if x['type'] == 'directory'
|
||||
]
|
||||
|
||||
def _get_version(self, dir):
|
||||
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
|
||||
plugins = [
|
||||
os.path.basename(x['name'])
|
||||
for x in fs.listdir(dir)
|
||||
if x['type'] == 'directory' and os.path.basename(x['name'])
|
||||
not in [
|
||||
'tabby-web-container',
|
||||
'tabby-web-demo',
|
||||
]
|
||||
]
|
||||
|
||||
return AppVersion(
|
||||
version=os.path.basename(dir),
|
||||
plugins=plugins,
|
||||
)
|
||||
|
||||
@method_decorator(cache_page(60))
|
||||
def list(self, request, *args, **kwargs):
|
||||
return Response(
|
||||
self.serializer_class(
|
||||
self._get_versions(),
|
||||
many=True,
|
||||
).data
|
||||
)
|
||||
9
backend/tabby/app/api/auth.py
Normal file
9
backend/tabby/app/api/auth.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.contrib.auth import logout
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
|
||||
class LogoutView(APIView):
|
||||
def post(self, request, format=None):
|
||||
logout(request)
|
||||
return Response(None)
|
||||
28
backend/tabby/app/api/config.py
Normal file
28
backend/tabby/app/api/config.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from rest_framework import fields
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from ..models import Config
|
||||
|
||||
|
||||
class ConfigSerializer(ModelSerializer):
|
||||
name = fields.CharField(required=False)
|
||||
|
||||
class Meta:
|
||||
model = Config
|
||||
read_only_fields = ('user', 'created_at', 'modified_at')
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class ConfigViewSet(ModelViewSet):
|
||||
queryset = Config.objects.all()
|
||||
serializer_class = ConfigSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
if self.request.user.is_authenticated:
|
||||
return Config.objects.filter(user=self.request.user)
|
||||
return Config.objects.none()
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
59
backend/tabby/app/api/gateway.py
Normal file
59
backend/tabby/app/api/gateway.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import asyncio
|
||||
import random
|
||||
from rest_framework import fields, status
|
||||
from rest_framework.exceptions import APIException, NotFound
|
||||
from rest_framework.mixins import RetrieveModelMixin
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from ..gateway import GatewayAdminConnection
|
||||
from ..models import Gateway
|
||||
|
||||
|
||||
class GatewaySerializer(ModelSerializer):
|
||||
url = fields.SerializerMethodField()
|
||||
auth_token = fields.CharField()
|
||||
|
||||
class Meta:
|
||||
fields = '__all__'
|
||||
model = Gateway
|
||||
|
||||
def get_url(self, gw):
|
||||
return f'{"wss" if gw.secure else "ws"}://{gw.host}:{gw.port}/'
|
||||
|
||||
|
||||
class NoGatewaysError(APIException):
|
||||
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||
default_detail = 'No connection gateways available.'
|
||||
default_code = 'no_gateways'
|
||||
|
||||
|
||||
class ChooseGatewayViewSet(RetrieveModelMixin, GenericViewSet):
|
||||
queryset = Gateway.objects.filter(enabled=True)
|
||||
serializer_class = GatewaySerializer
|
||||
|
||||
async def _authorize_client(self, gw):
|
||||
c = GatewayAdminConnection(gw)
|
||||
await c.connect()
|
||||
token = await c.authorize_client()
|
||||
await c.close()
|
||||
return token
|
||||
|
||||
def get_object(self):
|
||||
gateways = list(self.queryset)
|
||||
random.shuffle(gateways)
|
||||
if not len(gateways):
|
||||
raise NotFound()
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
try:
|
||||
for gw in gateways:
|
||||
try:
|
||||
gw.auth_token = loop.run_until_complete(self._authorize_client(gw))
|
||||
except ConnectionError as e:
|
||||
print(e)
|
||||
continue
|
||||
return gw
|
||||
|
||||
raise NoGatewaysError()
|
||||
finally:
|
||||
loop.close()
|
||||
21
backend/tabby/app/api/info.py
Normal file
21
backend/tabby/app/api/info.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from django.conf import settings
|
||||
from rest_framework import fields
|
||||
from rest_framework.mixins import RetrieveModelMixin
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.serializers import Serializer
|
||||
|
||||
|
||||
class InstanceInfoSerializer(Serializer):
|
||||
login_enabled = fields.BooleanField()
|
||||
homepage_enabled = fields.BooleanField()
|
||||
|
||||
|
||||
class InstanceInfoViewSet(RetrieveModelMixin, GenericViewSet):
|
||||
queryset = '' # type: ignore
|
||||
serializer_class = InstanceInfoSerializer
|
||||
|
||||
def get_object(self):
|
||||
return {
|
||||
'login_enabled': settings.ENABLE_LOGIN,
|
||||
'homepage_enabled': settings.ENABLE_HOMEPAGE,
|
||||
}
|
||||
55
backend/tabby/app/api/user.py
Normal file
55
backend/tabby/app/api/user.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from django.conf import settings
|
||||
from rest_framework import fields
|
||||
from rest_framework.exceptions import PermissionDenied
|
||||
from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from social_django.models import UserSocialAuth
|
||||
|
||||
from ..sponsors import check_is_sponsor_cached
|
||||
from ..models import User
|
||||
|
||||
|
||||
class UserSerializer(ModelSerializer):
|
||||
id = fields.IntegerField()
|
||||
is_pro = fields.SerializerMethodField()
|
||||
is_sponsor = fields.SerializerMethodField()
|
||||
github_username = fields.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = (
|
||||
'id',
|
||||
'username',
|
||||
'active_config',
|
||||
'custom_connection_gateway',
|
||||
'custom_connection_gateway_token',
|
||||
'config_sync_token',
|
||||
'is_pro',
|
||||
'is_sponsor',
|
||||
'github_username',
|
||||
)
|
||||
read_only_fields = ('id', 'username')
|
||||
|
||||
def get_is_pro(self, obj):
|
||||
return obj.force_pro or not settings.GITHUB_ELIGIBLE_SPONSORSHIPS or check_is_sponsor_cached(obj)
|
||||
|
||||
def get_is_sponsor(self, obj):
|
||||
return check_is_sponsor_cached(obj)
|
||||
|
||||
def get_github_username(self, obj):
|
||||
social_auth = UserSocialAuth.objects.filter(user=obj, provider='github').first()
|
||||
if not social_auth:
|
||||
return None
|
||||
|
||||
return social_auth.extra_data.get('login')
|
||||
|
||||
|
||||
class UserViewSet(RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
|
||||
def get_object(self):
|
||||
if self.request.user.is_authenticated:
|
||||
return self.request.user
|
||||
raise PermissionDenied()
|
||||
6
backend/tabby/app/apps.py
Normal file
6
backend/tabby/app/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AppConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'tabby.app'
|
||||
92
backend/tabby/app/gateway.py
Normal file
92
backend/tabby/app/gateway.py
Normal file
@@ -0,0 +1,92 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import ssl
|
||||
import websockets
|
||||
from django.conf import settings
|
||||
from urllib.parse import quote
|
||||
|
||||
from .models import Gateway
|
||||
|
||||
|
||||
class GatewayConnection:
|
||||
_ssl_context: ssl.SSLContext = None
|
||||
|
||||
def __init__(self, host: str, port: int):
|
||||
if settings.CONNECTION_GATEWAY_AUTH_KEY and not GatewayConnection._ssl_context:
|
||||
ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
|
||||
ctx.load_cert_chain(
|
||||
os.path.realpath(settings.CONNECTION_GATEWAY_AUTH_CERTIFICATE),
|
||||
os.path.realpath(settings.CONNECTION_GATEWAY_AUTH_KEY),
|
||||
)
|
||||
if settings.CONNECTION_GATEWAY_AUTH_CA:
|
||||
ctx.load_verify_locations(
|
||||
cafile=os.path.realpath(settings.CONNECTION_GATEWAY_AUTH_CA),
|
||||
)
|
||||
ctx.verify_mode = ssl.CERT_REQUIRED
|
||||
GatewayConnection._ssl_context = ctx
|
||||
|
||||
proto = 'wss' if GatewayConnection._ssl_context else 'ws'
|
||||
self.url = f'{proto}://localhost:9000/connect/{quote(host)}:{quote(str(port))}'
|
||||
|
||||
async def connect(self):
|
||||
self.context = websockets.connect(self.url, ssl=GatewayConnection._ssl_context)
|
||||
try:
|
||||
self.socket = await self.context.__aenter__()
|
||||
except OSError:
|
||||
raise ConnectionError()
|
||||
|
||||
async def send(self, data):
|
||||
await self.socket.send(data)
|
||||
|
||||
def recv(self, timeout=None):
|
||||
return asyncio.wait_for(self.socket.recv(), timeout)
|
||||
|
||||
async def close(self):
|
||||
await self.socket.close()
|
||||
await self.context.__aexit__(None, None, None)
|
||||
|
||||
|
||||
class GatewayAdminConnection:
|
||||
_ssl_context: ssl.SSLContext = None
|
||||
|
||||
def __init__(self, gateway: Gateway):
|
||||
if not settings.CONNECTION_GATEWAY_AUTH_KEY:
|
||||
raise RuntimeError('CONNECTION_GATEWAY_AUTH_KEY is required to manage connection gateways')
|
||||
if not GatewayAdminConnection._ssl_context:
|
||||
ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
|
||||
ctx.load_cert_chain(
|
||||
os.path.realpath(settings.CONNECTION_GATEWAY_AUTH_CERTIFICATE),
|
||||
os.path.realpath(settings.CONNECTION_GATEWAY_AUTH_KEY),
|
||||
)
|
||||
if settings.CONNECTION_GATEWAY_AUTH_CA:
|
||||
ctx.load_verify_locations(
|
||||
cafile=os.path.realpath(settings.CONNECTION_GATEWAY_AUTH_CA),
|
||||
)
|
||||
ctx.verify_mode = ssl.CERT_REQUIRED
|
||||
GatewayAdminConnection._ssl_context = ctx
|
||||
|
||||
self.url = f'wss://{gateway.host}:{gateway.admin_port}'
|
||||
|
||||
async def connect(self):
|
||||
self.context = websockets.connect(self.url, ssl=GatewayAdminConnection._ssl_context)
|
||||
try:
|
||||
self.socket = await self.context.__aenter__()
|
||||
except OSError:
|
||||
raise ConnectionError()
|
||||
|
||||
async def authorize_client(self) -> str:
|
||||
token = secrets.token_hex(32)
|
||||
await self.send(json.dumps({
|
||||
'_': 'authorize-client',
|
||||
'token': token,
|
||||
}))
|
||||
return token
|
||||
|
||||
async def send(self, data):
|
||||
await self.socket.send(data)
|
||||
|
||||
async def close(self):
|
||||
await self.socket.close()
|
||||
await self.context.__aexit__(None, None, None)
|
||||
0
backend/tabby/app/management/__init__.py
Normal file
0
backend/tabby/app/management/__init__.py
Normal file
0
backend/tabby/app/management/commands/__init__.py
Normal file
0
backend/tabby/app/management/commands/__init__.py
Normal file
65
backend/tabby/app/management/commands/add_version.py
Normal file
65
backend/tabby/app/management/commands/add_version.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import fsspec
|
||||
import logging
|
||||
import requests
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Downloads a new app version'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('version', type=str)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
version = options['version']
|
||||
target = f'{settings.APP_DIST_STORAGE}/{version}'
|
||||
|
||||
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
|
||||
|
||||
plugin_list = [
|
||||
'tabby-web-container',
|
||||
'tabby-core',
|
||||
'tabby-settings',
|
||||
'tabby-terminal',
|
||||
'tabby-ssh',
|
||||
'tabby-community-color-schemes',
|
||||
'tabby-serial',
|
||||
'tabby-telnet',
|
||||
'tabby-web',
|
||||
'tabby-web-demo',
|
||||
]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
tempdir = Path(tempdir)
|
||||
for plugin in plugin_list:
|
||||
logging.info(f'Resolving {plugin}@{version}')
|
||||
response = requests.get(f'{settings.NPM_REGISTRY}/{plugin}/{version}')
|
||||
response.raise_for_status()
|
||||
info = response.json()
|
||||
url = info['dist']['tarball']
|
||||
|
||||
logging.info(f'Downloading {plugin}@{version} from {url}')
|
||||
response = requests.get(url)
|
||||
|
||||
with tempfile.NamedTemporaryFile('wb') as f:
|
||||
f.write(response.content)
|
||||
plugin_final_target = Path(tempdir) / plugin
|
||||
|
||||
with tempfile.TemporaryDirectory() as extraction_tmp:
|
||||
subprocess.check_call(
|
||||
['tar', '-xzf', f.name, '-C', str(extraction_tmp)]
|
||||
)
|
||||
shutil.move(
|
||||
Path(extraction_tmp) / 'package', plugin_final_target
|
||||
)
|
||||
|
||||
if fs.exists(target):
|
||||
fs.rm(target, recursive=True)
|
||||
fs.mkdir(target)
|
||||
fs.put(str(tempdir), target, recursive=True)
|
||||
75
backend/tabby/app/migrations/0001_initial.py
Normal file
75
backend/tabby/app/migrations/0001_initial.py
Normal file
@@ -0,0 +1,75 @@
|
||||
# Generated by Django 3.2.3 on 2021-07-08 17:43
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
||||
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
|
||||
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
|
||||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
|
||||
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
|
||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
|
||||
('active_version', models.CharField(max_length=32, null=True)),
|
||||
('custom_connection_gateway', models.CharField(max_length=255, null=True, blank=True)),
|
||||
('custom_connection_gateway_token', models.CharField(max_length=255, null=True, blank=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('modified_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'user',
|
||||
'verbose_name_plural': 'users',
|
||||
'abstract': False,
|
||||
},
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Config',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('content', models.TextField(default='{}')),
|
||||
('last_used_with_version', models.CharField(max_length=32, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('modified_at', models.DateTimeField(auto_now=True)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='configs', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='active_config',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='app.config'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='groups',
|
||||
field=models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='user_permissions',
|
||||
field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions'),
|
||||
),
|
||||
]
|
||||
23
backend/tabby/app/migrations/0002_gateway.py
Normal file
23
backend/tabby/app/migrations/0002_gateway.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 3.2.3 on 2021-07-08 20:25
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('app', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Gateway',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('host', models.CharField(max_length=255)),
|
||||
('port', models.IntegerField(default=1234)),
|
||||
('enabled', models.BooleanField(default=True)),
|
||||
('secure', models.BooleanField(default=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
24
backend/tabby/app/migrations/0003_auto_20210711_1855.py
Normal file
24
backend/tabby/app/migrations/0003_auto_20210711_1855.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 3.2.3 on 2021-07-11 18:55
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('app', '0002_gateway'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='gateway',
|
||||
name='admin_port',
|
||||
field=models.IntegerField(default=1235),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='user',
|
||||
name='active_config',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='app.config'),
|
||||
),
|
||||
]
|
||||
29
backend/tabby/app/migrations/0004_sync_token.py
Normal file
29
backend/tabby/app/migrations/0004_sync_token.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import secrets
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def run_forward(apps, schema_editor):
|
||||
for user in apps.get_model('app', 'User').objects.all():
|
||||
user.config_sync_token = secrets.token_hex(64)
|
||||
user.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('app', '0003_auto_20210711_1855'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='config_sync_token',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
migrations.RunPython(run_forward, lambda _, __: None),
|
||||
migrations.AlterField(
|
||||
model_name='user',
|
||||
name='config_sync_token',
|
||||
field=models.CharField(max_length=255),
|
||||
),
|
||||
]
|
||||
18
backend/tabby/app/migrations/0005_user_force_pro.py
Normal file
18
backend/tabby/app/migrations/0005_user_force_pro.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2.3 on 2021-07-24 10:13
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('app', '0004_sync_token'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='force_pro',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
28
backend/tabby/app/migrations/0006_config_name.py
Normal file
28
backend/tabby/app/migrations/0006_config_name.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def run_forward(apps, schema_editor):
|
||||
for config in apps.get_model('app', 'Config').objects.all():
|
||||
config.name = f'Unnamed config ({config.created_at.date()})'
|
||||
config.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('app', '0005_user_force_pro'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='config',
|
||||
name='name',
|
||||
field=models.CharField(max_length=255, null=True),
|
||||
),
|
||||
migrations.RunPython(run_forward, lambda _, __: None),
|
||||
migrations.AlterField(
|
||||
model_name='config',
|
||||
name='name',
|
||||
field=models.CharField(max_length=255),
|
||||
),
|
||||
]
|
||||
0
backend/tabby/app/migrations/__init__.py
Normal file
0
backend/tabby/app/migrations/__init__.py
Normal file
45
backend/tabby/app/models.py
Normal file
45
backend/tabby/app/models.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import secrets
|
||||
from datetime import date
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
|
||||
|
||||
class Config(models.Model):
|
||||
user = models.ForeignKey('app.User', related_name='configs', on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=255)
|
||||
content = models.TextField(default='{}')
|
||||
last_used_with_version = models.CharField(max_length=32, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
modified_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.name:
|
||||
self.name = f'Unnamed config ({date.today()})'
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
active_config = models.ForeignKey(Config, null=True, on_delete=models.SET_NULL, related_name='+')
|
||||
active_version = models.CharField(max_length=32, null=True)
|
||||
custom_connection_gateway = models.CharField(max_length=255, null=True, blank=True)
|
||||
custom_connection_gateway_token = models.CharField(max_length=255, null=True, blank=True)
|
||||
config_sync_token = models.CharField(max_length=255)
|
||||
force_pro = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
modified_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.config_sync_token:
|
||||
self.config_sync_token = secrets.token_hex(64)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class Gateway(models.Model):
|
||||
host = models.CharField(max_length=255)
|
||||
port = models.IntegerField(default=1234)
|
||||
admin_port = models.IntegerField(default=1235)
|
||||
enabled = models.BooleanField(default=True)
|
||||
secure = models.BooleanField(default=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.host}:{self.port}'
|
||||
80
backend/tabby/app/sponsors.py
Normal file
80
backend/tabby/app/sponsors.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from gql import Client, gql
|
||||
from gql.transport.requests import RequestsHTTPTransport
|
||||
from social_django.models import UserSocialAuth
|
||||
|
||||
from .models import User
|
||||
|
||||
|
||||
GQL_ENDPOINT = 'https://api.github.com/graphql'
|
||||
CACHE_KEY = 'cached-sponsors:%s'
|
||||
|
||||
|
||||
def check_is_sponsor(user: User) -> bool:
|
||||
try:
|
||||
token = user.social_auth.get(provider='github').extra_data.get('access_token')
|
||||
except UserSocialAuth.DoesNotExist:
|
||||
return False
|
||||
|
||||
if not token:
|
||||
return False
|
||||
|
||||
client = Client(
|
||||
transport=RequestsHTTPTransport(
|
||||
url=GQL_ENDPOINT,
|
||||
use_json=True,
|
||||
headers={
|
||||
'Authorization': f'Bearer {token}',
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
after = None
|
||||
|
||||
while True:
|
||||
params = 'first: 1'
|
||||
if after:
|
||||
params += f', after:"{after}"'
|
||||
|
||||
query = '''
|
||||
query {
|
||||
viewer {
|
||||
sponsorshipsAsSponsor(%s) {
|
||||
pageInfo {
|
||||
startCursor
|
||||
hasNextPage
|
||||
endCursor
|
||||
}
|
||||
totalRecurringMonthlyPriceInDollars
|
||||
nodes {
|
||||
sponsorable {
|
||||
... on Organization { login }
|
||||
... on User { login }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
''' % (params,)
|
||||
|
||||
response = client.execute(gql(query))
|
||||
info = response['viewer']['sponsorshipsAsSponsor']
|
||||
after = info['pageInfo']['endCursor']
|
||||
nodes = info['nodes']
|
||||
if not len(nodes):
|
||||
break
|
||||
for node in nodes:
|
||||
if node['sponsorable']['login'].lower() not in settings.GITHUB_ELIGIBLE_SPONSORSHIPS:
|
||||
continue
|
||||
if info['totalRecurringMonthlyPriceInDollars'] >= settings.GITHUB_SPONSORS_MIN_PAYMENT:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def check_is_sponsor_cached(user: User) -> bool:
|
||||
cache_key = CACHE_KEY % user.id
|
||||
if not cache.get(cache_key):
|
||||
cache.set(cache_key, check_is_sponsor(user), timeout=30)
|
||||
return cache.get(cache_key)
|
||||
17
backend/tabby/app/urls.py
Normal file
17
backend/tabby/app/urls.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from . import api
|
||||
from . import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
*[
|
||||
path(p, views.IndexView.as_view())
|
||||
for p in ['', 'login', 'app', 'about', 'about/features']
|
||||
],
|
||||
|
||||
path('app-dist/<version>/<path:path>', views.AppDistView.as_view()),
|
||||
path('terminal', views.TerminalView.as_view()),
|
||||
|
||||
path('', include(api.urlpatterns)),
|
||||
]
|
||||
34
backend/tabby/app/views.py
Normal file
34
backend/tabby/app/views.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import fsspec
|
||||
import os
|
||||
from fsspec.implementations.local import LocalFileSystem
|
||||
from django.conf import settings
|
||||
from django.http.response import FileResponse, HttpResponseNotFound, HttpResponseRedirect
|
||||
from django.views import static
|
||||
from rest_framework.views import APIView
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
class IndexView(APIView):
|
||||
def get(self, request, format=None):
|
||||
if settings.FRONTEND_URL:
|
||||
return HttpResponseRedirect(settings.FRONTEND_URL)
|
||||
return static.serve(request, 'index.html', document_root=str(settings.FRONTEND_BUILD_DIR))
|
||||
|
||||
|
||||
class TerminalView(APIView):
|
||||
def get(self, request, format=None):
|
||||
response = static.serve(request, 'terminal.html', document_root=str(settings.FRONTEND_BUILD_DIR))
|
||||
response['X-Frame-Options'] = 'SAMEORIGIN'
|
||||
return response
|
||||
|
||||
|
||||
class AppDistView(APIView):
|
||||
def get(self, request, version=None, path=None, format=None):
|
||||
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
|
||||
url = f'{settings.APP_DIST_STORAGE}/{version}/{path}'
|
||||
if isinstance(fs, LocalFileSystem):
|
||||
if not fs.exists(url):
|
||||
return HttpResponseNotFound()
|
||||
return FileResponse(fs.open(url), filename=os.path.basename(url))
|
||||
else:
|
||||
return HttpResponseRedirect(fs.url(url))
|
||||
53
backend/tabby/middleware.py
Normal file
53
backend/tabby/middleware.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import logging
|
||||
from tabby.app.models import User
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import login
|
||||
from pyga.requests import Tracker, Page, Session, Visitor
|
||||
|
||||
|
||||
class BaseMiddleware:
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
|
||||
class TokenMiddleware(BaseMiddleware):
|
||||
def __call__(self, request):
|
||||
token_value = None
|
||||
if 'auth_token' in request.GET:
|
||||
token_value = request.GET['auth_token']
|
||||
if request.META.get('HTTP_AUTHORIZATION'):
|
||||
token_type, *credentials = request.META['HTTP_AUTHORIZATION'].split()
|
||||
if token_type == 'Bearer' and len(credentials):
|
||||
token_value = credentials[0]
|
||||
|
||||
user = User.objects.filter(config_sync_token=token_value).first()
|
||||
|
||||
if user:
|
||||
request.session.save = lambda *args, **kwargs: None
|
||||
setattr(user, 'backend', 'django.contrib.auth.backends.ModelBackend')
|
||||
login(request, user)
|
||||
setattr(request, '_dont_enforce_csrf_checks', True)
|
||||
|
||||
response = self.get_response(request)
|
||||
|
||||
if user:
|
||||
response.set_cookie = lambda *args, **kwargs: None
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class GAMiddleware(BaseMiddleware):
|
||||
def __init__(self, get_response):
|
||||
super().__init__(get_response)
|
||||
if settings.GA_ID:
|
||||
self.tracker = Tracker(settings.GA_ID, settings.GA_DOMAIN)
|
||||
|
||||
def __call__(self, request):
|
||||
response = self.get_response(request)
|
||||
if settings.GA_ID and request.path in ['/', '/app']:
|
||||
try:
|
||||
self.tracker.track_pageview(Page(request.path), Session(), Visitor())
|
||||
except Exception:
|
||||
logging.exception()
|
||||
|
||||
return response
|
||||
261
backend/tabby/settings.py
Normal file
261
backend/tabby/settings.py
Normal file
@@ -0,0 +1,261 @@
|
||||
import os
|
||||
import dj_database_url
|
||||
from dotenv import load_dotenv
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
FRONTEND_BUILD_DIR = BASE_DIR / '../frontend/build'
|
||||
|
||||
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure')
|
||||
DEBUG = bool(os.getenv('DEBUG', False))
|
||||
|
||||
ALLOWED_HOSTS = ['*']
|
||||
USE_X_FORWARDED_HOST = True
|
||||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'rest_framework',
|
||||
'social_django',
|
||||
'corsheaders',
|
||||
'tabby.app',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'corsheaders.middleware.CorsMiddleware',
|
||||
'tabby.middleware.TokenMiddleware',
|
||||
'tabby.middleware.GAMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'tabby.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'tabby.wsgi.application'
|
||||
|
||||
DATABASES = {
|
||||
'default': dj_database_url.config(conn_max_age=600)
|
||||
}
|
||||
|
||||
CACHES = {
|
||||
'default': {
|
||||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
||||
}
|
||||
}
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
AUTH_USER_MODEL = 'app.User'
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_RENDERER_CLASSES': (
|
||||
'rest_framework.renderers.JSONRenderer',
|
||||
)
|
||||
}
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
LOGGING = {
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'formatters': {
|
||||
'simple': {
|
||||
'format': '%(levelname)s %(message)s'
|
||||
},
|
||||
},
|
||||
'handlers': {
|
||||
'console': {
|
||||
'level': 'INFO',
|
||||
'class': 'logging.StreamHandler',
|
||||
'formatter': 'simple'
|
||||
},
|
||||
},
|
||||
'loggers': {
|
||||
'': {
|
||||
'handlers': ['console'],
|
||||
'propagate': False,
|
||||
'level': 'INFO',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
if FRONTEND_BUILD_DIR.exists():
|
||||
STATICFILES_DIRS = [FRONTEND_BUILD_DIR]
|
||||
STATIC_ROOT = BASE_DIR / 'public'
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
|
||||
CSRF_USE_SESSIONS = False
|
||||
CSRF_COOKIE_HTTPONLY = False
|
||||
CSRF_COOKIE_NAME = 'XSRF-TOKEN'
|
||||
CSRF_HEADER_NAME = 'HTTP_X_XSRF_TOKEN'
|
||||
|
||||
AUTHENTICATION_BACKENDS = (
|
||||
'social_core.backends.github.GithubOAuth2',
|
||||
'social_core.backends.gitlab.GitLabOAuth2',
|
||||
'social_core.backends.azuread.AzureADOAuth2',
|
||||
'social_core.backends.microsoft.MicrosoftOAuth2',
|
||||
'social_core.backends.google.GoogleOAuth2',
|
||||
'django.contrib.auth.backends.ModelBackend',
|
||||
)
|
||||
|
||||
SOCIAL_AUTH_GITHUB_SCOPE = ['read:user', 'user:email']
|
||||
SOCIAL_AUTH_PIPELINE = (
|
||||
'social_core.pipeline.social_auth.social_details',
|
||||
'social_core.pipeline.social_auth.social_uid',
|
||||
'social_core.pipeline.social_auth.auth_allowed',
|
||||
'social_core.pipeline.social_auth.social_user',
|
||||
'social_core.pipeline.user.get_username',
|
||||
'social_core.pipeline.social_auth.associate_by_email',
|
||||
'social_core.pipeline.user.create_user',
|
||||
'social_core.pipeline.social_auth.associate_user',
|
||||
'social_core.pipeline.social_auth.load_extra_data',
|
||||
'social_core.pipeline.user.user_details',
|
||||
)
|
||||
|
||||
APP_DIST_STORAGE = os.getenv('APP_DIST_STORAGE', 'file://' + str(BASE_DIR / 'app-dist'))
|
||||
NPM_REGISTRY = os.getenv('NPM_REGISTRY', 'https://registry.npmjs.org').rstrip('/')
|
||||
|
||||
FRONTEND_URL = None
|
||||
BACKEND_URL = None
|
||||
GITHUB_ELIGIBLE_SPONSORSHIPS = None
|
||||
|
||||
for key in [
|
||||
'FRONTEND_URL',
|
||||
'BACKEND_URL',
|
||||
'SOCIAL_AUTH_GITHUB_KEY',
|
||||
'SOCIAL_AUTH_GITHUB_SECRET',
|
||||
'SOCIAL_AUTH_GITLAB_KEY',
|
||||
'SOCIAL_AUTH_GITLAB_SECRET',
|
||||
'SOCIAL_AUTH_GOOGLE_OAUTH2_KEY',
|
||||
'SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET',
|
||||
'SOCIAL_AUTH_MICROSOFT_GRAPH_KEY',
|
||||
'SOCIAL_AUTH_MICROSOFT_GRAPH_SECRET',
|
||||
'CONNECTION_GATEWAY_AUTH_CA',
|
||||
'CONNECTION_GATEWAY_AUTH_CERTIFICATE',
|
||||
'CONNECTION_GATEWAY_AUTH_KEY',
|
||||
'GITHUB_ELIGIBLE_SPONSORSHIPS',
|
||||
'GITHUB_SPONSORS_MIN_PAYMENT',
|
||||
'ENABLE_LOGIN',
|
||||
'GA_ID',
|
||||
'GA_DOMAIN',
|
||||
'ENABLE_HOMEPAGE',
|
||||
]:
|
||||
globals()[key] = os.getenv(key)
|
||||
|
||||
|
||||
for key in [
|
||||
'GITHUB_SPONSORS_MIN_PAYMENT',
|
||||
]:
|
||||
globals()[key] = int(globals()[key]) if globals()[key] else None
|
||||
|
||||
|
||||
for key in [
|
||||
'ENABLE_LOGIN',
|
||||
'ENABLE_HOMEPAGE',
|
||||
]:
|
||||
globals()[key] = bool(globals()[key]) if globals()[key] else None
|
||||
|
||||
|
||||
for key in [
|
||||
'CONNECTION_GATEWAY_AUTH_CA',
|
||||
'CONNECTION_GATEWAY_AUTH_CERTIFICATE',
|
||||
'CONNECTION_GATEWAY_AUTH_KEY',
|
||||
]:
|
||||
v = globals()[key]
|
||||
if v and not os.path.exists(v):
|
||||
raise ValueError(f'{v} does not exist')
|
||||
|
||||
if GITHUB_ELIGIBLE_SPONSORSHIPS:
|
||||
GITHUB_ELIGIBLE_SPONSORSHIPS = GITHUB_ELIGIBLE_SPONSORSHIPS.split(',')
|
||||
else:
|
||||
GITHUB_ELIGIBLE_SPONSORSHIPS = []
|
||||
|
||||
|
||||
if FRONTEND_URL:
|
||||
CORS_ALLOWED_ORIGINS = [FRONTEND_URL]
|
||||
CORS_ALLOW_CREDENTIALS = True
|
||||
CORS_ALLOW_HEADERS = [
|
||||
'accept',
|
||||
'accept-encoding',
|
||||
'authorization',
|
||||
'content-type',
|
||||
'dnt',
|
||||
'origin',
|
||||
'user-agent',
|
||||
'x-xsrf-token',
|
||||
'x-requested-with',
|
||||
]
|
||||
frontend_domain = urlparse(FRONTEND_URL).hostname
|
||||
CSRF_TRUSTED_ORIGINS = [frontend_domain]
|
||||
if BACKEND_URL:
|
||||
CSRF_TRUSTED_ORIGINS.append(urlparse(BACKEND_URL).hostname)
|
||||
SESSION_COOKIE_DOMAIN = frontend_domain
|
||||
CSRF_COOKIE_DOMAIN = frontend_domain
|
||||
|
||||
FRONTEND_URL = FRONTEND_URL.rstrip('/')
|
||||
|
||||
if FRONTEND_URL.startswith('https://'):
|
||||
CSRF_COOKIE_SECURE = True
|
||||
SESSION_COOKIE_SECURE = True
|
||||
else:
|
||||
FRONTEND_URL = ''
|
||||
|
||||
LOGIN_REDIRECT_URL = FRONTEND_URL + '/app'
|
||||
10
backend/tabby/urls.py
Normal file
10
backend/tabby/urls.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
|
||||
from .app.urls import urlpatterns as app_urlpatterns
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(app_urlpatterns)),
|
||||
path('api/1/auth/social/', include('social_django.urls', namespace='social')),
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
7
backend/tabby/wsgi.py
Normal file
7
backend/tabby/wsgi.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tabby.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in New Issue
Block a user