32 {
33 int errorCode = 0;
35 Utils.PrintLogo();
36
38 {
39
40 config.Port = (ushort)new Random().Next(1024, 65535);
41 }
42
43 this.config = config;
44 config.
Debug.INFO(
"Starting logging...");
45
46 try
47 {
48
49
51 {
52
53 ipAddress = config.ListeningMode switch
54 {
55 IPMode.IPV4_ONLY => IPAddress.Any,
56 _ => IPAddress.IPv6Any,
57 };
58 }
59 else
60 {
61 List<IPAddress> addresses = [.. Dns.GetHostAddresses(config.
Address, AddressFamily.InterNetwork)];
62
63
64
65 if (addresses.Count != 0)
66 {
67 ipAddress = addresses.First();
68 config.ListeningMode =
IPMode.IPV4_ONLY;
69 }
70 else
71 {
72 addresses = [.. Dns.GetHostAddresses(config.
Address, AddressFamily.InterNetworkV6)];
73 if (addresses.Count != 0)
74 {
75 ipAddress = addresses.First();
76 config.ListeningMode =
IPMode.IPV6_ONLY;
77 }
78 else
79 {
80 errorCode = (int)SERVER_ERRORS.ADDRESS_NOT_FOUND;
81 throw new Exception("Cannot found address to listen to");
82 }
83 }
84 }
85
86
87 localEndPoint =
new(ipAddress, config.
Port);
88 listener = new(ipAddress.AddressFamily,
89 SocketType.Stream, ProtocolType.Tcp);
90
92
93
94
95 if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled()) && sslConf.PortMode ==
SSL_PORT_MODE.DUAL_PORT)
96 {
97 sslLocalEndPoint =
new(ipAddress, config.
SslSettings.SslPort);
98 if (sslLocalEndPoint == null)
99 {
100 errorCode = (int)SERVER_ERRORS.CANNOT_CREATE_SSL_ENDPOINT;
101 throw new Exception("Cannot create SSL endpoint");
102 }
103 sslListener = new(ipAddress.AddressFamily,
104 SocketType.Stream, ProtocolType.Tcp);
105 if (sslListener == null)
106 {
107 errorCode = (int)SERVER_ERRORS.CANNOT_CREATE_SSL_LISTENER;
108 throw new Exception("Cannot create SSL listener");
109 }
110 if (sslConf.UseDebugCertificate)
111 {
112 sslCertificate = SslConfiguration.TryLoadDebugCertificate(c: config);
113 if (sslCertificate == null)
114 {
115 errorCode = (int)SERVER_ERRORS.CANNOT_LOAD_DEBUG_CERTIFICATE;
116 throw new Exception("Cannot load debug certificate, server cannot start with this configuration! Make sure openssl is installed");
117 }
118 }
119 else if (sslConf.IsEnabled())
120 sslCertificate = sslConf.GetCertificate();
121 }
122
124 {
125 listener.DualMode = true;
127 sslListener!.DualMode = true;
128 }
129
130
131 if (sslConf.IsEnabled() || sslConf.IsDebugModeEnabled())
132 {
133 config.
Debug.INFO(
"Server is running in SSL mode");
134
135 }
136 var prefix = "http";
137 if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled()) && sslConf.PortMode ==
SSL_PORT_MODE.DUAL_PORT)
138 {
140 config.
Debug.INFO($
"Listening at https://{sslLocalEndPoint}/");
141 else config.
Debug.INFO($
"Listening at https://{config.PublicURL}:{sslConf.SslPort}/");
142 }
143
144 else if ((sslConf.IsEnabled() || sslConf.IsDebugModeEnabled())&& sslConf.PortMode ==
SSL_PORT_MODE.SINGLE_PORT)
145 prefix += "s";
146
148 config.
Debug.INFO($
"Listening at {prefix}://{localEndPoint}/");
149 else config.
Debug.INFO($
"Listening at {prefix}://{config.PublicURL}:{config.Port}/");
150
151 config.
Debug.INFO(
"Server started");
152 }
153 catch (Exception e)
154 {
155 config.
Debug.ERROR(
"An exception occurred while initializing the server ->\n" + e);
156 config.
Debug.INFO(
"Server will now exit...");
157 Environment.Exit(-errorCode);
158 }
159
160 }
bool HideBranding
Hide the HSB logo on startup.
string Address
The server listening address, ex : "127.0.0.1" or "192.168.1.2" or "" (for any address)
Debugger Debug
Holds all debug information and routines.
ushort Port
The server listening port.
IPMode ListeningMode
Set server listening mode to any, only ipv4 or only ipv6. This is valid only if the address is set to...
string PublicURL
When this field is set, it will be used for Unsecure SSL requests upgrade.
SslConfiguration SslSettings
Contains the SSL configuration properties.
SSL_PORT_MODE
This enum is used to determine if the server should listen on a single port for both HTTP and HTTPS o...
IPMode
Defines the listening mode of the server.