Skip to content

Commit

Permalink
Remove third-party relay services.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Feb 17, 2021
1 parent af9fd77 commit 16a6888
Show file tree
Hide file tree
Showing 23 changed files with 382 additions and 381 deletions.
11 changes: 5 additions & 6 deletions Agent.Installer.Win/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,18 @@ private async Task ExtractDeviceInitInfo()
var codeSection = string.Join("", fileName.Skip(i).Take(codeLength));

if (codeSection.StartsWith("[") &&
codeSection.EndsWith("]"))
codeSection.EndsWith("]") &&
!string.IsNullOrWhiteSpace(ServerUrl))
{
var relayCode = codeSection.Substring(1, 4);
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync($"{AppConstants.DeviceInitUrl}/{relayCode}").ConfigureAwait(false);
var response = await httpClient.GetAsync($"{ServerUrl}/api/relay/{relayCode}").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var serializer = new JavaScriptSerializer();
var contentString = await response.Content.ReadAsStringAsync();
var deviceInitParams = serializer.Deserialize<DeviceInitParams>(contentString);
OrganizationID = deviceInitParams.OrganizationId;
ServerUrl = deviceInitParams.Host;
var organizationId = await response.Content.ReadAsStringAsync();
OrganizationID = organizationId;
break;
}
}
Expand Down
35 changes: 16 additions & 19 deletions Desktop.Core/Services/DeviceInitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,11 @@ public async Task GetInitParams()

var config = _configService.GetConfig();

if (!string.IsNullOrWhiteSpace(_conductor.Host) &&
!string.IsNullOrWhiteSpace(_conductor.OrganizationId))
{
config.Host = _conductor.Host;
config.OrganizationId = _conductor.Host;
_configService.Save(config);
var brandingUrl = $"{_conductor.Host.TrimEnd('/')}/api/branding/{_conductor.OrganizationId}";
_brandingInfo = await httpClient.GetFromJsonAsync<BrandingInfo>(brandingUrl).ConfigureAwait(false);
return;
}

var fileName = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName);

if (fileName.Contains("[") && fileName.Contains("]"))
if (fileName.Contains("[") &&
fileName.Contains("]") &&
!string.IsNullOrWhiteSpace(_conductor?.Host))
{
var codeLength = AppConstants.RelayCodeLength + 2;

Expand All @@ -74,13 +65,12 @@ public async Task GetInitParams()
{
var relayCode = codeSection[1..5];

using var response = await httpClient.GetAsync($"{AppConstants.DeviceInitUrl}/{relayCode}").ConfigureAwait(false);
using var response = await httpClient.GetAsync($"{_conductor.Host}/api/Relay/{relayCode}").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var contentString = await response.Content.ReadAsStringAsync();
var deviceInitParams = JsonSerializer.Deserialize<DeviceInitParams>(contentString, JsonSerializerHelper.CaseInsensitiveOptions);
config.Host = deviceInitParams.Host;
config.OrganizationId = deviceInitParams.OrganizationId;
var organizationId = await response.Content.ReadAsStringAsync();
config.Host = _conductor.Host;
config.OrganizationId = organizationId;
_configService.Save(config);

var brandingUrl = $"{config.Host.TrimEnd('/')}/api/branding/{config.OrganizationId}";
Expand All @@ -89,8 +79,15 @@ public async Task GetInitParams()
}
}
}


}

if (!string.IsNullOrWhiteSpace(_conductor?.Host))
{
config.Host = _conductor.Host;
config.OrganizationId = _conductor.OrganizationId;
_configService.Save(config);
var brandingUrl = $"{_conductor.Host.TrimEnd('/')}/api/branding/{_conductor.OrganizationId}";
_brandingInfo = await httpClient.GetFromJsonAsync<BrandingInfo>(brandingUrl).ConfigureAwait(false);
}
}
catch (Exception ex)
Expand Down
14 changes: 1 addition & 13 deletions Server/API/BrandingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,14 @@ public BrandingController(IDataService dataService)
[HttpGet("{organizationId}")]
public async Task<BrandingInfo> Get(string organizationId)
{
var org = _dataService.GetOrganizationById(organizationId);
// We're doing the honor system here.
if (org.SponsorLevel < Shared.Enums.SponsorLevel.Branding)
{
return null;
}

return await _dataService.GetBrandingInfo(organizationId);
}

[HttpGet]
public async Task<BrandingInfo> GetDefault()
{
var defaultOrg = await _dataService.GetDefaultOrganization();
if (defaultOrg?.SponsorLevel >= Shared.Enums.SponsorLevel.Branding)
{
return await _dataService.GetBrandingInfo(defaultOrg.ID);
}

return null;
return await _dataService.GetBrandingInfo(defaultOrg.ID);
}
}
}
56 changes: 6 additions & 50 deletions Server/API/ClientDownloadsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<IActionResult> GetDesktop(string platformID)
}

[HttpGet("clickonce-setup/{architecture}/{organizationId}")]
public async Task<IActionResult> GetClickOnceSetup(string architecture, string organizationId)
public IActionResult GetClickOnceSetup(string architecture, string organizationId)
{
string clickOnceDir;

Expand All @@ -83,39 +83,7 @@ public async Task<IActionResult> GetClickOnceSetup(string architecture, string o
}

var setupFilePath = Path.Combine(clickOnceDir, "setup.exe");
string appContent;

try
{
await ClickOnceMiddleware.AppFileLock.WaitAsync();
appContent = await System.IO.File.ReadAllTextAsync(Path.Combine(clickOnceDir, "Remotely_Desktop.application"));
}
finally
{
ClickOnceMiddleware.AppFileLock.Release();
}

if (appContent.Contains($"{Request.Scheme}://{Request.Host}"))
{
return File(new FileStream(setupFilePath, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", "setup.exe");
}

using var client = _httpClientFactory.CreateClient();
var formContent = new MultipartFormDataContent();
var bytes = await System.IO.File.ReadAllBytesAsync(setupFilePath);
using var byteContent = new ByteArrayContent(bytes);
formContent.Add(byteContent, "setup", "setup.exe");

try
{
using var response = await client.PostAsync($"{AppConstants.ClickOnceSetupUrl}/?host={Request.Scheme}://{Request.Host}&organizationid={organizationId}&architecture={architecture}", formContent);
var responseBytes = await response.Content.ReadAsByteArrayAsync();
return File(responseBytes, "application/octet-stream", "setup.exe");
}
catch
{
return NotFound();
}
return File(new FileStream(setupFilePath, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", "setup.exe");
}


Expand Down Expand Up @@ -151,15 +119,12 @@ private async Task<IActionResult> GetBashInstaller(string fileName, string organ

private async Task<IActionResult> GetDesktopFile(string filePath)
{
var relayCode = string.Empty;
string relayCode;

if (User.Identity.IsAuthenticated)
{
var currentOrg = await _dataService.GetOrganizationByUserName(User.Identity.Name);
if (currentOrg.SponsorLevel >= Shared.Enums.SponsorLevel.Relay)
{
relayCode = currentOrg.RelayCode;
}
relayCode = currentOrg.RelayCode;
}
else
{
Expand Down Expand Up @@ -194,17 +159,8 @@ private async Task<IActionResult> GetInstallFile(string organizationId, string p
var filePath = Path.Combine(_hostEnv.WebRootPath, "Downloads", "Remotely_Installer.exe");
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var organization = _dataService.GetOrganizationById(organizationId);

if (organization.SponsorLevel > Shared.Enums.SponsorLevel.None)
{
var relayCode = organization.RelayCode;
return File(fs, "application/octet-stream", $"Remotely_Install-[{relayCode}].exe");

}
else
{
return File(fs, "application/octet-stream", $"Remotely_Installer-[{organizationId}].exe");
}
var relayCode = organization.RelayCode;
return File(fs, "application/octet-stream", $"Remotely_Install-[{relayCode}].exe");
}
// TODO: Remove after a few releases.
case "Manjaro-x64":
Expand Down
16 changes: 16 additions & 0 deletions Server/API/OrganizationManagementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ public IActionResult Name([FromBody] string organizationName)
DataService.UpdateOrganizationName(orgID, organizationName.Trim());
return Ok("ok");
}

[HttpPut("SetDefault")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public IActionResult SetDefault([FromBody] bool isDefault)
{
if (User.Identity.IsAuthenticated &&
!DataService.GetUserByName(User.Identity.Name).IsServerAdmin)
{
return Unauthorized();
}

Request.Headers.TryGetValue("OrganizationID", out var orgID);
DataService.SetIsDefaultOrganization(orgID, isDefault);
return Ok("ok");
}

[HttpPost("SendInvite")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public async Task<IActionResult> SendInvite([FromBody] Invite invite)
Expand Down
30 changes: 30 additions & 0 deletions Server/API/RelayController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Remotely.Server.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Remotely.Server.API
{
[Route("api/[controller]")]
[ApiController]
public class RelayController : ControllerBase
{
private readonly IDataService _dataService;

public RelayController(IDataService dataService)
{
_dataService = dataService;
}

[HttpGet("{relayCode}")]
public async Task<string> Get(string relayCode)
{
var organization = await _dataService.GetOrganizationByRelayCode(relayCode);
return organization?.ID;
}
}
}
8 changes: 1 addition & 7 deletions Server/Areas/Identity/Pages/Account/Manage/Branding.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
{
<h5 class="text-muted">Only organization administrators can view this page.</h5>
}
@* We're doing the honor system here. *@
else if (Model.Organization.SponsorLevel < Shared.Enums.SponsorLevel.Branding)
{
<h5 class="text-muted">GitHub sponsorship level is not high enough to use branding.</h5>
<h3 class="text-muted">After sponsoring, you can register on the Organization tab.</h3>
}
else
{
<div class="row">
Expand Down Expand Up @@ -78,4 +72,4 @@ else
</form>
</div>
</div>
}
}
4 changes: 0 additions & 4 deletions Server/Areas/Identity/Pages/Account/Manage/Branding.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ public async Task OnGet()
public async Task<IActionResult> OnPostAsync()
{
CurrentUser = _dataService.GetUserByName(User.Identity.Name);
if (!CurrentUser.IsAdministrator || CurrentUser.Organization.SponsorLevel < Shared.Enums.SponsorLevel.Branding)
{
return Unauthorized();
}

if (!ModelState.IsValid)
{
Expand Down
70 changes: 13 additions & 57 deletions Server/Areas/Identity/Pages/Account/Manage/Organization.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,10 @@
<label class="mb-1">Organization ID</label>
<input readonly class="form-control" value="@Model.Organization.ID" />
</div>
</div>
</div>

@* GitHub Sponsors Registration *@
<div class="row">
<div class="col-sm-8">
<form method="post" asp-page-handler="RegisterSponsor" autocomplete="off">
<div class="form-group mw">
<label class="mb-1">GitHub Sponsorship Level</label>
<span id="sponsorMainHelp" class="fas fa-question-circle pointer"></span>
@if (Model.Organization.SponsorLevel > Shared.Enums.SponsorLevel.None)
{
<div class="input-group">
<input readonly class="form-control" value="@Model.Organization.SponsorLevel.ToString()" />
<div class="input-group-append">
<div class="fa fa-check-circle input-group-text" style="color: limegreen"></div>
</div>
</div>
}
else
{
<div class="input-group">
<input readonly class="form-control" value="@Model.Organization.SponsorLevel.ToString()" />
<div class="input-group-append">
<div class="fa fa-times-circle input-group-text" style="color: red"></div>
</div>
</div>
}
</div>
<div class="form-group">
<label class="mb-1">Sponsor Relay Code</label>
<span id="sponsorRelayHelp" class="fas fa-question-circle pointer"></span>
<input readonly class="form-control" value="@Model.Organization.RelayCode" />
</div>
<div class="form-group">
<label class="mb-1">Sponsor Unlock Code (keep this safe and secret)</label>
<span id="sponsorUnlockHelp" class="fas fa-question-circle pointer"></span>
<input class="form-control" asp-for="UnlockCode" />
</div>
<div class="form-group">
<label class="mb-1">Register GitHub Username</label>
<input class="form-control" asp-for="GithubUsername" />
</div>
@if (Model.CurrentUser.IsServerAdmin)
{
<div class="form-group">
<label class="mb-1">Default Organization For Server</label>
<span id="defaultOrgHelp" class="fas fa-question-circle pointer"></span>
<br />
<input type="checkbox" asp-for="IsDefaultOrganization" />
</div>
}
<div class="form-group text-right">
<button class="btn btn-secondary" type="submit" asp-page-handler="Disable">Disable</button>
<button class="btn btn-primary" type="submit">Register</button>
</div>
</form>
<div class="form-group mw">
<label class="mb-1">Relay Code</label>
<input readonly class="form-control" value="@Model.Organization.RelayCode" />
</div>
</div>
</div>

Expand All @@ -99,6 +46,15 @@
</div>
</div>
</div>
@if (Model.CurrentUser.IsServerAdmin)
{
<div class="form-group">
<label class="mb-1">Default Organization For Server</label>
<span id="defaultOrgHelp" class="fas fa-question-circle pointer"></span>
<br />
<input id="defaultOrgCheckbox" type="checkbox" asp-for="IsDefaultOrganization" />
</div>
}
</div>
</div>

Expand Down
Loading

0 comments on commit 16a6888

Please sign in to comment.