forked from BlackPeter13/LightningTipBot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
165 lines (140 loc) · 6.39 KB
/
main.go
File metadata and controls
165 lines (140 loc) · 6.39 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"net/http"
"runtime/debug"
"strings"
"time"
"github.com/LightningTipBot/LightningTipBot/internal"
"github.com/LightningTipBot/LightningTipBot/internal/api"
"github.com/LightningTipBot/LightningTipBot/internal/api/admin"
"github.com/LightningTipBot/LightningTipBot/internal/api/userpage"
"github.com/LightningTipBot/LightningTipBot/internal/lndhub"
"github.com/LightningTipBot/LightningTipBot/internal/lnurl"
"github.com/LightningTipBot/LightningTipBot/internal/nostr"
"github.com/LightningTipBot/LightningTipBot/internal/runtime/mutex"
"github.com/LightningTipBot/LightningTipBot/internal/telegram"
_ "net/http/pprof"
tb "gopkg.in/lightningtipbot/telebot.v3"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits/webhook"
"github.com/LightningTipBot/LightningTipBot/internal/price"
"github.com/LightningTipBot/LightningTipBot/internal/utils"
log "github.com/sirupsen/logrus"
)
// setLogger will initialize the log format
func setLogger() {
log.SetLevel(log.DebugLevel)
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
log.SetFormatter(customFormatter)
}
func main() {
// set logger
setLogger()
// Create bot first
bot := telegram.NewBot()
defer withRecovery(bot.ErrorLogger)
price.NewPriceWatcher().Start()
startApiServer(&bot)
bot.Start()
}
func startApiServer(bot *telegram.TipBot) {
// log errors from interceptors
bot.Telegram.OnError = func(err error, ctx tb.Context) {
if err == nil {
return
}
errMsg := err.Error()
// Filter out empty/ghost errors from telebot (code:0, empty message)
if errMsg == "" || errMsg == `{"message":"","Err":{},"code":0}` {
return
}
// Filter out annoying interceptor errors
if strings.Contains(errMsg, "[requirePrivateChatInterceptor]") {
return
}
// Log errors to Telegram group
if bot.ErrorLogger != nil {
userInfo := []interface{}{}
if ctx != nil && ctx.Sender() != nil {
userInfo = append(userInfo, ctx.Sender())
}
if ctx != nil && ctx.Chat() != nil {
userInfo = append(userInfo, ctx.Chat())
}
bot.ErrorLogger.LogError(err, "Telegram Bot Error", userInfo...)
}
}
// start internal webhook server
webhook.NewServer(bot)
// start external api server
s := api.NewServer(internal.Configuration.Bot.LNURLServerUrl.Host)
s.AppendRoute("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}, http.MethodGet)
// append lnurl ctx functions
lnUrl := lnurl.New(bot)
s.AppendRoute("/.well-known/lnurlp/{username}", lnUrl.Handle, http.MethodGet)
// userpage server
userpage := userpage.New(bot)
s.AppendRoute("/@{username}", userpage.UserPageHandler, http.MethodGet)
s.AppendRoute("/app/@{username}", userpage.UserWebAppHandler, http.MethodGet)
// nostr nip05 identifier
nostr := nostr.New(bot)
s.AppendRoute("/.well-known/nostr.json", nostr.Handle, http.MethodGet)
// append lndhub ctx functions
hub := lndhub.New(bot)
s.AppendAuthorizedRoute(`/lndhub/ext/auth`, api.AuthTypeNone, api.AccessKeyTypeNone, bot.DB.Users, hub.Handle)
s.AppendAuthorizedRoute(`/lndhub/ext/{.*}`, api.AuthTypeBearerBase64, api.AccessKeyTypeAdmin, bot.DB.Users, hub.Handle)
s.AppendAuthorizedRoute(`/lndhub/ext`, api.AuthTypeBearerBase64, api.AccessKeyTypeAdmin, bot.DB.Users, hub.Handle)
// starting api service
apiService := api.Service{
Bot: bot,
MemoCache: utils.NewCache(time.Minute * 5),
}
s.AppendAuthorizedRoute(`/api/v1/paymentstatus/{payment_hash}`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.PaymentStatus, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/invoicestatus/{payment_hash}`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.InvoiceStatus, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/payinvoice`, api.AuthTypeBasic, api.AccessKeyTypeAdmin, bot.DB.Users, apiService.PayInvoice, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/invoicestream`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.InvoiceStream, http.MethodGet)
s.AppendAuthorizedRoute(`/api/v1/createinvoice`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.CreateInvoice, http.MethodPost)
s.AppendAuthorizedRoute(`/api/v1/balance`, api.AuthTypeBasic, api.AccessKeyTypeInvoice, bot.DB.Users, apiService.Balance, http.MethodGet)
// Analytics API endpoints (HMAC authenticated)
if internal.IsAPIAnalyticsEnabled() {
s.AppendRoute(`/api/v1/analytics/transactions`, api.AnalyticsHMACMiddleware(apiService.GetTransactionAnalytics), http.MethodGet)
s.AppendRoute(`/api/v1/analytics/user/{user_id}/transactions`, api.AnalyticsHMACMiddleware(apiService.GetUserTransactionHistory), http.MethodGet)
log.Infof("Analytics API endpoints registered with HMAC security")
} else {
log.Infof("Analytics API endpoints disabled in configuration")
}
// Bot pay HTTP API module with wallet-based HMAC security (only if enabled)
if internal.IsAPISendEnabled() {
s.AppendRoute(`/api/v1/send`, api.WalletHMACMiddleware(apiService.Send), http.MethodPost)
log.Infof("API Send endpoint registered at /api/v1/send with wallet-based HMAC security")
// User balance endpoint with wallet-based HMAC security
s.AppendRoute(`/api/v1/userbalance`, api.WalletHMACMiddleware(apiService.UserBalance), http.MethodPost)
log.Infof("API UserBalance endpoint registered at /api/v1/userbalance with wallet-based HMAC security")
} else {
log.Infof("API Send endpoint disabled in configuration")
}
// start internal admin server
adminService := admin.New(bot)
internalAdminServer := api.NewServer(internal.Configuration.Bot.AdminAPIHost)
internalAdminServer.AppendRoute("/mutex", mutex.ServeHTTP)
internalAdminServer.AppendRoute("/mutex/unlock/{id}", mutex.UnlockHTTP)
internalAdminServer.AppendRoute("/admin/ban/{id}", adminService.BanUser)
internalAdminServer.AppendRoute("/admin/unban/{id}", adminService.UnbanUser)
internalAdminServer.AppendRoute("/admin/dalle/enable", adminService.EnableDalle)
internalAdminServer.AppendRoute("/admin/dalle/disable", adminService.DisableDalle)
internalAdminServer.PathPrefix("/debug/pprof/", http.DefaultServeMux)
}
func withRecovery(errorLogger *telegram.ErrorLogger) {
if r := recover(); r != nil {
log.Errorln("Recovered panic: ", r)
debug.PrintStack()
// Log to Telegram if error logger is available
if errorLogger != nil {
errorLogger.LogPanic(r, "Main Application")
}
}
}