Skip to content

Commit

Permalink
refactor: raise when the protocol is not supported and swallow_errors…
Browse files Browse the repository at this point in the history
… is false
  • Loading branch information
khodedawsh committed Jul 29, 2024
1 parent 2aedb21 commit 6932603
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
15 changes: 12 additions & 3 deletions v2share/clash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from v2share.base import BaseConfig
from v2share.data import V2Data
from v2share.exceptions import ProtocolNotSupportedError, NotSupportedError


class ClashConfig(BaseConfig):
def __init__(self, template_path: Optional[str] = None):
def __init__(self, template_path: Optional[str] = None, swallow_errors=True):
if not template_path:
template_path = resources.files("v2share.templates") / "clash.yml"
with open(template_path) as f:
Expand All @@ -19,10 +20,16 @@ def __init__(self, template_path: Optional[str] = None):
"rules": [],
}
self.proxy_remarks = []
self._swallow_errors = swallow_errors

def add_proxies(self, proxies: List[V2Data]):
for proxy in proxies:
self._add_node(proxy)
try:
self._add_node(proxy)
except NotSupportedError:
if self._swallow_errors:
continue
raise

def render(self):
result = yaml.safe_load(self.template_data)
Expand Down Expand Up @@ -97,7 +104,7 @@ def _make_node(
if host:
net_opts["host"] = [host]

if network == "http" or network == "tcp":
if network in {"http", "tcp"}:
if path:
net_opts["method"] = "GET"
net_opts["path"] = [path]
Expand Down Expand Up @@ -138,3 +145,5 @@ def _add_node(self, config: V2Data):
node["cipher"] = config.shadowsocks_method
self.data["proxies"].append(node)
self.proxy_remarks.append(config.remark)
else:
raise ProtocolNotSupportedError
9 changes: 6 additions & 3 deletions v2share/clashmeta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from v2share.clash import ClashConfig
from v2share.data import V2Data
from v2share.exceptions import ProtocolNotSupportedError


class ClashMetaConfig(ClashConfig):
Expand Down Expand Up @@ -70,7 +71,7 @@ def _add_node(self, config: V2Data):
self.data["proxies"].append(node)
self.proxy_remarks.append(config.remark)

if config.protocol == "vless":
elif config.protocol == "vless":
node["uuid"] = str(config.uuid)

if config.transport_type in ("tcp", "kcp") and config.header_type != "http":
Expand All @@ -79,7 +80,7 @@ def _add_node(self, config: V2Data):
self.data["proxies"].append(node)
self.proxy_remarks.append(config.remark)

if config.protocol == "trojan":
elif config.protocol == "trojan":
node["password"] = config.password

if (
Expand All @@ -92,8 +93,10 @@ def _add_node(self, config: V2Data):
self.data["proxies"].append(node)
self.proxy_remarks.append(config.remark)

if config.protocol == "shadowsocks":
elif config.protocol == "shadowsocks":
node["password"] = config.password
node["cipher"] = config.shadowsocks_method
self.data["proxies"].append(node)
self.proxy_remarks.append(config.remark)
else:
raise ProtocolNotSupportedError
7 changes: 5 additions & 2 deletions v2share/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from uuid import UUID

from v2share._utils import filter_dict
from v2share.exceptions import ProtocolNotSupportedError


@dataclass
Expand Down Expand Up @@ -93,7 +94,7 @@ def to_link(self):
+ f"#{urlparse.quote(self.remark)}"
)

elif self.protocol == "vmess":
if self.protocol == "vmess":
payload = {
"add": self.address,
"aid": "0",
Expand All @@ -118,7 +119,7 @@ def to_link(self):
).decode()
)

elif self.protocol in ["trojan", "vless"]:
if self.protocol in ["trojan", "vless"]:
payload = {
"security": self.tls,
"type": self.transport_type,
Expand All @@ -144,3 +145,5 @@ def to_link(self):
+ urlparse.urlencode(payload)
+ f"#{(urlparse.quote(self.remark))}"
)

raise ProtocolNotSupportedError
14 changes: 14 additions & 0 deletions v2share/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class V2ShareError(Exception):
"""Base class for all v2share exceptions"""


class NotSupportedError(Exception):
"""indicating that the proxy is somehow not supported by v2share"""


class TransportNotSupportedError(Exception):
"""indicating that the specified transport is not supported"""


class ProtocolNotSupportedError(Exception):
"""indicating that the protocol is not supported"""
11 changes: 9 additions & 2 deletions v2share/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

from v2share.base import BaseConfig
from v2share.data import V2Data
from v2share.exceptions import NotSupportedError


class LinksConfig(BaseConfig):
def __init__(self):
def __init__(self, swallow_errors=True):
self._links = []
self._swallow_errors = swallow_errors

def render(self):
return "\n".join(self._links)

def add_proxies(self, proxies: List[V2Data]):
for proxy in proxies:
self._links.append(proxy.to_link())
try:
self._links.append(proxy.to_link())
except NotSupportedError:
if self._swallow_errors:
continue
raise
8 changes: 6 additions & 2 deletions v2share/singbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

from v2share.base import BaseConfig
from v2share.data import V2Data
from v2share.exceptions import ProtocolNotSupportedError


class SingBoxConfig(BaseConfig):
def __init__(self, template_path: str = None):
def __init__(self, template_path: str = None, swallow_errors=True):
if not template_path:
template_path = resources.files("v2share.templates") / "singbox.json"
with open(template_path) as f:
self._template_data = f.read()
self._outbounds = []
self._swallow_errors = swallow_errors

def render(self):
result = json.loads(self._template_data)
Expand Down Expand Up @@ -163,6 +165,8 @@ def add_proxies(self, proxies: List[V2Data]):
if data.header_type:
outbound["obfs"] = {"type": data.header_type, "password": data.path}
else:
continue
if self._swallow_errors:
continue
raise ProtocolNotSupportedError

self._outbounds.append(outbound)
17 changes: 12 additions & 5 deletions v2share/xray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

from v2share.base import BaseConfig
from v2share.data import V2Data
from v2share.exceptions import ProtocolNotSupportedError


class XrayConfig(BaseConfig):
def __init__(self, template_path: str = None, mux_template_path: str = None):
def __init__(
self,
template_path: str = None,
mux_template_path: str = None,
swallow_errors=True,
):
self.config = []
self._swallow_errors = swallow_errors
if not template_path:
template_path = resources.files("v2share.templates") / "xray.json"
if not mux_template_path:
Expand Down Expand Up @@ -48,7 +55,9 @@ def add_proxies(self, proxies: List[V2Data]):
method=data.shadowsocks_method,
)
else:
continue
if self._swallow_errors:
continue
raise ProtocolNotSupportedError

outbounds = [outbound]
dialer_proxy = None
Expand Down Expand Up @@ -105,7 +114,7 @@ def tls_config(sni, fingerprint, alpn=None, ais=False):
@staticmethod
def reality_config(public_key, short_id, sni, fingerprint="", spiderx=""):

realitySettings = {
return {
"serverName": sni,
"fingerprint": fingerprint,
"show": False,
Expand All @@ -114,8 +123,6 @@ def reality_config(public_key, short_id, sni, fingerprint="", spiderx=""):
"spiderX": spiderx,
}

return realitySettings

@staticmethod
def ws_config(path=None, host=None, headers=None):
if headers is None:
Expand Down

0 comments on commit 6932603

Please sign in to comment.