Skip to content

Commit

Permalink
Added Copy Terminal proxy command to clipboard in tray menu
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jan 12, 2025
1 parent cb94d64 commit 649e89e
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 1 deletion.
9 changes: 9 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1390,4 +1390,7 @@
<data name="TbSettingsTheme" xml:space="preserve">
<value>تم</value>
</data>
</root>
<data name="menuCopyProxyCmdToClipboard" xml:space="preserve">
<value>Copy proxy command to clipboard</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.hu.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1390,4 +1390,7 @@
<data name="TbSettingsTheme" xml:space="preserve">
<value>Theme</value>
</data>
<data name="menuCopyProxyCmdToClipboard" xml:space="preserve">
<value>Copy proxy command to clipboard</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1390,4 +1390,7 @@
<data name="TbSettingsTheme" xml:space="preserve">
<value>Theme</value>
</data>
<data name="menuCopyProxyCmdToClipboard" xml:space="preserve">
<value>Copy proxy command to clipboard</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1390,4 +1390,7 @@
<data name="TbSettingsTheme" xml:space="preserve">
<value>Theme</value>
</data>
<data name="menuCopyProxyCmdToClipboard" xml:space="preserve">
<value>Copy proxy command to clipboard</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1387,4 +1387,7 @@
<data name="TbSettingsTheme" xml:space="preserve">
<value>主题</value>
</data>
<data name="menuCopyProxyCmdToClipboard" xml:space="preserve">
<value>复制终端代理命令至剪贴板</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1388,4 +1388,7 @@
<data name="TbSettingsTheme" xml:space="preserve">
<value>主題</value>
</data>
<data name="menuCopyProxyCmdToClipboard" xml:space="preserve">
<value>複製終端代理指令至剪貼簿</value>
</data>
</root>
23 changes: 23 additions & 0 deletions v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class StatusBarViewModel : MyReactiveObject
public ReactiveCommand<Unit, Unit> AddServerViaScanCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateViaProxyCmd { get; }
public ReactiveCommand<Unit, Unit> CopyProxyCmdToClipboardCmd { get; }
public ReactiveCommand<Unit, Unit> NotifyLeftClickCmd { get; }

#region System Proxy
Expand Down Expand Up @@ -128,6 +129,11 @@ public StatusBarViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
y => y == true)
.Subscribe(async c => await DoEnableTun(c));

CopyProxyCmdToClipboardCmd = ReactiveCommand.CreateFromTask(async () =>
{
await CopyProxyCmdToClipboard();
});

NotifyLeftClickCmd = ReactiveCommand.CreateFromTask(async () =>
{
Locator.Current.GetService<MainWindowViewModel>()?.ShowHideWindow(null);
Expand Down Expand Up @@ -198,6 +204,23 @@ private async void OnNext(string x)
await _updateView?.Invoke(EViewAction.DispatcherRefreshServersBiz, null);
}

private async Task CopyProxyCmdToClipboard()
{
var cmd = Utils.IsWindows() ? "set" : "export";
var address = $"{Global.Loopback}:{AppHandler.Instance.GetLocalPort(EInboundProtocol.socks)}";

var sb = new StringBuilder();
sb.AppendLine($"{cmd} http_proxy=http://{address}");
sb.AppendLine($"{cmd} https_proxy=http://{address}");
sb.AppendLine($"{cmd} all_proxy=socks5://{address}");
sb.AppendLine("");
sb.AppendLine($"{cmd} HTTP_PROXY=http://{address}");
sb.AppendLine($"{cmd} HTTPS_PROXY=http://{address}");
sb.AppendLine($"{cmd} ALL_PROXY=socks5://{address}");

await _updateView?.Invoke(EViewAction.SetClipboardData, sb.ToString());
}

private async Task AddServerViaClipboard()
{
var service = Locator.Current.GetService<MainWindowViewModel>();
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN.Desktop/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
<NativeMenuItem Command="{Binding SubUpdateCmd}" Header="{x:Static resx:ResUI.menuSubUpdate}" />
<NativeMenuItem Command="{Binding SubUpdateViaProxyCmd}" Header="{x:Static resx:ResUI.menuSubUpdateViaProxy}" />
<NativeMenuItemSeparator />
<NativeMenuItem Command="{Binding CopyProxyCmdToClipboardCmd}" Header="{x:Static resx:ResUI.menuCopyProxyCmdToClipboard}" />
<NativeMenuItemSeparator />
<NativeMenuItem Command="{Binding NotifyLeftClickCmd}" Header="{x:Static resx:ResUI.menuShowOrHideMainWindow}" />
<NativeMenuItem Click="MenuExit_Click" Header="{x:Static resx:ResUI.menuExit}" />
</NativeMenu>
Expand Down
5 changes: 5 additions & 0 deletions v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
},
DispatcherPriority.Default);
break;

case EViewAction.SetClipboardData:
if (obj is null) return false;
await AvaUtils.SetClipboardData(this, (string)obj);
break;
}
return await Task.FromResult(true);
}
Expand Down
5 changes: 5 additions & 0 deletions v2rayN/v2rayN/Views/StatusBarView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
Height="{StaticResource MenuItemHeight}"
Header="{x:Static resx:ResUI.menuSubUpdateViaProxy}" />
<Separator />
<MenuItem
x:Name="menuCopyProxyCmdToClipboard"
Height="{StaticResource MenuItemHeight}"
Header="{x:Static resx:ResUI.menuCopyProxyCmdToClipboard}" />
<Separator />
<MenuItem
x:Name="menuExit"
Height="{StaticResource MenuItemHeight}"
Expand Down
6 changes: 6 additions & 0 deletions v2rayN/v2rayN/Views/StatusBarView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public StatusBarView()
this.BindCommand(ViewModel, vm => vm.SubUpdateCmd, v => v.menuSubUpdate2).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SubUpdateViaProxyCmd, v => v.menuSubUpdateViaProxy2).DisposeWith(disposables);

this.BindCommand(ViewModel, vm => vm.CopyProxyCmdToClipboardCmd, v => v.menuCopyProxyCmdToClipboard).DisposeWith(disposables);

this.OneWayBind(ViewModel, vm => vm.RunningServerToolTipText, v => v.tbNotify.ToolTipText).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.NotifyLeftClickCmd, v => v.tbNotify.LeftClickCommand).DisposeWith(disposables);

Expand Down Expand Up @@ -96,6 +98,10 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
Application.Current.MainWindow.Icon = WindowsHandler.Instance.GetAppIcon(_config);
}), DispatcherPriority.Normal);
break;
case EViewAction.SetClipboardData:
if (obj is null) return false;
WindowsUtils.SetClipboardData((string)obj);
break;
}
return await Task.FromResult(true);
}
Expand Down

2 comments on commit 649e89e

@junlongzzz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了 Win 内的 CMD 命令行工具,PowerShell 是这样设置的:

$env:HTTP_PROXY="http://127.0.0.1:10808"; $env:HTTPS_PROXY="http://127.0.0.1:10808"

@2dust
Copy link
Owner Author

@2dust 2dust commented on 649e89e Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

windows版本只是凑和写下,应该是没有需要用的时候

Please sign in to comment.