Skip to content

Commit

Permalink
Merge pull request #1067 from M03ED/1.8.11
Browse files Browse the repository at this point in the history
add random user agent support
  • Loading branch information
SaintShit authored Jul 1, 2024
2 parents c607c46 + b285133 commit f7a2aae
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 23 deletions.
1 change: 1 addition & 0 deletions app/dashboard/build/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"hostsDialog.host.wildcard": "Use <badge>*</badge> to generate a random string (works for wildcard domain names)",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "Enable MUX",
"hostsDialog.randomUserAgent":"Use random user agent",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "Fragment pattern",
"hostsDialog.fragment.info": "Correct pattern: length,interval,packets",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/build/locales/fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"hostsDialog.host.wildcard": "از <badge>*</badge> برای ساخت عبارت تصادفی استفاده کنید (برای نام‌های wildcard کار می‌کند)",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "فعالسازی MUX",
"hostsDialog.randomUserAgent":"استفاده از User Agent تصادفی",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "الگو فرگمنت",
"hostsDialog.fragment.info": "length,interval,packet (e.g. 10-100,100-200,tlshello)",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/build/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"hostsDialog.host.wildcard": "Используйте <badge>*</badge>, чтобы сгенерировать случайную строку (работает для wildcard доменов)",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "Давать возможность MUX",
"hostsDialog.randomUserAgent":"Use random user agent",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "Шаблон фрагмента",
"hostsDialog.fragment.info": "length,interval,packet (e.g. 10-100,100-200,tlshello)",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/build/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"hostsDialog.fingerprint": "指纹",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "使能够 MUX",
"hostsDialog.randomUserAgent":"Use random user agent",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "碎片图案",
"hostsDialog.fragment.info": "length,interval,packet (e.g. 10-100,100-200,tlshello)",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"hostsDialog.host.wildcard": "Use <badge>*</badge> to generate a random string (works for wildcard domain names)",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "Enable MUX",
"hostsDialog.randomUserAgent":"Use random user agent",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "Fragment pattern",
"hostsDialog.fragment.info": "Correct pattern: length,interval,packets",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/public/locales/fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"hostsDialog.host.wildcard": "از <badge>*</badge> برای ساخت عبارت تصادفی استفاده کنید (برای نام‌های wildcard کار می‌کند)",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "فعالسازی MUX",
"hostsDialog.randomUserAgent":"استفاده از User Agent تصادفی",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "الگو فرگمنت",
"hostsDialog.fragment.info": "length,interval,packet (e.g. 10-100,100-200,tlshello)",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/public/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"hostsDialog.host.wildcard": "Используйте <badge>*</badge>, чтобы сгенерировать случайную строку (работает для wildcard доменов)",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "Давать возможность MUX",
"hostsDialog.randomUserAgent":"Use random user agent",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "Шаблон фрагмента",
"hostsDialog.fragment.info": "length,interval,packet (e.g. 10-100,100-200,tlshello)",
Expand Down
1 change: 1 addition & 0 deletions app/dashboard/public/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"hostsDialog.fingerprint": "指纹",
"hostsDialog.sockopt": "Sockopt",
"hostsDialog.muxEnable": "使能够 MUX",
"hostsDialog.randomUserAgent":"Use random user agent",
"hostsDialog.allowinsecure": "Allow Insecure",
"hostsDialog.fragment": "碎片图案",
"hostsDialog.fragment.info": "length,interval,packet (e.g. 10-100,100-200,tlshello)",
Expand Down
24 changes: 24 additions & 0 deletions app/dashboard/src/components/HostsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const hostsSchema = z.record(
allowinsecure: z.boolean().nullable().default(false),
is_disabled: z.boolean().default(true),
fragment_setting: z.string().nullable(),
random_user_agent: z.boolean().default(false),
security: z.string(),
alpn: z.string(),
fingerprint: z.string(),
Expand Down Expand Up @@ -175,6 +176,7 @@ const AccordionInbound: FC<AccordionInboundType> = ({
allowinsecure: false,
is_disabled: false,
fragment_setting: "",
random_user_agent: false,
security: "inbound_default",
alpn: "",
fingerprint: "",
Expand Down Expand Up @@ -920,6 +922,28 @@ const AccordionInbound: FC<AccordionInboundType> = ({
</Error>
)}
</FormControl>
<FormControl
isInvalid={
!!(
accordionErrors &&
accordionErrors[index]?.random_user_agent
)
}
>
<Checkbox
{...form.register(
hostKey + "." + index + ".random_user_agent"
)}
>
<FormLabel>{t("hostsDialog.randomUserAgent")}</FormLabel>
</Checkbox>
{accordionErrors &&
accordionErrors[index]?.random_user_agent && (
<Error>
{accordionErrors[index]?.random_user_agent?.message}
</Error>
)}
</FormControl>
</VStack>
</AccordionPanel>
</AccordionItem>
Expand Down
1 change: 1 addition & 0 deletions app/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def update_hosts(db: Session, inbound_tag: str, modified_hosts: List[ProxyHostMo
is_disabled=host.is_disabled,
mux_enable=host.mux_enable,
fragment_setting=host.fragment_setting,
random_user_agent=host.random_user_agent,
) for host in modified_hosts
]
db.commit()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Add Support Random User-Agent
Revision ID: 31f92220c0d0
Revises: 4f045f53bef8
Create Date: 2024-06-01 21:28:33.310627
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '31f92220c0d0'
down_revision = '4f045f53bef8'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('hosts', sa.Column('random_user_agent', sa.Boolean(), server_default='0', nullable=False))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('hosts', 'random_user_agent')
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class ProxyHost(Base):
is_disabled = Column(Boolean, nullable=True, default=False)
mux_enable = Column(Boolean, nullable=False, default=False, server_default='0')
fragment_setting = Column(String(100), nullable=True)
random_user_agent = Column(Boolean, nullable=False, default=False, server_default='0')


class System(Base):
Expand Down
1 change: 1 addition & 0 deletions app/models/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class ProxyHost(BaseModel):
is_disabled: Union[bool, None] = None
mux_enable: Union[bool, None] = None
fragment_setting: Optional[str] = Field(None, nullable=True)
random_user_agent: Union[bool, None] = None

class Config:
orm_mode = True
Expand Down
3 changes: 2 additions & 1 deletion app/subscription/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ def process_inbounds_and_tags(
"ais": host["allowinsecure"]
or inbound.get("allowinsecure", ""),
"mux_enable": host["mux_enable"],
"fragment_setting": host["fragment_setting"]
"fragment_setting": host["fragment_setting"],
"random_user_agent": host["random_user_agent"],
}
)

Expand Down
35 changes: 29 additions & 6 deletions app/subscription/singbox.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
from random import choice
from app.templates import render_template
from app.subscription.funcs import get_grpc_gun

from config import SINGBOX_SUBSCRIPTION_TEMPLATE, MUX_TEMPLATE
from config import SINGBOX_SUBSCRIPTION_TEMPLATE, MUX_TEMPLATE, USER_AGENT_TEMPLATE


class SingBoxConfiguration(str):
Expand All @@ -11,6 +12,13 @@ def __init__(self):
template = render_template(SINGBOX_SUBSCRIPTION_TEMPLATE)
self.config = json.loads(template)
self.mux_template = render_template(MUX_TEMPLATE)
temp_user_agent_data = render_template(USER_AGENT_TEMPLATE)
user_agent_data = json.loads(temp_user_agent_data)

if 'list' in user_agent_data and isinstance(user_agent_data['list'], list):
self.user_agent_list = user_agent_data['list']
else:
self.user_agent_list = []

def add_outbound(self, outbound_data):
self.config["outbounds"].append(outbound_data)
Expand Down Expand Up @@ -65,16 +73,17 @@ def tls_config(sni=None, fp=None, tls=None, pbk=None,

return config

@staticmethod
def transport_config(transport_type='',
def transport_config(self,
transport_type='',
host='',
path='',
method='',
idle_timeout="15s",
ping_timeout="15s",
max_early_data=None,
early_data_header_name=None,
permit_without_stream=False):
permit_without_stream=False,
random_user_agent: bool = False):

transport_config = {}

Expand All @@ -87,8 +96,12 @@ def transport_config(transport_type='',
transport_config['path'] = path
if method:
transport_config['method'] = method
if host or random_user_agent:
transport_config['headers'] = {}
if host:
transport_config["host"] = [host]
if random_user_agent:
transport_config['headers']['User-Agent'] = choice(self.user_agent_list)
if idle_timeout:
transport_config['idle_timeout'] = idle_timeout
if ping_timeout:
Expand All @@ -97,8 +110,12 @@ def transport_config(transport_type='',
elif transport_type == "ws":
if path:
transport_config['path'] = path
if host or random_user_agent:
transport_config['headers'] = {}
if host:
transport_config['headers'] = {'Host': host}
if random_user_agent:
transport_config['headers']['User-Agent'] = choice(self.user_agent_list)
if max_early_data is not None:
transport_config['max_early_data'] = max_early_data
if early_data_header_name:
Expand All @@ -118,6 +135,9 @@ def transport_config(transport_type='',
transport_config['host'] = host
if path:
transport_config['path'] = path
if random_user_agent:
transport_config['headers'] = {}
transport_config['headers']['User-Agent'] = choice(self.user_agent_list)

return transport_config

Expand All @@ -139,6 +159,7 @@ def make_outbound(self,
headers='',
ais='',
mux_enable: bool = False,
random_user_agent: bool = False,
):

config = {
Expand Down Expand Up @@ -173,7 +194,8 @@ def make_outbound(self,
host=host,
path=path,
max_early_data=max_early_data,
early_data_header_name=early_data_header_name
early_data_header_name=early_data_header_name,
random_user_agent=random_user_agent,
)
else:
config["network"] = net
Expand Down Expand Up @@ -215,7 +237,8 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
sid=inbound.get('sid', ''),
headers=inbound['header_type'],
ais=inbound.get('ais', ''),
mux_enable=inbound.get('mux_enable', False))
mux_enable=inbound.get('mux_enable', False),
random_user_agent=inbound.get('random_user_agent', False),)

if inbound['protocol'] == 'vmess':
outbound['uuid'] = settings['id']
Expand Down
Loading

0 comments on commit f7a2aae

Please sign in to comment.