コード例 #1
0
        static void Main(string[] args)
        {
            PrintLogo();

            var settings = ConfigurationManager.GetSection("portBridge") as PortBridgeSection;

            if (settings != null)
            {
                serviceNamespace = settings.ServiceNamespace;
                accessRuleName   = settings.AccessRuleName;
                accessRuleKey    = settings.AccessRuleKey;
                if (!string.IsNullOrEmpty(settings.LocalHostName))
                {
                    localHostName = settings.LocalHostName;
                }
            }

            if (!ParseCommandLine(args))
            {
                PrintUsage();
                return;
            }

            PortBridgeServiceForwarderHost host = new PortBridgeServiceForwarderHost();

            if (settings != null && settings.HostMappings.Count > 0)
            {
                foreach (HostMappingElement hostMapping in settings.HostMappings)
                {
                    string targetHostAlias = hostMapping.TargetHost;
                    if (string.Equals(targetHostAlias, "localhost", StringComparison.OrdinalIgnoreCase))
                    {
                        targetHostAlias = localHostName;
                    }
                    host.Forwarders.Add(
                        new ServiceConnectionForwarder(
                            serviceNamespace,
                            accessRuleName,
                            accessRuleKey,
                            hostMapping.TargetHost,
                            targetHostAlias,
                            hostMapping.AllowedPorts,
                            hostMapping.AllowedPipes));
                }
            }
            else
            {
                string targetHostAlias = localHostName;
                if (string.Equals(targetHostAlias, "localhost", StringComparison.OrdinalIgnoreCase))
                {
                    targetHostAlias = localHostName;
                }
                host.Forwarders.Add(
                    new ServiceConnectionForwarder(
                        serviceNamespace,
                        accessRuleName,
                        accessRuleKey,
                        "localhost",
                        targetHostAlias,
                        permittedPorts,
                        string.Empty));
            }


            if (!runOnConsole)
            {
                ServiceController sc = new ServiceController("PortBridgeService");
                try
                {
                    var status = sc.Status;
                }
                catch (SystemException)
                {
                    runOnConsole = true;
                }
            }

            if (runOnConsole)
            {
                host.Open();
                Console.WriteLine("Press [ENTER] to exit.");
                Console.ReadLine();
                host.Close();
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new PortBridgeService(host)
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
コード例 #2
0
 public PortBridgeService(PortBridgeServiceForwarderHost host)
 {
     this.host = host;
     InitializeComponent();
 }