Skip to content

Commit

Permalink
merge(swiftui): Merge pull request #17 from genshen/feature-macos-cop…
Browse files Browse the repository at this point in the history
…y-proxy-command

Feature of "copy proxy command" for git/ssh/http/https in swiftui client
  • Loading branch information
genshen authored Feb 3, 2024
2 parents df3d4f3 + 72788b7 commit 2383252
Showing 1 changed file with 56 additions and 13 deletions.
69 changes: 56 additions & 13 deletions swiftui-client/wssocks-ustb-client/menu/MenuBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

import SwiftUI

enum CopyProxyCommandType: Int{
case GIT = 1
case HTTP = 2
case SSH = 3
}

struct MenuBarView: View {
@Namespace var animation
@State private var showingAlert = false
Expand Down Expand Up @@ -82,8 +88,18 @@ struct MenuBarView: View {
.alert(isPresented: $showingAlert) {
Alert(title: Text("Error"), message: Text("\(alertMessage)"), dismissButton: .default(Text("OK")))
}
Spacer()
// quit button
Button(action: quitApp) {
if #available(macOS 11.0, *) {
Image(systemName: "xmark.circle.fill")
.foregroundColor(Color.accentColor)
}else {
Text("Quit")
}
}.controlSize(.large) // .buttonStyle(BorderlessButtonStyle())
}
.padding(.horizontal)
.padding(.horizontal, 8)
// .padding(.top)

// Divider().padding(.top, 4)
Expand All @@ -106,18 +122,23 @@ struct MenuBarView: View {
// Fallback on earlier versions
}

// quit button
if #available(macOS 11.0, *) {
Button(action: quitApp) {
Label("Quit App", systemImage: "xmark.circle.fill")
//.foregroundColor(Color.accentColor)
}.buttonStyle(BorderlessButtonStyle())
} else {
// Fallback on earlier versions
}

Divider().padding(.top, 4)

HStack{
Menu("Copy Proxy Command") {
Button(action: {
copyProxyCommand(tp: CopyProxyCommandType.GIT)
}, label: {
Label("git (ssh)", systemImage: "heart.circle.fill")
.symbolRenderingMode(.multicolor)
})
Button(action: {
copyProxyCommand(tp: CopyProxyCommandType.HTTP)
}, label: { Text("http/https")})
Button(action: {
copyProxyCommand(tp: CopyProxyCommandType.SSH)
}, label: { Text("ssh/scp/sftp")})
}
}.padding(.horizontal, 8)
// bottom view
HStack{
Button (action:{
Expand All @@ -144,7 +165,29 @@ struct MenuBarView: View {
.padding(.bottom, 4)
.padding(.horizontal, 8)
}
.frame(width: 250, height: 250)
.frame(width: 250, height: 280)
}

private func copyProxyCommand(tp: CopyProxyCommandType) {
let socks_proxy_addr = self.configsInView.uiSocks5Addr
let http_proxy_addr = self.configsInView.uiHttpAddr
var text = ""

switch tp {
case .GIT:
text = "export GIT_SSH_COMMAND=\"ssh -o ProxyCommand='nc -x \(socks_proxy_addr) %h %p' \""
break;
case .HTTP:
text = "export https_proxy=http://\(socks_proxy_addr) http_proxy=http://\(http_proxy_addr)"
break;
case .SSH:
text = "ssh -o ProxyCommand='nc -x \(socks_proxy_addr) %h %p'"
break;
}

let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(text, forType: .string)
}

func checkPref() -> Bool {
Expand Down

0 comments on commit 2383252

Please sign in to comment.