Skip to content

Commit

Permalink
refactor: V2rayShareLink usage
Browse files Browse the repository at this point in the history
  • Loading branch information
M03ED committed Apr 28, 2024
1 parent 98ee34f commit 13f3374
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 107 deletions.
121 changes: 14 additions & 107 deletions app/subscription/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,88 +33,11 @@
"on_hold": ONHOLD_STATUS_TEXT,
}

def get_v2ray_link(remark: str, address: str, inbound: dict, settings: dict):
if inbound["protocol"] == "vmess":
return V2rayShareLink.vmess(
remark=remark,
address=address,
port=inbound["port"],
id=settings["id"],
net=inbound["network"],
tls=inbound["tls"],
sni=inbound.get("sni", ""),
fp=inbound.get("fp", ""),
alpn=inbound.get("alpn", ""),
pbk=inbound.get("pbk", ""),
sid=inbound.get("sid", ""),
spx=inbound.get("spx", ""),
host=inbound["host"],
path=inbound["path"],
type=inbound["header_type"],
ais=inbound.get("ais", ""),
fs=inbound.get("fragment_setting", ""),
multiMode=inbound.get("multiMode", False),
)

if inbound["protocol"] == "vless":
return V2rayShareLink.vless(
remark=remark,
address=address,
port=inbound["port"],
id=settings["id"],
flow=settings.get("flow", ""),
net=inbound["network"],
tls=inbound["tls"],
sni=inbound.get("sni", ""),
fp=inbound.get("fp", ""),
alpn=inbound.get("alpn", ""),
pbk=inbound.get("pbk", ""),
sid=inbound.get("sid", ""),
spx=inbound.get("spx", ""),
host=inbound["host"],
path=inbound["path"],
type=inbound["header_type"],
ais=inbound.get("ais", ""),
fs=inbound.get("fragment_setting", ""),
multiMode=inbound.get("multiMode", False),
)

if inbound["protocol"] == "trojan":
return V2rayShareLink.trojan(
remark=remark,
address=address,
port=inbound["port"],
password=settings["password"],
flow=settings.get("flow", ""),
net=inbound["network"],
tls=inbound["tls"],
sni=inbound.get("sni", ""),
fp=inbound.get("fp", ""),
alpn=inbound.get("alpn", ""),
pbk=inbound.get("pbk", ""),
sid=inbound.get("sid", ""),
spx=inbound.get("spx", ""),
host=inbound["host"],
path=inbound["path"],
type=inbound["header_type"],
ais=inbound.get("ais", ""),
fs=inbound.get("fragment_setting", ""),
multiMode=inbound.get("multiMode", False),
)

if inbound["protocol"] == "shadowsocks":
return V2rayShareLink.shadowsocks(
remark=remark,
address=address,
port=inbound["port"],
password=settings["password"],
method=settings["method"],
)


def generate_v2ray_links(proxies: dict, inbounds: dict, extra_data: dict) -> list:
format_variables = setup_format_variables(extra_data)
return process_inbounds_and_tags(inbounds, proxies, format_variables, mode="v2ray")
conf = V2rayShareLink()
return process_inbounds_and_tags(inbounds, proxies, format_variables, conf=conf)


def generate_clash_subscription(
Expand All @@ -127,7 +50,7 @@ def generate_clash_subscription(

format_variables = setup_format_variables(extra_data)
return process_inbounds_and_tags(
inbounds, proxies, format_variables, mode="clash", conf=conf
inbounds, proxies, format_variables, conf=conf
)


Expand All @@ -138,7 +61,7 @@ def generate_singbox_subscription(

format_variables = setup_format_variables(extra_data)
return process_inbounds_and_tags(
inbounds, proxies, format_variables, mode="sing-box", conf=conf
inbounds, proxies, format_variables, conf=conf
)


Expand All @@ -153,7 +76,7 @@ def generate_outline_subscription(

format_variables = setup_format_variables(extra_data)
return process_inbounds_and_tags(
inbounds, proxies, format_variables, mode="outline", conf=conf
inbounds, proxies, format_variables, conf=conf
)


Expand All @@ -164,7 +87,7 @@ def generate_v2ray_json_subscription(

format_variables = setup_format_variables(extra_data)
return process_inbounds_and_tags(
inbounds, proxies, format_variables, mode="v2ray-json", conf=conf
inbounds, proxies, format_variables, conf=conf
)


Expand Down Expand Up @@ -298,10 +221,8 @@ def process_inbounds_and_tags(
inbounds: dict,
proxies: dict,
format_variables: dict,
mode: str = "v2ray",
conf=None,
) -> Union[List, str]:
results = []

_inbounds = []
for protocol, tags in inbounds.items():
Expand Down Expand Up @@ -359,28 +280,14 @@ def process_inbounds_and_tags(
}
)

if mode == "v2ray":
results.append(
get_v2ray_link(
remark=host["remark"].format_map(format_variables),
address=address.format_map(
format_variables),
inbound=host_inbound,
settings=settings.dict(no_obj=True),
)
)
elif mode in ["clash", "sing-box", "outline", "v2ray-json"]:
conf.add(
remark=host["remark"].format_map(format_variables),
address=address.format_map(format_variables),
inbound=host_inbound,
settings=settings.dict(no_obj=True),
)

if mode in ["clash", "sing-box", "outline", "v2ray-json"]:
return conf.render()

return results
conf.add(
remark=host["remark"].format_map(format_variables),
address=address.format_map(format_variables),
inbound=host_inbound,
settings=settings.dict(no_obj=True),
)

return conf.render()


def encode_title(text: str) -> str:
Expand Down
91 changes: 91 additions & 0 deletions app/subscription/v2ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,97 @@


class V2rayShareLink(str):
def __init__(self):
self.links = []

def add_link(self, link):
self.links.append(link)

def render(self):
return self.links

def add(self, remark: str, address: str, inbound: dict, settings: dict):

if inbound["protocol"] == "vmess":
link = self.vmess(
remark=remark,
address=address,
port=inbound["port"],
id=settings["id"],
net=inbound["network"],
tls=inbound["tls"],
sni=inbound.get("sni", ""),
fp=inbound.get("fp", ""),
alpn=inbound.get("alpn", ""),
pbk=inbound.get("pbk", ""),
sid=inbound.get("sid", ""),
spx=inbound.get("spx", ""),
host=inbound["host"],
path=inbound["path"],
type=inbound["header_type"],
ais=inbound.get("ais", ""),
fs=inbound.get("fragment_setting", ""),
multiMode=inbound.get("multiMode", False),
)

elif inbound["protocol"] == "vless":
link = self.vless(
remark=remark,
address=address,
port=inbound["port"],
id=settings["id"],
flow=settings.get("flow", ""),
net=inbound["network"],
tls=inbound["tls"],
sni=inbound.get("sni", ""),
fp=inbound.get("fp", ""),
alpn=inbound.get("alpn", ""),
pbk=inbound.get("pbk", ""),
sid=inbound.get("sid", ""),
spx=inbound.get("spx", ""),
host=inbound["host"],
path=inbound["path"],
type=inbound["header_type"],
ais=inbound.get("ais", ""),
fs=inbound.get("fragment_setting", ""),
multiMode=inbound.get("multiMode", False),
)

elif inbound["protocol"] == "trojan":
link = self.trojan(
remark=remark,
address=address,
port=inbound["port"],
password=settings["password"],
flow=settings.get("flow", ""),
net=inbound["network"],
tls=inbound["tls"],
sni=inbound.get("sni", ""),
fp=inbound.get("fp", ""),
alpn=inbound.get("alpn", ""),
pbk=inbound.get("pbk", ""),
sid=inbound.get("sid", ""),
spx=inbound.get("spx", ""),
host=inbound["host"],
path=inbound["path"],
type=inbound["header_type"],
ais=inbound.get("ais", ""),
fs=inbound.get("fragment_setting", ""),
multiMode=inbound.get("multiMode", False),
)

elif inbound["protocol"] == "shadowsocks":
link = self.shadowsocks(
remark=remark,
address=address,
port=inbound["port"],
password=settings["password"],
method=settings["method"],
)

self.add_link(link=link)


@classmethod
def vmess(
cls,
Expand Down

0 comments on commit 13f3374

Please sign in to comment.