feta/instance.go

295 lines
5.8 KiB
Go
Raw Normal View History

2019-10-24 10:38:16 +00:00
package main
2019-11-02 06:56:17 +00:00
import "encoding/json"
import "fmt"
import "io/ioutil"
2019-11-02 06:56:17 +00:00
import "net/http"
import "strings"
2019-11-03 10:56:50 +00:00
import "sync"
2019-11-02 06:56:17 +00:00
import "time"
import "github.com/rs/zerolog/log"
2019-10-24 10:38:16 +00:00
const NodeInfoSchemaVersionTwoName = "http://nodeinfo.diaspora.software/ns/schema/2.0"
const INSTANCE_HTTP_TIMEOUT = time.Second * 60
const INSTANCE_SPIDER_INTERVAL = time.Second * 60
const INSTANCE_ERROR_INTERVAL = time.Second * 60 * 30
type InstanceImplementation int
const (
Unknown InstanceImplementation = iota
Mastodon
Pleroma
)
type InstanceStatus int
2019-10-24 11:56:44 +00:00
const (
InstanceStatusNone InstanceStatus = iota
InstanceStatusUnknown
InstanceStatusAlive
InstanceStatusFailure
2019-10-24 11:56:44 +00:00
)
type Instance struct {
2019-11-03 10:56:50 +00:00
sync.Mutex
errorCount uint
successCount uint
highestId int
hostName string
2019-11-03 11:09:04 +00:00
identified bool
impl InstanceImplementation
2019-11-03 13:17:00 +00:00
backend *InstanceBackend
status InstanceStatus
2019-11-03 10:56:50 +00:00
nextCheck *time.Time
nodeInfoUrl string
serverVersion string
2019-10-24 10:38:16 +00:00
}
2019-10-24 11:56:44 +00:00
func NewInstance(hostname string) *Instance {
i := new(Instance)
i.hostName = hostname
i.status = InstanceStatusUnknown
t := time.Now().Add(-1 * time.Second)
i.nextCheck = &t
// FIXME make checks detect the node type instead of in the constructor
2019-10-24 11:56:44 +00:00
return i
}
func (i *Instance) setNextCheck(d time.Duration) {
2019-11-03 10:56:50 +00:00
i.Lock()
defer i.Unlock()
then := time.Now().Add(d)
2019-11-03 11:09:04 +00:00
i.nextCheck = &then
2019-11-03 10:56:50 +00:00
}
func (i *Instance) dueForCheck() bool {
i.Lock()
defer i.Unlock()
2019-11-03 11:09:04 +00:00
return i.nextCheck.Before(time.Now())
2019-11-03 10:56:50 +00:00
}
2019-10-24 11:56:44 +00:00
func (i *Instance) detectNodeType() {
2019-11-03 10:56:50 +00:00
i.Lock()
if i.impl > Unknown {
2019-11-03 10:56:50 +00:00
i.Unlock()
2019-10-24 11:56:44 +00:00
return
}
2019-11-03 10:56:50 +00:00
i.Unlock()
i.fetchNodeInfo()
2019-10-24 11:56:44 +00:00
}
func (i *Instance) registerError() {
i.setNextCheck(INSTANCE_ERROR_INTERVAL)
2019-11-03 10:56:50 +00:00
i.Lock()
defer i.Unlock()
i.errorCount++
}
func (i *Instance) registerSuccess() {
i.setNextCheck(INSTANCE_SPIDER_INTERVAL)
2019-11-03 10:56:50 +00:00
i.Lock()
defer i.Unlock()
i.successCount++
}
func (i *Instance) fetchNodeInfoURL() {
url := fmt.Sprintf("https://%s/.well-known/nodeinfo", i.hostName)
2019-10-24 11:56:44 +00:00
var c = &http.Client{
Timeout: INSTANCE_HTTP_TIMEOUT,
2019-10-24 11:56:44 +00:00
}
log.Debug().
Str("url", url).
Str("hostname", i.hostName).
Msg("fetching nodeinfo reference URL")
resp, err := c.Get(url)
2019-10-24 11:56:44 +00:00
if err != nil {
log.Debug().
Str("hostname", i.hostName).
Err(err).
Msg("unable to fetch nodeinfo, node is down?")
i.registerError()
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Debug().
Str("hostname", i.hostName).
Err(err).
Msg("unable to read nodeinfo")
i.registerError()
return
}
nir := new(NodeInfoWellKnownResponse)
err = json.Unmarshal(body, &nir)
if err != nil {
log.Error().
Str("hostname", i.hostName).
Err(err).
Msg("unable to parse nodeinfo, node is weird")
i.registerError()
return
}
for _, item := range nir.Links {
if item.Rel == NodeInfoSchemaVersionTwoName {
log.Info().
Str("hostname", i.hostName).
Str("nodeinfourl", item.Href).
Msg("success fetching url for nodeinfo")
i.Lock()
i.nodeInfoUrl = item.Href
i.Unlock()
i.registerSuccess()
return
}
}
log.Error().
Str("hostname", i.hostName).
Msg("incomplete nodeinfo")
i.registerError()
return
}
func (i *Instance) fetchNodeInfo() {
i.fetchNodeInfoURL()
2019-11-03 10:56:50 +00:00
i.Lock()
failure := false
if i.nodeInfoUrl == "" {
log.Error().
Str("hostname", i.hostName).
Msg("unable to fetch nodeinfo as nodeinfo URL cannot be determined")
2019-11-03 10:56:50 +00:00
failure = true
}
i.Unlock()
if failure == true {
return
2019-10-24 11:56:44 +00:00
}
var c = &http.Client{
Timeout: INSTANCE_HTTP_TIMEOUT,
}
//FIXME make sure the nodeinfourl is on the same domain as the instance
//hostname
2019-11-03 10:56:50 +00:00
i.Lock()
url := i.nodeInfoUrl
i.Unlock()
resp, err := c.Get(url)
if err != nil {
log.Error().
Str("hostname", i.hostName).
Err(err).
Msgf("unable to fetch nodeinfo data")
i.registerError()
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error().
Str("hostname", i.hostName).
Err(err).
Msgf("unable to read nodeinfo data")
i.registerError()
return
}
ni := new(NodeInfoVersionTwoSchema)
err = json.Unmarshal(body, &ni)
if err != nil {
log.Error().
Str("hostname", i.hostName).
Err(err).
Msgf("unable to parse nodeinfo")
i.registerError()
return
}
log.Info().
Str("serverVersion", ni.Software.Version).
Str("software", ni.Software.Name).
Str("hostName", i.hostName).
Str("nodeInfoUrl", i.nodeInfoUrl).
Msg("received nodeinfo from instance")
i.Lock()
defer i.Unlock()
i.serverVersion = ni.Software.Version
ni.Software.Name = strings.ToLower(ni.Software.Name)
if ni.Software.Name == "pleroma" {
log.Info().
Str("hostname", i.hostName).
Str("software", ni.Software.Name).
Msg("detected server software")
i.registerSuccess()
i.identified = true
i.impl = Pleroma
i.status = InstanceStatusAlive
} else if ni.Software.Name == "mastodon" {
log.Info().
Str("hostname", i.hostName).
Str("software", ni.Software.Name).
Msg("detected server software")
i.registerSuccess()
i.identified = true
i.impl = Mastodon
i.status = InstanceStatusAlive
} else {
log.Error().
Str("hostname", i.hostName).
Str("software", ni.Software.Name).
Msg("unknown implementation on server")
i.registerError()
}
return
2019-10-24 11:56:44 +00:00
}
2019-11-03 13:17:00 +00:00
/*
2019-10-24 11:56:44 +00:00
func (i *Instance) fetchRecentToots() ([]byte, error) {
2019-11-03 10:56:50 +00:00
i.Lock()
impl := i.impl
i.Unlock()
if impl == Mastodon {
2019-10-24 11:56:44 +00:00
return i.fetchRecentTootsJsonFromMastodon()
} else if impl == Pleroma {
return i.fetchRecentTootsJsonFromPleroma()
2019-10-24 11:56:44 +00:00
} else {
2019-11-03 10:56:50 +00:00
panic("unimplemented")
2019-10-24 11:56:44 +00:00
}
}
2019-11-03 13:17:00 +00:00
*/
2019-10-24 11:56:44 +00:00
2019-11-03 13:17:00 +00:00
/*
func (self *PleromaBackend) fetchRecentToots() ([]byte, error) {
2019-10-24 11:56:44 +00:00
//url := fmt.Sprintf("https://%s/api/statuses/public_and_external_timeline.json?count=100", i.hostName)
return nil, nil
}
2019-10-24 10:38:16 +00:00
2019-11-03 13:17:00 +00:00
func (self *MastodonBackend) fetchRecentTootsJsonFromMastodon() ([]byte, error) {
2019-10-24 11:56:44 +00:00
//url := fmt.Sprintf("https://%s/api/v1/timelines/public?limit=40&local=true", i.hostName)
return nil, nil
}
2019-11-03 13:17:00 +00:00
*/