Skip to content

Commit

Permalink
Fix(options) Allow multiple --urls in command line options separated …
Browse files Browse the repository at this point in the history
…with ;(#1171)
  • Loading branch information
rnwood committed Feb 25, 2024
1 parent 1be7aa6 commit 1c36d0a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static MapOptions<CommandLineOptions> TryParseCommandLine(IEnumerable<str
options.Add("service", "Required to run when registered as a Windows service. To register service: sc.exe create Smtp4dev binPath= \"{PathToExe} --service\"", _ => { });

options.Add(
"urls=", "The URLs the web interface should listen on. For example, http://localhost:123. Use `*` in place of hostname to listen for requests on any IP address or hostname using the specified port and protocol (for example, http://*:5000)", data => map.Add(data, x => x.Urls));
"urls=", "The URLs the web interface should listen on. For example, http://localhost:123. Use `*` in place of hostname to listen for requests on any IP address or hostname using the specified port and protocol (for example, http://*:5000). Separate multiple values with ;", data => map.Add(data, x => x.Urls));

options.Add(
"basepath=", "Specifies the virtual path from web server root where SMTP4DEV web interface will be hosted. e.g. \"/\" or \"/smtp4dev\"", data => map.Add(data, x => x.ServerOptions.BasePath));
Expand Down
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static IHost BuildWebHost(string[] args, CommandLineOptions cmdLineOptio
if (!string.IsNullOrEmpty(cmdLineOptions.Urls))
{
c.UseUrls(cmdLineOptions.Urls);
c.UseUrls(cmdLineOptions.Urls.Split(';', StringSplitOptions.RemoveEmptyEntries).Select(u => u.Trim()).ToArray());
}
});

Expand Down
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"Rnwood.Smtp4dev": {
"commandName": "Project",
"commandLineArgs": "--imapport=0",
"commandLineArgs": "--imapport=0 --urls=http://localhost:5000;http://localhost:5001",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
50 changes: 25 additions & 25 deletions Rnwood.Smtp4dev/Server/Smtp4devServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void CreateSmtpServer()
{
if (this.smtpServer.IsRunning) return;
log.Information("SMTP server stopped.");
this.notificationsHub.OnServerChanged().Wait();
this.notificationsHub.OnServerChanged().Wait();
});
}

Expand All @@ -93,8 +93,8 @@ private X509Certificate2 GetTlsCertificate()
{
if (!string.IsNullOrEmpty(serverOptions.CurrentValue.TlsCertificate))
{
var pfxPassword = serverOptions.CurrentValue.TlsCertificatePassword ?? "";
var pfxPassword = serverOptions.CurrentValue.TlsCertificatePassword ?? "";

if (string.IsNullOrEmpty(serverOptions.CurrentValue.TlsCertificatePrivateKey))
{
cert = CertificateHelper.LoadCertificate(serverOptions.CurrentValue.TlsCertificate, pfxPassword);
Expand Down Expand Up @@ -273,20 +273,20 @@ private async Task OnMessageReceived(object sender, MessageEventArgs e)
Message message = new MessageConverter().ConvertAsync(e.Message).Result;
log.Information("Message received. Client address {clientAddress}, From {messageFrom}, To {messageTo}, SecureConnection: {secure}.",
e.Message.Session.ClientAddress, e.Message.From, message.To, e.Message.SecureConnection);
message.IsUnread = true;
message.IsUnread = true;

await taskQueue.QueueTask(() =>
{
await taskQueue.QueueTask(() =>
{
log.Information("Processing received message");
using var scope = serviceScopeFactory.CreateScope();
Smtp4devDbContext dbContext = scope.ServiceProvider.GetService<Smtp4devDbContext>();
var relayResult = TryRelayMessage(message, null);
message.RelayError = string.Join("\n", relayResult.Exceptions.Select(e => e.Key + ": " + e.Value.Message));
ImapState imapState = dbContext.ImapState.Single();
ImapState imapState = dbContext.ImapState.Single();
imapState.LastUid = Math.Max(0, imapState.LastUid + 1);
message.ImapUid = imapState.LastUid;
message.ImapUid = imapState.LastUid;
message.Session = dbContext.Sessions.Find(activeSessionsToDbId[e.Message.Session]);
if (relayResult.WasRelayed)
{
Expand All @@ -296,16 +296,16 @@ await taskQueue.QueueTask(() =>
}
}
dbContext.Messages.Add(message);
dbContext.Messages.Add(message);
dbContext.SaveChanges();
dbContext.SaveChanges();
TrimMessages(dbContext);
dbContext.SaveChanges();
notificationsHub.OnMessagesChanged().Wait();
TrimMessages(dbContext);
dbContext.SaveChanges();
notificationsHub.OnMessagesChanged().Wait();
log.Information("Processing received message DONE");
}, false).ConfigureAwait(false);
}
}, false).ConfigureAwait(false);
}

public RelayResult TryRelayMessage(Message message, MailboxAddress[] overrideRecipients)
{
Expand Down Expand Up @@ -338,15 +338,15 @@ public RelayResult TryRelayMessage(Message message, MailboxAddress[] overrideRec
log.Information("Relaying message to {recipient}", recipient);

using SmtpClient relaySmtpClient = relaySmtpClientFactory(relayOptions.CurrentValue);
var apiMsg = new ApiModel.Message(message);
MimeMessage newEmail = apiMsg.MimeMessage;
MailboxAddress sender = MailboxAddress.Parse(
!string.IsNullOrEmpty(relayOptions.CurrentValue.SenderAddress)
? relayOptions.CurrentValue.SenderAddress
: apiMsg.From);
relaySmtpClient.Send(newEmail, sender, new[] { recipient });
var apiMsg = new ApiModel.Message(message);
MimeMessage newEmail = apiMsg.MimeMessage;
MailboxAddress sender = MailboxAddress.Parse(
!string.IsNullOrEmpty(relayOptions.CurrentValue.SenderAddress)
? relayOptions.CurrentValue.SenderAddress
: apiMsg.From);
relaySmtpClient.Send(newEmail, sender, new[] { recipient });
result.RelayRecipients.Add(new RelayRecipientResult() { Email = recipient.Address, RelayDate = DateTime.UtcNow });
}
}
catch (Exception e)
{
log.Error(e, "Can not relay message to {recipient}: {errorMessage}", recipient, e.ToString());
Expand Down Expand Up @@ -381,12 +381,12 @@ private void TrimSessions(Smtp4devDbContext dbContext)
public bool IsRunning
{
get { return this.smtpServer.IsRunning; }
}
}

public int PortNumber
{
get { return this.smtpServer.PortNumber; }
}
}

public void TryStart()
{
Expand Down

0 comments on commit 1c36d0a

Please sign in to comment.