Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
1212 lines (1028 loc) · 44 KB
/
Copy pathapp.py
File metadata and controls
1212 lines (1028 loc) · 44 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Windows Autounattend Generator — Flask backend."""
import json
import os
import re
import shutil
import subprocess
import threading
import xml.etree.ElementTree as ET
from datetime import datetime
from pathlib import Path
from xml.sax.saxutils import escape
from flask import Flask, jsonify, request, send_from_directory, Response, stream_with_context
app = Flask(__name__, static_folder='.', static_url_path='')
CONFIGS_DIR = Path(os.environ.get('CONFIGS_DIR', '/app/configs'))
CONFIGS_DIR.mkdir(parents=True, exist_ok=True)
OUTPUT_DIR = Path(os.environ.get('OUTPUT_DIR', './iso'))
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
XML_DIR = Path(os.environ.get('XML_DIR', './xml'))
XML_DIR.mkdir(parents=True, exist_ok=True)
DRIVERS_DIR = Path(os.environ.get('DRIVERS_DIR', '/app/drivers'))
DRIVERS_DIR.mkdir(parents=True, exist_ok=True)
BUILTIN_DRIVERS = Path('/app/vmdp-drivers-builtin')
IMAGES_DIR = Path(os.environ.get('IMAGES_DIR', '/app/images'))
IMAGES_DIR.mkdir(parents=True, exist_ok=True)
build_lock = threading.Lock()
def init_drivers():
"""Copy builtin VMDP drivers to DRIVERS_DIR/vmdp/ if not already present."""
vmdp_dest = DRIVERS_DIR / 'vmdp'
if BUILTIN_DRIVERS.is_dir() and any(BUILTIN_DRIVERS.iterdir()):
if not vmdp_dest.exists() or not any(vmdp_dest.iterdir()):
vmdp_dest.mkdir(parents=True, exist_ok=True)
for f in BUILTIN_DRIVERS.iterdir():
dest = vmdp_dest / f.name
if f.is_file():
shutil.copy2(f, dest)
elif f.is_dir():
shutil.copytree(f, dest, dirs_exist_ok=True)
print(f"Copied builtin VMDP drivers to {vmdp_dest}")
# Always ensure custom/ exists
(DRIVERS_DIR / 'custom').mkdir(exist_ok=True)
init_drivers()
# ──────────────────────────────────────────────────────────────
# Constants
# ──────────────────────────────────────────────────────────────
DEFAULTS = {
"hostname": "win2025-core",
"password": "",
"ip": "10.0.0.50",
"subnet": "255.255.255.0",
"gateway": "10.0.0.1",
"dns": "10.0.0.2",
"timezone": "Romance Standard Time",
"edition": "Windows Server 2025 Standard Evaluation",
"partition_scheme": "mbr",
"ui_language": "en-US",
"input_locale": "en-US",
"rdp_enabled": True,
"nla_enabled": True,
"ssh_enabled": True,
"ssh_default_shell": "powershell",
"winrm_enabled": True,
"icmp_enabled": False,
"firewall_ports": [],
"features": [],
"additional_users": [],
"organization": "Homelab",
"product_key": "",
"network_location": "Work",
"protect_your_pc": 3,
}
EDITIONS = [
"Windows Server 2025 Standard Evaluation",
"Windows Server 2025 Standard Evaluation (Desktop Experience)",
"Windows Server 2025 Datacenter Evaluation",
"Windows Server 2025 Datacenter Evaluation (Desktop Experience)",
]
TIMEZONES = [
{"value": "Romance Standard Time", "label": "Romance Standard Time (France, Belgique, Espagne)"},
{"value": "W. Europe Standard Time", "label": "W. Europe Standard Time (Allemagne, Italie, Suisse)"},
{"value": "GMT Standard Time", "label": "GMT Standard Time (Royaume-Uni, Irlande, Portugal)"},
{"value": "Central Europe Standard Time", "label": "Central Europe Standard Time (Pologne, Hongrie)"},
{"value": "Central European Standard Time", "label": "Central European Standard Time (Varsovie)"},
{"value": "E. Europe Standard Time", "label": "E. Europe Standard Time (Roumanie, Bulgarie)"},
{"value": "FLE Standard Time", "label": "FLE Standard Time (Finlande, Estonie, Lettonie)"},
{"value": "GTB Standard Time", "label": "GTB Standard Time (Grece)"},
{"value": "Russian Standard Time", "label": "Russian Standard Time (Moscou)"},
{"value": "UTC", "label": "UTC"},
{"value": "Eastern Standard Time", "label": "Eastern Standard Time (US East)"},
{"value": "Pacific Standard Time", "label": "Pacific Standard Time (US West)"},
]
LOCALES = [
{"value": "en-US", "label": "en-US (English, United States)"},
{"value": "fr-FR", "label": "fr-FR (Francais, France)"},
{"value": "de-DE", "label": "de-DE (Deutsch, Deutschland)"},
{"value": "es-ES", "label": "es-ES (Espanol, Espana)"},
{"value": "it-IT", "label": "it-IT (Italiano, Italia)"},
{"value": "pt-PT", "label": "pt-PT (Portugues, Portugal)"},
{"value": "nl-NL", "label": "nl-NL (Nederlands, Nederland)"},
{"value": "pl-PL", "label": "pl-PL (Polski, Polska)"},
{"value": "ru-RU", "label": "ru-RU (Russian)"},
{"value": "ja-JP", "label": "ja-JP (Japanese)"},
]
FEATURES = [
{"value": "Hyper-V", "label": "Hyper-V"},
{"value": "Web-Server", "label": "IIS Web Server"},
{"value": "DNS", "label": "DNS Server"},
{"value": "DHCP", "label": "DHCP Server"},
{"value": "AD-Domain-Services", "label": "Active Directory Domain Services"},
{"value": "Containers", "label": "Windows Containers"},
{"value": "Failover-Clustering", "label": "Failover Clustering"},
{"value": "FileAndStorage-Services", "label": "File and Storage Services"},
{"value": "NET-Framework-45-Core", "label": ".NET Framework 4.5"},
{"value": "Telnet-Client", "label": "Telnet Client"},
{"value": "SNMP-Service", "label": "SNMP Service"},
{"value": "BitLocker", "label": "BitLocker Drive Encryption"},
]
# Component XML attributes (reused everywhere)
COMP_ATTRS = (
'processorArchitecture="amd64" '
'publicKeyToken="31bf3856ad364e35" '
'language="neutral" '
'versionScope="nonSxS" '
'xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" '
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
)
NS = 'urn:schemas-microsoft-com:unattend'
# ──────────────────────────────────────────────────────────────
# XML Generation
# ──────────────────────────────────────────────────────────────
def generate_xml(c):
"""Generate a complete autounattend.xml from a config dict."""
# Merge defaults for missing keys
cfg = {**DEFAULTS, **c}
parts = []
parts.append('<?xml version="1.0" encoding="utf-8"?>')
parts.append(f'<unattend xmlns="urn:schemas-microsoft-com:unattend">')
parts.append('')
parts.append(_build_windows_pe(cfg))
parts.append('')
parts.append(_build_specialize(cfg))
parts.append('')
parts.append(_build_oobe(cfg))
parts.append('')
parts.append('</unattend>')
return '\n'.join(parts) + '\n'
def _build_windows_pe(c):
locale = escape(c['ui_language'])
input_loc = escape(c['input_locale'])
edition = escape(c['edition'])
org = escape(c.get('organization', 'Homelab'))
product_key = c.get('product_key', '').strip()
scheme = c.get('partition_scheme', 'mbr')
# Partition ID for Windows install target
win_partition = '3' if scheme == 'gpt' else '2'
product_key_xml = ''
if product_key:
product_key_xml = f'''
<ProductKey>
<Key>{escape(product_key)}</Key>
<WillShowUI>Never</WillShowUI>
</ProductKey>'''
disk_config = _build_disk_mbr() if scheme == 'mbr' else _build_disk_gpt()
return f''' <!-- ============================================================ -->
<!-- Pass 1: windowsPE - Disk partitioning, image selection -->
<!-- ============================================================ -->
<settings pass="windowsPE">
<component name="Microsoft-Windows-International-Core-WinPE"
{COMP_ATTRS}>
<SetupUILanguage>
<UILanguage>{locale}</UILanguage>
</SetupUILanguage>
<InputLocale>{input_loc}</InputLocale>
<SystemLocale>{locale}</SystemLocale>
<UILanguage>{locale}</UILanguage>
<UserLocale>{locale}</UserLocale>
</component>
<component name="Microsoft-Windows-Setup"
{COMP_ATTRS}>
<!-- SUSE VMDP VirtIO driver paths - scan all possible drive letters -->
<DriverPaths>
<PathAndCredentials wcm:action="add" wcm:keyValue="1">
<Path>D:\\win10-11-server22\\x64\\pvvx</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="2">
<Path>D:\\</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="3">
<Path>E:\\win10-11-server22\\x64\\pvvx</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="4">
<Path>E:\\</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="5">
<Path>F:\\win10-11-server22\\x64\\pvvx</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="6">
<Path>F:\\</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="7">
<Path>G:\\win10-11-server22\\x64\\pvvx</Path>
</PathAndCredentials>
<PathAndCredentials wcm:action="add" wcm:keyValue="8">
<Path>G:\\</Path>
</PathAndCredentials>
</DriverPaths>
{disk_config}
<!-- Image selection -->
<ImageInstall>
<OSImage>
<InstallFrom>
<MetaData wcm:action="add">
<Key>/IMAGE/NAME</Key>
<Value>{edition}</Value>
</MetaData>
</InstallFrom>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>{win_partition}</PartitionID>
</InstallTo>
</OSImage>
</ImageInstall>
<UserData>
<AcceptEula>true</AcceptEula>
<FullName>Administrator</FullName>
<Organization>{org}</Organization>{product_key_xml}
</UserData>
</component>
</settings>'''
def _build_disk_mbr():
return ''' <!-- Disk partitioning (BIOS/MBR) -->
<DiskConfiguration>
<Disk wcm:action="add">
<DiskID>0</DiskID>
<WillWipeDisk>true</WillWipeDisk>
<CreatePartitions>
<CreatePartition wcm:action="add">
<Order>1</Order>
<Type>Primary</Type>
<Size>500</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>2</Order>
<Type>Primary</Type>
<Extend>true</Extend>
</CreatePartition>
</CreatePartitions>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Order>1</Order>
<PartitionID>1</PartitionID>
<Label>System</Label>
<Format>NTFS</Format>
<Active>true</Active>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>2</Order>
<PartitionID>2</PartitionID>
<Label>Windows</Label>
<Format>NTFS</Format>
<Letter>C</Letter>
</ModifyPartition>
</ModifyPartitions>
</Disk>
</DiskConfiguration>'''
def _build_disk_gpt():
return ''' <!-- Disk partitioning (UEFI/GPT) -->
<DiskConfiguration>
<Disk wcm:action="add">
<DiskID>0</DiskID>
<WillWipeDisk>true</WillWipeDisk>
<CreatePartitions>
<CreatePartition wcm:action="add">
<Order>1</Order>
<Type>EFI</Type>
<Size>260</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>2</Order>
<Type>MSR</Type>
<Size>128</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Order>3</Order>
<Type>Primary</Type>
<Extend>true</Extend>
</CreatePartition>
</CreatePartitions>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Order>1</Order>
<PartitionID>1</PartitionID>
<Format>FAT32</Format>
<Label>EFI</Label>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>2</Order>
<PartitionID>2</PartitionID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>3</Order>
<PartitionID>3</PartitionID>
<Format>NTFS</Format>
<Label>Windows</Label>
<Letter>C</Letter>
</ModifyPartition>
</ModifyPartitions>
</Disk>
</DiskConfiguration>'''
def _build_specialize(c):
hostname = escape(c['hostname'][:15])
tz = escape(c['timezone'])
rdp = c.get('rdp_enabled', True)
nla = c.get('nla_enabled', True)
rdp_section = ''
if rdp:
rdp_section = f'''
<component name="Microsoft-Windows-TerminalServices-LocalSessionManager"
{COMP_ATTRS}>
<fDenyTSConnections>false</fDenyTSConnections>
</component>'''
nla_section = ''
if rdp and not nla:
nla_section = f'''
<component name="Microsoft-Windows-TerminalServices-RDP-WinStationExtensions"
{COMP_ATTRS}>
<UserAuthentication>0</UserAuthentication>
</component>'''
# Firewall groups
firewall_groups = []
if rdp:
firewall_groups.append(('RemoteDesktop', 'Remote Desktop'))
if c.get('winrm_enabled', False):
firewall_groups.append(('WinRM', 'Windows Remote Management'))
firewall_section = ''
if firewall_groups:
groups_xml = '\n'.join(
f''' <FirewallGroup wcm:action="add" wcm:keyValue="{key}">
<Active>true</Active>
<Group>{group}</Group>
<Profile>all</Profile>
</FirewallGroup>'''
for key, group in firewall_groups
)
firewall_section = f'''
<component name="Networking-MPSSVC-Svc"
{COMP_ATTRS}>
<FirewallGroups>
{groups_xml}
</FirewallGroups>
</component>'''
return f''' <!-- ============================================================ -->
<!-- Pass 2: specialize - Hostname, timezone, RDP -->
<!-- ============================================================ -->
<settings pass="specialize">
<component name="Microsoft-Windows-Shell-Setup"
{COMP_ATTRS}>
<ComputerName>{hostname}</ComputerName>
<TimeZone>{tz}</TimeZone>
</component>{rdp_section}{nla_section}{firewall_section}
</settings>'''
def _build_oobe(c):
password = escape(c['password'])
net_loc = escape(c.get('network_location', 'Work'))
pypc = int(c.get('protect_your_pc', 3))
# Additional users
users = c.get('additional_users', [])
local_accounts_xml = ''
if users:
accounts = []
for u in users[:5]:
uname = escape(u.get('name', ''))
upwd = escape(u.get('password', ''))
ugrp = escape(u.get('group', 'Users'))
if uname:
accounts.append(f''' <LocalAccount wcm:action="add">
<Name>{uname}</Name>
<DisplayName>{uname}</DisplayName>
<Group>{ugrp}</Group>
<Password>
<Value>{upwd}</Value>
<PlainText>true</PlainText>
</Password>
</LocalAccount>''')
if accounts:
local_accounts_xml = '\n <LocalAccounts>\n' + '\n'.join(accounts) + '\n </LocalAccounts>'
commands = _build_first_logon_commands(c)
return f''' <!-- ============================================================ -->
<!-- Pass 3: oobeSystem - Admin password, auto-config scripts -->
<!-- ============================================================ -->
<settings pass="oobeSystem">
<component name="Microsoft-Windows-Shell-Setup"
{COMP_ATTRS}>
<UserAccounts>
<AdministratorPassword>
<Value>{password}</Value>
<PlainText>true</PlainText>
</AdministratorPassword>{local_accounts_xml}
</UserAccounts>
<AutoLogon>
<Enabled>true</Enabled>
<Username>Administrator</Username>
<Password>
<Value>{password}</Value>
<PlainText>true</PlainText>
</Password>
<LogonCount>1</LogonCount>
</AutoLogon>
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideLocalAccountScreen>true</HideLocalAccountScreen>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>{net_loc}</NetworkLocation>
<ProtectYourPC>{pypc}</ProtectYourPC>
</OOBE>
{commands}
</component>
</settings>'''
def _build_first_logon_commands(c):
ip = escape(c['ip'])
subnet = escape(c['subnet'])
gateway = escape(c['gateway'])
dns = escape(c['dns'])
cmds = []
order = [0] # mutable counter
def add(cmd_line, desc):
order[0] += 1
cmds.append(f''' <!-- {order[0]}. {desc} -->
<SynchronousCommand wcm:action="add">
<Order>{order[0]}</Order>
<CommandLine>{escape(cmd_line)}</CommandLine>
<Description>{escape(desc)}</Description>
</SynchronousCommand>''')
# Always: static IP
add(
f'cmd /c netsh interface ip set address "Ethernet" static {ip} {subnet} {gateway}',
'Set static IP address'
)
# Always: DNS
add(
f'cmd /c netsh interface ip set dns "Ethernet" static {dns} primary',
'Set DNS server'
)
# Conditional: WinRM
if c.get('winrm_enabled', False):
add(
'powershell -Command "Enable-PSRemoting -Force; Set-Item WSMan:\\localhost\\Service\\AllowUnencrypted -Value $true; Set-Item WSMan:\\localhost\\Service\\Auth\\Basic -Value $true"',
'Enable WinRM'
)
# Conditional: SSH
if c.get('ssh_enabled', False):
add(
'powershell -Command "Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0; Start-Service sshd; Set-Service -Name sshd -StartupType Automatic"',
'Install OpenSSH Server'
)
# SSH default shell
shell = c.get('ssh_default_shell', 'powershell')
if shell == 'powershell':
shell_path = r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
else:
shell_path = r"C:\Windows\System32\cmd.exe"
add(
f"powershell -Command \"New-ItemProperty -Path 'HKLM:\\SOFTWARE\\OpenSSH' -Name DefaultShell -Value '{shell_path}' -PropertyType String -Force\"",
'Set SSH default shell'
)
# Conditional: Firewall rules
fw_rules = []
if c.get('ssh_enabled', False):
fw_rules.append("New-NetFirewallRule -DisplayName 'SSH' -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow")
if c.get('winrm_enabled', False):
fw_rules.append("New-NetFirewallRule -DisplayName 'WinRM HTTP' -Direction Inbound -Protocol TCP -LocalPort 5985 -Action Allow")
fw_rules.append("New-NetFirewallRule -DisplayName 'WinRM HTTPS' -Direction Inbound -Protocol TCP -LocalPort 5986 -Action Allow")
if c.get('icmp_enabled', False):
fw_rules.append("New-NetFirewallRule -DisplayName 'Allow ICMPv4' -Protocol ICMPv4 -IcmpType 8 -Action Allow")
for port_entry in c.get('firewall_ports', []):
port = str(port_entry.get('port', ''))
proto = port_entry.get('protocol', 'TCP').upper()
if port:
fw_rules.append(f"New-NetFirewallRule -DisplayName 'Custom {proto}/{port}' -Direction Inbound -Protocol {proto} -LocalPort {port} -Action Allow")
if fw_rules:
combined = '; '.join(fw_rules)
add(f'powershell -Command "{combined}"', 'Open firewall ports')
# Conditional: Windows features
for feat in c.get('features', []):
add(
f'powershell -Command "Install-WindowsFeature -Name {escape(feat)} -IncludeManagementTools"',
f'Install {feat}'
)
# Always last: disable AutoLogon
add(
'reg add "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f',
'Disable AutoLogon'
)
return ''' <FirstLogonCommands>
''' + '\n'.join(cmds) + '''
</FirstLogonCommands>'''
# ──────────────────────────────────────────────────────────────
# Validation
# ──────────────────────────────────────────────────────────────
def validate_config(c):
"""Validate config dict, return list of error strings."""
errors = []
hostname = c.get('hostname', '')
if not hostname:
errors.append('Hostname is required')
elif len(hostname) > 15:
errors.append(f'Hostname must be 15 characters or less (currently {len(hostname)})')
elif not re.match(r'^[a-zA-Z0-9-]+$', hostname):
errors.append('Hostname must contain only letters, digits, and hyphens')
if not c.get('password'):
errors.append('Password is required')
for field in ('ip', 'subnet', 'gateway', 'dns'):
val = c.get(field, '')
if not val:
errors.append(f'{field} is required')
elif not re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', val):
errors.append(f'{field} must be a valid IPv4 address')
return errors
def validate_xml(xml_string):
"""Validate XML string, return list of error dicts with line/severity/message."""
errors = []
# Level 1: XML syntax
try:
root = ET.fromstring(xml_string)
except ET.ParseError as e:
line = getattr(e, 'position', (0, 0))[0] if hasattr(e, 'position') else 0
errors.append({"line": line, "severity": "error", "message": f"XML syntax error: {e}"})
return errors
ns = {'u': NS}
# Level 2: required structure
pe = root.find('.//u:settings[@pass="windowsPE"]', ns)
if pe is None:
errors.append({"line": 0, "severity": "error", "message": "Missing <settings pass=\"windowsPE\">"})
else:
if pe.find('.//u:DiskConfiguration', ns) is None:
errors.append({"line": 0, "severity": "warning", "message": "Missing DiskConfiguration in windowsPE"})
if pe.find('.//u:ImageInstall', ns) is None:
errors.append({"line": 0, "severity": "warning", "message": "Missing ImageInstall in windowsPE"})
spec = root.find('.//u:settings[@pass="specialize"]', ns)
if spec is None:
errors.append({"line": 0, "severity": "error", "message": "Missing <settings pass=\"specialize\">"})
else:
cn = spec.find('.//u:ComputerName', ns)
if cn is None:
errors.append({"line": 0, "severity": "warning", "message": "Missing ComputerName in specialize"})
elif cn.text and len(cn.text) > 15:
errors.append({"line": 0, "severity": "error", "message": f"ComputerName exceeds 15 characters: {cn.text}"})
oobe = root.find('.//u:settings[@pass="oobeSystem"]', ns)
if oobe is None:
errors.append({"line": 0, "severity": "error", "message": "Missing <settings pass=\"oobeSystem\">"})
else:
if oobe.find('.//u:AdministratorPassword', ns) is None:
errors.append({"line": 0, "severity": "warning", "message": "Missing AdministratorPassword in oobeSystem"})
# Check FirstLogonCommands order
flc = root.find('.//u:FirstLogonCommands', ns)
if flc is not None:
orders = []
for cmd in flc.findall('.//u:SynchronousCommand', ns):
o = cmd.find('u:Order', ns)
if o is not None and o.text:
orders.append(int(o.text))
if orders:
if len(orders) != len(set(orders)):
errors.append({"line": 0, "severity": "error", "message": "Duplicate Order values in FirstLogonCommands"})
expected = list(range(1, len(orders) + 1))
if sorted(orders) != expected:
errors.append({"line": 0, "severity": "warning", "message": "Order values in FirstLogonCommands are not sequential"})
return errors
# ──────────────────────────────────────────────────────────────
# XML Parsing (import existing XML)
# ──────────────────────────────────────────────────────────────
def parse_xml(xml_string):
"""Parse an autounattend.xml and extract a config dict."""
cfg = dict(DEFAULTS)
try:
root = ET.fromstring(xml_string)
except ET.ParseError:
return cfg
ns = {'u': NS, 'wcm': 'http://schemas.microsoft.com/WMIConfig/2002/State'}
def find_text(parent, path, default=''):
el = parent.find(path, ns)
return el.text.strip() if el is not None and el.text else default
# windowsPE
pe = root.find('.//u:settings[@pass="windowsPE"]', ns)
if pe is not None:
cfg['ui_language'] = find_text(pe, './/u:UILanguage', cfg['ui_language'])
cfg['input_locale'] = find_text(pe, './/u:InputLocale', cfg['input_locale'])
# Edition
meta_val = find_text(pe, './/u:ImageInstall//u:Value')
if meta_val:
cfg['edition'] = meta_val
# Partition scheme detection
disk = pe.find('.//u:DiskConfiguration', ns)
if disk is not None:
efi = disk.find('.//u:CreatePartition/[u:Type="EFI"]', ns)
if efi is not None:
cfg['partition_scheme'] = 'gpt'
else:
cfg['partition_scheme'] = 'mbr'
# Product key
pk = find_text(pe, './/u:ProductKey/u:Key')
if pk:
cfg['product_key'] = pk
# Organization
org = find_text(pe, './/u:Organization')
if org:
cfg['organization'] = org
# specialize
spec = root.find('.//u:settings[@pass="specialize"]', ns)
if spec is not None:
cfg['hostname'] = find_text(spec, './/u:ComputerName', cfg['hostname'])
cfg['timezone'] = find_text(spec, './/u:TimeZone', cfg['timezone'])
# RDP
deny = find_text(spec, './/u:fDenyTSConnections')
if deny:
cfg['rdp_enabled'] = deny.lower() == 'false'
# NLA
nla = find_text(spec, './/u:UserAuthentication')
if nla:
cfg['nla_enabled'] = nla != '0'
# oobeSystem
oobe = root.find('.//u:settings[@pass="oobeSystem"]', ns)
if oobe is not None:
cfg['password'] = find_text(oobe, './/u:AdministratorPassword/u:Value', cfg['password'])
net_loc = find_text(oobe, './/u:NetworkLocation')
if net_loc:
cfg['network_location'] = net_loc
pypc = find_text(oobe, './/u:ProtectYourPC')
if pypc:
try:
cfg['protect_your_pc'] = int(pypc)
except ValueError:
pass
# Parse FirstLogonCommands
cfg['ssh_enabled'] = False
cfg['winrm_enabled'] = False
cfg['icmp_enabled'] = False
cfg['features'] = []
for cmd_el in oobe.findall('.//u:SynchronousCommand', ns):
cmd_line = find_text(cmd_el, 'u:CommandLine', '')
# IP / subnet / gateway
m = re.search(r'set address "Ethernet" static (\S+) (\S+) (\S+)', cmd_line)
if m:
cfg['ip'] = m.group(1)
cfg['subnet'] = m.group(2)
cfg['gateway'] = m.group(3)
# DNS
m = re.search(r'set dns "Ethernet" static (\S+)', cmd_line)
if m:
cfg['dns'] = m.group(1)
# SSH detection
if 'OpenSSH.Server' in cmd_line:
cfg['ssh_enabled'] = True
if 'DefaultShell' in cmd_line:
if 'powershell.exe' in cmd_line.lower():
cfg['ssh_default_shell'] = 'powershell'
else:
cfg['ssh_default_shell'] = 'cmd'
# WinRM detection
if 'Enable-PSRemoting' in cmd_line:
cfg['winrm_enabled'] = True
# ICMP detection
if 'ICMPv4' in cmd_line:
cfg['icmp_enabled'] = True
# Features
m = re.search(r'Install-WindowsFeature\s+-Name\s+(\S+)', cmd_line)
if m:
cfg['features'].append(m.group(1))
# Additional users
cfg['additional_users'] = []
for acct in oobe.findall('.//u:LocalAccounts/u:LocalAccount', ns):
name = find_text(acct, 'u:Name')
pwd = find_text(acct, 'u:Password/u:Value')
grp = find_text(acct, 'u:Group', 'Users')
if name:
cfg['additional_users'].append({'name': name, 'password': pwd, 'group': grp})
return cfg
# ──────────────────────────────────────────────────────────────
# Config storage helpers
# ──────────────────────────────────────────────────────────────
def client_timestamp(config):
"""Use browser-provided timestamp if valid, else fall back to server time."""
ts = config.get('client_timestamp', '')
if ts and re.match(r'^\d{4}-\d{2}-\d{2}_\d{2}h\d{2}$', ts):
return ts
return datetime.now().strftime('%Y-%m-%d_%Hh%M')
def slugify(name):
s = re.sub(r'[^a-zA-Z0-9]+', '-', name.strip().lower())
s = s.strip('-')
return s[:50] if s else 'unnamed'
def list_configs():
configs = []
for f in sorted(CONFIGS_DIR.glob('*.json')):
try:
data = json.loads(f.read_text())
configs.append({
'name': data.get('name', f.stem),
'slug': f.stem,
'updated': data.get('updated', ''),
})
except (json.JSONDecodeError, OSError):
pass
return configs
# ──────────────────────────────────────────────────────────────
# Routes
# ──────────────────────────────────────────────────────────────
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/api/defaults')
def api_defaults():
return jsonify({
'defaults': DEFAULTS,
'editions': EDITIONS,
'timezones': TIMEZONES,
'locales': LOCALES,
'features': FEATURES,
'driver_sources': _list_driver_sources(),
})
@app.route('/api/generate', methods=['POST'])
def api_generate():
config = request.json or {}
errors = validate_config(config)
if errors:
return jsonify({'errors': errors}), 400
xml = generate_xml(config)
return jsonify({'xml': xml})
@app.route('/api/download', methods=['POST'])
def api_download():
config = request.json or {}
errors = validate_config(config)
if errors:
return jsonify({'errors': errors}), 400
xml = generate_xml(config)
hostname = config.get('hostname', 'autounattend')[:15]
timestamp = client_timestamp(config)
xml_filename = f"autounattend-{hostname}-{timestamp}.xml"
(XML_DIR / xml_filename).write_text(xml)
return jsonify({'xml': xml, 'filename': xml_filename})
@app.route('/api/build-iso', methods=['POST'])
def api_build_iso():
config = request.json or {}
errors = validate_config(config)
if errors:
return jsonify({'errors': errors}), 400
if not build_lock.acquire(blocking=False):
return jsonify({'error': 'A build is already in progress'}), 409
xml = generate_xml(config)
hostname = config.get('hostname', 'autounattend')[:15]
timestamp = client_timestamp(config)
iso_filename = f"autounattend-{hostname}-{timestamp}.iso"
xml_filename = f"autounattend-{hostname}-{timestamp}.xml"
# Build driver paths from selected sources
driver_sources = config.get('driver_sources', [])
driver_paths = []
for src in driver_sources:
src_dir = DRIVERS_DIR / src
if src_dir.is_dir() and any(src_dir.iterdir()):
driver_paths.append(str(src_dir))
driver_paths_arg = ':'.join(driver_paths) if driver_paths else ''
def generate():
try:
yield f"data: === Starting ISO build\n\n"
yield f"data: Output directory: {OUTPUT_DIR.resolve()}\n\n"
# Save XML to xml/ directory
xml_path = XML_DIR / xml_filename
xml_path.write_text(xml)
yield f"data: XML saved as {xml_filename}\n\n"
yield f"data: __XML_FILE__:{xml_filename}\n\n"
output_path = OUTPUT_DIR / iso_filename
script = str(Path(__file__).parent / 'build-iso-from-xml.sh')
cmd = [script, str(xml_path), str(output_path)]
if driver_paths_arg:
cmd.append(driver_paths_arg)
yield f"data: Driver sources: {', '.join(driver_sources)}\n\n"
yield f"data: === Running: {script}\n\n"
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True,
)
for line in proc.stdout:
yield f"data: {line.rstrip()}\n\n"
proc.wait()
if proc.returncode == 0:
yield f"data: === ISO build complete\n\n"
yield f"data: __ISO_FILE__:{iso_filename}\n\n"
yield f"data: __DONE_OK__\n\n"
else:
yield f"data: === Build failed (exit code {proc.returncode})\n\n"
yield f"data: __DONE_FAIL__\n\n"
except Exception as e:
yield f"data: === ERROR: {e}\n\n"
yield f"data: __DONE_FAIL__\n\n"
finally:
build_lock.release()
return Response(stream_with_context(generate()), mimetype='text/event-stream')
@app.route('/api/download-iso/<filename>')
def api_download_iso(filename):
# Sanitize: only allow expected filename pattern
if not re.match(r'^autounattend-[\w-]+\.iso$', filename):
return jsonify({'error': 'Invalid filename'}), 400
filepath = OUTPUT_DIR / filename
if not filepath.exists():
return jsonify({'error': 'ISO not found'}), 404
return send_from_directory(str(OUTPUT_DIR), filename,
mimetype='application/octet-stream',
as_attachment=True)
@app.route('/api/parse', methods=['POST'])
def api_parse():
if request.content_type and 'multipart' in request.content_type:
f = request.files.get('file')
if not f:
return jsonify({'error': 'No file uploaded'}), 400
xml_string = f.read().decode('utf-8', errors='replace')
else:
data = request.json or {}
xml_string = data.get('xml', '')
if not xml_string:
return jsonify({'error': 'No XML content'}), 400
config = parse_xml(xml_string)
return jsonify({'config': config})
@app.route('/api/validate', methods=['POST'])
def api_validate():
data = request.json or {}
xml_string = data.get('xml', '')
if not xml_string:
return jsonify({'error': 'No XML content'}), 400
errors = validate_xml(xml_string)
return jsonify({'errors': errors})
@app.route('/api/configs', methods=['GET'])
def api_list_configs():
return jsonify({'configs': list_configs()})
@app.route('/api/configs', methods=['POST'])
def api_save_config():
data = request.json or {}
name = data.get('name', '').strip()
config = data.get('config')
if not name or not config:
return jsonify({'error': 'Name and config required'}), 400
slug = slugify(name)
now = datetime.now().isoformat(timespec='seconds')
filepath = CONFIGS_DIR / f'{slug}.json'
payload = {
'name': name,
'config': config,
'created': now,
'updated': now,
}
# Preserve original creation date if file exists
if filepath.exists():
try: