proxies: add smart proxy support
still to do: * document new configuration options * add smart proxies to groups view * import bandwidth and CPU alarms
This commit is contained in:
parent
9b90101cf4
commit
66af6e6550
15 changed files with 275 additions and 32 deletions
|
@ -12,10 +12,32 @@ from tldextract import tldextract
|
|||
from app import app
|
||||
from app.extensions import db
|
||||
from app.models.base import Group
|
||||
from app.models.mirrors import Proxy
|
||||
from app.models.mirrors import Proxy, Origin, SmartProxy
|
||||
from app.terraform.terraform import TerraformAutomation
|
||||
|
||||
|
||||
def update_smart_proxy_instance(group_id: int,
|
||||
provider: str,
|
||||
region: str,
|
||||
instance_id: str) -> None:
|
||||
print("SMART PROXY")
|
||||
instance = SmartProxy.query.filter(
|
||||
SmartProxy.group_id == group_id,
|
||||
SmartProxy.region == region,
|
||||
SmartProxy.provider == provider,
|
||||
SmartProxy.destroyed.is_(None)
|
||||
).first()
|
||||
if instance is None:
|
||||
instance = SmartProxy()
|
||||
instance.added = datetime.datetime.utcnow()
|
||||
instance.group_id = group_id
|
||||
instance.provider = provider
|
||||
instance.region = region
|
||||
db.session.add(instance)
|
||||
instance.updated = datetime.datetime.utcnow()
|
||||
instance.instance_id = instance_id
|
||||
|
||||
|
||||
class ProxyAutomation(TerraformAutomation):
|
||||
subgroup_max = math.inf
|
||||
"""
|
||||
|
@ -35,6 +57,11 @@ class ProxyAutomation(TerraformAutomation):
|
|||
in the templating of the Terraform configuration.
|
||||
"""
|
||||
|
||||
smart_proxies = False
|
||||
"""
|
||||
Whether this provider supports "smart" proxies.
|
||||
"""
|
||||
|
||||
def get_subgroups(self) -> Dict[int, Dict[int, int]]:
|
||||
conn = db.engine.connect()
|
||||
result = conn.execute(text("""
|
||||
|
@ -118,18 +145,40 @@ class ProxyAutomation(TerraformAutomation):
|
|||
self.import_state(self.tf_show())
|
||||
|
||||
def tf_generate(self) -> None:
|
||||
groups = Group.query.all()
|
||||
self.tf_write(
|
||||
self.template,
|
||||
groups=Group.query.all(),
|
||||
groups=groups,
|
||||
proxies=Proxy.query.filter(
|
||||
Proxy.provider == self.provider,
|
||||
Proxy.destroyed.is_(None)
|
||||
).all(),
|
||||
subgroups=self.get_subgroups(),
|
||||
global_namespace=app.config['GLOBAL_NAMESPACE'],
|
||||
bypass_token=app.config['BYPASS_TOKEN'],
|
||||
**{
|
||||
k: app.config[k.upper()]
|
||||
for k in self.template_parameters
|
||||
}
|
||||
)
|
||||
Proxy.provider == self.provider, Proxy.destroyed.is_(None)).all(), subgroups=self.get_subgroups(),
|
||||
global_namespace=app.config['GLOBAL_NAMESPACE'], bypass_token=app.config['BYPASS_TOKEN'],
|
||||
**{k: app.config[k.upper()] for k in self.template_parameters})
|
||||
if self.smart_proxies:
|
||||
for group in groups:
|
||||
self.sp_config(group)
|
||||
|
||||
def sp_config(self, group: Group) -> None:
|
||||
group_origins: List[Origin] = Origin.query.filter(
|
||||
Origin.group_id == group.id,
|
||||
Origin.destroyed.is_(None),
|
||||
Origin.smart.is_(True)
|
||||
).all()
|
||||
self.tmpl_write(f"smart_proxy.{group.id}.conf", """
|
||||
{% for origin in origins %}
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name origin-{{ origin.id }}.{{ provider }}.smart.censorship.guide;
|
||||
location / {
|
||||
proxy_set_header Accept-Encoding "";
|
||||
proxy_ssl_server_name on;
|
||||
proxy_pass https://{{ origin.domain_name }}/;
|
||||
subs_filter_types text/html text/css text/xml;
|
||||
subs_filter https://{{ origin.domain_name }}/ /;
|
||||
}
|
||||
ssl_certificate /etc/ssl/smart_proxy.crt;
|
||||
ssl_certificate_key /etc/ssl/private/smart_proxy.key;
|
||||
}
|
||||
{% endfor %}
|
||||
""",
|
||||
provider=self.provider,
|
||||
origins=group_origins)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue