// // Copyright (c) 2019 Ted Unangst // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package main import ( "bytes" "context" "crypto/tls" "database/sql" "fmt" "html" "io" notrand "math/rand" "net/http" "os" "regexp" "strings" "time" "humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/gate" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/junk" "humungus.tedunangst.com/r/webs/templates" ) var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"` var thefakename = `application/activity+json` var falsenames = []string{ `application/ld+json`, `application/activity+json`, } var itiswhatitis = "https://www.w3.org/ns/activitystreams" var thewholeworld = "https://www.w3.org/ns/activitystreams#Public" var fastTimeout time.Duration = 5 var slowTimeout time.Duration = 30 func friendorfoe(ct string) bool { ct = strings.ToLower(ct) for _, at := range falsenames { if strings.HasPrefix(ct, at) { return true } } return false } var honkClient = http.Client{} func gogglesDoNothing() { honkClient.Transport = &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, } } func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error { return PostMsg(keyname, key, url, j.ToBytes()) } func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error { req, err := http.NewRequest("POST", url, bytes.NewReader(msg)) if err != nil { return err } req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName) req.Header.Set("Content-Type", theonetruename) httpsig.SignRequest(keyname, key, req, msg) ctx, cancel := context.WithTimeout(context.Background(), 2*slowTimeout*time.Second) defer cancel() req = req.WithContext(ctx) resp, err := honkClient.Do(req) if err != nil { return err } resp.Body.Close() switch resp.StatusCode { case 200: case 201: case 202: default: return fmt.Errorf("http post status: %d", resp.StatusCode) } ilog.Printf("successful post: %s %d", url, resp.StatusCode) return nil } func GetJunk(userid int64, url string) (junk.Junk, error) { return GetJunkTimeout(userid, url, slowTimeout*time.Second) } func GetJunkFast(userid int64, url string) (junk.Junk, error) { return GetJunkTimeout(userid, url, fastTimeout*time.Second) } func GetJunkHardMode(userid int64, url string) (junk.Junk, error) { j, err := GetJunk(userid, url) if err != nil { emsg := err.Error() if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") { ilog.Printf("trying again after error: %s", emsg) time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second) j, err = GetJunk(userid, url) if err != nil { ilog.Printf("still couldn't get it") } else { ilog.Printf("retry success!") } } } return j, err } var flightdeck = gate.NewSerializer() func GetJunkTimeout(userid int64, url string, timeout time.Duration) (junk.Junk, error) { if rejectorigin(userid, url, false) { return nil, fmt.Errorf("rejected origin: %s", url) } sign := func(req *http.Request) error { var ki *KeyInfo ok := ziggies.Get(userid, &ki) if ok { httpsig.SignRequest(ki.keyname, ki.seckey, req, nil) } return nil } if develMode { sign = nil } client := honkClient client.CheckRedirect = func(req *http.Request, via []*http.Request) error { if len(via) >= 5 { return fmt.Errorf("stopped after 5 redirects") } if sign != nil { sign(req) } return nil } fn := func() (interface{}, error) { at := theonetruename if strings.Contains(url, ".well-known/webfinger?resource") { at = "application/jrd+json" } j, err := junk.Get(url, junk.GetArgs{ Accept: at, Agent: "honksnonk/5.0; " + serverName, Timeout: timeout, Client: &client, Fixup: sign, }) return j, err } ji, err := flightdeck.Call(url, fn) if err != nil { return nil, err } j := ji.(junk.Junk) return j, nil } func fetchsome(url string) ([]byte, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { ilog.Printf("error fetching %s: %s", url, err) return nil, err } req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() req = req.WithContext(ctx) resp, err := honkClient.Do(req) if err != nil { ilog.Printf("error fetching %s: %s", url, err) return nil, err } defer resp.Body.Close() switch resp.StatusCode { case 200: case 201: case 202: default: return nil, fmt.Errorf("http get not 200: %d %s", resp.StatusCode, url) } var buf bytes.Buffer limiter := io.LimitReader(resp.Body, 10*1024*1024) io.Copy(&buf, limiter) return buf.Bytes(), nil } func savedonk(url string, name, desc, media string, localize bool) *Donk { if url == "" { return nil } if donk := finddonk(url); donk != nil { return donk } ilog.Printf("saving donk: %s", url) data := []byte{} if localize { fn := func() (interface{}, error) { return fetchsome(url) } ii, err := flightdeck.Call(url, fn) if err != nil { ilog.Printf("error fetching donk: %s", err) localize = false goto saveit } data = ii.([]byte) if len(data) == 10*1024*1024 { ilog.Printf("truncation likely") } if strings.HasPrefix(media, "image") { img, err := shrinkit(data) if err != nil { ilog.Printf("unable to decode image: %s", err) localize = false data = []byte{} goto saveit } data = img.Data media = "image/" + img.Format } else if media == "application/pdf" { if len(data) > 1000000 { ilog.Printf("not saving large pdf") localize = false data = []byte{} } } else if len(data) > 100000 { ilog.Printf("not saving large attachment") localize = false data = []byte{} } } saveit: fileid, err := savefile(name, desc, url, media, localize, data) if err != nil { elog.Printf("error saving file %s: %s", url, err) return nil } donk := new(Donk) donk.FileID = fileid return donk } func iszonked(userid int64, xid string) bool { var id int64 row := stmtFindZonk.QueryRow(userid, xid) err := row.Scan(&id) if err == nil { return true } if err != sql.ErrNoRows { ilog.Printf("error querying zonk: %s", err) } return false } func needxonk(user *WhatAbout, x *Honk) bool { if rejectxonk(x) { return false } return needxonkid(user, x.XID) } func needbonkid(user *WhatAbout, xid string) bool { return needxonkidX(user, xid, true) } func needxonkid(user *WhatAbout, xid string) bool { return needxonkidX(user, xid, false) } func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool { if !strings.HasPrefix(xid, "https://") { return false } if strings.HasPrefix(xid, user.URL+"/") { return false } if rejectorigin(user.ID, xid, isannounce) { ilog.Printf("rejecting origin: %s", xid) return false } if iszonked(user.ID, xid) { ilog.Printf("already zonked: %s", xid) return false } var id int64 row := stmtFindXonk.QueryRow(user.ID, xid) err := row.Scan(&id) if err == nil { return false } if err != sql.ErrNoRows { ilog.Printf("error querying xonk: %s", err) } return true } func eradicatexonk(userid int64, xid string) { xonk := getxonk(userid, xid) if xonk != nil { deletehonk(xonk.ID) _, err := stmtSaveZonker.Exec(userid, xid, "zonk") if err != nil { elog.Printf("error eradicating: %s", err) } } } func savexonk(x *Honk) { ilog.Printf("saving xonk: %s", x.XID) go handles(x.Honker) go handles(x.Oonker) savehonk(x) } type Box struct { In string Out string Shared string } var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) { var info string row := stmtGetXonker.QueryRow(ident, "boxes") err := row.Scan(&info) if err != nil { dlog.Printf("need to get boxes for %s", ident) var j junk.Junk j, err = GetJunk(readyLuserOne, ident) if err != nil { dlog.Printf("error getting boxes: %s", err) return nil, false } allinjest(originate(ident), j) row = stmtGetXonker.QueryRow(ident, "boxes") err = row.Scan(&info) } if err == nil { m := strings.Split(info, " ") b := &Box{In: m[0], Out: m[1], Shared: m[2]} return b, true } return nil, false }}) func gimmexonks(user *WhatAbout, outbox string) { dlog.Printf("getting outbox: %s", outbox) j, err := GetJunk(user.ID, outbox) if err != nil { ilog.Printf("error getting outbox: %s", err) return } t, _ := j.GetString("type") origin := originate(outbox) if t == "OrderedCollection" { items, _ := j.GetArray("orderedItems") if items == nil { items, _ = j.GetArray("items") } if items == nil { obj, ok := j.GetMap("first") if ok { items, _ = obj.GetArray("orderedItems") } else { page1, ok := j.GetString("first") if ok { j, err = GetJunk(user.ID, page1) if err != nil { ilog.Printf("error getting page1: %s", err) return } items, _ = j.GetArray("orderedItems") } } } if len(items) > 20 { items = items[0:20] } for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 { items[i], items[j] = items[j], items[i] } for _, item := range items { obj, ok := item.(junk.Junk) if ok { xonksaver(user, obj, origin) continue } xid, ok := item.(string) if ok { if !needxonkid(user, xid) { continue } obj, err = GetJunk(user.ID, xid) if err != nil { ilog.Printf("error getting item: %s", err) continue } xonksaver(user, obj, originate(xid)) } } } } func newphone(a []string, obj junk.Junk) []string { for _, addr := range []string{"to", "cc", "attributedTo"} { who, _ := obj.GetString(addr) if who != "" { a = append(a, who) } whos, _ := obj.GetArray(addr) for _, w := range whos { who, _ := w.(string) if who != "" { a = append(a, who) } } } return a } func extractattrto(obj junk.Junk) string { arr := oneforall(obj, "attributedTo") for _, a := range arr { s, ok := a.(string) if ok { return s } o, ok := a.(junk.Junk) if ok { t, _ := o.GetString("type") id, _ := o.GetString("id") if t == "Person" || t == "" { return id } } } return "" } func oneforall(obj junk.Junk, key string) []interface{} { if val, ok := obj.GetMap(key); ok { return []interface{}{val} } if str, ok := obj.GetString(key); ok { return []interface{}{str} } arr, _ := obj.GetArray(key) return arr } func firstofmany(obj junk.Junk, key string) string { if val, _ := obj.GetString(key); val != "" { return val } if arr, _ := obj.GetArray(key); len(arr) > 0 { val, ok := arr[0].(string) if ok { return val } } return "" } var re_mast0link = regexp.MustCompile(`https://[[:alnum:].]+/users/[[:alnum:]]+/statuses/[[:digit:]]+`) var re_masto1ink = regexp.MustCompile(`https://([[:alnum:].]+)/@([[:alnum:]]+)/([[:digit:]]+)`) var re_misslink = regexp.MustCompile(`https://[[:alnum:].]+/notes/[[:alnum:]]+`) var re_honklink = regexp.MustCompile(`https://[[:alnum:].]+/u/[[:alnum:]]+/h/[[:alnum:]]+`) var re_r0malink = regexp.MustCompile(`https://[[:alnum:].]+/objects/[[:alnum:]-]+`) var re_roma1ink = regexp.MustCompile(`https://[[:alnum:].]+/notice/[[:alnum:]]+`) var re_qtlinks = regexp.MustCompile(`>https://[^\s<]+<`) func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { depth := 0 maxdepth := 10 currenttid := "" goingup := 0 var xonkxonkfn func(junk.Junk, string, bool, string) *Honk qutify := func(user *WhatAbout, content string) string { if depth >= maxdepth { ilog.Printf("in too deep") return content } // well this is gross malcontent := strings.ReplaceAll(content, ``, "") malcontent = strings.ReplaceAll(malcontent, `