-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvmess_to_singbox.py
More file actions
158 lines (131 loc) · 4.29 KB
/
Copy pathvmess_to_singbox.py
File metadata and controls
158 lines (131 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
import argparse
import base64
import json
import sys
from pathlib import Path
from typing import Dict, Any
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Convert vmess:// URL to sing-box format JSON"
)
parser.add_argument(
"vmess_url", type=str, help="The vmess:// URL string to convert"
)
parser.add_argument(
"-o", "--output", type=Path, help="Output file path (default: stdout)"
)
parser.add_argument(
"-p", "--pretty", action="store_true", help="Pretty print JSON output"
)
parser.add_argument(
"-t",
"--tag",
type=str,
default="vmess-proxy",
help="Tag name for the proxy (default: vmess-proxy)",
)
return parser.parse_args()
def decode_vmess_url(vmess_url: str) -> Dict[str, Any]:
"""
Decode vmess:// URL to extract configuration parameters.
vmess:// URLs are base64 encoded JSON objects.
"""
if not vmess_url.startswith("vmess://"):
raise ValueError("Invalid vmess URL: must start with 'vmess://'")
# Remove vmess:// prefix and decode base64
encoded_data = vmess_url[8:]
try:
decoded_bytes = base64.b64decode(encoded_data)
decoded_str = decoded_bytes.decode("utf-8")
vmess_config = json.loads(decoded_str)
except Exception as e:
raise ValueError(f"Failed to decode vmess URL: {e}")
return vmess_config
def vmess_to_singbox(
vmess_config: Dict[str, Any], tag: str = "vmess-proxy"
) -> Dict[str, Any]:
"""
Convert vmess configuration to sing-box outbound format.
"""
# Extract vmess parameters
server = vmess_config.get("add", "")
port = int(vmess_config.get("port", 443))
uuid = vmess_config.get("id", "")
alter_id = int(vmess_config.get("aid", 0))
security = vmess_config.get("scy", "auto")
network = vmess_config.get("net", "tcp")
tls = vmess_config.get("tls", "")
host = vmess_config.get("host", "")
path = vmess_config.get("path", "")
sni = vmess_config.get("sni", "")
alpn = vmess_config.get("alpn", "")
# Build sing-box outbound configuration
outbound = {
"type": "vmess",
"tag": tag,
"server": server,
"server_port": port,
"uuid": uuid,
"security": security,
"alter_id": alter_id,
}
# Add transport configuration based on network type
transport = {}
if network == "ws":
transport = {
"type": "ws",
"path": path if path else "/",
}
if host:
transport["headers"] = {"Host": host}
elif network == "h2" or network == "http":
transport = {
"type": "http",
"path": path if path else "/",
}
if host:
transport["host"] = [host]
elif network == "grpc":
transport = {"type": "grpc", "service_name": path if path else ""}
elif network == "quic":
transport = {"type": "quic"}
if host:
transport["host"] = host
if transport:
outbound["transport"] = transport
# Add TLS configuration
if tls == "tls":
tls_config = {"enabled": True, "insecure": False}
if sni:
tls_config["server_name"] = sni
elif host:
tls_config["server_name"] = host
if alpn:
tls_config["alpn"] = alpn.split(",") if "," in alpn else [alpn]
outbound["tls"] = tls_config
return outbound
def main() -> None:
args = get_args()
try:
# Decode vmess URL
vmess_config = decode_vmess_url(args.vmess_url)
# Convert to sing-box format
singbox_outbound = vmess_to_singbox(vmess_config, args.tag)
# Format JSON output
if args.pretty:
json_output = json.dumps(singbox_outbound, indent=2, ensure_ascii=False)
else:
json_output = json.dumps(singbox_outbound, ensure_ascii=False)
# Output result
if args.output:
with args.output.open("w", encoding="utf-8") as f:
f.write(json_output)
print(f"Output written to: {args.output}")
else:
print(json_output)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()