1313using System . Threading . Tasks ;
1414using System . Security . Principal ;
1515using System . Management . Automation ;
16+ using System . Net . NetworkInformation ;
1617
1718namespace CustomActions
1819{
1920 public class CustomActions
2021 {
2122 public static Session Session ;
22-
23+ public static string FixACL = @"$out = icacls ""$dirrectory"" /verify /t /q;
24+ foreach($line in $out)
25+ {
26+ if ($line -match '(.:[^:]*): (.*)')
27+ {
28+ $path = $Matches[1];
29+ Set-Acl $path(Get-Acl $path);
30+ }
31+ }" ;
32+
33+
2334 [ CustomAction ]
2435 public static ActionResult GetAPIsListCA ( Session session )
2536 {
@@ -89,7 +100,7 @@ public static ActionResult TryFindConfigs(Session session)
89100 return ActionResult . Success ;
90101 }
91102
92- var tmp = Path . Combine ( Path . GetTempPath ( ) , "MicrotingTemp" ) ;
103+ var tmp = Path . Combine ( "c: \\ " , "MicrotingTemp" ) ;
93104 Directory . CreateDirectory ( tmp ) ;
94105 Directory . CreateDirectory ( Path . Combine ( tmp , "files" ) ) ;
95106 Directory . CreateDirectory ( Path . Combine ( tmp , "dirs" ) ) ;
@@ -245,7 +256,7 @@ private static void HandlePreviousConfigs(Session session, string installFolder)
245256 {
246257 var keepFiles = session . CustomActionData [ "KEEPFILES" ] . Split ( ',' ) ;
247258 var keepFolders = session . CustomActionData [ "KEEPFOLDERS" ] . Split ( ',' ) ;
248- var tmpConfigs = Path . Combine ( Path . GetTempPath ( ) , "MicrotingTemp" ) ;
259+ var tmpConfigs = Path . Combine ( "c: \\ " , "MicrotingTemp" ) ;
249260
250261 foreach ( var keepFolder in keepFolders )
251262 {
@@ -306,6 +317,9 @@ public static ActionResult UpdateCA(Session session)
306317 RunProcess ( @"C:\Program Files\nodejs\node.exe" , "svc.js uninstall" , uiIisDir ) ;
307318 IncrementProgressBar ( session ) ;
308319
320+ DeleteDirectory ( Path . Combine ( uiIisDir , "node_modules" ) ) ;
321+ DeleteDirectory ( Path . Combine ( uiIisDir , "dist" ) ) ;
322+
309323 session . Log ( "Set proper names to folders" ) ;
310324
311325 DirectoryCopy ( apiTemp , webApiLocation ) ;
@@ -490,24 +504,28 @@ public static void CongigureSecurity(string folder)
490504 {
491505 using ( var powershell = PowerShell . Create ( ) )
492506 {
493- powershell . AddScript ( $ "$path = \" { folder } \" ; $acl = Get-Acl $path; Set-Acl $path $acl;") ;
507+ powershell . AddScript ( FixACL . Replace ( "$dirrectory" , folder ) ) ;
508+ powershell . Invoke ( ) ;
494509
495510 var dInfo = new DirectoryInfo ( folder ) ;
496511
497512 var dSecurity = dInfo . GetAccessControl ( ) ;
498513
499514 dSecurity . AddAccessRule ( new FileSystemAccessRule ( "IUSR" , FileSystemRights . FullControl , InheritanceFlags . ContainerInherit | InheritanceFlags . ObjectInherit , PropagationFlags . None , AccessControlType . Allow ) ) ;
515+ powershell . AddScript ( FixACL . Replace ( "$dirrectory" , folder ) ) ;
516+ powershell . Invoke ( ) ;
517+
500518 dSecurity . AddAccessRule ( new FileSystemAccessRule ( "IIS_IUSRS" , FileSystemRights . FullControl , InheritanceFlags . ContainerInherit | InheritanceFlags . ObjectInherit , PropagationFlags . None , AccessControlType . Allow ) ) ;
519+ powershell . AddScript ( FixACL . Replace ( "$dirrectory" , folder ) ) ;
520+ powershell . Invoke ( ) ;
501521
502522 dInfo . SetAccessControl ( dSecurity ) ;
503523
504- ReplaceAllDescendantPermissionsFromObject ( dInfo , dSecurity , powershell ) ;
505-
506- powershell . Invoke ( ) ;
524+ ReplaceAllDescendantPermissionsFromObject ( dInfo , dSecurity ) ;
507525 }
508526 }
509527
510- private static void ReplaceAllDescendantPermissionsFromObject ( DirectoryInfo dInfo , DirectorySecurity dSecurity , PowerShell powerShell )
528+ private static void ReplaceAllDescendantPermissionsFromObject ( DirectoryInfo dInfo , DirectorySecurity dSecurity )
511529 {
512530 dInfo . SetAccessControl ( dSecurity ) ;
513531
@@ -520,9 +538,7 @@ private static void ReplaceAllDescendantPermissionsFromObject(DirectoryInfo dInf
520538 fi . SetAccessControl ( ac ) ;
521539 }
522540
523- powerShell . AddScript ( $ "$path = \" { dInfo . FullName } \" ; $acl = Get-Acl $path; Set-Acl $path $acl;") ;
524-
525- dInfo . GetDirectories ( ) . ToList ( ) . ForEach ( d => ReplaceAllDescendantPermissionsFromObject ( d , dSecurity , powerShell ) ) ;
541+ dInfo . GetDirectories ( ) . ToList ( ) . ForEach ( d => ReplaceAllDescendantPermissionsFromObject ( d , dSecurity ) ) ;
526542 }
527543
528544 private static void BuildAngularApp ( string appLocation )
@@ -671,8 +687,32 @@ private static int GetWebApiPort()
671687 using ( var serverManager = new ServerManager ( ) )
672688 {
673689 var usedPorts = serverManager . Sites . Where ( t => t . Bindings . First ( ) . EndPoint . Port >= 5000 ) . Select ( t => t . Bindings . First ( ) . EndPoint . Port ) ;
674- return usedPorts . Any ( ) ? usedPorts . Max ( ) + 1 : 5000 ;
690+ var port = usedPorts . Any ( ) ? usedPorts . Max ( ) + 1 : 5000 ;
691+
692+ while ( ! IsPortAvailable ( port ) )
693+ port += 1 ;
694+
695+ return port ;
696+ }
697+ }
698+
699+ private static bool IsPortAvailable ( int port )
700+ {
701+ bool isAvailable = true ;
702+
703+ IPGlobalProperties ipGlobalProperties = IPGlobalProperties . GetIPGlobalProperties ( ) ;
704+ var ports = new List < int > ( ipGlobalProperties . GetActiveTcpConnections ( ) . Select ( t => t . LocalEndPoint . Port ) ) ;
705+ ports . AddRange ( ipGlobalProperties . GetActiveTcpListeners ( ) . Select ( t => t . Port ) ) ;
706+ foreach ( var usedPort in ports )
707+ {
708+ if ( usedPort == port || usedPort == port - 2000 )
709+ {
710+ isAvailable = false ;
711+ break ;
712+ }
675713 }
714+
715+ return isAvailable ;
676716 }
677717
678718 private static void CreateAppPool ( ServerManager serverManager , string name )
0 commit comments