2024-03-08 17:38:27 +08:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
2024-03-13 08:30:41 +08:00
|
|
|
"bytes"
|
2024-03-08 17:38:27 +08:00
|
|
|
"context"
|
2024-03-13 08:30:41 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2024-03-08 17:38:27 +08:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2024-03-13 08:30:41 +08:00
|
|
|
"strings"
|
2024-03-08 19:27:41 +08:00
|
|
|
"sync"
|
2024-03-08 17:38:27 +08:00
|
|
|
|
2024-03-08 19:27:41 +08:00
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
2024-03-08 17:38:27 +08:00
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
2024-03-08 19:27:41 +08:00
|
|
|
"github.com/metacubex/mihomo/component/proxydialer"
|
2024-03-08 17:38:27 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-03-08 19:27:41 +08:00
|
|
|
|
|
|
|
"github.com/zhangyunhao116/fastrand"
|
2024-03-08 17:38:27 +08:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Ssh struct {
|
|
|
|
*Base
|
|
|
|
|
|
|
|
option *SshOption
|
2024-03-08 19:27:41 +08:00
|
|
|
client *sshClient // using a standalone struct to avoid its inner loop invalidate the Finalizer
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SshOption struct {
|
|
|
|
BasicOption
|
2024-03-13 08:30:41 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
UserName string `proxy:"username"`
|
|
|
|
Password string `proxy:"password,omitempty"`
|
|
|
|
PrivateKey string `proxy:"private-key,omitempty"`
|
|
|
|
PrivateKeyPassphrase string `proxy:"private-key-passphrase,omitempty"`
|
|
|
|
HostKey []string `proxy:"host-key,omitempty"`
|
|
|
|
HostKeyAlgorithms []string `proxy:"host-key-algorithms,omitempty"`
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
|
2024-03-08 19:27:41 +08:00
|
|
|
func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
|
|
|
var cDialer C.Dialer = dialer.NewDialer(s.Base.DialOptions(opts...)...)
|
|
|
|
if len(s.option.DialerProxy) > 0 {
|
|
|
|
cDialer, err = proxydialer.NewByName(s.option.DialerProxy, cDialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client, err := s.client.connect(ctx, cDialer, s.addr)
|
2024-03-08 17:38:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-08 19:27:41 +08:00
|
|
|
c, err := client.DialContext(ctx, "tcp", metadata.RemoteAddress())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConn(N.NewRefConn(c, s), s), nil
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
|
2024-03-08 19:27:41 +08:00
|
|
|
type sshClient struct {
|
|
|
|
config *ssh.ClientConfig
|
|
|
|
client *ssh.Client
|
|
|
|
cMutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sshClient) connect(ctx context.Context, cDialer C.Dialer, addr string) (client *ssh.Client, err error) {
|
|
|
|
s.cMutex.Lock()
|
|
|
|
defer s.cMutex.Unlock()
|
|
|
|
if s.client != nil {
|
|
|
|
return s.client, nil
|
|
|
|
}
|
|
|
|
c, err := cDialer.DialContext(ctx, "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
N.TCPKeepAlive(c)
|
|
|
|
|
|
|
|
defer func(c net.Conn) {
|
|
|
|
safeConnClose(c, err)
|
|
|
|
}(c)
|
|
|
|
|
|
|
|
if ctx.Done() != nil {
|
|
|
|
done := N.SetupContextForConn(ctx, c)
|
|
|
|
defer done(&err)
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
2024-03-08 19:27:41 +08:00
|
|
|
|
|
|
|
clientConn, chans, reqs, err := ssh.NewClientConn(c, addr, s.config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client = ssh.NewClient(clientConn, chans, reqs)
|
|
|
|
|
|
|
|
s.client = client
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_ = client.Wait() // wait shutdown
|
|
|
|
_ = client.Close()
|
|
|
|
s.cMutex.Lock()
|
|
|
|
defer s.cMutex.Unlock()
|
|
|
|
if s.client == client {
|
|
|
|
s.client = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sshClient) Close() error {
|
|
|
|
s.cMutex.Lock()
|
|
|
|
defer s.cMutex.Unlock()
|
|
|
|
if s.client != nil {
|
|
|
|
return s.client.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeSsh(s *Ssh) {
|
|
|
|
_ = s.client.Close()
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSsh(option SshOption) (*Ssh, error) {
|
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
|
|
|
|
|
|
config := ssh.ClientConfig{
|
2024-03-13 08:30:41 +08:00
|
|
|
User: option.UserName,
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
HostKeyAlgorithms: option.HostKeyAlgorithms,
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
|
2024-03-13 08:30:41 +08:00
|
|
|
if option.PrivateKey != "" {
|
|
|
|
var b []byte
|
|
|
|
var err error
|
|
|
|
if strings.Contains(option.PrivateKey, "PRIVATE KEY") {
|
|
|
|
b = []byte(option.PrivateKey)
|
|
|
|
} else {
|
|
|
|
b, err = os.ReadFile(C.Path.Resolve(option.PrivateKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var pKey ssh.Signer
|
|
|
|
if option.PrivateKeyPassphrase != "" {
|
|
|
|
pKey, err = ssh.ParsePrivateKeyWithPassphrase(b, []byte(option.PrivateKeyPassphrase))
|
|
|
|
} else {
|
|
|
|
pKey, err = ssh.ParsePrivateKey(b)
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-13 08:30:41 +08:00
|
|
|
config.Auth = append(config.Auth, ssh.PublicKeys(pKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.Password != "" {
|
|
|
|
config.Auth = append(config.Auth, ssh.Password(option.Password))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(option.HostKey) != 0 {
|
|
|
|
keys := make([]ssh.PublicKey, len(option.HostKey))
|
|
|
|
for i, hostKey := range option.HostKey {
|
|
|
|
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(hostKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse host key :%s", key)
|
|
|
|
}
|
|
|
|
keys[i] = key
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
2024-03-13 08:30:41 +08:00
|
|
|
config.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
serverKey := key.Marshal()
|
|
|
|
for _, hostKey := range keys {
|
|
|
|
if bytes.Equal(serverKey, hostKey.Marshal()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("host key mismatch, server send :%s %s", key.Type(), base64.StdEncoding.EncodeToString(serverKey))
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 19:27:41 +08:00
|
|
|
version := "SSH-2.0-OpenSSH_"
|
|
|
|
if fastrand.Intn(2) == 0 {
|
|
|
|
version += "7." + strconv.Itoa(fastrand.Intn(10))
|
|
|
|
} else {
|
|
|
|
version += "8." + strconv.Itoa(fastrand.Intn(9))
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
2024-03-08 19:27:41 +08:00
|
|
|
config.ClientVersion = version
|
2024-03-08 17:38:27 +08:00
|
|
|
|
|
|
|
outbound := &Ssh{
|
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.Ssh,
|
2024-03-08 19:27:41 +08:00
|
|
|
udp: false,
|
2024-03-08 17:38:27 +08:00
|
|
|
iface: option.Interface,
|
|
|
|
rmark: option.RoutingMark,
|
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
|
|
},
|
|
|
|
option: &option,
|
2024-03-08 19:27:41 +08:00
|
|
|
client: &sshClient{
|
|
|
|
config: &config,
|
|
|
|
},
|
2024-03-08 17:38:27 +08:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(outbound, closeSsh)
|
|
|
|
|
|
|
|
return outbound, nil
|
|
|
|
}
|