Files
sing-box-extended-mirror/common/sniff/ssh.go
T

32 lines
689 B
Go
Raw Normal View History

2024-08-20 18:56:50 +08:00
package sniff
import (
"bufio"
"context"
"io"
"os"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
2025-04-04 16:21:50 +08:00
E "github.com/sagernet/sing/common/exceptions"
2024-08-20 18:56:50 +08:00
)
func SSH(_ context.Context, metadata *adapter.InboundContext, reader io.Reader) error {
2025-04-04 16:21:50 +08:00
const sshPrefix = "SSH-2.0-"
bReader := bufio.NewReader(reader)
prefix, err := bReader.Peek(len(sshPrefix))
2025-04-22 14:44:55 +08:00
if string(prefix[:]) != sshPrefix[:len(prefix)] {
return os.ErrInvalid
}
2025-04-04 16:21:50 +08:00
if err != nil {
return E.Cause1(ErrNeedMoreData, err)
2024-08-20 18:56:50 +08:00
}
2025-04-04 16:21:50 +08:00
fistLine, _, err := bReader.ReadLine()
if err != nil {
return err
2024-08-20 18:56:50 +08:00
}
metadata.Protocol = C.ProtocolSSH
2025-04-04 16:21:50 +08:00
metadata.Client = string(fistLine)[8:]
2024-08-20 18:56:50 +08:00
return nil
}