mirror of
https://github.com/daeuniverse/dae.git
synced 2025-01-05 13:08:57 +07:00
c3ccf179d9
Co-authored-by: dae-prow[bot] <136105375+dae-prow[bot]@users.noreply.github.com> Co-authored-by: Sumire (菫) <151038614+sumire88@users.noreply.github.com>
37 lines
859 B
Go
37 lines
859 B
Go
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
|
*/
|
|
|
|
package sniffing
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
Error = fmt.Errorf("sniffing error")
|
|
ErrNotApplicable = fmt.Errorf("%w: not applicable", Error)
|
|
ErrNeedMore = fmt.Errorf("%w: need more", Error)
|
|
ErrNotFound = fmt.Errorf("%w: not found", Error)
|
|
)
|
|
|
|
func IsSniffingError(err error) bool {
|
|
return errors.Is(err, Error)
|
|
}
|
|
|
|
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, ".")
|
|
}
|