2023-02-15 00:53:53 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-15 00:53:53 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package sniffing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-06-16 13:47:02 +07:00
|
|
|
"net"
|
|
|
|
"strings"
|
2023-02-15 00:53:53 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-11-15 13:32:57 +07:00
|
|
|
Error = fmt.Errorf("sniffing error")
|
|
|
|
ErrNotApplicable = fmt.Errorf("%w: not applicable", Error)
|
2024-07-22 07:51:26 +07:00
|
|
|
ErrNeedMore = fmt.Errorf("%w: need more", Error)
|
2023-11-15 13:32:57 +07:00
|
|
|
ErrNotFound = fmt.Errorf("%w: not found", Error)
|
2023-02-15 00:53:53 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsSniffingError(err error) bool {
|
|
|
|
return errors.Is(err, Error)
|
|
|
|
}
|
2024-06-16 13:47:02 +07:00
|
|
|
|
|
|
|
func NormalizeDomain(host string) string {
|
|
|
|
host = strings.ToLower(strings.TrimSpace(host))
|
|
|
|
if strings.HasSuffix(host, "]") {
|
|
|
|
// Sniffed domain may be like `[2606:4700:20::681a:d1f]`. We should remove the brackets.
|
|
|
|
return strings.Trim(host, "[]")
|
|
|
|
}
|
|
|
|
if domain, _, err := net.SplitHostPort(host); err == nil {
|
|
|
|
return domain
|
|
|
|
}
|
|
|
|
return strings.TrimSuffix(host, ".")
|
|
|
|
}
|