-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_pairing_code.py
More file actions
executable file
·224 lines (183 loc) · 6.94 KB
/
generate_pairing_code.py
File metadata and controls
executable file
·224 lines (183 loc) · 6.94 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
"""
Generate a pairing code for Open Claw Gateway to use with iOSLobster.
Usage:
python3 generate_pairing_code.py <tailscale-address> [options]
Examples:
# HTTPS with fingerprint (standard format)
python3 generate_pairing_code.py 100.64.1.2 --scheme https --fingerprint "SHA256:abc123..."
# HTTP/WS (simplified format with token)
python3 generate_pairing_code.py my-gateway.tailscale.ts.net --scheme ws --token "your-pairing-token"
# HTTPS/WSS (simplified format with token)
python3 generate_pairing_code.py 100.64.1.2 --scheme wss --token "your-pairing-token"
"""
import json
import base64
import argparse
import secrets
import sys
from urllib.parse import urlparse
def generate_pairing_code(
endpoint_url: str,
fingerprint: str = None,
challenge: str = None,
token: str = None,
gateway_name: str = None,
version: int = 1
) -> str:
"""
Generate a pairing code in openclaw://pair format.
Args:
endpoint_url: Full WebSocket URL (e.g., wss://100.64.1.2/ws or ws://gateway/ws)
fingerprint: SHA256 fingerprint (for HTTPS/WSS standard format)
challenge: Challenge string (for standard format)
token: Pairing token (for simplified format - used as both fingerprint and challenge)
gateway_name: Optional gateway name
version: Protocol version (default: 1)
Returns:
Pairing code string in openclaw://pair?data=... format
"""
# Parse the endpoint URL
parsed = urlparse(endpoint_url)
# Ensure it's a WebSocket URL
if parsed.scheme not in ['ws', 'wss']:
raise ValueError(f"Endpoint must be a WebSocket URL (ws:// or wss://), got: {endpoint_url}")
# Build the endpoint URL
endpoint = endpoint_url
# Determine format: simplified (token) or standard (fingerprint + challenge)
if token:
# Simplified format: token is used as both fingerprint and challenge
fingerprint = token
challenge = token
elif not fingerprint or not challenge:
raise ValueError("Either provide --token (simplified format) or both --fingerprint and --challenge (standard format)")
# Create the payload
payload = {
"endpoint": endpoint,
"fingerprint": fingerprint,
"challenge": challenge,
"version": version
}
if gateway_name:
payload["gateway_name"] = gateway_name
# Encode as JSON
json_data = json.dumps(payload, sort_keys=True, separators=(',', ':'))
# Base64 encode
base64_data = base64.b64encode(json_data.encode('utf-8')).decode('utf-8')
# Generate the pairing code
pairing_code = f"openclaw://pair?data={base64_data}"
return pairing_code
def main():
parser = argparse.ArgumentParser(
description="Generate a pairing code for Open Claw Gateway",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# HTTPS with fingerprint (standard format)
%(prog)s 100.64.1.2 --scheme https --fingerprint "SHA256:abc123..." --challenge "random123"
# HTTP/WS (simplified format with token)
%(prog)s my-gateway.tailscale.ts.net --scheme ws --token "your-pairing-token"
# HTTPS/WSS (simplified format with token)
%(prog)s 100.64.1.2 --scheme wss --token "your-pairing-token" --name "My Gateway"
"""
)
parser.add_argument(
'address',
help='Tailscale IP address or hostname (e.g., 100.64.1.2 or my-gateway.tailscale.ts.net)'
)
parser.add_argument(
'--scheme',
choices=['ws', 'wss', 'http', 'https'],
default='wss',
help='URL scheme (default: wss). For WebSocket, use ws/wss. For HTTP pairing endpoint, use http/https.'
)
parser.add_argument(
'--port',
type=int,
help='Port number (default: 443 for wss/https, 80 for ws/http)'
)
parser.add_argument(
'--path',
default='/ws',
help='WebSocket path (default: /ws)'
)
parser.add_argument(
'--fingerprint',
help='SHA256 fingerprint (for standard format). Format: SHA256:... or hex string'
)
parser.add_argument(
'--challenge',
help='Challenge string (for standard format). If not provided and using standard format, a random one will be generated.'
)
parser.add_argument(
'--token',
help='Pairing token (for simplified format). Used as both fingerprint and challenge.'
)
parser.add_argument(
'--name',
dest='gateway_name',
help='Gateway name (optional)'
)
parser.add_argument(
'--version',
type=int,
default=1,
help='Protocol version (default: 1)'
)
parser.add_argument(
'--output-json',
action='store_true',
help='Output as JSON instead of openclaw:// URL'
)
args = parser.parse_args()
# Determine port
if args.port:
port = args.port
elif args.scheme in ['wss', 'https']:
port = 443
else:
port = 80
# Build WebSocket URL
if port in [443, 80] and ((args.scheme == 'wss' and port == 443) or (args.scheme == 'ws' and port == 80)):
# Omit port for default ports
endpoint_url = f"{args.scheme}://{args.address}{args.path}"
else:
endpoint_url = f"{args.scheme}://{args.address}:{port}{args.path}"
# Generate challenge if needed (standard format without challenge)
challenge = args.challenge
if not args.token and not challenge:
# Generate a random challenge
challenge = secrets.token_urlsafe(32)
print(f"Generated random challenge: {challenge}", file=sys.stderr)
try:
if args.output_json:
# Output as JSON
payload = {
"endpoint": endpoint_url,
"fingerprint": args.token or args.fingerprint or "",
"challenge": args.token or challenge or "",
"version": args.version
}
if args.gateway_name:
payload["gateway_name"] = args.gateway_name
print(json.dumps(payload, indent=2))
else:
# Generate pairing code
pairing_code = generate_pairing_code(
endpoint_url=endpoint_url,
fingerprint=args.fingerprint,
challenge=challenge,
token=args.token,
gateway_name=args.gateway_name,
version=args.version
)
print(pairing_code)
# Also print QR code-friendly format
print("\n# To generate a QR code, you can use:", file=sys.stderr)
print(f"# qrencode '{pairing_code}' -o pairing.png", file=sys.stderr)
print(f"# Or use an online QR code generator with the URL above", file=sys.stderr)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()