2019-10-24 10:38:16 +00:00
|
|
|
package main
|
|
|
|
|
2019-11-02 06:56:17 +00:00
|
|
|
import "encoding/json"
|
|
|
|
import "fmt"
|
|
|
|
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
|
|
|
|
2019-10-24 15:11:07 +00:00
|
|
|
const NodeInfoSchemaVersionTwoName = "http://nodeinfo.diaspora.software/ns/schema/2.0"
|
|
|
|
|
2019-11-02 06:56:17 +00:00
|
|
|
const NODE_TIMEOUT = time.Second * 10
|
2019-11-03 10:56:50 +00:00
|
|
|
const ONE_HOUR = time.Second * 60 * 60
|
|
|
|
const ONE_DAY = time.Second * 60 * 60 * 24
|
2019-10-24 15:11:07 +00:00
|
|
|
|
2019-10-24 11:56:44 +00:00
|
|
|
type ServerImplementation int
|
|
|
|
|
|
|
|
const (
|
2019-10-24 12:14:36 +00:00
|
|
|
ServerUnknown ServerImplementation = iota
|
|
|
|
ServerMastodon
|
2019-10-24 15:11:07 +00:00
|
|
|
ServerPleroma
|
2019-10-24 11:56:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Instance struct {
|
2019-11-03 10:56:50 +00:00
|
|
|
sync.Mutex
|
2019-10-24 15:11:07 +00:00
|
|
|
errorCount uint
|
|
|
|
highestId int
|
|
|
|
hostName string
|
2019-11-03 11:09:04 +00:00
|
|
|
up bool
|
|
|
|
identified bool
|
2019-10-24 15:11:07 +00:00
|
|
|
impl ServerImplementation
|
2019-11-03 10:56:50 +00:00
|
|
|
lastError *time.Time
|
2019-10-24 15:11:07 +00:00
|
|
|
lastSuccess *time.Time
|
2019-11-03 10:56:50 +00:00
|
|
|
nextCheck *time.Time
|
2019-10-24 15:11:07 +00:00
|
|
|
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 {
|
2019-11-03 10:56:50 +00:00
|
|
|
foreverago := time.Now().Add((-1 * 86400 * 365 * 100) * time.Second)
|
2019-10-24 11:56:44 +00:00
|
|
|
i := new(Instance)
|
|
|
|
i.hostName = hostname
|
2019-11-03 10:56:50 +00:00
|
|
|
i.nextCheck = &foreverago
|
2019-10-24 15:11:07 +00:00
|
|
|
i.up = false
|
|
|
|
go func() {
|
|
|
|
i.detectNodeType()
|
|
|
|
}()
|
2019-10-24 11:56:44 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-11-03 10:56:50 +00:00
|
|
|
func (i *Instance) setNextCheck(d *time.Duration) {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2019-11-03 11:09:04 +00:00
|
|
|
then := time.Now().Add(*d)
|
|
|
|
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()
|
2019-10-24 12:14:36 +00:00
|
|
|
if i.impl > ServerUnknown {
|
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()
|
2019-10-24 15:11:07 +00:00
|
|
|
i.fetchNodeInfo()
|
2019-10-24 11:56:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 15:11:07 +00:00
|
|
|
type NodeInfoWellKnownResponse struct {
|
|
|
|
Links []struct {
|
|
|
|
Rel string `json:"rel"`
|
|
|
|
Href string `json:"href"`
|
|
|
|
} `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeInfoVersionTwoSchema struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Software struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
} `json:"software"`
|
|
|
|
Protocols []string `json:"protocols"`
|
|
|
|
Usage struct {
|
|
|
|
Users struct {
|
|
|
|
Total int `json:"total"`
|
|
|
|
ActiveMonth int `json:"activeMonth"`
|
|
|
|
ActiveHalfyear int `json:"activeHalfyear"`
|
|
|
|
} `json:"users"`
|
|
|
|
LocalPosts int `json:"localPosts"`
|
|
|
|
} `json:"usage"`
|
|
|
|
OpenRegistrations bool `json:"openRegistrations"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) registerError() {
|
2019-11-03 10:56:50 +00:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2019-10-24 15:11:07 +00:00
|
|
|
i.errorCount = i.errorCount + 1
|
|
|
|
t := time.Now()
|
2019-11-03 10:56:50 +00:00
|
|
|
i.lastError = &t
|
2019-10-24 15:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) registerSuccess() {
|
2019-11-03 10:56:50 +00:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2019-10-24 15:11:07 +00:00
|
|
|
t := time.Now()
|
|
|
|
i.lastSuccess = &t
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2019-10-24 15:11:07 +00:00
|
|
|
Timeout: NODE_TIMEOUT,
|
2019-10-24 11:56:44 +00:00
|
|
|
}
|
2019-10-24 15:11:07 +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 {
|
2019-10-24 15:11:07 +00:00
|
|
|
log.Error().
|
|
|
|
Str("hostname", i.hostName).
|
|
|
|
Msg("unable to fetch nodeinfo, node is down?")
|
|
|
|
i.registerError()
|
|
|
|
} else {
|
|
|
|
i.up = true // node is alive and responding to us
|
|
|
|
nir := new(NodeInfoWellKnownResponse)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&nir)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("hostname", i.hostName).
|
|
|
|
Msg("unable to parse nodeinfo")
|
|
|
|
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")
|
2019-11-03 10:56:50 +00:00
|
|
|
|
|
|
|
i.Lock()
|
2019-10-24 15:11:07 +00:00
|
|
|
i.nodeInfoUrl = item.Href
|
2019-11-03 10:56:50 +00:00
|
|
|
i.Unlock()
|
2019-10-24 15:11:07 +00:00
|
|
|
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
|
2019-10-24 15:11:07 +00:00
|
|
|
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 {
|
2019-10-24 15:11:07 +00:00
|
|
|
return
|
2019-10-24 11:56:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 15:11:07 +00:00
|
|
|
var c = &http.Client{
|
|
|
|
Timeout: NODE_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)
|
2019-10-24 15:11:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("hostname", i.hostName).
|
|
|
|
Msgf("unable to fetch nodeinfo data: %s", err)
|
|
|
|
i.registerError()
|
|
|
|
} else {
|
|
|
|
ni := new(NodeInfoVersionTwoSchema)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&ni)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("hostname", i.hostName).
|
|
|
|
Msgf("unable to parse nodeinfo: %s", err)
|
|
|
|
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")
|
|
|
|
|
2019-11-03 10:56:50 +00:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2019-10-24 15:11:07 +00:00
|
|
|
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 = ServerPleroma
|
|
|
|
} 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 = ServerMastodon
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) fetchRecentToots() ([]byte, error) {
|
2019-11-03 10:56:50 +00:00
|
|
|
i.Lock()
|
|
|
|
impl := i.impl
|
|
|
|
i.Unlock()
|
|
|
|
|
|
|
|
if impl == ServerMastodon {
|
2019-10-24 11:56:44 +00:00
|
|
|
return i.fetchRecentTootsJsonFromMastodon()
|
2019-11-03 10:56:50 +00:00
|
|
|
} else if impl == ServerPleroma {
|
2019-10-24 15:11:07 +00:00
|
|
|
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-10-24 15:11:07 +00:00
|
|
|
func (i *Instance) fetchRecentTootsJsonFromPleroma() ([]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-10-24 11:56:44 +00:00
|
|
|
func (i *Instance) fetchRecentTootsJsonFromMastodon() ([]byte, error) {
|
|
|
|
//url := fmt.Sprintf("https://%s/api/v1/timelines/public?limit=40&local=true", i.hostName)
|
|
|
|
return nil, nil
|
|
|
|
}
|