forked from SagerNet/LibSagerNetCore
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathssh.go
More file actions
176 lines (155 loc) · 4.02 KB
/
Copy pathssh.go
File metadata and controls
176 lines (155 loc) · 4.02 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
package libcore
import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/Dreamacro/clash/adapter/outbound"
clashC "github.com/Dreamacro/clash/constant"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"net"
"strconv"
"strings"
"sync"
)
type sshClient struct {
*outbound.Base
access sync.Mutex
client *ssh.Client
username string
auth []ssh.AuthMethod
hostKeyCallback ssh.HostKeyCallback
}
func (s *sshClient) Close() error {
if s.client == nil {
return nil
}
return s.client.Close()
}
func (s *sshClient) StreamConn(_ net.Conn, _ *clashC.Metadata) (net.Conn, error) {
panic("unimplemented")
}
func (s *sshClient) connect() (*ssh.Client, error) {
if s.client != nil {
return s.client, nil
}
s.access.Lock()
defer s.access.Unlock()
if s.client != nil {
return s.client, nil
}
config := &ssh.ClientConfig{
User: s.username,
Auth: s.auth,
HostKeyCallback: s.hostKeyCallback,
}
logrus.Debugf("SSH dial to %s", s.Addr())
client, err := ssh.Dial("tcp", s.Addr(), config)
if err != nil {
err = errors.WithMessage(err, "connect ssh")
logrus.Warnf("%v", err)
return nil, err
}
logrus.Debug("SSH conn success")
go func() {
err := client.Wait()
logrus.Debugf("SSH connection closed: %v", err)
s.client = nil
}()
s.client = client
return client, nil
}
func (s *sshClient) DialContext(_ context.Context, metadata *clashC.Metadata) (_ clashC.Conn, err error) {
client, err := s.connect()
if err != nil {
return nil, err
}
c, err := client.Dial(metadata.NetWork.String(), metadata.RemoteAddress())
if err != nil {
err = fmt.Errorf("%s connect error: %w", s.Addr(), err)
logrus.Warn("%v", err)
return nil, err
}
tcpKeepAlive(c)
defer safeConnClose(c, err)
return outbound.NewConn(c, s), nil
}
const (
authTypeNone = iota
authTypePassword
authTypePublicKey
)
func NewSSHInstance(socksPort int32, serverAddress string, serverPort int32, username string, auth int32, password string, pem string, passphrase string, pubKey string) (*ClashBasedInstance, error) {
addr := net.JoinHostPort(serverAddress, strconv.Itoa(int(serverPort)))
if username == "" {
username = "root"
}
out := &sshClient{
Base: outbound.NewBase("", addr, -1, false),
username: username,
}
switch auth {
case authTypePassword:
out.auth = []ssh.AuthMethod{ssh.Password(password)}
case authTypePublicKey:
var signer ssh.Signer
var err error
if passphrase == "" {
signer, err = ssh.ParsePrivateKey([]byte(pem))
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(pem), []byte(passphrase))
}
if err != nil {
return nil, errors.WithMessage(err, "parse private key")
}
out.auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
}
var keys []ssh.PublicKey
if pubKey != "" {
for _, str := range strings.Split(pubKey, "\n") {
str = strings.TrimSpace(str)
if str == "" {
continue
}
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubKey))
if err != nil {
if err != nil {
return nil, errors.WithMessage(err, "parse public key")
}
}
keys = append(keys, key)
}
}
if keys != nil {
out.hostKeyCallback = (&fixedHostKey{keys}).check
} else {
out.hostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
logrus.Infof("ssh: server send %s %s", key.Type(), base64Encode(key.Marshal()))
return nil
}
}
return newClashBasedInstance(socksPort, out), nil
}
type fixedHostKey struct {
keys []ssh.PublicKey
}
func (f *fixedHostKey) check(_ string, _ net.Addr, key ssh.PublicKey) error {
if f.keys == nil {
return fmt.Errorf("ssh: required host key was nil")
}
for _, pk := range f.keys {
if bytes.Equal(key.Marshal(), pk.Marshal()) {
return nil
}
}
return fmt.Errorf("ssh: host key mismatch, server send %s %s", key.Type(), base64Encode(key.Marshal()))
}
func base64Encode(data []byte) string {
b := bytes.Buffer{}
w := base64.NewEncoder(base64.StdEncoding, &b)
_, _ = w.Write(data)
return hex.EncodeToString(b.Bytes())
}