-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdssh_windows.go
More file actions
194 lines (173 loc) · 4.17 KB
/
Copy pathdssh_windows.go
File metadata and controls
194 lines (173 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//go:build windows
// +build windows
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
su "github.com/nyaosorg/go-windows-su"
"github.com/xlab/closer"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
const win = true
var (
PuTTY = `SOFTWARE\SimonTatham\PuTTY` //root
Sessions = filepath.Join(PuTTY, "Sessions") //dir
SshHostCAs = filepath.Join(PuTTY, "SshHostCAs") //dir
SshHostKeys = filepath.Join(PuTTY, "SshHostKeys") //file
)
// Конфиг для putty на Windows
func Conf(name, _ string, kv map[string]string) {
rk, _, err := registry.CreateKey(registry.CURRENT_USER,
name,
registry.CREATE_SUB_KEY|registry.SET_VALUE)
if err != nil {
Println(err)
return
}
defer rk.Close()
for k, v := range kv {
if i, err := strconv.Atoi(v); err == nil {
rk.SetDWordValue(k, uint32(i))
} else {
rk.SetStringValue(k, v)
}
}
}
func GlobalSshPath() string {
return filepath.Join(os.Getenv("ProgramData"), SSH)
}
// cmd = exec.Command("cmd", "/c", "start", "/b", bin, opt)
func createNewConsole(cmd *exec.Cmd) {
const CREATE_NEW_CONSOLE = 0x10
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: CREATE_NEW_CONSOLE,
NoInheritHandles: true,
}
}
func isWin7() bool {
maj, min, _ := windows.RtlGetNtVersionNumbers()
return maj < 6 || (maj == 6 && min <= 1)
}
func ConsoleCP() {
const CP_UTF8 uint32 = 65001
var kernel32 = windows.NewLazyDLL("kernel32.dll")
getConsoleCP := func() uint32 {
result, _, _ := kernel32.NewProc("GetConsoleCP").Call()
return uint32(result)
}
getConsoleOutputCP := func() uint32 {
result, _, _ := kernel32.NewProc("GetConsoleOutputCP").Call()
return uint32(result)
}
setConsoleCP := func(cp uint32) {
kernel32.NewProc("SetConsoleCP").Call(uintptr(cp))
}
setConsoleOutputCP := func(cp uint32) {
kernel32.NewProc("SetConsoleOutputCP").Call(uintptr(cp))
}
inCP := getConsoleCP()
outCP := getConsoleOutputCP()
setConsoleCP(CP_UTF8)
setConsoleOutputCP(CP_UTF8)
closer.Bind(func() { setConsoleCP(inCP) })
closer.Bind(func() { setConsoleOutputCP(outCP) })
}
func sx(ctx context.Context, u, hp string) {
x := "sftp"
if args.Scp {
x = "scp"
if args.Sftp {
// -39
// -93
x = "ssh"
}
}
opt := fmt.Sprintf("%s://%s@%s/", x, u, hp)
err := fmt.Errorf("not found handler - не найден обработчик %q", x)
if tryWinSCP {
_, err = su.ShellExecute(su.OPEN, opt, "", "")
Println("start", opt, err)
if err == nil {
return
}
}
bin := ""
if x == "ssh" && tryPuTTY {
bin = PUTTY
opt := []string{}
if args.LoginName != "" {
opt = append(opt, "-l", args.LoginName)
}
if args.Port > 0 {
opt = append(opt, "-P", strconv.Itoa(args.Port))
}
opt = append(opt, "-load", args.Destination)
cmd := exec.CommandContext(ctx, bin, opt...)
cmdStart(cmd)
return
}
// scp
// sftp
if x == "scp" {
Println(err)
x = "sftp"
}
if tryFileZilla {
bin, err = exec.LookPath(fileZillaBin)
if err != nil {
bin = filepath.Join(os.Getenv("ProgramFiles"), "FileZilla FTP Client", fileZillaBin)
bin, err = exec.LookPath(bin)
}
if err == nil {
ucd := ""
ucd, err = os.UserConfigDir()
if err == nil {
u, err = uhp2u(u, filepath.Join(ucd, fileZillaBin, fileZillaXml))
if err == nil {
cmdStart(exec.CommandContext(ctx, bin, "-l", "interactive", fmt.Sprintf("%s://%s@%s/", x, u, hp)))
return
}
}
}
}
if tryOpenSSH {
bin, err = exec.LookPath(x)
if err == nil {
opt := []string{}
if args.ProxyJump != "" {
opt = append(opt, "-J", args.ProxyJump)
}
for _, v := range args.Option.Marshal() {
opt = append(opt, "-o", v)
}
cmd := exec.CommandContext(ctx, bin, append(opt, args.Destination)...)
createNewConsole(cmd)
cmdStart(cmd)
return
}
}
if tryPuTTY {
bin, err = exec.LookPath("p" + x)
if err == nil {
opt := []string{}
if args.LoginName != "" {
opt = append(opt, "-l", args.LoginName)
}
if args.Port > 0 {
opt = append(opt, "-P", strconv.Itoa(args.Port))
}
opt = append(opt, "-load", args.Destination)
cmd := exec.CommandContext(ctx, bin, opt...)
createNewConsole(cmd)
cmdStart(cmd)
return
}
}
Println(err)
}