2019-10-24 10:38:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-24 15:11:07 +00:00
|
|
|
"encoding/json"
|
2019-10-24 11:56:44 +00:00
|
|
|
"fmt"
|
2019-10-24 10:38:16 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2019-10-24 11:56:44 +00:00
|
|
|
"net/http"
|
2019-10-24 15:11:07 +00:00
|
|
|
"strings"
|
2019-10-24 11:56:44 +00:00
|
|
|
"time"
|
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"
|
|
|
|
|
|
|
|
const NODE_TIMEOUT = time.Second * 30
|
|
|
|
|
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-10-24 15:11:07 +00:00
|
|
|
errorCount uint
|
|
|
|
highestId int
|
|
|
|
hostName string
|
|
|
|
impl ServerImplementation
|
|
|
|
lastFailure *time.Time
|
|
|
|
lastSuccess *time.Time
|
|
|
|
nodeInfoUrl string
|
|
|
|
serverVersion string
|
|
|
|
identified bool
|
|
|
|
up bool
|
|
|
|
shouldSkip bool
|
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
|
2019-10-24 15:11:07 +00:00
|
|
|
foreverago := time.Now().Add((-1 * 86400 * 365 * 100) * time.Second)
|
|
|
|
i.lastSuccess = &foreverago
|
|
|
|
i.lastFailure = &foreverago
|
|
|
|
i.identified = false
|
|
|
|
i.up = false
|
|
|
|
go func() {
|
|
|
|
i.detectNodeType()
|
|
|
|
}()
|
2019-10-24 11:56:44 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) detectNodeType() {
|
2019-10-24 12:14:36 +00:00
|
|
|
if i.impl > ServerUnknown {
|
2019-10-24 11:56:44 +00:00
|
|
|
return
|
|
|
|
}
|
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() {
|
|
|
|
i.errorCount = i.errorCount + 1
|
|
|
|
t := time.Now()
|
|
|
|
i.lastFailure = &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) registerSuccess() {
|
|
|
|
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")
|
|
|
|
i.nodeInfoUrl = item.Href
|
|
|
|
i.registerSuccess()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Error().
|
|
|
|
Str("hostname", i.hostName).
|
|
|
|
Msg("incomplete nodeinfo")
|
|
|
|
i.registerError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) fetchNodeInfo() {
|
|
|
|
i.fetchNodeInfoURL()
|
|
|
|
if i.nodeInfoUrl == "" {
|
|
|
|
log.Error().
|
|
|
|
Str("hostname", i.hostName).
|
|
|
|
Msg("unable to fetch nodeinfo as nodeinfo URL cannot be determined")
|
|
|
|
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
|
|
|
|
resp, err := c.Get(i.nodeInfoUrl)
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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-10-24 12:41:05 +00:00
|
|
|
if i.impl == ServerMastodon {
|
2019-10-24 11:56:44 +00:00
|
|
|
return i.fetchRecentTootsJsonFromMastodon()
|
2019-10-24 15:11:07 +00:00
|
|
|
} else if i.impl == ServerPleroma {
|
|
|
|
return i.fetchRecentTootsJsonFromPleroma()
|
2019-10-24 11:56:44 +00:00
|
|
|
} else {
|
|
|
|
panic("nope")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchLatestToots(lastId int) {
|
2019-10-24 10:38:16 +00:00
|
|
|
log.Debug().Msg("This message appears only when log level set to Debug")
|
|
|
|
log.Info().Msg("This message appears when log level set to Debug or Info")
|
|
|
|
}
|