Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/sixgrid/BuildService into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ktwrd committed May 22, 2022
2 parents 194bfd3 + 6ecd9b8 commit e9d52ee
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
2 changes: 2 additions & 0 deletions BuildService.Shared/Configuration/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class ConfigManager

public static Bindable<string> authUsername;
public static Bindable<string> authPassword;
public static BindableBool authEnable;

public static Bindable<string> sysRootDataLocation;

Expand All @@ -45,6 +46,7 @@ public static void Initialize()

authUsername = ReadString(@"authUsername", @"admin");
authPassword = ReadString(@"authPassword", @"password");
authEnable = ReadBool(@"authEnable", true);

sysRootDataLocation = ReadString(@"sysRootDataLocation", Path.Combine(ApplicationDirectory, @"build_data"));

Expand Down
7 changes: 5 additions & 2 deletions BuildService.Shared/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ public void Thread_WebSocketServer(EventWaitHandle handle)
WebSocketServer.AddWebSocketService<WebSocket.Root>(@"/");
WebSocketServer.Realm = String.Format(@"BuildService");

WebSocketServer.UserCredentialsFinder = WebSocketServerCredentialHandle;
WebSocketServer.AuthenticationSchemes = AuthenticationSchemes.Basic;
if (ConfigManager.authEnable)
{
WebSocketServer.UserCredentialsFinder = WebSocketServerCredentialHandle;
WebSocketServer.AuthenticationSchemes = AuthenticationSchemes.Basic;
}

WebSocketServer.Start();

Expand Down
46 changes: 33 additions & 13 deletions BuildService.Shared/WebServer/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ public static class Instance

public static string WebDataWithTemplatedStrings = @"";

public static string WebData =>
WebDataWithTemplatedStrings
.Replace(@"$WEBSOCKET_URL",
$@"ws://{ConfigManager.authUsername}:{ConfigManager.authPassword}@$HTTP_HOST:{ConfigManager.svwsPort}/")
.Replace(@"$CREDENTIAL_INJECT",
$@"{ConfigManager.authUsername}:{ConfigManager.authPassword}");
public static string WebData
{
get
{
if (ConfigManager.authEnable)
{
return WebDataWithTemplatedStrings
.Replace(@"$WEBSOCKET_URL",
$@"ws://{ConfigManager.authUsername}:{ConfigManager.authPassword}@$HTTP_HOST:{ConfigManager.svwsPort}/")
.Replace(@"$CREDENTIAL_INJECT",
$@"{ConfigManager.authUsername}:{ConfigManager.authPassword}@");
}
return WebDataWithTemplatedStrings.Replace(@"WEBSOCKET_URL", $@"ws://$HTTP_HOST:{ConfigManager.svwsPort}")
.Replace($@"$CREDENTIAL_INJECT", @"");
}
set { }
}

public static void WebServerThread(EventWaitHandle handle)
{
var assembly = Assembly.GetExecutingAssembly();
Expand All @@ -40,7 +52,8 @@ public static void WebServerThread(EventWaitHandle handle)
// Create a Http server and start listening for incoming connections
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
if (ConfigManager.authEnable)
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
listener.Start();
Console.WriteLine("Listening for connections on {0}", url);

Expand Down Expand Up @@ -72,13 +85,20 @@ public static async Task HandleIncomingConnections()
if (req.Url != null) Console.WriteLine(req.Url.ToString());

bool isAuthenticated = false;

HttpListenerBasicIdentity? identity = (HttpListenerBasicIdentity)ctx.User!.Identity!;
if (identity == null)
isAuthenticated = false;

if (ConfigManager.authEnable)
{
HttpListenerBasicIdentity? identity = (HttpListenerBasicIdentity)ctx.User!.Identity!;
if (identity == null)
isAuthenticated = false;
else
isAuthenticated = identity.Name == ConfigManager.authUsername &&
identity.Password == ConfigManager.authPassword;
}
else
isAuthenticated = identity.Name == ConfigManager.authUsername &&
identity.Password == ConfigManager.authPassword;
{
isAuthenticated = true;
}

// Write the response info
string disableSubmit = !runServer ? "disabled" : "";
Expand Down
2 changes: 1 addition & 1 deletion BuildService/websocket.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
buttonConnect = document.querySelector("button[action=connect]"),
output = document.querySelector("#output"),
textarea = document.querySelector("textarea"),
wsUri = `ws${window.location.protocol.replace('http', '')}//$CREDENTIAL_INJECT@${window.location.host}/socket/`,
wsUri = `ws${window.location.protocol.replace('http', '')}//$CREDENTIAL_INJECT${window.location.host}/socket/`,
websocket = null;
// button.addEventListener("click", onClickButton);
buttonConnect.addEventListener("click", onClickButtonConnect);
Expand Down

0 comments on commit e9d52ee

Please sign in to comment.