mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-08 03:34:13 +08:00

* Fix sniffer errors override each others * Do not return ErrNeedMoreData if header is not expected
32 lines
689 B
Go
32 lines
689 B
Go
package sniff
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
)
|
|
|
|
func SSH(_ context.Context, metadata *adapter.InboundContext, reader io.Reader) error {
|
|
const sshPrefix = "SSH-2.0-"
|
|
bReader := bufio.NewReader(reader)
|
|
prefix, err := bReader.Peek(len(sshPrefix))
|
|
if string(prefix[:]) != sshPrefix[:len(prefix)] {
|
|
return os.ErrInvalid
|
|
}
|
|
if err != nil {
|
|
return E.Cause1(ErrNeedMoreData, err)
|
|
}
|
|
fistLine, _, err := bReader.ReadLine()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
metadata.Protocol = C.ProtocolSSH
|
|
metadata.Client = string(fistLine)[8:]
|
|
return nil
|
|
}
|