2024-01-05 11:54:38 +00:00
#!/usr/bin/env python3
2022-06-03 15:24:14 +00:00
2023-04-23 14:16:10 +00:00
from io import StringIO
2022-06-03 15:24:14 +00:00
from json import loads
from glob import glob
2023-11-07 11:09:18 +00:00
from pathlib import Path
2022-06-03 15:24:14 +00:00
from pytablewriter import MarkdownTableWriter
2024-03-11 13:50:47 +00:00
import requests
import zipfile
2024-03-11 16:12:31 +00:00
import shutil
2024-03-14 09:51:12 +00:00
from contextlib import suppress
2022-06-03 15:24:14 +00:00
2024-04-05 13:00:34 +00:00
from os import getenv
2024-03-21 13:57:54 +00:00
2024-04-06 11:55:16 +00:00
2023-04-23 14:16:10 +00:00
def print_md_table ( settings ) - > MarkdownTableWriter :
2022-12-14 16:09:57 +00:00
writer = MarkdownTableWriter (
headers = [ " Setting " , " Default " , " Context " , " Multiple " , " Description " ] ,
value_matrix = [
2022-10-19 15:37:13 +00:00
[
f " ` { setting } ` " ,
" " if data [ " default " ] == " " else f " ` { data [ ' default ' ] } ` " ,
data [ " context " ] ,
2023-10-03 10:01:24 +00:00
" no " if " multiple " not in data else " yes " ,
2022-10-19 15:37:13 +00:00
data [ " help " ] ,
]
2022-12-14 16:09:57 +00:00
for setting , data in settings . items ( )
] ,
2022-06-03 15:24:14 +00:00
)
2023-04-23 14:16:10 +00:00
return writer
2022-06-03 15:24:14 +00:00
2024-03-21 13:57:54 +00:00
2023-04-27 18:27:07 +00:00
def stream_support ( support ) - > str :
md = " STREAM support "
if support == " no " :
md + = " :x: "
elif support == " yes " :
md + = " :white_check_mark: "
else :
md + = " :warning: "
return md
2022-10-19 15:37:13 +00:00
2024-03-21 13:57:54 +00:00
2024-03-19 11:27:17 +00:00
def pro_title ( title : str ) - > str :
2024-03-25 22:31:48 +00:00
return f " ## { title } <img src= ' ../assets/img/pro-icon.svg ' alt= ' crow pro icon ' height= ' 24px ' width= ' 24px ' style= ' transform : translateY(3px); ' > \n "
2023-04-30 07:15:06 +00:00
2024-03-21 13:57:54 +00:00
2023-04-23 14:16:10 +00:00
doc = StringIO ( )
print ( " # Settings \n " , file = doc )
2022-10-19 15:37:13 +00:00
print (
2023-12-11 17:48:08 +00:00
' !!! info " Settings generator tool " \n \n To help you tune BunkerWeb, we have made an easy-to-use settings generator tool available at [config.bunkerweb.io](https://config.bunkerweb.io/?utm_campaign=self&utm_source=doc). \n ' ,
2023-04-23 14:16:10 +00:00
file = doc ,
2022-10-19 15:37:13 +00:00
)
print (
2023-10-03 10:01:24 +00:00
" This section contains the full list of settings supported by BunkerWeb. "
+ " If you are not yet familiar with BunkerWeb, you should first read the [concepts](concepts.md) section of the documentation. "
+ " Please follow the instructions for your own [integration](integrations.md) on how to apply the settings. \n " ,
2023-04-23 14:16:10 +00:00
file = doc ,
2022-10-19 15:37:13 +00:00
)
print (
2023-10-03 10:01:24 +00:00
" As a general rule when multisite mode is enabled, if you want to apply settings with multisite context to a specific server, you will need to add the primary "
+ " (first) server name as a prefix like `www.example.com_USE_ANTIBOT=captcha` or `myapp.example.com_USE_GZIP=yes` for example. \n " ,
2023-04-23 14:16:10 +00:00
file = doc ,
2022-10-19 15:37:13 +00:00
)
print (
2023-10-03 10:01:24 +00:00
' When settings are considered as " multiple " , it means that you can have multiple groups of settings for the same feature by adding numbers as suffix like `REVERSE_PROXY_URL_1=/subdir`, '
+ " `REVERSE_PROXY_HOST_1=http://myhost1`, `REVERSE_PROXY_URL_2=/anotherdir`, `REVERSE_PROXY_HOST_2=http://myhost2`, ... for example. \n " ,
2023-04-23 14:16:10 +00:00
file = doc ,
2022-10-19 15:37:13 +00:00
)
2022-06-03 15:24:14 +00:00
# Print global settings
2023-04-23 14:16:10 +00:00
print ( " ## Global settings \n " , file = doc )
2023-04-27 18:27:07 +00:00
print ( f " \n { stream_support ( ' partial ' ) } \n " , file = doc )
2022-11-11 13:55:04 +00:00
with open ( " src/common/settings.json " , " r " ) as f :
2023-04-23 14:16:10 +00:00
print ( print_md_table ( loads ( f . read ( ) ) ) , file = doc )
print ( file = doc )
2022-06-03 15:24:14 +00:00
2024-03-19 11:27:17 +00:00
# Get core plugins
2022-06-03 15:24:14 +00:00
core_settings = { }
2022-11-11 13:55:04 +00:00
for core in glob ( " src/common/core/*/plugin.json " ) :
2022-10-19 15:37:13 +00:00
with open ( core , " r " ) as f :
2024-03-14 09:51:12 +00:00
with suppress ( Exception ) :
2024-03-11 16:12:31 +00:00
core_plugin = loads ( f . read ( ) )
if len ( core_plugin [ " settings " ] ) > 0 :
core_settings [ core_plugin [ " name " ] ] = core_plugin
2023-04-23 14:16:10 +00:00
2024-03-19 11:27:17 +00:00
# Get PRO plugins
2024-04-05 13:00:34 +00:00
if getenv ( " VERSION " ) :
version = getenv ( " VERSION " )
else :
with open ( " src/VERSION " , " r " ) as f :
version = f . read ( ) . strip ( )
2024-03-11 13:50:47 +00:00
url = f " https://assets.bunkerity.com/bw-pro/preview/v { version } .zip "
response = requests . get ( url )
response . raise_for_status ( )
2024-03-14 09:51:12 +00:00
Path ( f " v { version } .zip " ) . write_bytes ( response . content )
2024-03-11 13:50:47 +00:00
with zipfile . ZipFile ( f " v { version } .zip " , " r " ) as zip_ref :
zip_ref . extractall ( f " v { version } " )
pro_settings = { }
for pro in glob ( f " v { version } /*/plugin.json " ) :
with open ( pro , " r " ) as f :
2024-03-14 09:51:12 +00:00
with suppress ( Exception ) :
2024-03-11 16:12:31 +00:00
pro_plugin = loads ( f . read ( ) )
2024-04-06 11:55:16 +00:00
core_settings [ pro_plugin [ " name " ] ] = pro_plugin
core_settings [ pro_plugin [ " name " ] ] [ " is_pro " ] = True
2024-03-11 13:50:47 +00:00
2024-03-19 11:27:17 +00:00
# Print plugins and their settings
2024-03-25 12:59:43 +00:00
for data in dict ( sorted ( core_settings . items ( ) ) ) . values ( ) :
2024-03-19 11:27:17 +00:00
pro_crown = " "
if " is_pro " in data :
2024-03-25 22:31:48 +00:00
pro_crown = " <img src= ' ../assets/img/pro-icon.svg ' alt= ' crow pro icon ' height= ' 24px ' width= ' 24px ' style= ' transform : translateY(3px); ' > (PRO) \n "
2024-03-19 11:27:17 +00:00
print ( f " ## { data [ ' name ' ] } { pro_crown } \n " , file = doc )
2024-03-11 13:50:47 +00:00
print ( f " { stream_support ( data [ ' stream ' ] ) } \n " , file = doc )
print ( f " { data [ ' description ' ] } \n " , file = doc )
2024-04-06 11:55:16 +00:00
if data [ " settings " ] :
print ( print_md_table ( data [ " settings " ] ) , file = doc )
2024-03-11 13:50:47 +00:00
# Remove zip file
Path ( f " v { version } .zip " ) . unlink ( )
2024-03-11 16:12:31 +00:00
# Remove folder using shutil
shutil . rmtree ( f " v { version } " )
2024-03-11 13:50:47 +00:00
2023-04-23 14:16:10 +00:00
doc . seek ( 0 )
content = doc . read ( )
doc = StringIO ( content . replace ( " \\ | " , " | " ) )
doc . seek ( 0 )
2023-11-07 11:09:18 +00:00
Path ( " docs " , " settings.md " ) . write_text ( doc . read ( ) , encoding = " utf-8 " )