Skip to content

Commit 10d746c

Browse files
committed
feat: Write Environment Variables when run the script to make script identify which session is connecting now.
1 parent 97a686d commit 10d746c

File tree

8 files changed

+68
-179
lines changed

8 files changed

+68
-179
lines changed

Shawn.Utils

Ui/Controls/NoteDisplay/NoteDisplayAndEditor.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
</FrameworkElement.CommandBindings>
9292
<Grid Margin="10">
9393
<wpf:MarkdownViewer Name="MarkdownViewer"
94-
Foreground="{DynamicResource PrimaryTextBrush}">
94+
Foreground="{DynamicResource PrimaryTextBrush}">
9595
</wpf:MarkdownViewer>
9696

9797
<Grid Name="GridEditor">

Ui/Model/Protocol/Base/ProtocolBase.cs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using _1RM.Service.DataSource;
1111
using _1RM.Service.DataSource.Model;
1212
using _1RM.Utils;
13+
using FluentFTP.Helpers;
1314
using Newtonsoft.Json;
1415
using Shawn.Utils;
1516
using Shawn.Utils.Interface;
@@ -291,40 +292,90 @@ public ProtocolBase Clone()
291292
return clone;
292293
}
293294

294-
public int RunScriptBeforeConnect()
295+
private Dictionary<string, string> GetEnvironmentVariablesForScript()
296+
{
297+
var evs = new Dictionary<string, string>
298+
{
299+
{ "SESSION_ID", this.GetHashCode().ToString() },
300+
{ "SERVER_ID", this.Id },
301+
{ "SERVER_NAME", this.DisplayName },
302+
{ "SERVER_HOST", "" },
303+
{ "SERVER_TAGS", string.Join(',',this.Tags) }
304+
};
305+
if (this is ProtocolBaseWithAddressPort p)
306+
evs["SERVER_HOST"] = $"{p.Address}:{p.Port}";
307+
return evs;
308+
}
309+
310+
public int RunScriptBeforeConnect(bool isTestRun = false)
295311
{
296312
int exitCode = 0;
297313
try
298314
{
299315
if (!string.IsNullOrWhiteSpace(CommandBeforeConnected))
300316
{
301317
var tuple = WinCmdRunner.DisassembleOneLineScriptCmd(CommandBeforeConnected);
302-
exitCode = WinCmdRunner.RunFile(tuple.Item1, arguments: tuple.Item2, isAsync: false, isHideWindow: HideCommandBeforeConnectedWindow);
318+
319+
if (isTestRun)
320+
{
321+
if (string.IsNullOrEmpty(tuple.Item2) == false)
322+
MessageBoxHelper.Info($"We will run: '{tuple.Item1}' with parameters '{tuple.Item2}'");
323+
else
324+
MessageBoxHelper.Info($"We will run: '{CommandBeforeConnected}'");
325+
}
326+
327+
exitCode = WinCmdRunner.RunFile(tuple.Item1, arguments: tuple.Item2, isAsync: false,
328+
isHideWindow: HideCommandBeforeConnectedWindow && isTestRun != true,
329+
workingDirectory: tuple.Item3,
330+
envVariables: GetEnvironmentVariablesForScript());
331+
332+
if (isTestRun)
333+
{
334+
MessageBoxHelper.Info($"The exit code of the script = {exitCode}.\r\nOnce the code != 0, we will terminate your connection request.");
335+
}
303336
}
304337
}
305338
catch (Exception e)
306339
{
307340
exitCode = 1;
308341
SimpleLogHelper.Error(e);
309-
MessageBoxHelper.ErrorAlert(e.Message, IoC.Get<ILanguageService>().Translate("Script before connect"));
342+
MessageBoxHelper.ErrorAlert("We encountered a problem while running the script: " + e.Message, IoC.Get<ILanguageService>().Translate("Script before connect"));
310343
}
311344
return exitCode;
312345
}
313346

314-
public void RunScriptAfterDisconnected()
347+
public void RunScriptAfterDisconnected(bool isTestRun = false)
315348
{
349+
int exitCode = 0;
316350
try
317351
{
318352
if (!string.IsNullOrWhiteSpace(CommandAfterDisconnected))
319353
{
320354
var tuple = WinCmdRunner.DisassembleOneLineScriptCmd(CommandAfterDisconnected);
321-
WinCmdRunner.RunFile(tuple.Item1, arguments: tuple.Item2, isAsync: true, isHideWindow: true);
355+
356+
if (isTestRun)
357+
{
358+
if (string.IsNullOrEmpty(tuple.Item2) == false)
359+
MessageBoxHelper.Info($"We will run: '{tuple.Item1}' with parameters '{tuple.Item2}'");
360+
else
361+
MessageBoxHelper.Info($"We will run: '{CommandBeforeConnected}'");
362+
}
363+
364+
exitCode = WinCmdRunner.RunFile(tuple.Item1, arguments: tuple.Item2, isAsync: true,
365+
isHideWindow: isTestRun != true,
366+
workingDirectory: tuple.Item3,
367+
envVariables: GetEnvironmentVariablesForScript());
368+
369+
if (isTestRun)
370+
{
371+
MessageBoxHelper.Info($"The exit code of the script = {exitCode}.");
372+
}
322373
}
323374
}
324375
catch (Exception e)
325376
{
326377
SimpleLogHelper.Error(e);
327-
MessageBoxHelper.ErrorAlert(e.Message, IoC.Get<ILanguageService>().Translate("Script after disconnected"));
378+
MessageBoxHelper.ErrorAlert("We encountered a problem while running the script: " + e.Message, IoC.Get<ILanguageService>().Translate("Script after disconnected"));
328379
}
329380
}
330381

Ui/Utils/PRemoteM/PRemoteMTransferHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void TransAsync()
6666
Debug.Assert(localSource != null);
6767
localSource!.Database_InsertServer(_servers);
6868
IoC.Get<GlobalData>().ReloadServerList(true);
69-
MessageBoxHelper.Info($"All done! You may need to delete `{_dbPath}`...", ownerViewModel: IoC.Get<MainWindowViewModel>());
69+
MessageBoxHelper.Info($"All done! \r\n\r\nYou may want to delete the old data at:\r\n`{_dbPath}`.", ownerViewModel: IoC.Get<MainWindowViewModel>());
7070
}
7171
catch (Exception e)
7272
{

Ui/View/Editor/ServerEditorPageViewModel.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -647,43 +647,18 @@ public RelayCommand CmdTestScript
647647
{
648648
return _cmdTestScript ??= new RelayCommand((o) =>
649649
{
650-
string cmd;
651-
if (o?.ToString()?.ToLower() == "before")
652-
{
653-
cmd = Server.CommandBeforeConnected;
654-
}
655-
else
656-
{
657-
cmd = Server.CommandAfterDisconnected;
658-
}
659-
660650
MaskLayerController.ShowProcessingRing(assignLayerContainer: IoC.Get<MainWindowViewModel>());
661651
Task.Factory.StartNew(() =>
662652
{
663-
try
664-
{
665-
if (!string.IsNullOrWhiteSpace(cmd))
666-
{
667-
var tuple = WinCmdRunner.DisassembleOneLineScriptCmd(cmd);
668-
if (string.IsNullOrEmpty(tuple.Item2) == false)
669-
MessageBoxHelper.Info($"We will run: '{tuple.Item1}' with parameters '{tuple.Item2}'");
670-
else
671-
MessageBoxHelper.Info($"We will run: '{cmd}'");
672-
var code = WinCmdRunner.RunFile(tuple.Item1, arguments: tuple.Item2, isHideWindow: false);
673-
if (code != 0)
674-
{
675-
MessageBoxHelper.Info($"Exit code: {code}");
676-
}
677-
}
678-
}
679-
catch (Exception ex)
653+
if (o?.ToString()?.ToLower() == "before")
680654
{
681-
MessageBoxHelper.ErrorAlert(ex.Message);
655+
Server.RunScriptBeforeConnect(true);
682656
}
683-
finally
657+
else
684658
{
685-
MaskLayerController.HideMask(IoC.Get<MainWindowViewModel>());
659+
Server.RunScriptAfterDisconnected(true);
686660
}
661+
MaskLayerController.HideMask(IoC.Get<MainWindowViewModel>());
687662
});
688663
});
689664
}

Ui/View/ProtocolBaseViewModel.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Linq;
54
using System.Windows.Controls;
6-
using System.Windows.Forms;
7-
using _1RM.Controls;
85
using _1RM.Controls.NoteDisplay;
96
using _1RM.Model;
10-
using _1RM.Model.DAO.Dapper;
11-
using _1RM.Model.Protocol;
127
using _1RM.Model.Protocol.Base;
138
using _1RM.Service;
149
using _1RM.Service.DataSource;
15-
using _1RM.Service.DataSource.Model;
16-
using Newtonsoft.Json;
17-
using NUlid;
1810
using Shawn.Utils;
1911
using Shawn.Utils.Wpf;
2012
using Stylet;

Ui/View/Utils/MessageBoxView.xaml

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -17,115 +17,5 @@
1717
FlowDirection="{Binding PageViewModel.FlowDirection}"
1818
ShowInTaskbar="False"
1919
MinWidth="160" MaxWidth="485">
20-
<Window.Resources>
21-
<Style TargetType="TextBlock">
22-
<Setter Property="Foreground" Value="{DynamicResource BackgroundTextBrush}"></Setter>
23-
<Setter Property="VerticalAlignment" Value="Center"></Setter>
24-
<Setter Property="TextWrapping" Value="Wrap"></Setter>
25-
</Style>
26-
<Style TargetType="TextBox" BasedOn="{StaticResource TextBoxAccentStyle}">
27-
<Setter Property="Margin" Value="0 5 0 15"></Setter>
28-
</Style>
29-
</Window.Resources>
30-
<!--<Border Margin="0,0,0,0"
31-
Background="{DynamicResource BackgroundBrush}"
32-
BorderBrush="{DynamicResource BackgroundTextBrush}"
33-
BorderThickness="1"
34-
CornerRadius="{StaticResource DefaultCornerRadius}" ClipToBounds="True">
35-
<Grid>
36-
<Grid.ColumnDefinitions>
37-
<ColumnDefinition Width="6"/>
38-
--><!-- Padding --><!--
39-
<ColumnDefinition Width="Auto"/>
40-
<ColumnDefinition/>
41-
</Grid.ColumnDefinitions>
42-
<Grid.RowDefinitions>
43-
<RowDefinition Height="30"/>
44-
<RowDefinition Height="Auto"/>
45-
<RowDefinition Height="50"/>
46-
</Grid.RowDefinitions>
47-
48-
--><!--TITLE--><!--
49-
<Border Background="{DynamicResource AccentMidBrush}" Grid.ColumnSpan="3" ClipToBounds="True">
50-
<Border.CornerRadius>
51-
<CornerRadius TopLeft="{StaticResource DefaultCornerRadiusValue}" TopRight="{StaticResource DefaultCornerRadiusValue}"></CornerRadius>
52-
</Border.CornerRadius>
53-
</Border>
54-
<StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" VerticalAlignment="Center" Orientation="Horizontal">
55-
<StackPanel.Resources>
56-
<Style TargetType="TextBlock">
57-
<Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
58-
<Setter Property="VerticalAlignment" Value="Center"></Setter>
59-
<Setter Property="TextWrapping" Value="Wrap"></Setter>
60-
</Style>
61-
</StackPanel.Resources>
62-
<Grid Background="Transparent" Width="20" Height="20" Margin="5 0 0 0" VerticalAlignment="Center">
63-
<Image Source="/Resources/Image/Logo/logo32.png"></Image>
64-
</Grid>
65-
<TextBlock Text="{x:Static service:AppPathHelper.APP_DISPLAY_NAME}" Margin=" 5 0 0 0" TextTrimming="CharacterEllipsis"/>
66-
<TextBlock Text="-" Margin="5 0 0 0" TextTrimming="CharacterEllipsis"/>
67-
<TextBlock Text="{Binding DisplayName}" Margin="5 0 0 0" TextTrimming="CharacterEllipsis"/>
68-
</StackPanel>
69-
70-
71-
72-
--><!--CONTENT--><!--
73-
<Image Grid.Row="1" Grid.Column="1"
74-
Source="{Binding ImageIcon, Converter={x:Static xaml:IconToBitmapSourceConverter.Instance}}"
75-
Visibility="{Binding ImageIcon, Converter={x:Static xaml:BoolToVisibilityConverter.Instance}}">
76-
<Image.Style>
77-
<Style TargetType="Image">
78-
<Setter Property="VerticalAlignment" Value="Center"/>
79-
<Setter Property="Margin" Value="19,0,0,0"/>
80-
<Style.Triggers>
81-
<DataTrigger Binding="{Binding TextIsMultiline}" Value="True">
82-
<Setter Property="VerticalAlignment" Value="Top"/>
83-
<Setter Property="Margin" Value="19,25,0,0"/>
84-
</DataTrigger>
85-
</Style.Triggers>
86-
</Style>
87-
</Image.Style>
88-
</Image>
89-
90-
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Text}" Margin="8,25,15,30" TextWrapping="Wrap" TextAlignment="{Binding TextAlignment}"/>
91-
92-
93-
94-
--><!--BUTTONS--><!--
95-
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
96-
<ItemsControl ItemsSource="{Binding ButtonList}"
97-
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="43,0,0,0">
98-
<ItemsControl.ItemsPanel>
99-
<ItemsPanelTemplate>
100-
<StackPanel Orientation="Horizontal"/>
101-
</ItemsPanelTemplate>
102-
</ItemsControl.ItemsPanel>
103-
<ItemsControl.ItemTemplate>
104-
<DataTemplate>
105-
<Button Content="{Binding Label}" Width="85" Height="25" Margin="0,0,10,0"
106-
Style="{DynamicResource ButtonPrimaryStyle}"
107-
Command="{xaml:Action ButtonClicked}"
108-
CommandParameter="{Binding Value}">
109-
--><!--<Button.IsDefault>
110-
<MultiBinding Converter="{x:Static xaml:EqualityConverter.Instance}">
111-
<Binding/>
112-
<Binding Path="DataContext.DefaultButton" ElementName="RootObject"/>
113-
</MultiBinding>
114-
</Button.IsDefault>
115-
<Button.IsCancel>
116-
<MultiBinding Converter="{x:Static xaml:EqualityConverter.Instance}">
117-
<Binding/>
118-
<Binding Path="DataContext.CancelButton" ElementName="RootObject"/>
119-
</MultiBinding>
120-
</Button.IsCancel>--><!--
121-
</Button>
122-
</DataTemplate>
123-
</ItemsControl.ItemTemplate>
124-
</ItemsControl>
125-
</Grid>
126-
</Grid>
127-
</Border>-->
128-
129-
13020
<ContentControl s:View.Model="{Binding PageViewModel}"></ContentControl>
13121
</styles:WindowChromeBase>

readme.md

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,10 @@ This App Will Rename to 1Remote in the feature..
3131

3232
Latest Version: 0.7.2.8
3333

34-
|| Stable | Preview |
35-
|---| --- | --- |
36-
| Download | <li>[GitHub release](https://github.com/1Remote/1Remote/releases)</li><li>[Microsoft Store](https://www.microsoft.com/store/productId/9PNMNF92JNFP)</li><li>[Winget](https://github.com/microsoft/winget-cli): `winget install premotem`</li><li>[Chocolatey](https://chocolatey.org/packages/premotem): `choco install premotem`</li> | [Nightly](https://github.com/1Remote/1Remote/releases/tag/Nightly) |
37-
38-
39-
💥New Features in Preview:
40-
41-
- [x] [MySQL support](https://1remote.github.io/usage/database/data-synchronization/#by-using-mysql)
42-
- [x] [Custom servers order by drag](https://1remote.github.io/usage/overview/#sorting)
43-
- [x] [Multi-Credentials for RDP\VNC\SHH...(e.g. keep root and normal-user credentials in one server)](https://1remote.github.io/usage/alternative-credential/#addedit)
44-
- [x] Multi-Address for RDP\VNC\SHH... (e.g. 192.168.0.100 for LAN, and xxx.xx.xxx.xx for WAN)
45-
- [x] [Auto switching between multi-addresses (you don't have to select the address manually)](https://1remote.github.io/usage/alternative-credential/#auto-switching-address)
46-
- [x] [Servers sharing within team (e.g. share servers with your colleagues)](https://1remote.github.io/usage/team/team-sharing/)
47-
48-
### ⚠Requirements
49-
50-
- [Windows10 17763 and above](https://support.lenovo.com/us/en/solutions/ht502786)
51-
- [.NET 6 Desktop Runtime](https://dotnet.microsoft.com/en-us/download/dotnet/6.0/runtime)
52-
53-
> P.S. You can clone the code and remove all of the Win10 dependencies if you are likely to use 1Remote in Win7.
54-
5534
### 🗺[Quick start](https://1remote.github.io/usage/quick-start/)
5635

36+
### 🔻[Download](!https://1remote.github.io/download/)
37+
5738

5839
## 👓Overview
5940

@@ -107,7 +88,7 @@ Latest Version: 0.7.2.8
10788
If you like **1Remote**, help us make it stronger by doing any of the following:
10889

10990
1. Simply star this repository
110-
2. [Help translation](https://github.com/1Remote/1Remote/wiki/Help-wanted:-Translation)
91+
2. [Help translation](https://1remote.github.io/usage/misc/help-translation/)
11192
3. [Buy a coffee](https://ko-fi.com/VShawn)
11293
4. [Join DEV](DEVELOP.md)
11394

0 commit comments

Comments
 (0)