Skip to content

Commit 4e75d5f

Browse files
author
neil
committed
support vnc link file
1 parent 99d006c commit 4e75d5f

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

anyvm.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ def strip_ansi(text):
20902090
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
20912091
return ansi_escape.sub('', text)
20922092

2093-
def start_vnc_web_proxy(vnc_port, web_port, vm_info="", qemu_pid=None, audio_enabled=False, qmon_port=None, error_log_path=None, is_console_vnc=False, listen_addr='127.0.0.1', remote_vnc=False, debug=False):
2093+
def start_vnc_web_proxy(vnc_port, web_port, vm_info="", qemu_pid=None, audio_enabled=False, qmon_port=None, error_log_path=None, is_console_vnc=False, listen_addr='127.0.0.1', remote_vnc=False, debug=False, remote_vnc_link_file=None):
20942094
# Handle termination signals for immediate cleanup
20952095
def signal_handler(sig, frame):
20962096
sys.exit(0)
@@ -2101,8 +2101,7 @@ def signal_handler(sig, frame):
21012101
signal.signal(signal.SIGINT, signal_handler)
21022102
if error_log_path:
21032103
try:
2104-
# Clean up old remote file if it exists
2105-
remote_file = error_log_path.replace(".vncproxy.log", ".remote")
2104+
remote_file = remote_vnc_link_file if remote_vnc_link_file else error_log_path.replace(".vncproxy.log", ".remote")
21062105
if os.path.exists(remote_file):
21072106
os.remove(remote_file)
21082107

@@ -2189,6 +2188,13 @@ def tunnel_manager():
21892188
with open(error_log_path, 'a') as f:
21902189
f.write("[VNCProxy] " + log_msg + "\n")
21912190
except: pass
2191+
2192+
# Pre-cleanup of the link file if it was specified
2193+
if remote_vnc_link_file:
2194+
try:
2195+
if os.path.exists(remote_vnc_link_file):
2196+
os.remove(remote_vnc_link_file)
2197+
except: pass
21922198

21932199
try:
21942200
kwargs = {
@@ -2224,7 +2230,7 @@ def monitor():
22242230
with open(error_log_path, 'a') as f:
22252231
f.write("[VNCProxy] " + msg + "\n")
22262232
# Write URL to .remote file
2227-
remote_file = error_log_path.replace(".vncproxy.log", ".remote")
2233+
remote_file = remote_vnc_link_file if remote_vnc_link_file else error_log_path.replace(".vncproxy.log", ".remote")
22282234
with open(remote_file, 'w') as f:
22292235
f.write(found_url[0] + "\n")
22302236
except: pass
@@ -2390,6 +2396,7 @@ def print_usage():
23902396
Usage: --remote-vnc (auto), --remote-vnc cf, --remote-vnc lhr, --remote-vnc pinggy.
23912397
Enabled by default if no local browser is detected (e.g., in Cloud Shell).
23922398
Use "--remote-vnc no" to disable.
2399+
--remote-vnc-link-file Specify a file to write the remote VNC link to (instead of the default .remote file).
23932400
--vga <type> VGA device type (e.g., virtio, std, virtio-gpu). Default: virtio (std for NetBSD).
23942401
--res, --resolution Set initial screen resolution (e.g., 1280x800). Default: 1280x800.
23952402
--mon <port> QEMU monitor telnet port (localhost).
@@ -3448,7 +3455,8 @@ def main():
34483455
# Correctly parse string values like 'True', 'cf', 'lhr'
34493456
remote_vnc = remote_vnc_val if remote_vnc_val not in ['0', 'False', 'false', None] else False
34503457
debug_vnc = sys.argv[12] == '1' if len(sys.argv) > 12 else False
3451-
start_vnc_web_proxy(vnc_port, web_port, vm_info, qemu_pid, audio_enabled, qmon_port, error_log_path, is_console_vnc, listen_addr=listen_addr, remote_vnc=remote_vnc, debug=debug_vnc)
3458+
link_file = sys.argv[13] if len(sys.argv) > 13 and sys.argv[13] != '0' else None
3459+
start_vnc_web_proxy(vnc_port, web_port, vm_info, qemu_pid, audio_enabled, qmon_port, error_log_path, is_console_vnc, listen_addr=listen_addr, remote_vnc=remote_vnc, debug=debug_vnc, remote_vnc_link_file=link_file)
34523460
except Exception as e:
34533461
# If we have an error log path, try to write to it even if startup fails
34543462
try:
@@ -3500,7 +3508,8 @@ def main():
35003508
'public_ssh': False,
35013509
'accept_vm_ssh': False,
35023510
'remote_vnc': None,
3503-
'remote_vnc_is_default': False
3511+
'remote_vnc_is_default': False,
3512+
'remote_vnc_link_file': None
35043513
}
35053514

35063515
ssh_passthrough = []
@@ -3625,11 +3634,9 @@ def main():
36253634
i += 1
36263635
else:
36273636
config['remote_vnc'] = True
3628-
# No i += 1 here as we already incremented if value present,
3629-
# and the while loop does a global i += 1 at the end.
3630-
# However, the current structure has 'i += 1' inside individual elifs or at the bottom.
3631-
# In ANYVM, most elifs have i += 1, and there is an 'i += 1' at the end of the loop (line 3510).
3632-
# So if we consume an extra arg, we i += 1. If not, we don't.
3637+
elif arg == "--remote-vnc-link-file":
3638+
config['remote_vnc_link_file'] = os.path.abspath(args[i+1])
3639+
i += 1
36333640
elif arg == "--accept-vm-ssh":
36343641
config['accept_vm_ssh'] = True
36353642
elif arg == "--whpx":
@@ -4615,7 +4622,8 @@ def start_vnc_proxy_for_pid(qemu_pid):
46154622
'1' if is_vnc_console else '0',
46164623
'0.0.0.0' if (config['public'] or config['public_vnc']) else '127.0.0.1',
46174624
str(config['remote_vnc']) if config['remote_vnc'] else '0',
4618-
'1' if config['debug'] else '0'
4625+
'1' if config['debug'] else '0',
4626+
str(config['remote_vnc_link_file']) if config['remote_vnc_link_file'] else '0'
46194627
]
46204628
popen_kwargs = {}
46214629
if IS_WINDOWS:
@@ -4650,7 +4658,7 @@ def start_vnc_proxy_for_pid(qemu_pid):
46504658
try:
46514659
if os.path.exists(vnc_log_path):
46524660
os.remove(vnc_log_path)
4653-
remote_file = vnc_log_path.replace(".vncproxy.log", ".remote")
4661+
remote_file = config['remote_vnc_link_file'] if config['remote_vnc_link_file'] else vnc_log_path.replace(".vncproxy.log", ".remote")
46544662
if os.path.exists(remote_file):
46554663
os.remove(remote_file)
46564664
except:

0 commit comments

Comments
 (0)