mirror of
https://github.com/wweir/sower.git
synced 2024-04-21 12:42:15 +00:00
40 lines
623 B
Go
40 lines
623 B
Go
package router
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/wweir/sower/pkg/deferlog"
|
|
)
|
|
|
|
var pingClient = http.Client{
|
|
Timeout: 2 * time.Second,
|
|
}
|
|
|
|
func (r *Router) isAccess(domain string, port uint16) bool {
|
|
switch port {
|
|
case 80:
|
|
case 443:
|
|
default:
|
|
return false
|
|
}
|
|
|
|
p := &ping{}
|
|
_ = r.accessCache.Remember(p, domain)
|
|
return p.isAccess
|
|
}
|
|
|
|
type ping struct {
|
|
isAccess bool
|
|
}
|
|
|
|
func (p *ping) Fulfill(key string) error {
|
|
_, err := pingClient.Head(net.JoinHostPort(key, "80"))
|
|
deferlog.Std.DebugWarn(err).
|
|
Str("domain", key).
|
|
Msg("detect if site is accessible")
|
|
p.isAccess = (err == nil)
|
|
return nil
|
|
}
|