Initial commit
This commit is contained in:
commit
7a7addcc4a
5 changed files with 261 additions and 0 deletions
9
LICENSE
Normal file
9
LICENSE
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright © 2022 <copyright holders>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Fake Mastodon Endpoints
|
||||||
|
|
||||||
|
Yup. That's literally what this is. Run this as you would any other Go project, I guess (I don't feel like writing instructions right now).
|
18
go.mod
Normal file
18
go.mod
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
module fake-mastodon-endpoints
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Code-Hex/go-generics-cache v1.2.1
|
||||||
|
github.com/fasthttp/router v1.4.14
|
||||||
|
github.com/google/uuid v1.3.0
|
||||||
|
github.com/valyala/fasthttp v1.43.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||||
|
github.com/klauspost/compress v1.15.9 // indirect
|
||||||
|
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect
|
||||||
|
)
|
32
go.sum
Normal file
32
go.sum
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
github.com/Code-Hex/go-generics-cache v1.2.1 h1:jKof8Hk8mr28lcEAo9g90oj7H3vb8y2DdccoGpL4lyE=
|
||||||
|
github.com/Code-Hex/go-generics-cache v1.2.1/go.mod h1:qxcC9kRVrct9rHeiYpFWSoW1vxyillCVzX13KZG8dl4=
|
||||||
|
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
|
||||||
|
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||||
|
github.com/fasthttp/router v1.4.14 h1:+W65VCKgyI4BZszhDiCRfONoFieePZIoQ7D8vGhiuzM=
|
||||||
|
github.com/fasthttp/router v1.4.14/go.mod h1:+svLaOvqF9Lc0yjX9aHAD4NUMf+mggLPOT4UMdS6fjM=
|
||||||
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
|
||||||
|
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||||
|
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d h1:Q+gqLBOPkFGHyCJxXMRqtUgUbTjI8/Ze8vu8GGyNFwo=
|
||||||
|
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
github.com/valyala/fasthttp v1.42.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY=
|
||||||
|
github.com/valyala/fasthttp v1.43.0 h1:Gy4sb32C98fbzVWZlTM1oTMdLWGyvxR03VhM6cBIU4g=
|
||||||
|
github.com/valyala/fasthttp v1.43.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY=
|
||||||
|
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
||||||
|
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
|
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
|
||||||
|
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||||
|
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
|
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
199
main.go
Normal file
199
main.go
Normal file
|
@ -0,0 +1,199 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
cache "github.com/Code-Hex/go-generics-cache"
|
||||||
|
"github.com/Code-Hex/go-generics-cache/policy/lru"
|
||||||
|
"github.com/fasthttp/router"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const hexDigits = "0123456789abcdef"
|
||||||
|
|
||||||
|
var emptyResponse = []byte("[]")
|
||||||
|
|
||||||
|
var peersCache *cache.Cache[string, []byte]
|
||||||
|
var domainBlockCache *cache.Cache[string, []byte]
|
||||||
|
|
||||||
|
type domainBlockEntry struct {
|
||||||
|
Domain string `json:"domain"`
|
||||||
|
Digest string `json:"digest"`
|
||||||
|
Severity string `json:"severity"`
|
||||||
|
Comment string `json:"comment"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func b2s(b []byte) string {
|
||||||
|
return *(*string)(unsafe.Pointer(&b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func s2b(s string) (b []byte) {
|
||||||
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||||
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||||
|
bh.Data = sh.Data
|
||||||
|
bh.Cap = sh.Len
|
||||||
|
bh.Len = sh.Len
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Yoinked from Go 1.19.4 stdlib: https://github.com/golang/go/blob/dc04f3ba1f25313bc9c97e728620206c235db9ee/src/internal/itoa/itoa.go
|
||||||
|
func uitoa(val uint) string {
|
||||||
|
if val == 0 { // avoid string allocation
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
var buf [20]byte // big enough for 64bit value base 10
|
||||||
|
i := len(buf) - 1
|
||||||
|
for val >= 10 {
|
||||||
|
q := val / 10
|
||||||
|
buf[i] = byte('0' + val - q*10)
|
||||||
|
i--
|
||||||
|
val = q
|
||||||
|
}
|
||||||
|
// val < 10
|
||||||
|
buf[i] = byte('0' + val)
|
||||||
|
return string(buf[i:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Yoinked from Go 1.19.4 stdlib: https://github.com/golang/go/blob/dc04f3ba1f25313bc9c97e728620206c235db9ee/src/net/dnsclient.go#L26-L50
|
||||||
|
func reverseaddr(ip net.IP) (arpa string) {
|
||||||
|
if ip.To4() != nil {
|
||||||
|
return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa."
|
||||||
|
}
|
||||||
|
// Must be IPv6
|
||||||
|
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
|
||||||
|
// Add it, in reverse, to the buffer
|
||||||
|
for i := len(ip) - 1; i >= 0; i-- {
|
||||||
|
v := ip[i]
|
||||||
|
buf = append(buf, hexDigits[v&0xF],
|
||||||
|
'.',
|
||||||
|
hexDigits[v>>4],
|
||||||
|
'.')
|
||||||
|
}
|
||||||
|
// Append "ip6.arpa." and return (buf already has the final .)
|
||||||
|
buf = append(buf, "ip6.arpa."...)
|
||||||
|
return string(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setCommonHeaders(ctx *fasthttp.RequestCtx) {
|
||||||
|
ctx.Response.Header.SetStatusCode(200)
|
||||||
|
ctx.Response.Header.SetContentType("application/json; charset=utf-8")
|
||||||
|
ctx.Response.Header.Add("Vary", "Accept-Encoding")
|
||||||
|
ctx.Response.Header.SetServer("Mastodon")
|
||||||
|
ctx.Response.Header.Add("X-Frame-Options", "DENY")
|
||||||
|
ctx.Response.Header.Add("X-Content-Type-Options", "nosniff")
|
||||||
|
ctx.Response.Header.Add("X-XSS-Protection", "0")
|
||||||
|
ctx.Response.Header.Add("Referrer-Policy", "same-origin")
|
||||||
|
ctx.Response.Header.Add("X-Clacks-Overhead", "GNU Natalie Nguyen")
|
||||||
|
ctx.Response.Header.Add("X-RateLimit-Limit", "300")
|
||||||
|
ctx.Response.Header.Add("X-RateLimit-Remaining", "0")
|
||||||
|
ctx.Response.Header.Add("X-RateLimit-Reset", time.Now().UTC().Add(50*time.Millisecond).Format("2006-01-02T15:04:05.999999Z"))
|
||||||
|
ctx.Response.Header.Add("Cache-Control", "private, no-store")
|
||||||
|
ctx.Response.Header.Add("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; form-action 'none'")
|
||||||
|
ctx.Response.Header.Add("X-Request-Id", uuid.New().String())
|
||||||
|
ctx.Response.Header.Add("X-Runtime", fmt.Sprintf("%.6f", 0.05+rand.Float32()*(0.1-0.05)))
|
||||||
|
ctx.Response.Header.Add("Vary", "Origin")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIP(ctx *fasthttp.RequestCtx) net.IP {
|
||||||
|
bytes := ctx.Request.Header.Peek("X-Real-IP")
|
||||||
|
if len(bytes) == 0 {
|
||||||
|
bytes = ctx.Request.Header.Peek(fasthttp.HeaderXForwardedFor)
|
||||||
|
if len(bytes) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return net.ParseIP(b2s(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePeers(ctx *fasthttp.RequestCtx) {
|
||||||
|
setCommonHeaders(ctx)
|
||||||
|
ip := getIP(ctx)
|
||||||
|
if ip == nil {
|
||||||
|
ctx.SetBody(emptyResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ipString := ip.String()
|
||||||
|
if cachedResponse, ok := peersCache.Get(ipString); ok {
|
||||||
|
ctx.SetBody(cachedResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonResponse, err := json.Marshal([1]string{reverseaddr(ip)})
|
||||||
|
if err != nil {
|
||||||
|
ctx.SetBody(emptyResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
peersCache.Set(ipString, jsonResponse)
|
||||||
|
ctx.SetBody(jsonResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDomainBlocks(ctx *fasthttp.RequestCtx) {
|
||||||
|
setCommonHeaders(ctx)
|
||||||
|
ip := getIP(ctx)
|
||||||
|
if ip == nil {
|
||||||
|
ctx.SetBody(emptyResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ipString := ip.String()
|
||||||
|
if cachedResponse, ok := domainBlockCache.Get(ipString); ok {
|
||||||
|
ctx.SetBody(cachedResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
arpa := reverseaddr(ip)
|
||||||
|
arpaBytes := s2b(arpa)
|
||||||
|
|
||||||
|
jsonResponse, err := json.Marshal([1]domainBlockEntry{{
|
||||||
|
Domain: arpa,
|
||||||
|
Digest: fmt.Sprintf("%x", sha256.Sum256(arpaBytes)),
|
||||||
|
Severity: "suspend",
|
||||||
|
Comment: fmt.Sprintf("This scraper uses %s", ipString),
|
||||||
|
}})
|
||||||
|
if err != nil {
|
||||||
|
ctx.SetBody(emptyResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
domainBlockCache.Set(ipString, jsonResponse)
|
||||||
|
ctx.SetBody(jsonResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
args := os.Args[1:]
|
||||||
|
if len(args) != 1 {
|
||||||
|
_, _ = fmt.Fprintln(os.Stderr, "The first, only and required command line argument must be a UNIX socket path.")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
peersContext, cancelPeersContext := context.WithCancel(context.Background())
|
||||||
|
domainBlocksContext, cancelDomainBlocksContext := context.WithCancel(context.Background())
|
||||||
|
defer cancelPeersContext()
|
||||||
|
defer cancelDomainBlocksContext()
|
||||||
|
|
||||||
|
peersCache = cache.NewContext[string, []byte](
|
||||||
|
peersContext,
|
||||||
|
cache.AsLRU[string, []byte](lru.WithCapacity(65535)),
|
||||||
|
cache.WithJanitorInterval[string, []byte](1<<63-1))
|
||||||
|
domainBlockCache = cache.NewContext[string, []byte](
|
||||||
|
domainBlocksContext,
|
||||||
|
cache.AsLRU[string, []byte](lru.WithCapacity(65535)),
|
||||||
|
cache.WithJanitorInterval[string, []byte](1<<63-1))
|
||||||
|
|
||||||
|
r := router.New()
|
||||||
|
r.RedirectTrailingSlash = false
|
||||||
|
r.GET("/api/v1/instance/peers", handlePeers)
|
||||||
|
r.GET("/api/v1/instance/domain_blocks", handleDomainBlocks)
|
||||||
|
|
||||||
|
log.Fatal(fasthttp.ListenAndServeUNIX(args[0], os.ModeSocket|os.ModePerm, r.Handler))
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue