public PortKnockResponse DoPost(PortKnockRequest req) { // need to log here. if (req.Action.ToLower() == "allow") { try { FirewallUtils.AllowAddressPort(req.Ip, req.Port); return(new PortKnockResponse() { Result = "OK", Message = null }); } catch (Exception e) { return(new PortKnockResponse() { Result = "NG", Message = e.Message }); } } else if (req.Action.ToLower() == "deny") { try { FirewallUtils.CloseAddressPort(req.Ip, req.Port); return(new PortKnockResponse() { Result = "OK", Message = null }); } catch (Exception e) { return(new PortKnockResponse() { Result = "NG", Message = e.Message }); } } return(new PortKnockResponse() { Result = "NG", Message = "unknown error" }); }
/// <summary> /// Constructor. /// </summary> /// <param name="ale_layer">The pre-defined ALE layer.</param> public FirewallLayerGuid(FirewallAleLayer ale_layer) : base(FirewallUtils.GetLayerGuidForAleLayer(ale_layer)) { }
#pragma warning restore /// <summary> /// The application's main entry point /// </summary> /// <param name="args">Command line arguments</param> /// <returns>Return code</returns> public static int Main(string[] args) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(GREETING_MESSAGE); #if DEBUG AppDomain.MonitoringIsEnabled = true; #endif WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); ConsoleLogger.Flush(); $"Application started with the following {args.Length} argument(s):".Msg(); for (int i = 0; i < args.Length; i++) { $" [{i}]: {args[i].Trim()}".Msg(); } try { Win32.ShowWindow(Process.GetCurrentProcess().MainWindowHandle, 3); } catch { "Unable to resize console window ... aren't you running in GUI mode?".Warn(); } if (!principal.IsInRole(WindowsBuiltInRole.Administrator)) { "This application is not running as administrator and will therefore have privilege and authorisation problems. Please restart it with elevated privilege.".Err(); return(-1); } else { "Running as administrator. Perfect.".Ok(); } string dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; int retcode = 0; Directory.SetCurrentDirectory(dir); $"Running from '{dir}'".Info(); using (Mutex m = new Mutex(false, Win32.MUTEX)) try { Console.CancelKeyPress += Console_CancelKeyPress; AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; if (m.WaitOne(0, false) || containsarg(args, ARG_IGNMTX)) { if (containsarg(args, ARG_SLOWSTART)) { foreach (Action <string[]> task in StartupTasks) { task(args); } } else Parallel.ForEach(StartupTasks, _ => _(args)); fixed(bool *bptr = &acceptconnections) using (ServiceHost sh = BindCertificatePort(IPAddress.Any.ToString(), Win32.PORT, StoreName.TrustedPublisher, nameof(Properties.Resources.ASC))) using (ASCServer ws = new ASCServer(Win32.PORT, bptr, null)) new Program().Inner(Win32.PORT, dir, ws); } else { "Cannot start the server, as an other instance of this application is already running.".Warn(); } } catch (ForcedShutdown) { "Remote-forced (controlled) shutdown ...".Warn(); } catch (Exception ex) { if (ex != null) { ex.Err(); } "Application-forced shutdown ...".Err(); retcode = -1; } finally { Console.CancelKeyPress -= Console_CancelKeyPress; AppDomain.CurrentDomain.ProcessExit -= CurrentDomain_ProcessExit; if (containsarg(args, ARG_DELFWL)) { "Removing previously set firewall rules ...".Msg(); foreach (int port in new int[] { Win32.PORT, Win32.PORT + 1 }) { FirewallUtils.ClosePort(port); $"Port {port} was successfully un-registered".Ok(); } } "Server shut down".Ok(); m.Close(); LoggerBase.StopAll(logger => { if (!containsarg(args, ARG_NOLOG)) { logger.Save(Directory.GetCurrentDirectory()); } }); if (Debugger.IsAttached | (Win32.GetConsoleWindow() != IntPtr.Zero)) { "Press any key ...".Msg(); Console.ReadKey(true); } } return(retcode); }