mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-14 05:58:09 +08:00
chore: rebuild ssh outbound
This commit is contained in:
parent
0bb5568de9
commit
5702d28cda
@ -6,10 +6,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
|
"github.com/zhangyunhao116/fastrand"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,7 +21,7 @@ type Ssh struct {
|
|||||||
*Base
|
*Base
|
||||||
|
|
||||||
option *SshOption
|
option *SshOption
|
||||||
client *ssh.Client
|
client *sshClient // using a standalone struct to avoid its inner loop invalidate the Finalizer
|
||||||
}
|
}
|
||||||
|
|
||||||
type SshOption struct {
|
type SshOption struct {
|
||||||
@ -30,18 +34,85 @@ type SshOption struct {
|
|||||||
PrivateKey string `proxy:"privateKey,omitempty"`
|
PrivateKey string `proxy:"privateKey,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Ssh) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
c, err := h.client.Dial("tcp", metadata.RemoteAddress())
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return NewConn(CN.NewRefConn(c, h), h), nil
|
}
|
||||||
|
client, err := s.client.connect(ctx, cDialer, s.addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c, err := client.DialContext(ctx, "tcp", metadata.RemoteAddress())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewConn(N.NewRefConn(c, s), s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeSsh(h *Ssh) {
|
type sshClient struct {
|
||||||
if h.client != nil {
|
config *ssh.ClientConfig
|
||||||
_ = h.client.Close()
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSsh(option SshOption) (*Ssh, error) {
|
func NewSsh(option SshOption) (*Ssh, error) {
|
||||||
@ -55,7 +126,6 @@ func NewSsh(option SshOption) (*Ssh, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if option.Password == "" {
|
if option.Password == "" {
|
||||||
|
|
||||||
b, err := os.ReadFile(option.PrivateKey)
|
b, err := os.ReadFile(option.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -74,23 +144,28 @@ func NewSsh(option SshOption) (*Ssh, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := ssh.Dial("tcp", addr, &config)
|
version := "SSH-2.0-OpenSSH_"
|
||||||
if err != nil {
|
if fastrand.Intn(2) == 0 {
|
||||||
return nil, err
|
version += "7." + strconv.Itoa(fastrand.Intn(10))
|
||||||
|
} else {
|
||||||
|
version += "8." + strconv.Itoa(fastrand.Intn(9))
|
||||||
}
|
}
|
||||||
|
config.ClientVersion = version
|
||||||
|
|
||||||
outbound := &Ssh{
|
outbound := &Ssh{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Ssh,
|
tp: C.Ssh,
|
||||||
udp: true,
|
udp: false,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
client: client,
|
client: &sshClient{
|
||||||
|
config: &config,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(outbound, closeSsh)
|
runtime.SetFinalizer(outbound, closeSsh)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user