-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (105 loc) · 3.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
_ "embed"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/policies"
"github.com/kelseyhightower/envconfig"
"github.com/nbd-wtf/go-nostr/nip11"
"github.com/oschwald/maxminddb-golang"
"github.com/rs/zerolog"
bolt "go.etcd.io/bbolt"
"golang.org/x/sync/errgroup"
)
//go:embed GeoLite2-Country.mmdb
var maxmindData []byte
var mm *maxminddb.Reader
var (
s Settings
log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()
relay = khatru.NewRelay()
globalDB *bolt.DB
)
type Settings struct {
Port string `envconfig:"PORT" default:"40404"`
DatabaseDir string `envconfig:"DATABASE_DIR" default:"./db"`
RelayContact string `envconfig:"RELAY_CONTACT" required:"false"`
RelayIcon string `envconfig:"RELAY_ICON" required:"false"`
BlockedCountries []string `envconfig:"BLOCKED_COUNTRIES"`
}
func main() {
// load environment variables
err := envconfig.Process("", &s)
if err != nil {
log.Fatal().Err(err).Msg("couldn't process envconfig")
return
}
// load ip data
mm, _ = maxminddb.FromBytes(maxmindData)
if mm == nil {
log.Fatal().Msg("failed to open maxmind db")
return
}
// open global boltdb
os.MkdirAll(s.DatabaseDir, 0755)
globalDB, err = bolt.Open(s.DatabaseDir+"/global", 0644, nil)
if err != nil {
log.Fatal().Err(err).Msg("failed to open global db")
return
}
if err := globalDB.Update(func(txn *bolt.Tx) error {
_, err := txn.CreateBucketIfNotExists([]byte("idMap"))
return err
}); err != nil {
log.Fatal().Err(err).Msg("failed to open idMap bucket on global db")
return
}
// start cloudflare thing
go updateCloudflareRangesRoutine()
// ratelimiter
go bucketFillingRoutine()
// delete old stuff every now and then
go deleteOldStuffRoutine()
relay.Info.Name = "countries"
relay.Info.Description = "serves notes according to your country"
relay.Info.Contact = s.RelayContact
relay.Info.Icon = s.RelayIcon
relay.Info.Limitation = &nip11.RelayLimitationDocument{}
relay.StoreEvent = append(relay.StoreEvent,
storeEventForCountryDB,
trackEventOnGlobalDB,
memoryTrack,
)
relay.QueryEvents = append(relay.QueryEvents, queryEventForCountryDB)
relay.DeleteEvent = append(relay.DeleteEvent, deleteEventForCountryDB)
relay.RejectEvent = append(relay.RejectEvent,
policies.RestrictToSpecifiedKinds(1),
rejectEventForCountry,
rejectCloudflareEvents,
policies.PreventLargeTags(12),
rejectIfAlreadyHaveInAnyOtherDB,
rateLimit,
)
relay.RejectFilter = append(relay.RejectFilter,
policies.NoSearchQueries,
)
// http routes
relay.Router().HandleFunc("/", homePage)
log.Info().Msg("running on http://0.0.0.0:" + s.Port)
server := &http.Server{Addr: ":" + s.Port, Handler: relay}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
g.Go(server.ListenAndServe)
g.Go(func() error {
<-ctx.Done()
return server.Shutdown(context.Background())
})
if err := g.Wait(); err != nil {
log.Debug().Err(err).Msg("exit reason")
}
}