tgbot.go 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839
  1. package service
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "embed"
  6. "encoding/base64"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "html"
  11. "io"
  12. "math/big"
  13. "net"
  14. "net/http"
  15. "net/url"
  16. "os"
  17. "regexp"
  18. "slices"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "time"
  23. "github.com/mhsanaei/3x-ui/v2/config"
  24. "github.com/mhsanaei/3x-ui/v2/database"
  25. "github.com/mhsanaei/3x-ui/v2/database/model"
  26. "github.com/mhsanaei/3x-ui/v2/logger"
  27. "github.com/mhsanaei/3x-ui/v2/util/common"
  28. "github.com/mhsanaei/3x-ui/v2/web/global"
  29. "github.com/mhsanaei/3x-ui/v2/web/locale"
  30. "github.com/mhsanaei/3x-ui/v2/xray"
  31. "github.com/google/uuid"
  32. "github.com/mymmrac/telego"
  33. th "github.com/mymmrac/telego/telegohandler"
  34. tu "github.com/mymmrac/telego/telegoutil"
  35. "github.com/skip2/go-qrcode"
  36. "github.com/valyala/fasthttp"
  37. "github.com/valyala/fasthttp/fasthttpproxy"
  38. )
  39. var (
  40. bot *telego.Bot
  41. // botCancel stores the function to cancel the context, stopping Long Polling gracefully.
  42. botCancel context.CancelFunc
  43. // tgBotMutex protects concurrent access to botCancel variable
  44. tgBotMutex sync.Mutex
  45. // botWG waits for the OnReceive Long Polling goroutine to finish.
  46. botWG sync.WaitGroup
  47. botHandler *th.BotHandler
  48. adminIds []int64
  49. isRunning bool
  50. hostname string
  51. hashStorage *global.HashStorage
  52. // Performance improvements
  53. messageWorkerPool chan struct{} // Semaphore for limiting concurrent message processing
  54. optimizedHTTPClient *http.Client // HTTP client with connection pooling and timeouts
  55. // Simple cache for frequently accessed data
  56. statusCache struct {
  57. data *Status
  58. timestamp time.Time
  59. mutex sync.RWMutex
  60. }
  61. serverStatsCache struct {
  62. data string
  63. timestamp time.Time
  64. mutex sync.RWMutex
  65. }
  66. // clients data to adding new client
  67. receiver_inbound_ID int
  68. client_Id string
  69. client_Flow string
  70. client_Email string
  71. client_LimitIP int
  72. client_TotalGB int64
  73. client_ExpiryTime int64
  74. client_Enable bool
  75. client_TgID string
  76. client_SubID string
  77. client_Comment string
  78. client_Reset int
  79. client_Security string
  80. client_ShPassword string
  81. client_TrPassword string
  82. client_Method string
  83. )
  84. var userStates = make(map[int64]string)
  85. // LoginStatus represents the result of a login attempt.
  86. type LoginStatus byte
  87. // Login status constants
  88. const (
  89. LoginSuccess LoginStatus = 1 // Login was successful
  90. LoginFail LoginStatus = 0 // Login failed
  91. EmptyTelegramUserID = int64(0) // Default value for empty Telegram user ID
  92. )
  93. // Tgbot provides business logic for Telegram bot integration.
  94. // It handles bot commands, user interactions, and status reporting via Telegram.
  95. type Tgbot struct {
  96. inboundService InboundService
  97. settingService SettingService
  98. serverService ServerService
  99. xrayService XrayService
  100. lastStatus *Status
  101. }
  102. // NewTgbot creates a new Tgbot instance.
  103. func (t *Tgbot) NewTgbot() *Tgbot {
  104. return new(Tgbot)
  105. }
  106. // I18nBot retrieves a localized message for the bot interface.
  107. func (t *Tgbot) I18nBot(name string, params ...string) string {
  108. return locale.I18n(locale.Bot, name, params...)
  109. }
  110. // GetHashStorage returns the hash storage instance for callback queries.
  111. func (t *Tgbot) GetHashStorage() *global.HashStorage {
  112. return hashStorage
  113. }
  114. // getCachedStatus returns cached server status if it's fresh enough (less than 5 seconds old)
  115. func (t *Tgbot) getCachedStatus() (*Status, bool) {
  116. statusCache.mutex.RLock()
  117. defer statusCache.mutex.RUnlock()
  118. if statusCache.data != nil && time.Since(statusCache.timestamp) < 5*time.Second {
  119. return statusCache.data, true
  120. }
  121. return nil, false
  122. }
  123. // setCachedStatus updates the status cache
  124. func (t *Tgbot) setCachedStatus(status *Status) {
  125. statusCache.mutex.Lock()
  126. defer statusCache.mutex.Unlock()
  127. statusCache.data = status
  128. statusCache.timestamp = time.Now()
  129. }
  130. // getCachedServerStats returns cached server stats if it's fresh enough (less than 10 seconds old)
  131. func (t *Tgbot) getCachedServerStats() (string, bool) {
  132. serverStatsCache.mutex.RLock()
  133. defer serverStatsCache.mutex.RUnlock()
  134. if serverStatsCache.data != "" && time.Since(serverStatsCache.timestamp) < 10*time.Second {
  135. return serverStatsCache.data, true
  136. }
  137. return "", false
  138. }
  139. // setCachedServerStats updates the server stats cache
  140. func (t *Tgbot) setCachedServerStats(stats string) {
  141. serverStatsCache.mutex.Lock()
  142. defer serverStatsCache.mutex.Unlock()
  143. serverStatsCache.data = stats
  144. serverStatsCache.timestamp = time.Now()
  145. }
  146. // Start initializes and starts the Telegram bot with the provided translation files.
  147. func (t *Tgbot) Start(i18nFS embed.FS) error {
  148. // Initialize localizer
  149. err := locale.InitLocalizer(i18nFS, &t.settingService)
  150. if err != nil {
  151. return err
  152. }
  153. // If Start is called again (e.g. during reload), ensure any previous long-polling
  154. // loop is stopped before creating a new bot / receiver.
  155. StopBot()
  156. // Initialize hash storage to store callback queries
  157. hashStorage = global.NewHashStorage(20 * time.Minute)
  158. // Initialize worker pool for concurrent message processing (max 10 concurrent handlers)
  159. messageWorkerPool = make(chan struct{}, 10)
  160. // Initialize optimized HTTP client with connection pooling
  161. optimizedHTTPClient = &http.Client{
  162. Timeout: 15 * time.Second,
  163. Transport: &http.Transport{
  164. MaxIdleConns: 100,
  165. MaxIdleConnsPerHost: 10,
  166. IdleConnTimeout: 30 * time.Second,
  167. DisableKeepAlives: false,
  168. },
  169. }
  170. t.SetHostname()
  171. // Get Telegram bot token
  172. tgBotToken, err := t.settingService.GetTgBotToken()
  173. if err != nil || tgBotToken == "" {
  174. logger.Warning("Failed to get Telegram bot token:", err)
  175. return err
  176. }
  177. // Get Telegram bot chat ID(s)
  178. tgBotID, err := t.settingService.GetTgBotChatId()
  179. if err != nil {
  180. logger.Warning("Failed to get Telegram bot chat ID:", err)
  181. return err
  182. }
  183. parsedAdminIds := make([]int64, 0)
  184. // Parse admin IDs from comma-separated string
  185. if tgBotID != "" {
  186. for _, adminID := range strings.Split(tgBotID, ",") {
  187. id, err := strconv.ParseInt(adminID, 10, 64)
  188. if err != nil {
  189. logger.Warning("Failed to parse admin ID from Telegram bot chat ID:", err)
  190. return err
  191. }
  192. parsedAdminIds = append(parsedAdminIds, int64(id))
  193. }
  194. }
  195. tgBotMutex.Lock()
  196. adminIds = parsedAdminIds
  197. tgBotMutex.Unlock()
  198. // Get Telegram bot proxy URL
  199. tgBotProxy, err := t.settingService.GetTgBotProxy()
  200. if err != nil {
  201. logger.Warning("Failed to get Telegram bot proxy URL:", err)
  202. }
  203. // Get Telegram bot API server URL
  204. tgBotAPIServer, err := t.settingService.GetTgBotAPIServer()
  205. if err != nil {
  206. logger.Warning("Failed to get Telegram bot API server URL:", err)
  207. }
  208. // Create new Telegram bot instance
  209. bot, err = t.NewBot(tgBotToken, tgBotProxy, tgBotAPIServer)
  210. if err != nil {
  211. logger.Error("Failed to initialize Telegram bot API:", err)
  212. return err
  213. }
  214. // After bot initialization, set up bot commands with localized descriptions
  215. err = bot.SetMyCommands(context.Background(), &telego.SetMyCommandsParams{
  216. Commands: []telego.BotCommand{
  217. {Command: "start", Description: t.I18nBot("tgbot.commands.startDesc")},
  218. {Command: "help", Description: t.I18nBot("tgbot.commands.helpDesc")},
  219. {Command: "status", Description: t.I18nBot("tgbot.commands.statusDesc")},
  220. {Command: "id", Description: t.I18nBot("tgbot.commands.idDesc")},
  221. },
  222. })
  223. if err != nil {
  224. logger.Warning("Failed to set bot commands:", err)
  225. }
  226. // Start receiving Telegram bot messages
  227. tgBotMutex.Lock()
  228. alreadyRunning := isRunning || botCancel != nil
  229. tgBotMutex.Unlock()
  230. if !alreadyRunning {
  231. logger.Info("Telegram bot receiver started")
  232. go t.OnReceive()
  233. }
  234. return nil
  235. }
  236. // createRobustFastHTTPClient creates a fasthttp.Client with proper connection handling
  237. func (t *Tgbot) createRobustFastHTTPClient(proxyUrl string) *fasthttp.Client {
  238. client := &fasthttp.Client{
  239. // Connection timeouts
  240. ReadTimeout: 30 * time.Second,
  241. WriteTimeout: 30 * time.Second,
  242. MaxIdleConnDuration: 60 * time.Second,
  243. MaxConnDuration: 0, // unlimited, but controlled by MaxIdleConnDuration
  244. MaxIdemponentCallAttempts: 3,
  245. ReadBufferSize: 4096,
  246. WriteBufferSize: 4096,
  247. MaxConnsPerHost: 100,
  248. MaxConnWaitTimeout: 10 * time.Second,
  249. DisableHeaderNamesNormalizing: false,
  250. DisablePathNormalizing: false,
  251. // Retry on connection errors
  252. RetryIf: func(request *fasthttp.Request) bool {
  253. // Retry on connection errors for GET requests
  254. return string(request.Header.Method()) == "GET" || string(request.Header.Method()) == "POST"
  255. },
  256. }
  257. // Set proxy if provided
  258. if proxyUrl != "" {
  259. client.Dial = fasthttpproxy.FasthttpSocksDialer(proxyUrl)
  260. }
  261. return client
  262. }
  263. // NewBot creates a new Telegram bot instance with optional proxy and API server settings.
  264. func (t *Tgbot) NewBot(token string, proxyUrl string, apiServerUrl string) (*telego.Bot, error) {
  265. // Validate proxy URL if provided
  266. if proxyUrl != "" {
  267. if !strings.HasPrefix(proxyUrl, "socks5://") {
  268. logger.Warning("Invalid socks5 URL, ignoring proxy")
  269. proxyUrl = "" // Clear invalid proxy
  270. } else {
  271. _, err := url.Parse(proxyUrl)
  272. if err != nil {
  273. logger.Warningf("Can't parse proxy URL, ignoring proxy: %v", err)
  274. proxyUrl = ""
  275. }
  276. }
  277. }
  278. // Validate API server URL if provided
  279. if apiServerUrl != "" {
  280. if !strings.HasPrefix(apiServerUrl, "http") {
  281. logger.Warning("Invalid http(s) URL for API server, using default")
  282. apiServerUrl = ""
  283. } else {
  284. _, err := url.Parse(apiServerUrl)
  285. if err != nil {
  286. logger.Warningf("Can't parse API server URL, using default: %v", err)
  287. apiServerUrl = ""
  288. }
  289. }
  290. }
  291. // Create robust fasthttp client
  292. client := t.createRobustFastHTTPClient(proxyUrl)
  293. // Build bot options
  294. var options []telego.BotOption
  295. options = append(options, telego.WithFastHTTPClient(client))
  296. if apiServerUrl != "" {
  297. options = append(options, telego.WithAPIServer(apiServerUrl))
  298. }
  299. return telego.NewBot(token, options...)
  300. }
  301. // IsRunning checks if the Telegram bot is currently running.
  302. func (t *Tgbot) IsRunning() bool {
  303. tgBotMutex.Lock()
  304. defer tgBotMutex.Unlock()
  305. return isRunning
  306. }
  307. // SetHostname sets the hostname for the bot.
  308. func (t *Tgbot) SetHostname() {
  309. host, err := os.Hostname()
  310. if err != nil {
  311. logger.Error("get hostname error:", err)
  312. hostname = ""
  313. return
  314. }
  315. hostname = host
  316. }
  317. // Stop safely stops the Telegram bot's Long Polling operation.
  318. // This method now calls the global StopBot function and cleans up other resources.
  319. func (t *Tgbot) Stop() {
  320. StopBot()
  321. logger.Info("Stop Telegram receiver ...")
  322. tgBotMutex.Lock()
  323. adminIds = nil
  324. tgBotMutex.Unlock()
  325. }
  326. // StopBot safely stops the Telegram bot's Long Polling operation by cancelling its context.
  327. // This is the global function called from main.go's signal handler and t.Stop().
  328. func StopBot() {
  329. // Don't hold the mutex while cancelling/waiting.
  330. tgBotMutex.Lock()
  331. cancel := botCancel
  332. botCancel = nil
  333. handler := botHandler
  334. botHandler = nil
  335. isRunning = false
  336. tgBotMutex.Unlock()
  337. if handler != nil {
  338. handler.Stop()
  339. }
  340. if cancel != nil {
  341. logger.Info("Sending cancellation signal to Telegram bot...")
  342. // Cancels the context passed to UpdatesViaLongPolling; this closes updates channel
  343. // and lets botHandler.Start() exit cleanly.
  344. cancel()
  345. botWG.Wait()
  346. logger.Info("Telegram bot successfully stopped.")
  347. }
  348. }
  349. // encodeQuery encodes the query string if it's longer than 64 characters.
  350. func (t *Tgbot) encodeQuery(query string) string {
  351. // NOTE: we only need to hash for more than 64 chars
  352. if len(query) <= 64 {
  353. return query
  354. }
  355. return hashStorage.SaveHash(query)
  356. }
  357. // decodeQuery decodes a hashed query string back to its original form.
  358. func (t *Tgbot) decodeQuery(query string) (string, error) {
  359. if !hashStorage.IsMD5(query) {
  360. return query, nil
  361. }
  362. decoded, exists := hashStorage.GetValue(query)
  363. if !exists {
  364. return "", common.NewError("hash not found in storage!")
  365. }
  366. return decoded, nil
  367. }
  368. // OnReceive starts the message receiving loop for the Telegram bot.
  369. func (t *Tgbot) OnReceive() {
  370. params := telego.GetUpdatesParams{
  371. Timeout: 20, // Reduced timeout to detect connection issues faster
  372. }
  373. // Strict singleton: never start a second long-polling loop.
  374. tgBotMutex.Lock()
  375. if botCancel != nil || isRunning {
  376. tgBotMutex.Unlock()
  377. logger.Warning("TgBot OnReceive called while already running; ignoring.")
  378. return
  379. }
  380. ctx, cancel := context.WithCancel(context.Background())
  381. botCancel = cancel
  382. isRunning = true
  383. // Add to WaitGroup before releasing the lock so StopBot() can't return
  384. // before this receiver goroutine is accounted for.
  385. botWG.Add(1)
  386. tgBotMutex.Unlock()
  387. // Get updates channel using the context with shorter timeout for better error recovery
  388. updates, _ := bot.UpdatesViaLongPolling(ctx, &params)
  389. go func() {
  390. defer botWG.Done()
  391. h, _ := th.NewBotHandler(bot, updates)
  392. tgBotMutex.Lock()
  393. botHandler = h
  394. tgBotMutex.Unlock()
  395. h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
  396. delete(userStates, message.Chat.ID)
  397. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
  398. return nil
  399. }, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
  400. h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
  401. // Use goroutine with worker pool for concurrent command processing
  402. go func() {
  403. messageWorkerPool <- struct{}{} // Acquire worker
  404. defer func() { <-messageWorkerPool }() // Release worker
  405. delete(userStates, message.Chat.ID)
  406. t.answerCommand(&message, message.Chat.ID, checkAdmin(message.From.ID))
  407. }()
  408. return nil
  409. }, th.AnyCommand())
  410. h.HandleCallbackQuery(func(ctx *th.Context, query telego.CallbackQuery) error {
  411. // Use goroutine with worker pool for concurrent callback processing
  412. go func() {
  413. messageWorkerPool <- struct{}{} // Acquire worker
  414. defer func() { <-messageWorkerPool }() // Release worker
  415. delete(userStates, query.Message.GetChat().ID)
  416. t.answerCallback(&query, checkAdmin(query.From.ID))
  417. }()
  418. return nil
  419. }, th.AnyCallbackQueryWithMessage())
  420. h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
  421. if userState, exists := userStates[message.Chat.ID]; exists {
  422. switch userState {
  423. case "awaiting_id":
  424. if client_Id == strings.TrimSpace(message.Text) {
  425. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
  426. delete(userStates, message.Chat.ID)
  427. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  428. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  429. t.addClient(message.Chat.ID, message_text)
  430. return nil
  431. }
  432. client_Id = strings.TrimSpace(message.Text)
  433. if t.isSingleWord(client_Id) {
  434. userStates[message.Chat.ID] = "awaiting_id"
  435. cancel_btn_markup := tu.InlineKeyboard(
  436. tu.InlineKeyboardRow(
  437. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  438. ),
  439. )
  440. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.messages.incorrect_input"), cancel_btn_markup)
  441. } else {
  442. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_id"), 3, tu.ReplyKeyboardRemove())
  443. delete(userStates, message.Chat.ID)
  444. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  445. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  446. t.addClient(message.Chat.ID, message_text)
  447. }
  448. case "awaiting_password_tr":
  449. if client_TrPassword == strings.TrimSpace(message.Text) {
  450. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
  451. delete(userStates, message.Chat.ID)
  452. return nil
  453. }
  454. client_TrPassword = strings.TrimSpace(message.Text)
  455. if t.isSingleWord(client_TrPassword) {
  456. userStates[message.Chat.ID] = "awaiting_password_tr"
  457. cancel_btn_markup := tu.InlineKeyboard(
  458. tu.InlineKeyboardRow(
  459. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  460. ),
  461. )
  462. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.messages.incorrect_input"), cancel_btn_markup)
  463. } else {
  464. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_password"), 3, tu.ReplyKeyboardRemove())
  465. delete(userStates, message.Chat.ID)
  466. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  467. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  468. t.addClient(message.Chat.ID, message_text)
  469. }
  470. case "awaiting_password_sh":
  471. if client_ShPassword == strings.TrimSpace(message.Text) {
  472. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
  473. delete(userStates, message.Chat.ID)
  474. return nil
  475. }
  476. client_ShPassword = strings.TrimSpace(message.Text)
  477. if t.isSingleWord(client_ShPassword) {
  478. userStates[message.Chat.ID] = "awaiting_password_sh"
  479. cancel_btn_markup := tu.InlineKeyboard(
  480. tu.InlineKeyboardRow(
  481. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  482. ),
  483. )
  484. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.messages.incorrect_input"), cancel_btn_markup)
  485. } else {
  486. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_password"), 3, tu.ReplyKeyboardRemove())
  487. delete(userStates, message.Chat.ID)
  488. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  489. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  490. t.addClient(message.Chat.ID, message_text)
  491. }
  492. case "awaiting_email":
  493. if client_Email == strings.TrimSpace(message.Text) {
  494. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
  495. delete(userStates, message.Chat.ID)
  496. return nil
  497. }
  498. client_Email = strings.TrimSpace(message.Text)
  499. if t.isSingleWord(client_Email) {
  500. userStates[message.Chat.ID] = "awaiting_email"
  501. cancel_btn_markup := tu.InlineKeyboard(
  502. tu.InlineKeyboardRow(
  503. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  504. ),
  505. )
  506. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.messages.incorrect_input"), cancel_btn_markup)
  507. } else {
  508. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_email"), 3, tu.ReplyKeyboardRemove())
  509. delete(userStates, message.Chat.ID)
  510. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  511. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  512. t.addClient(message.Chat.ID, message_text)
  513. }
  514. case "awaiting_comment":
  515. if client_Comment == strings.TrimSpace(message.Text) {
  516. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
  517. delete(userStates, message.Chat.ID)
  518. return nil
  519. }
  520. client_Comment = strings.TrimSpace(message.Text)
  521. t.SendMsgToTgbotDeleteAfter(message.Chat.ID, t.I18nBot("tgbot.messages.received_comment"), 3, tu.ReplyKeyboardRemove())
  522. delete(userStates, message.Chat.ID)
  523. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  524. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  525. t.addClient(message.Chat.ID, message_text)
  526. }
  527. } else {
  528. if message.UsersShared != nil {
  529. if checkAdmin(message.From.ID) {
  530. for _, sharedUser := range message.UsersShared.Users {
  531. userID := sharedUser.UserID
  532. needRestart, err := t.inboundService.SetClientTelegramUserID(message.UsersShared.RequestID, userID)
  533. if needRestart {
  534. t.xrayService.SetToNeedRestart()
  535. }
  536. output := ""
  537. if err != nil {
  538. output += t.I18nBot("tgbot.messages.selectUserFailed")
  539. } else {
  540. output += t.I18nBot("tgbot.messages.userSaved")
  541. }
  542. t.SendMsgToTgbot(message.Chat.ID, output, tu.ReplyKeyboardRemove())
  543. }
  544. } else {
  545. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.noResult"), tu.ReplyKeyboardRemove())
  546. }
  547. }
  548. }
  549. return nil
  550. }, th.AnyMessage())
  551. h.Start()
  552. }()
  553. }
  554. // answerCommand processes incoming command messages from Telegram users.
  555. func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin bool) {
  556. msg, onlyMessage := "", false
  557. command, _, commandArgs := tu.ParseCommand(message.Text)
  558. // Helper function to handle unknown commands.
  559. handleUnknownCommand := func() {
  560. msg += t.I18nBot("tgbot.commands.unknown")
  561. }
  562. // Handle the command.
  563. switch command {
  564. case "help":
  565. msg += t.I18nBot("tgbot.commands.help")
  566. msg += t.I18nBot("tgbot.commands.pleaseChoose")
  567. case "start":
  568. msg += t.I18nBot("tgbot.commands.start", "Firstname=="+html.EscapeString(message.From.FirstName))
  569. if isAdmin {
  570. msg += t.I18nBot("tgbot.commands.welcome", "Hostname=="+hostname)
  571. }
  572. msg += "\n\n" + t.I18nBot("tgbot.commands.pleaseChoose")
  573. case "status":
  574. onlyMessage = true
  575. msg += t.I18nBot("tgbot.commands.status")
  576. case "id":
  577. onlyMessage = true
  578. msg += t.I18nBot("tgbot.commands.getID", "ID=="+strconv.FormatInt(message.From.ID, 10))
  579. case "usage":
  580. onlyMessage = true
  581. if len(commandArgs) > 0 {
  582. if isAdmin {
  583. t.searchClient(chatId, commandArgs[0])
  584. } else {
  585. t.getClientUsage(chatId, int64(message.From.ID), commandArgs[0])
  586. }
  587. } else {
  588. msg += t.I18nBot("tgbot.commands.usage")
  589. }
  590. case "inbound":
  591. onlyMessage = true
  592. if isAdmin && len(commandArgs) > 0 {
  593. t.searchInbound(chatId, commandArgs[0])
  594. } else {
  595. handleUnknownCommand()
  596. }
  597. case "restart":
  598. onlyMessage = true
  599. if isAdmin {
  600. if len(commandArgs) == 0 {
  601. if t.xrayService.IsXrayRunning() {
  602. err := t.xrayService.RestartXray(true)
  603. if err != nil {
  604. msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error())
  605. } else {
  606. msg += t.I18nBot("tgbot.commands.restartSuccess")
  607. }
  608. } else {
  609. msg += t.I18nBot("tgbot.commands.xrayNotRunning")
  610. }
  611. } else {
  612. handleUnknownCommand()
  613. msg += t.I18nBot("tgbot.commands.restartUsage")
  614. }
  615. } else {
  616. handleUnknownCommand()
  617. }
  618. default:
  619. handleUnknownCommand()
  620. }
  621. if msg != "" {
  622. t.sendResponse(chatId, msg, onlyMessage, isAdmin)
  623. }
  624. }
  625. // sendResponse sends the response message based on the onlyMessage flag.
  626. func (t *Tgbot) sendResponse(chatId int64, msg string, onlyMessage, isAdmin bool) {
  627. if onlyMessage {
  628. t.SendMsgToTgbot(chatId, msg)
  629. } else {
  630. t.SendAnswer(chatId, msg, isAdmin)
  631. }
  632. }
  633. // randomLowerAndNum generates a random string of lowercase letters and numbers.
  634. func (t *Tgbot) randomLowerAndNum(length int) string {
  635. charset := "abcdefghijklmnopqrstuvwxyz0123456789"
  636. bytes := make([]byte, length)
  637. for i := range bytes {
  638. randomIndex, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
  639. bytes[i] = charset[randomIndex.Int64()]
  640. }
  641. return string(bytes)
  642. }
  643. // randomShadowSocksPassword generates a random password for Shadowsocks.
  644. func (t *Tgbot) randomShadowSocksPassword() string {
  645. array := make([]byte, 32)
  646. _, err := rand.Read(array)
  647. if err != nil {
  648. return t.randomLowerAndNum(32)
  649. }
  650. return base64.StdEncoding.EncodeToString(array)
  651. }
  652. // answerCallback processes callback queries from inline keyboards.
  653. func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool) {
  654. chatId := callbackQuery.Message.GetChat().ID
  655. if isAdmin {
  656. // get query from hash storage
  657. decodedQuery, err := t.decodeQuery(callbackQuery.Data)
  658. if err != nil {
  659. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.noQuery"))
  660. return
  661. }
  662. dataArray := strings.Split(decodedQuery, " ")
  663. if len(dataArray) >= 2 && len(dataArray[1]) > 0 {
  664. email := dataArray[1]
  665. switch dataArray[0] {
  666. case "get_clients_for_sub":
  667. inboundId := dataArray[1]
  668. inboundIdInt, err := strconv.Atoi(inboundId)
  669. if err != nil {
  670. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  671. return
  672. }
  673. clientsKB, err := t.getInboundClientsFor(inboundIdInt, "client_sub_links")
  674. if err != nil {
  675. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  676. return
  677. }
  678. inbound, _ := t.inboundService.GetInbound(inboundIdInt)
  679. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clientsKB)
  680. case "get_clients_for_individual":
  681. inboundId := dataArray[1]
  682. inboundIdInt, err := strconv.Atoi(inboundId)
  683. if err != nil {
  684. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  685. return
  686. }
  687. clientsKB, err := t.getInboundClientsFor(inboundIdInt, "client_individual_links")
  688. if err != nil {
  689. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  690. return
  691. }
  692. inbound, _ := t.inboundService.GetInbound(inboundIdInt)
  693. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clientsKB)
  694. case "get_clients_for_qr":
  695. inboundId := dataArray[1]
  696. inboundIdInt, err := strconv.Atoi(inboundId)
  697. if err != nil {
  698. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  699. return
  700. }
  701. clientsKB, err := t.getInboundClientsFor(inboundIdInt, "client_qr_links")
  702. if err != nil {
  703. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  704. return
  705. }
  706. inbound, _ := t.inboundService.GetInbound(inboundIdInt)
  707. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clientsKB)
  708. case "client_sub_links":
  709. t.sendClientSubLinks(chatId, email)
  710. return
  711. case "client_individual_links":
  712. t.sendClientIndividualLinks(chatId, email)
  713. return
  714. case "client_qr_links":
  715. t.sendClientQRLinks(chatId, email)
  716. return
  717. case "client_get_usage":
  718. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.messages.email", "Email=="+email))
  719. t.searchClient(chatId, email)
  720. case "client_refresh":
  721. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clientRefreshSuccess", "Email=="+email))
  722. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  723. case "client_cancel":
  724. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
  725. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  726. case "ips_refresh":
  727. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.IpRefreshSuccess", "Email=="+email))
  728. t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
  729. case "ips_cancel":
  730. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
  731. t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
  732. case "tgid_refresh":
  733. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.TGIdRefreshSuccess", "Email=="+email))
  734. t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
  735. case "tgid_cancel":
  736. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
  737. t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
  738. case "reset_traffic":
  739. inlineKeyboard := tu.InlineKeyboard(
  740. tu.InlineKeyboardRow(
  741. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  742. ),
  743. tu.InlineKeyboardRow(
  744. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic_c "+email)),
  745. ),
  746. )
  747. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  748. case "reset_traffic_c":
  749. err := t.inboundService.ResetClientTrafficByEmail(email)
  750. if err == nil {
  751. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetTrafficSuccess", "Email=="+email))
  752. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  753. } else {
  754. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  755. }
  756. case "limit_traffic":
  757. inlineKeyboard := tu.InlineKeyboard(
  758. tu.InlineKeyboardRow(
  759. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  760. ),
  761. tu.InlineKeyboardRow(
  762. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 0")),
  763. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" 0")),
  764. ),
  765. tu.InlineKeyboardRow(
  766. tu.InlineKeyboardButton("1 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 1")),
  767. tu.InlineKeyboardButton("5 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 5")),
  768. tu.InlineKeyboardButton("10 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 10")),
  769. ),
  770. tu.InlineKeyboardRow(
  771. tu.InlineKeyboardButton("20 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 20")),
  772. tu.InlineKeyboardButton("30 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 30")),
  773. tu.InlineKeyboardButton("40 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 40")),
  774. ),
  775. tu.InlineKeyboardRow(
  776. tu.InlineKeyboardButton("50 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 50")),
  777. tu.InlineKeyboardButton("60 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 60")),
  778. tu.InlineKeyboardButton("80 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 80")),
  779. ),
  780. tu.InlineKeyboardRow(
  781. tu.InlineKeyboardButton("100 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 100")),
  782. tu.InlineKeyboardButton("150 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 150")),
  783. tu.InlineKeyboardButton("200 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 200")),
  784. ),
  785. )
  786. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  787. case "limit_traffic_c":
  788. if len(dataArray) == 3 {
  789. limitTraffic, err := strconv.Atoi(dataArray[2])
  790. if err == nil {
  791. needRestart, err := t.inboundService.ResetClientTrafficLimitByEmail(email, limitTraffic)
  792. if needRestart {
  793. t.xrayService.SetToNeedRestart()
  794. }
  795. if err == nil {
  796. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.setTrafficLimitSuccess", "Email=="+email))
  797. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  798. return
  799. }
  800. }
  801. }
  802. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  803. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  804. case "limit_traffic_in":
  805. if len(dataArray) >= 3 {
  806. oldInputNumber, err := strconv.Atoi(dataArray[2])
  807. inputNumber := oldInputNumber
  808. if err == nil {
  809. if len(dataArray) == 4 {
  810. num, err := strconv.Atoi(dataArray[3])
  811. if err == nil {
  812. switch num {
  813. case -2:
  814. inputNumber = 0
  815. case -1:
  816. if inputNumber > 0 {
  817. inputNumber = (inputNumber / 10)
  818. }
  819. default:
  820. inputNumber = (inputNumber * 10) + num
  821. }
  822. }
  823. if inputNumber == oldInputNumber {
  824. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  825. return
  826. }
  827. if inputNumber >= 999999 {
  828. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  829. return
  830. }
  831. }
  832. inlineKeyboard := tu.InlineKeyboard(
  833. tu.InlineKeyboardRow(
  834. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  835. ),
  836. tu.InlineKeyboardRow(
  837. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumberAdd", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" "+strconv.Itoa(inputNumber))),
  838. ),
  839. tu.InlineKeyboardRow(
  840. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 1")),
  841. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 2")),
  842. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 3")),
  843. ),
  844. tu.InlineKeyboardRow(
  845. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 4")),
  846. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 5")),
  847. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 6")),
  848. ),
  849. tu.InlineKeyboardRow(
  850. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 7")),
  851. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 8")),
  852. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 9")),
  853. ),
  854. tu.InlineKeyboardRow(
  855. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" -2")),
  856. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 0")),
  857. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" -1")),
  858. ),
  859. )
  860. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  861. return
  862. }
  863. }
  864. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  865. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  866. case "add_client_limit_traffic_c":
  867. limitTraffic, _ := strconv.ParseInt(dataArray[1], 10, 64)
  868. client_TotalGB = limitTraffic * 1024 * 1024 * 1024
  869. messageId := callbackQuery.Message.GetMessageID()
  870. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  871. if err != nil {
  872. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  873. return
  874. }
  875. message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  876. if err != nil {
  877. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  878. return
  879. }
  880. t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId)
  881. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  882. case "add_client_limit_traffic_in":
  883. if len(dataArray) >= 2 {
  884. oldInputNumber, err := strconv.Atoi(dataArray[1])
  885. inputNumber := oldInputNumber
  886. if err == nil {
  887. if len(dataArray) == 3 {
  888. num, err := strconv.Atoi(dataArray[2])
  889. if err == nil {
  890. switch num {
  891. case -2:
  892. inputNumber = 0
  893. case -1:
  894. if inputNumber > 0 {
  895. inputNumber = (inputNumber / 10)
  896. }
  897. default:
  898. inputNumber = (inputNumber * 10) + num
  899. }
  900. }
  901. if inputNumber == oldInputNumber {
  902. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  903. return
  904. }
  905. if inputNumber >= 999999 {
  906. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  907. return
  908. }
  909. }
  910. inlineKeyboard := tu.InlineKeyboard(
  911. tu.InlineKeyboardRow(
  912. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_traffic_exp")),
  913. ),
  914. tu.InlineKeyboardRow(
  915. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumberAdd", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("add_client_limit_traffic_c "+strconv.Itoa(inputNumber))),
  916. ),
  917. tu.InlineKeyboardRow(
  918. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 1")),
  919. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 2")),
  920. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 3")),
  921. ),
  922. tu.InlineKeyboardRow(
  923. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 4")),
  924. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 5")),
  925. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 6")),
  926. ),
  927. tu.InlineKeyboardRow(
  928. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 7")),
  929. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 8")),
  930. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 9")),
  931. ),
  932. tu.InlineKeyboardRow(
  933. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" -2")),
  934. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" 0")),
  935. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("add_client_limit_traffic_in "+strconv.Itoa(inputNumber)+" -1")),
  936. ),
  937. )
  938. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  939. return
  940. }
  941. }
  942. case "reset_exp":
  943. inlineKeyboard := tu.InlineKeyboard(
  944. tu.InlineKeyboardRow(
  945. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  946. ),
  947. tu.InlineKeyboardRow(
  948. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 0")),
  949. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("reset_exp_in "+email+" 0")),
  950. ),
  951. tu.InlineKeyboardRow(
  952. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 7 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 7")),
  953. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 10 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 10")),
  954. ),
  955. tu.InlineKeyboardRow(
  956. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 14 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 14")),
  957. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 20 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 20")),
  958. ),
  959. tu.InlineKeyboardRow(
  960. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 1 "+t.I18nBot("tgbot.month")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 30")),
  961. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 3 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 90")),
  962. ),
  963. tu.InlineKeyboardRow(
  964. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 6 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 180")),
  965. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 12 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 365")),
  966. ),
  967. )
  968. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  969. case "reset_exp_c":
  970. if len(dataArray) == 3 {
  971. days, err := strconv.ParseInt(dataArray[2], 10, 64)
  972. if err == nil {
  973. var date int64
  974. if days > 0 {
  975. traffic, err := t.inboundService.GetClientTrafficByEmail(email)
  976. if err != nil {
  977. logger.Warning(err)
  978. msg := t.I18nBot("tgbot.wentWrong")
  979. t.SendMsgToTgbot(chatId, msg)
  980. return
  981. }
  982. if traffic == nil {
  983. msg := t.I18nBot("tgbot.noResult")
  984. t.SendMsgToTgbot(chatId, msg)
  985. return
  986. }
  987. if traffic.ExpiryTime > 0 {
  988. if traffic.ExpiryTime-time.Now().Unix()*1000 < 0 {
  989. date = -int64(days * 24 * 60 * 60000)
  990. } else {
  991. date = traffic.ExpiryTime + int64(days*24*60*60000)
  992. }
  993. } else {
  994. date = traffic.ExpiryTime - int64(days*24*60*60000)
  995. }
  996. }
  997. needRestart, err := t.inboundService.ResetClientExpiryTimeByEmail(email, date)
  998. if needRestart {
  999. t.xrayService.SetToNeedRestart()
  1000. }
  1001. if err == nil {
  1002. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.expireResetSuccess", "Email=="+email))
  1003. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1004. return
  1005. }
  1006. }
  1007. }
  1008. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1009. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1010. case "reset_exp_in":
  1011. if len(dataArray) >= 3 {
  1012. oldInputNumber, err := strconv.Atoi(dataArray[2])
  1013. inputNumber := oldInputNumber
  1014. if err == nil {
  1015. if len(dataArray) == 4 {
  1016. num, err := strconv.Atoi(dataArray[3])
  1017. if err == nil {
  1018. switch num {
  1019. case -2:
  1020. inputNumber = 0
  1021. case -1:
  1022. if inputNumber > 0 {
  1023. inputNumber = (inputNumber / 10)
  1024. }
  1025. default:
  1026. inputNumber = (inputNumber * 10) + num
  1027. }
  1028. }
  1029. if inputNumber == oldInputNumber {
  1030. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1031. return
  1032. }
  1033. if inputNumber >= 999999 {
  1034. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1035. return
  1036. }
  1037. }
  1038. inlineKeyboard := tu.InlineKeyboard(
  1039. tu.InlineKeyboardRow(
  1040. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  1041. ),
  1042. tu.InlineKeyboardRow(
  1043. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" "+strconv.Itoa(inputNumber))),
  1044. ),
  1045. tu.InlineKeyboardRow(
  1046. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 1")),
  1047. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 2")),
  1048. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 3")),
  1049. ),
  1050. tu.InlineKeyboardRow(
  1051. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 4")),
  1052. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 5")),
  1053. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 6")),
  1054. ),
  1055. tu.InlineKeyboardRow(
  1056. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 7")),
  1057. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 8")),
  1058. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 9")),
  1059. ),
  1060. tu.InlineKeyboardRow(
  1061. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" -2")),
  1062. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 0")),
  1063. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" -1")),
  1064. ),
  1065. )
  1066. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1067. return
  1068. }
  1069. }
  1070. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1071. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1072. case "add_client_reset_exp_c":
  1073. client_ExpiryTime = 0
  1074. days, _ := strconv.ParseInt(dataArray[1], 10, 64)
  1075. var date int64
  1076. if client_ExpiryTime > 0 {
  1077. if client_ExpiryTime-time.Now().Unix()*1000 < 0 {
  1078. date = -int64(days * 24 * 60 * 60000)
  1079. } else {
  1080. date = client_ExpiryTime + int64(days*24*60*60000)
  1081. }
  1082. } else {
  1083. date = client_ExpiryTime - int64(days*24*60*60000)
  1084. }
  1085. client_ExpiryTime = date
  1086. messageId := callbackQuery.Message.GetMessageID()
  1087. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  1088. if err != nil {
  1089. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1090. return
  1091. }
  1092. message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  1093. if err != nil {
  1094. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1095. return
  1096. }
  1097. t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId)
  1098. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1099. case "add_client_reset_exp_in":
  1100. if len(dataArray) >= 2 {
  1101. oldInputNumber, err := strconv.Atoi(dataArray[1])
  1102. inputNumber := oldInputNumber
  1103. if err == nil {
  1104. if len(dataArray) == 3 {
  1105. num, err := strconv.Atoi(dataArray[2])
  1106. if err == nil {
  1107. switch num {
  1108. case -2:
  1109. inputNumber = 0
  1110. case -1:
  1111. if inputNumber > 0 {
  1112. inputNumber = (inputNumber / 10)
  1113. }
  1114. default:
  1115. inputNumber = (inputNumber * 10) + num
  1116. }
  1117. }
  1118. if inputNumber == oldInputNumber {
  1119. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1120. return
  1121. }
  1122. if inputNumber >= 999999 {
  1123. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1124. return
  1125. }
  1126. }
  1127. inlineKeyboard := tu.InlineKeyboard(
  1128. tu.InlineKeyboardRow(
  1129. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_traffic_exp")),
  1130. ),
  1131. tu.InlineKeyboardRow(
  1132. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumberAdd", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("add_client_reset_exp_c "+strconv.Itoa(inputNumber))),
  1133. ),
  1134. tu.InlineKeyboardRow(
  1135. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 1")),
  1136. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 2")),
  1137. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 3")),
  1138. ),
  1139. tu.InlineKeyboardRow(
  1140. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 4")),
  1141. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 5")),
  1142. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 6")),
  1143. ),
  1144. tu.InlineKeyboardRow(
  1145. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 7")),
  1146. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 8")),
  1147. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 9")),
  1148. ),
  1149. tu.InlineKeyboardRow(
  1150. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" -2")),
  1151. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" 0")),
  1152. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("add_client_reset_exp_in "+strconv.Itoa(inputNumber)+" -1")),
  1153. ),
  1154. )
  1155. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1156. return
  1157. }
  1158. }
  1159. case "ip_limit":
  1160. inlineKeyboard := tu.InlineKeyboard(
  1161. tu.InlineKeyboardRow(
  1162. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelIpLimit")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  1163. ),
  1164. tu.InlineKeyboardRow(
  1165. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 0")),
  1166. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("ip_limit_in "+email+" 0")),
  1167. ),
  1168. tu.InlineKeyboardRow(
  1169. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 1")),
  1170. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 2")),
  1171. ),
  1172. tu.InlineKeyboardRow(
  1173. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 3")),
  1174. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 4")),
  1175. ),
  1176. tu.InlineKeyboardRow(
  1177. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 5")),
  1178. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 6")),
  1179. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 7")),
  1180. ),
  1181. tu.InlineKeyboardRow(
  1182. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 8")),
  1183. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 9")),
  1184. tu.InlineKeyboardButton("10").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 10")),
  1185. ),
  1186. )
  1187. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1188. case "ip_limit_c":
  1189. if len(dataArray) == 3 {
  1190. count, err := strconv.Atoi(dataArray[2])
  1191. if err == nil {
  1192. needRestart, err := t.inboundService.ResetClientIpLimitByEmail(email, count)
  1193. if needRestart {
  1194. t.xrayService.SetToNeedRestart()
  1195. }
  1196. if err == nil {
  1197. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetIpSuccess", "Email=="+email, "Count=="+strconv.Itoa(count)))
  1198. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1199. return
  1200. }
  1201. }
  1202. }
  1203. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1204. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1205. case "ip_limit_in":
  1206. if len(dataArray) >= 3 {
  1207. oldInputNumber, err := strconv.Atoi(dataArray[2])
  1208. inputNumber := oldInputNumber
  1209. if err == nil {
  1210. if len(dataArray) == 4 {
  1211. num, err := strconv.Atoi(dataArray[3])
  1212. if err == nil {
  1213. switch num {
  1214. case -2:
  1215. inputNumber = 0
  1216. case -1:
  1217. if inputNumber > 0 {
  1218. inputNumber = (inputNumber / 10)
  1219. }
  1220. default:
  1221. inputNumber = (inputNumber * 10) + num
  1222. }
  1223. }
  1224. if inputNumber == oldInputNumber {
  1225. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1226. return
  1227. }
  1228. if inputNumber >= 999999 {
  1229. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1230. return
  1231. }
  1232. }
  1233. inlineKeyboard := tu.InlineKeyboard(
  1234. tu.InlineKeyboardRow(
  1235. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  1236. ),
  1237. tu.InlineKeyboardRow(
  1238. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" "+strconv.Itoa(inputNumber))),
  1239. ),
  1240. tu.InlineKeyboardRow(
  1241. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 1")),
  1242. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 2")),
  1243. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 3")),
  1244. ),
  1245. tu.InlineKeyboardRow(
  1246. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 4")),
  1247. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 5")),
  1248. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 6")),
  1249. ),
  1250. tu.InlineKeyboardRow(
  1251. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 7")),
  1252. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 8")),
  1253. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 9")),
  1254. ),
  1255. tu.InlineKeyboardRow(
  1256. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" -2")),
  1257. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 0")),
  1258. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" -1")),
  1259. ),
  1260. )
  1261. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1262. return
  1263. }
  1264. }
  1265. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1266. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1267. case "add_client_ip_limit_c":
  1268. if len(dataArray) == 2 {
  1269. count, _ := strconv.Atoi(dataArray[1])
  1270. client_LimitIP = count
  1271. }
  1272. messageId := callbackQuery.Message.GetMessageID()
  1273. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  1274. if err != nil {
  1275. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1276. return
  1277. }
  1278. message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  1279. if err != nil {
  1280. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1281. return
  1282. }
  1283. t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId)
  1284. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1285. case "add_client_ip_limit_in":
  1286. if len(dataArray) >= 2 {
  1287. oldInputNumber, err := strconv.Atoi(dataArray[1])
  1288. inputNumber := oldInputNumber
  1289. if err == nil {
  1290. if len(dataArray) == 3 {
  1291. num, err := strconv.Atoi(dataArray[2])
  1292. if err == nil {
  1293. switch num {
  1294. case -2:
  1295. inputNumber = 0
  1296. case -1:
  1297. if inputNumber > 0 {
  1298. inputNumber = (inputNumber / 10)
  1299. }
  1300. default:
  1301. inputNumber = (inputNumber * 10) + num
  1302. }
  1303. }
  1304. if inputNumber == oldInputNumber {
  1305. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1306. return
  1307. }
  1308. if inputNumber >= 999999 {
  1309. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1310. return
  1311. }
  1312. }
  1313. inlineKeyboard := tu.InlineKeyboard(
  1314. tu.InlineKeyboardRow(
  1315. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_ip_limit")),
  1316. ),
  1317. tu.InlineKeyboardRow(
  1318. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("add_client_ip_limit_c "+strconv.Itoa(inputNumber))),
  1319. ),
  1320. tu.InlineKeyboardRow(
  1321. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 1")),
  1322. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 2")),
  1323. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 3")),
  1324. ),
  1325. tu.InlineKeyboardRow(
  1326. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 4")),
  1327. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 5")),
  1328. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 6")),
  1329. ),
  1330. tu.InlineKeyboardRow(
  1331. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 7")),
  1332. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 8")),
  1333. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 9")),
  1334. ),
  1335. tu.InlineKeyboardRow(
  1336. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" -2")),
  1337. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" 0")),
  1338. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("add_client_ip_limit_in "+strconv.Itoa(inputNumber)+" -1")),
  1339. ),
  1340. )
  1341. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1342. return
  1343. }
  1344. }
  1345. case "clear_ips":
  1346. inlineKeyboard := tu.InlineKeyboard(
  1347. tu.InlineKeyboardRow(
  1348. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("ips_cancel "+email)),
  1349. ),
  1350. tu.InlineKeyboardRow(
  1351. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmClearIps")).WithCallbackData(t.encodeQuery("clear_ips_c "+email)),
  1352. ),
  1353. )
  1354. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1355. case "clear_ips_c":
  1356. err := t.inboundService.ClearClientIps(email)
  1357. if err == nil {
  1358. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clearIpSuccess", "Email=="+email))
  1359. t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
  1360. } else {
  1361. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1362. }
  1363. case "ip_log":
  1364. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getIpLog", "Email=="+email))
  1365. t.searchClientIps(chatId, email)
  1366. case "tg_user":
  1367. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getUserInfo", "Email=="+email))
  1368. t.clientTelegramUserInfo(chatId, email)
  1369. case "tgid_remove":
  1370. inlineKeyboard := tu.InlineKeyboard(
  1371. tu.InlineKeyboardRow(
  1372. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("tgid_cancel "+email)),
  1373. ),
  1374. tu.InlineKeyboardRow(
  1375. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmRemoveTGUser")).WithCallbackData(t.encodeQuery("tgid_remove_c "+email)),
  1376. ),
  1377. )
  1378. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1379. case "tgid_remove_c":
  1380. traffic, err := t.inboundService.GetClientTrafficByEmail(email)
  1381. if err != nil || traffic == nil {
  1382. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1383. return
  1384. }
  1385. needRestart, err := t.inboundService.SetClientTelegramUserID(traffic.Id, EmptyTelegramUserID)
  1386. if needRestart {
  1387. t.xrayService.SetToNeedRestart()
  1388. }
  1389. if err == nil {
  1390. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.removedTGUserSuccess", "Email=="+email))
  1391. t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
  1392. } else {
  1393. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1394. }
  1395. case "toggle_enable":
  1396. inlineKeyboard := tu.InlineKeyboard(
  1397. tu.InlineKeyboardRow(
  1398. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  1399. ),
  1400. tu.InlineKeyboardRow(
  1401. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmToggle")).WithCallbackData(t.encodeQuery("toggle_enable_c "+email)),
  1402. ),
  1403. )
  1404. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1405. case "toggle_enable_c":
  1406. enabled, needRestart, err := t.inboundService.ToggleClientEnableByEmail(email)
  1407. if needRestart {
  1408. t.xrayService.SetToNeedRestart()
  1409. }
  1410. if err == nil {
  1411. if enabled {
  1412. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.enableSuccess", "Email=="+email))
  1413. } else {
  1414. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.disableSuccess", "Email=="+email))
  1415. }
  1416. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  1417. } else {
  1418. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  1419. }
  1420. case "get_clients":
  1421. inboundId := dataArray[1]
  1422. inboundIdInt, err := strconv.Atoi(inboundId)
  1423. if err != nil {
  1424. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1425. return
  1426. }
  1427. inbound, err := t.inboundService.GetInbound(inboundIdInt)
  1428. if err != nil {
  1429. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1430. return
  1431. }
  1432. clients, err := t.getInboundClients(inboundIdInt)
  1433. if err != nil {
  1434. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1435. return
  1436. }
  1437. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clients)
  1438. case "add_client_to":
  1439. // assign default values to clients variables
  1440. client_Id = uuid.New().String()
  1441. client_Flow = ""
  1442. client_Email = t.randomLowerAndNum(8)
  1443. client_LimitIP = 0
  1444. client_TotalGB = 0
  1445. client_ExpiryTime = 0
  1446. client_Enable = true
  1447. client_TgID = ""
  1448. client_SubID = t.randomLowerAndNum(16)
  1449. client_Comment = ""
  1450. client_Reset = 0
  1451. client_Security = "auto"
  1452. client_ShPassword = t.randomShadowSocksPassword()
  1453. client_TrPassword = t.randomLowerAndNum(10)
  1454. client_Method = ""
  1455. inboundId := dataArray[1]
  1456. inboundIdInt, err := strconv.Atoi(inboundId)
  1457. if err != nil {
  1458. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1459. return
  1460. }
  1461. receiver_inbound_ID = inboundIdInt
  1462. inbound, err := t.inboundService.GetInbound(inboundIdInt)
  1463. if err != nil {
  1464. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1465. return
  1466. }
  1467. message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  1468. if err != nil {
  1469. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1470. return
  1471. }
  1472. t.addClient(callbackQuery.Message.GetChat().ID, message_text)
  1473. }
  1474. return
  1475. } else {
  1476. switch callbackQuery.Data {
  1477. case "get_inbounds":
  1478. inbounds, err := t.getInbounds()
  1479. if err != nil {
  1480. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1481. return
  1482. }
  1483. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.allClients"))
  1484. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
  1485. case "admin_client_sub_links":
  1486. inbounds, err := t.getInboundsFor("get_clients_for_sub")
  1487. if err != nil {
  1488. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1489. return
  1490. }
  1491. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
  1492. case "admin_client_individual_links":
  1493. inbounds, err := t.getInboundsFor("get_clients_for_individual")
  1494. if err != nil {
  1495. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1496. return
  1497. }
  1498. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
  1499. case "admin_client_qr_links":
  1500. inbounds, err := t.getInboundsFor("get_clients_for_qr")
  1501. if err != nil {
  1502. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1503. return
  1504. }
  1505. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
  1506. }
  1507. }
  1508. }
  1509. switch callbackQuery.Data {
  1510. case "get_usage":
  1511. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.serverUsage"))
  1512. t.getServerUsage(chatId)
  1513. case "usage_refresh":
  1514. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1515. t.getServerUsage(chatId, callbackQuery.Message.GetMessageID())
  1516. case "inbounds":
  1517. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getInbounds"))
  1518. t.SendMsgToTgbot(chatId, t.getInboundUsages())
  1519. case "deplete_soon":
  1520. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.depleteSoon"))
  1521. t.getExhausted(chatId)
  1522. case "get_backup":
  1523. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.dbBackup"))
  1524. t.sendBackup(chatId)
  1525. case "get_banlogs":
  1526. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getBanLogs"))
  1527. t.sendBanLogs(chatId, true)
  1528. case "client_traffic":
  1529. tgUserID := callbackQuery.From.ID
  1530. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.clientUsage"))
  1531. t.getClientUsage(chatId, tgUserID)
  1532. case "client_commands":
  1533. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
  1534. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpClientCommands"))
  1535. case "client_sub_links":
  1536. // show user's own clients to choose one for sub links
  1537. tgUserID := callbackQuery.From.ID
  1538. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
  1539. if err != nil {
  1540. // fallback to message
  1541. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  1542. return
  1543. }
  1544. if len(traffics) == 0 {
  1545. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
  1546. return
  1547. }
  1548. var buttons []telego.InlineKeyboardButton
  1549. for _, tr := range traffics {
  1550. buttons = append(buttons, tu.InlineKeyboardButton(tr.Email).WithCallbackData(t.encodeQuery("client_sub_links "+tr.Email)))
  1551. }
  1552. cols := 1
  1553. if len(buttons) >= 6 {
  1554. cols = 2
  1555. }
  1556. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  1557. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.pleaseChoose"), keyboard)
  1558. case "client_individual_links":
  1559. // show user's clients to choose for individual links
  1560. tgUserID := callbackQuery.From.ID
  1561. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
  1562. if err != nil {
  1563. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  1564. return
  1565. }
  1566. if len(traffics) == 0 {
  1567. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
  1568. return
  1569. }
  1570. var buttons2 []telego.InlineKeyboardButton
  1571. for _, tr := range traffics {
  1572. buttons2 = append(buttons2, tu.InlineKeyboardButton(tr.Email).WithCallbackData(t.encodeQuery("client_individual_links "+tr.Email)))
  1573. }
  1574. cols2 := 1
  1575. if len(buttons2) >= 6 {
  1576. cols2 = 2
  1577. }
  1578. keyboard2 := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols2, buttons2...))
  1579. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.pleaseChoose"), keyboard2)
  1580. case "client_qr_links":
  1581. // show user's clients to choose for QR codes
  1582. tgUserID := callbackQuery.From.ID
  1583. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
  1584. if err != nil {
  1585. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOccurred")+"\r\n"+err.Error())
  1586. return
  1587. }
  1588. if len(traffics) == 0 {
  1589. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
  1590. return
  1591. }
  1592. var buttons3 []telego.InlineKeyboardButton
  1593. for _, tr := range traffics {
  1594. buttons3 = append(buttons3, tu.InlineKeyboardButton(tr.Email).WithCallbackData(t.encodeQuery("client_qr_links "+tr.Email)))
  1595. }
  1596. cols3 := 1
  1597. if len(buttons3) >= 6 {
  1598. cols3 = 2
  1599. }
  1600. keyboard3 := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols3, buttons3...))
  1601. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.pleaseChoose"), keyboard3)
  1602. case "onlines":
  1603. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.onlines"))
  1604. t.onlineClients(chatId)
  1605. case "onlines_refresh":
  1606. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  1607. t.onlineClients(chatId, callbackQuery.Message.GetMessageID())
  1608. case "commands":
  1609. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
  1610. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpAdminCommands"))
  1611. case "add_client":
  1612. // assign default values to clients variables
  1613. client_Id = uuid.New().String()
  1614. client_Flow = ""
  1615. client_Email = t.randomLowerAndNum(8)
  1616. client_LimitIP = 0
  1617. client_TotalGB = 0
  1618. client_ExpiryTime = 0
  1619. client_Enable = true
  1620. client_TgID = ""
  1621. client_SubID = t.randomLowerAndNum(16)
  1622. client_Comment = ""
  1623. client_Reset = 0
  1624. client_Security = "auto"
  1625. client_ShPassword = t.randomShadowSocksPassword()
  1626. client_TrPassword = t.randomLowerAndNum(10)
  1627. client_Method = ""
  1628. inbounds, err := t.getInboundsAddClient()
  1629. if err != nil {
  1630. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1631. return
  1632. }
  1633. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.addClient"))
  1634. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
  1635. case "add_client_ch_default_email":
  1636. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1637. userStates[chatId] = "awaiting_email"
  1638. cancel_btn_markup := tu.InlineKeyboard(
  1639. tu.InlineKeyboardRow(
  1640. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  1641. ),
  1642. )
  1643. prompt_message := t.I18nBot("tgbot.messages.email_prompt", "ClientEmail=="+client_Email)
  1644. t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
  1645. case "add_client_ch_default_id":
  1646. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1647. userStates[chatId] = "awaiting_id"
  1648. cancel_btn_markup := tu.InlineKeyboard(
  1649. tu.InlineKeyboardRow(
  1650. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  1651. ),
  1652. )
  1653. prompt_message := t.I18nBot("tgbot.messages.id_prompt", "ClientId=="+client_Id)
  1654. t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
  1655. case "add_client_ch_default_pass_tr":
  1656. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1657. userStates[chatId] = "awaiting_password_tr"
  1658. cancel_btn_markup := tu.InlineKeyboard(
  1659. tu.InlineKeyboardRow(
  1660. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  1661. ),
  1662. )
  1663. prompt_message := t.I18nBot("tgbot.messages.pass_prompt", "ClientPassword=="+client_TrPassword)
  1664. t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
  1665. case "add_client_ch_default_pass_sh":
  1666. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1667. userStates[chatId] = "awaiting_password_sh"
  1668. cancel_btn_markup := tu.InlineKeyboard(
  1669. tu.InlineKeyboardRow(
  1670. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  1671. ),
  1672. )
  1673. prompt_message := t.I18nBot("tgbot.messages.pass_prompt", "ClientPassword=="+client_ShPassword)
  1674. t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
  1675. case "add_client_ch_default_comment":
  1676. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1677. userStates[chatId] = "awaiting_comment"
  1678. cancel_btn_markup := tu.InlineKeyboard(
  1679. tu.InlineKeyboardRow(
  1680. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.use_default")).WithCallbackData("add_client_default_info"),
  1681. ),
  1682. )
  1683. prompt_message := t.I18nBot("tgbot.messages.comment_prompt", "ClientComment=="+client_Comment)
  1684. t.SendMsgToTgbot(chatId, prompt_message, cancel_btn_markup)
  1685. case "add_client_ch_default_traffic":
  1686. inlineKeyboard := tu.InlineKeyboard(
  1687. tu.InlineKeyboardRow(
  1688. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_traffic_exp")),
  1689. ),
  1690. tu.InlineKeyboardRow(
  1691. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 0")),
  1692. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("add_client_limit_traffic_in 0")),
  1693. ),
  1694. tu.InlineKeyboardRow(
  1695. tu.InlineKeyboardButton("1 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 1")),
  1696. tu.InlineKeyboardButton("5 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 5")),
  1697. tu.InlineKeyboardButton("10 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 10")),
  1698. ),
  1699. tu.InlineKeyboardRow(
  1700. tu.InlineKeyboardButton("20 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 20")),
  1701. tu.InlineKeyboardButton("30 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 30")),
  1702. tu.InlineKeyboardButton("40 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 40")),
  1703. ),
  1704. tu.InlineKeyboardRow(
  1705. tu.InlineKeyboardButton("50 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 50")),
  1706. tu.InlineKeyboardButton("60 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 60")),
  1707. tu.InlineKeyboardButton("80 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 80")),
  1708. ),
  1709. tu.InlineKeyboardRow(
  1710. tu.InlineKeyboardButton("100 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 100")),
  1711. tu.InlineKeyboardButton("150 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 150")),
  1712. tu.InlineKeyboardButton("200 GB").WithCallbackData(t.encodeQuery("add_client_limit_traffic_c 200")),
  1713. ),
  1714. )
  1715. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1716. case "add_client_ch_default_exp":
  1717. inlineKeyboard := tu.InlineKeyboard(
  1718. tu.InlineKeyboardRow(
  1719. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_traffic_exp")),
  1720. ),
  1721. tu.InlineKeyboardRow(
  1722. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 0")),
  1723. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("add_client_reset_exp_in 0")),
  1724. ),
  1725. tu.InlineKeyboardRow(
  1726. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 7 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 7")),
  1727. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 10 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 10")),
  1728. ),
  1729. tu.InlineKeyboardRow(
  1730. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 14 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 14")),
  1731. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 20 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 20")),
  1732. ),
  1733. tu.InlineKeyboardRow(
  1734. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 1 "+t.I18nBot("tgbot.month")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 30")),
  1735. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 3 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 90")),
  1736. ),
  1737. tu.InlineKeyboardRow(
  1738. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 6 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 180")),
  1739. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 12 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("add_client_reset_exp_c 365")),
  1740. ),
  1741. )
  1742. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1743. case "add_client_ch_default_ip_limit":
  1744. inlineKeyboard := tu.InlineKeyboard(
  1745. tu.InlineKeyboardRow(
  1746. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("add_client_default_ip_limit")),
  1747. ),
  1748. tu.InlineKeyboardRow(
  1749. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("add_client_ip_limit_c 0")),
  1750. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("add_client_ip_limit_in 0")),
  1751. ),
  1752. tu.InlineKeyboardRow(
  1753. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 1")),
  1754. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 2")),
  1755. ),
  1756. tu.InlineKeyboardRow(
  1757. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 3")),
  1758. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 4")),
  1759. ),
  1760. tu.InlineKeyboardRow(
  1761. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 5")),
  1762. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 6")),
  1763. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 7")),
  1764. ),
  1765. tu.InlineKeyboardRow(
  1766. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 8")),
  1767. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 9")),
  1768. tu.InlineKeyboardButton("10").WithCallbackData(t.encodeQuery("add_client_ip_limit_c 10")),
  1769. ),
  1770. )
  1771. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  1772. case "add_client_default_info":
  1773. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1774. t.SendMsgToTgbotDeleteAfter(chatId, t.I18nBot("tgbot.messages.using_default_value"), 3, tu.ReplyKeyboardRemove())
  1775. delete(userStates, chatId)
  1776. inbound, _ := t.inboundService.GetInbound(receiver_inbound_ID)
  1777. message_text, _ := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  1778. t.addClient(chatId, message_text)
  1779. case "add_client_cancel":
  1780. delete(userStates, chatId)
  1781. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1782. t.SendMsgToTgbotDeleteAfter(chatId, t.I18nBot("tgbot.messages.cancel"), 3, tu.ReplyKeyboardRemove())
  1783. case "add_client_default_traffic_exp":
  1784. messageId := callbackQuery.Message.GetMessageID()
  1785. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  1786. if err != nil {
  1787. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1788. return
  1789. }
  1790. message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  1791. if err != nil {
  1792. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1793. return
  1794. }
  1795. t.addClient(chatId, message_text, messageId)
  1796. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+client_Email))
  1797. case "add_client_default_ip_limit":
  1798. messageId := callbackQuery.Message.GetMessageID()
  1799. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  1800. if err != nil {
  1801. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1802. return
  1803. }
  1804. message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol)
  1805. if err != nil {
  1806. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  1807. return
  1808. }
  1809. t.addClient(chatId, message_text, messageId)
  1810. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+client_Email))
  1811. case "add_client_submit_disable":
  1812. client_Enable = false
  1813. _, err := t.SubmitAddClient()
  1814. if err != nil {
  1815. errorMessage := fmt.Sprintf("%v", err)
  1816. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.error_add_client", "error=="+errorMessage), tu.ReplyKeyboardRemove())
  1817. } else {
  1818. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1819. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.successfulOperation"), tu.ReplyKeyboardRemove())
  1820. }
  1821. case "add_client_submit_enable":
  1822. client_Enable = true
  1823. _, err := t.SubmitAddClient()
  1824. if err != nil {
  1825. errorMessage := fmt.Sprintf("%v", err)
  1826. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.error_add_client", "error=="+errorMessage), tu.ReplyKeyboardRemove())
  1827. } else {
  1828. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1829. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.successfulOperation"), tu.ReplyKeyboardRemove())
  1830. }
  1831. case "reset_all_traffics_cancel":
  1832. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1833. t.SendMsgToTgbotDeleteAfter(chatId, t.I18nBot("tgbot.messages.cancel"), 1, tu.ReplyKeyboardRemove())
  1834. case "reset_all_traffics":
  1835. inlineKeyboard := tu.InlineKeyboard(
  1836. tu.InlineKeyboardRow(
  1837. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("reset_all_traffics_cancel")),
  1838. ),
  1839. tu.InlineKeyboardRow(
  1840. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_all_traffics_c")),
  1841. ),
  1842. )
  1843. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.AreYouSure"), inlineKeyboard)
  1844. case "reset_all_traffics_c":
  1845. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1846. emails, err := t.inboundService.getAllEmails()
  1847. if err != nil {
  1848. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
  1849. return
  1850. }
  1851. for _, email := range emails {
  1852. err := t.inboundService.ResetClientTrafficByEmail(email)
  1853. if err == nil {
  1854. msg := t.I18nBot("tgbot.messages.SuccessResetTraffic", "ClientEmail=="+email)
  1855. t.SendMsgToTgbot(chatId, msg, tu.ReplyKeyboardRemove())
  1856. } else {
  1857. msg := t.I18nBot("tgbot.messages.FailedResetTraffic", "ClientEmail=="+email, "ErrorMessage=="+err.Error())
  1858. t.SendMsgToTgbot(chatId, msg, tu.ReplyKeyboardRemove())
  1859. }
  1860. }
  1861. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.messages.FinishProcess"), tu.ReplyKeyboardRemove())
  1862. case "get_sorted_traffic_usage_report":
  1863. t.deleteMessageTgBot(chatId, callbackQuery.Message.GetMessageID())
  1864. emails, err := t.inboundService.getAllEmails()
  1865. if err != nil {
  1866. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
  1867. return
  1868. }
  1869. valid_emails, extra_emails, err := t.inboundService.FilterAndSortClientEmails(emails)
  1870. if err != nil {
  1871. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove())
  1872. return
  1873. }
  1874. for _, valid_emails := range valid_emails {
  1875. traffic, err := t.inboundService.GetClientTrafficByEmail(valid_emails)
  1876. if err != nil {
  1877. logger.Warning(err)
  1878. msg := t.I18nBot("tgbot.wentWrong")
  1879. t.SendMsgToTgbot(chatId, msg)
  1880. continue
  1881. }
  1882. if traffic == nil {
  1883. msg := t.I18nBot("tgbot.noResult")
  1884. t.SendMsgToTgbot(chatId, msg)
  1885. continue
  1886. }
  1887. output := t.clientInfoMsg(traffic, false, false, false, false, true, false)
  1888. t.SendMsgToTgbot(chatId, output, tu.ReplyKeyboardRemove())
  1889. }
  1890. for _, extra_emails := range extra_emails {
  1891. msg := fmt.Sprintf("📧 %s\n%s", extra_emails, t.I18nBot("tgbot.noResult"))
  1892. t.SendMsgToTgbot(chatId, msg, tu.ReplyKeyboardRemove())
  1893. }
  1894. default:
  1895. if after, ok := strings.CutPrefix(callbackQuery.Data, "client_sub_links "); ok {
  1896. email := after
  1897. t.sendClientSubLinks(chatId, email)
  1898. return
  1899. }
  1900. if after, ok := strings.CutPrefix(callbackQuery.Data, "client_individual_links "); ok {
  1901. email := after
  1902. t.sendClientIndividualLinks(chatId, email)
  1903. return
  1904. }
  1905. if after, ok := strings.CutPrefix(callbackQuery.Data, "client_qr_links "); ok {
  1906. email := after
  1907. t.sendClientQRLinks(chatId, email)
  1908. return
  1909. }
  1910. }
  1911. }
  1912. // BuildInboundClientDataMessage builds a message with client data for the given inbound and protocol.
  1913. func (t *Tgbot) BuildInboundClientDataMessage(inbound_remark string, protocol model.Protocol) (string, error) {
  1914. var message string
  1915. currentTime := time.Now()
  1916. timestampMillis := currentTime.UnixNano() / int64(time.Millisecond)
  1917. expiryTime := ""
  1918. diff := client_ExpiryTime/1000 - timestampMillis
  1919. if client_ExpiryTime == 0 {
  1920. expiryTime = t.I18nBot("tgbot.unlimited")
  1921. } else if diff > 172800 {
  1922. expiryTime = time.Unix((client_ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  1923. } else if client_ExpiryTime < 0 {
  1924. expiryTime = fmt.Sprintf("%d %s", client_ExpiryTime/-86400000, t.I18nBot("tgbot.days"))
  1925. } else {
  1926. expiryTime = fmt.Sprintf("%d %s", diff/3600, t.I18nBot("tgbot.hours"))
  1927. }
  1928. traffic_value := ""
  1929. if client_TotalGB == 0 {
  1930. traffic_value = "♾️ Unlimited(Reset)"
  1931. } else {
  1932. traffic_value = common.FormatTraffic(client_TotalGB)
  1933. }
  1934. ip_limit := ""
  1935. if client_LimitIP == 0 {
  1936. ip_limit = "♾️ Unlimited(Reset)"
  1937. } else {
  1938. ip_limit = fmt.Sprint(client_LimitIP)
  1939. }
  1940. switch protocol {
  1941. case model.VMESS, model.VLESS:
  1942. message = t.I18nBot("tgbot.messages.inbound_client_data_id", "InboundRemark=="+inbound_remark, "ClientId=="+client_Id, "ClientEmail=="+client_Email, "ClientTraffic=="+traffic_value, "ClientExp=="+expiryTime, "IpLimit=="+ip_limit, "ClientComment=="+client_Comment)
  1943. case model.Trojan:
  1944. message = t.I18nBot("tgbot.messages.inbound_client_data_pass", "InboundRemark=="+inbound_remark, "ClientPass=="+client_TrPassword, "ClientEmail=="+client_Email, "ClientTraffic=="+traffic_value, "ClientExp=="+expiryTime, "IpLimit=="+ip_limit, "ClientComment=="+client_Comment)
  1945. case model.Shadowsocks:
  1946. message = t.I18nBot("tgbot.messages.inbound_client_data_pass", "InboundRemark=="+inbound_remark, "ClientPass=="+client_ShPassword, "ClientEmail=="+client_Email, "ClientTraffic=="+traffic_value, "ClientExp=="+expiryTime, "IpLimit=="+ip_limit, "ClientComment=="+client_Comment)
  1947. default:
  1948. return "", errors.New("unknown protocol")
  1949. }
  1950. return message, nil
  1951. }
  1952. // BuildJSONForProtocol builds a JSON string for the given protocol with client data.
  1953. func (t *Tgbot) BuildJSONForProtocol(protocol model.Protocol) (string, error) {
  1954. var jsonString string
  1955. switch protocol {
  1956. case model.VMESS:
  1957. jsonString = fmt.Sprintf(`{
  1958. "clients": [{
  1959. "id": "%s",
  1960. "security": "%s",
  1961. "email": "%s",
  1962. "limitIp": %d,
  1963. "totalGB": %d,
  1964. "expiryTime": %d,
  1965. "enable": %t,
  1966. "tgId": "%s",
  1967. "subId": "%s",
  1968. "comment": "%s",
  1969. "reset": %d
  1970. }]
  1971. }`, client_Id, client_Security, client_Email, client_LimitIP, client_TotalGB, client_ExpiryTime, client_Enable, client_TgID, client_SubID, client_Comment, client_Reset)
  1972. case model.VLESS:
  1973. jsonString = fmt.Sprintf(`{
  1974. "clients": [{
  1975. "id": "%s",
  1976. "flow": "%s",
  1977. "email": "%s",
  1978. "limitIp": %d,
  1979. "totalGB": %d,
  1980. "expiryTime": %d,
  1981. "enable": %t,
  1982. "tgId": "%s",
  1983. "subId": "%s",
  1984. "comment": "%s",
  1985. "reset": %d
  1986. }]
  1987. }`, client_Id, client_Flow, client_Email, client_LimitIP, client_TotalGB, client_ExpiryTime, client_Enable, client_TgID, client_SubID, client_Comment, client_Reset)
  1988. case model.Trojan:
  1989. jsonString = fmt.Sprintf(`{
  1990. "clients": [{
  1991. "password": "%s",
  1992. "email": "%s",
  1993. "limitIp": %d,
  1994. "totalGB": %d,
  1995. "expiryTime": %d,
  1996. "enable": %t,
  1997. "tgId": "%s",
  1998. "subId": "%s",
  1999. "comment": "%s",
  2000. "reset": %d
  2001. }]
  2002. }`, client_TrPassword, client_Email, client_LimitIP, client_TotalGB, client_ExpiryTime, client_Enable, client_TgID, client_SubID, client_Comment, client_Reset)
  2003. case model.Shadowsocks:
  2004. jsonString = fmt.Sprintf(`{
  2005. "clients": [{
  2006. "method": "%s",
  2007. "password": "%s",
  2008. "email": "%s",
  2009. "limitIp": %d,
  2010. "totalGB": %d,
  2011. "expiryTime": %d,
  2012. "enable": %t,
  2013. "tgId": "%s",
  2014. "subId": "%s",
  2015. "comment": "%s",
  2016. "reset": %d
  2017. }]
  2018. }`, client_Method, client_ShPassword, client_Email, client_LimitIP, client_TotalGB, client_ExpiryTime, client_Enable, client_TgID, client_SubID, client_Comment, client_Reset)
  2019. default:
  2020. return "", errors.New("unknown protocol")
  2021. }
  2022. return jsonString, nil
  2023. }
  2024. // SubmitAddClient submits the client addition request to the inbound service.
  2025. func (t *Tgbot) SubmitAddClient() (bool, error) {
  2026. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  2027. if err != nil {
  2028. logger.Warning("getIboundClients run failed:", err)
  2029. return false, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2030. }
  2031. jsonString, err := t.BuildJSONForProtocol(inbound.Protocol)
  2032. if err != nil {
  2033. logger.Warning("BuildJSONForProtocol run failed:", err)
  2034. return false, errors.New("failed to build JSON for protocol")
  2035. }
  2036. newInbound := &model.Inbound{
  2037. Id: receiver_inbound_ID,
  2038. Settings: jsonString,
  2039. }
  2040. return t.inboundService.AddInboundClient(newInbound)
  2041. }
  2042. // checkAdmin checks if the given Telegram ID is an admin.
  2043. func checkAdmin(tgId int64) bool {
  2044. for _, adminId := range adminIds {
  2045. if adminId == tgId {
  2046. return true
  2047. }
  2048. }
  2049. return false
  2050. }
  2051. // SendAnswer sends a response message with an inline keyboard to the specified chat.
  2052. func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
  2053. numericKeyboard := tu.InlineKeyboard(
  2054. tu.InlineKeyboardRow(
  2055. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.SortedTrafficUsageReport")).WithCallbackData(t.encodeQuery("get_sorted_traffic_usage_report")),
  2056. ),
  2057. tu.InlineKeyboardRow(
  2058. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.serverUsage")).WithCallbackData(t.encodeQuery("get_usage")),
  2059. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ResetAllTraffics")).WithCallbackData(t.encodeQuery("reset_all_traffics")),
  2060. ),
  2061. tu.InlineKeyboardRow(
  2062. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.dbBackup")).WithCallbackData(t.encodeQuery("get_backup")),
  2063. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getBanLogs")).WithCallbackData(t.encodeQuery("get_banlogs")),
  2064. ),
  2065. tu.InlineKeyboardRow(
  2066. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getInbounds")).WithCallbackData(t.encodeQuery("inbounds")),
  2067. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.depleteSoon")).WithCallbackData(t.encodeQuery("deplete_soon")),
  2068. ),
  2069. tu.InlineKeyboardRow(
  2070. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("commands")),
  2071. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.onlines")).WithCallbackData(t.encodeQuery("onlines")),
  2072. ),
  2073. tu.InlineKeyboardRow(
  2074. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")),
  2075. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.addClient")).WithCallbackData(t.encodeQuery("add_client")),
  2076. ),
  2077. tu.InlineKeyboardRow(
  2078. tu.InlineKeyboardButton(t.I18nBot("pages.settings.subSettings")).WithCallbackData(t.encodeQuery("admin_client_sub_links")),
  2079. tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("admin_client_individual_links")),
  2080. tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("admin_client_qr_links")),
  2081. ),
  2082. // TODOOOOOOOOOOOOOO: Add restart button here.
  2083. )
  2084. numericKeyboardClient := tu.InlineKeyboard(
  2085. tu.InlineKeyboardRow(
  2086. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clientUsage")).WithCallbackData(t.encodeQuery("client_traffic")),
  2087. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("client_commands")),
  2088. ),
  2089. tu.InlineKeyboardRow(
  2090. tu.InlineKeyboardButton(t.I18nBot("pages.settings.subSettings")).WithCallbackData(t.encodeQuery("client_sub_links")),
  2091. tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("client_individual_links")),
  2092. ),
  2093. tu.InlineKeyboardRow(
  2094. tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("client_qr_links")),
  2095. ),
  2096. )
  2097. var ReplyMarkup telego.ReplyMarkup
  2098. if isAdmin {
  2099. ReplyMarkup = numericKeyboard
  2100. } else {
  2101. ReplyMarkup = numericKeyboardClient
  2102. }
  2103. t.SendMsgToTgbot(chatId, msg, ReplyMarkup)
  2104. }
  2105. // SendMsgToTgbot sends a message to the Telegram bot with optional reply markup.
  2106. func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.ReplyMarkup) {
  2107. if !isRunning {
  2108. return
  2109. }
  2110. if msg == "" {
  2111. logger.Info("[tgbot] message is empty!")
  2112. return
  2113. }
  2114. var allMessages []string
  2115. limit := 2000
  2116. // paging message if it is big
  2117. if len(msg) > limit {
  2118. messages := strings.Split(msg, "\r\n\r\n")
  2119. lastIndex := -1
  2120. for _, message := range messages {
  2121. if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) {
  2122. allMessages = append(allMessages, message)
  2123. lastIndex++
  2124. } else {
  2125. allMessages[lastIndex] += "\r\n\r\n" + message
  2126. }
  2127. }
  2128. if strings.TrimSpace(allMessages[len(allMessages)-1]) == "" {
  2129. allMessages = allMessages[:len(allMessages)-1]
  2130. }
  2131. } else {
  2132. allMessages = append(allMessages, msg)
  2133. }
  2134. for n, message := range allMessages {
  2135. params := telego.SendMessageParams{
  2136. ChatID: tu.ID(chatId),
  2137. Text: message,
  2138. ParseMode: "HTML",
  2139. }
  2140. // only add replyMarkup to last message
  2141. if len(replyMarkup) > 0 && n == (len(allMessages)-1) {
  2142. params.ReplyMarkup = replyMarkup[0]
  2143. }
  2144. // Retry logic with exponential backoff for connection errors
  2145. maxRetries := 3
  2146. for attempt := range maxRetries {
  2147. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  2148. _, err := bot.SendMessage(ctx, &params)
  2149. cancel()
  2150. if err == nil {
  2151. break // Success
  2152. }
  2153. // Check if error is a connection error
  2154. errStr := err.Error()
  2155. isConnectionError := strings.Contains(errStr, "connection") ||
  2156. strings.Contains(errStr, "timeout") ||
  2157. strings.Contains(errStr, "closed")
  2158. if isConnectionError && attempt < maxRetries-1 {
  2159. // Exponential backoff: 1s, 2s, 4s
  2160. backoff := time.Duration(1<<uint(attempt)) * time.Second
  2161. logger.Warningf("Connection error sending telegram message (attempt %d/%d), retrying in %v: %v",
  2162. attempt+1, maxRetries, backoff, err)
  2163. time.Sleep(backoff)
  2164. } else {
  2165. logger.Warning("Error sending telegram message:", err)
  2166. break
  2167. }
  2168. }
  2169. // Reduced delay to improve performance (only needed for rate limiting)
  2170. if n < len(allMessages)-1 { // Only delay between messages, not after the last one
  2171. time.Sleep(100 * time.Millisecond)
  2172. }
  2173. }
  2174. }
  2175. // buildSubscriptionURLs builds the HTML sub page URL and JSON subscription URL for a client email
  2176. func (t *Tgbot) buildSubscriptionURLs(email string) (string, string, error) {
  2177. // Resolve subId from client email
  2178. traffic, client, err := t.inboundService.GetClientByEmail(email)
  2179. _ = traffic
  2180. if err != nil || client == nil {
  2181. return "", "", errors.New("client not found")
  2182. }
  2183. // Gather settings to construct absolute URLs
  2184. subURI, _ := t.settingService.GetSubURI()
  2185. subJsonURI, _ := t.settingService.GetSubJsonURI()
  2186. subDomain, _ := t.settingService.GetSubDomain()
  2187. subPort, _ := t.settingService.GetSubPort()
  2188. subPath, _ := t.settingService.GetSubPath()
  2189. subJsonPath, _ := t.settingService.GetSubJsonPath()
  2190. subJsonEnable, _ := t.settingService.GetSubJsonEnable()
  2191. subKeyFile, _ := t.settingService.GetSubKeyFile()
  2192. subCertFile, _ := t.settingService.GetSubCertFile()
  2193. tls := (subKeyFile != "" && subCertFile != "")
  2194. scheme := "http"
  2195. if tls {
  2196. scheme = "https"
  2197. }
  2198. // Fallbacks
  2199. if subDomain == "" {
  2200. // try panel domain, otherwise OS hostname
  2201. if d, err := t.settingService.GetWebDomain(); err == nil && d != "" {
  2202. subDomain = d
  2203. } else if hostname != "" {
  2204. subDomain = hostname
  2205. } else {
  2206. subDomain = "localhost"
  2207. }
  2208. }
  2209. host := subDomain
  2210. if (subPort == 443 && tls) || (subPort == 80 && !tls) {
  2211. // standard ports: no port in host
  2212. } else {
  2213. host = fmt.Sprintf("%s:%d", subDomain, subPort)
  2214. }
  2215. // Ensure paths
  2216. if !strings.HasPrefix(subPath, "/") {
  2217. subPath = "/" + subPath
  2218. }
  2219. if !strings.HasSuffix(subPath, "/") {
  2220. subPath = subPath + "/"
  2221. }
  2222. if !strings.HasPrefix(subJsonPath, "/") {
  2223. subJsonPath = "/" + subJsonPath
  2224. }
  2225. if !strings.HasSuffix(subJsonPath, "/") {
  2226. subJsonPath = subJsonPath + "/"
  2227. }
  2228. var subURL string
  2229. var subJsonURL string
  2230. // If pre-configured URIs are available, use them directly
  2231. if subURI != "" {
  2232. if !strings.HasSuffix(subURI, "/") {
  2233. subURI = subURI + "/"
  2234. }
  2235. subURL = fmt.Sprintf("%s%s", subURI, client.SubID)
  2236. } else {
  2237. subURL = fmt.Sprintf("%s://%s%s%s", scheme, host, subPath, client.SubID)
  2238. }
  2239. if subJsonURI != "" {
  2240. if !strings.HasSuffix(subJsonURI, "/") {
  2241. subJsonURI = subJsonURI + "/"
  2242. }
  2243. subJsonURL = fmt.Sprintf("%s%s", subJsonURI, client.SubID)
  2244. } else {
  2245. subJsonURL = fmt.Sprintf("%s://%s%s%s", scheme, host, subJsonPath, client.SubID)
  2246. }
  2247. if !subJsonEnable {
  2248. subJsonURL = ""
  2249. }
  2250. return subURL, subJsonURL, nil
  2251. }
  2252. // sendClientSubLinks sends the subscription links for the client to the chat.
  2253. func (t *Tgbot) sendClientSubLinks(chatId int64, email string) {
  2254. subURL, subJsonURL, err := t.buildSubscriptionURLs(email)
  2255. if err != nil {
  2256. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2257. return
  2258. }
  2259. msg := "Subscription URL:\r\n<code>" + subURL + "</code>"
  2260. if subJsonURL != "" {
  2261. msg += "\r\n\r\nJSON URL:\r\n<code>" + subJsonURL + "</code>"
  2262. }
  2263. inlineKeyboard := tu.InlineKeyboard(
  2264. tu.InlineKeyboardRow(
  2265. tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("client_individual_links "+email)),
  2266. ),
  2267. tu.InlineKeyboardRow(
  2268. tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("client_qr_links "+email)),
  2269. ),
  2270. )
  2271. t.SendMsgToTgbot(chatId, msg, inlineKeyboard)
  2272. }
  2273. // sendClientIndividualLinks fetches the subscription content (individual links) and sends it to the user
  2274. func (t *Tgbot) sendClientIndividualLinks(chatId int64, email string) {
  2275. // Build the HTML sub page URL; we'll call it with header Accept to get raw content
  2276. subURL, _, err := t.buildSubscriptionURLs(email)
  2277. if err != nil {
  2278. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2279. return
  2280. }
  2281. // Try to fetch raw subscription links. Prefer plain text response.
  2282. req, err := http.NewRequest("GET", subURL, nil)
  2283. if err != nil {
  2284. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2285. return
  2286. }
  2287. // Force plain text to avoid HTML page; controller respects Accept header
  2288. req.Header.Set("Accept", "text/plain, */*;q=0.1")
  2289. // Use optimized client with connection pooling
  2290. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  2291. defer cancel()
  2292. req = req.WithContext(ctx)
  2293. resp, err := optimizedHTTPClient.Do(req)
  2294. if err != nil {
  2295. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2296. return
  2297. }
  2298. defer resp.Body.Close()
  2299. bodyBytes, err := io.ReadAll(resp.Body)
  2300. if err != nil {
  2301. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2302. return
  2303. }
  2304. // If service is configured to encode (Base64), decode it
  2305. encoded, _ := t.settingService.GetSubEncrypt()
  2306. var content string
  2307. if encoded {
  2308. decoded, err := base64.StdEncoding.DecodeString(string(bodyBytes))
  2309. if err != nil {
  2310. // fallback to raw text
  2311. content = string(bodyBytes)
  2312. } else {
  2313. content = string(decoded)
  2314. }
  2315. } else {
  2316. content = string(bodyBytes)
  2317. }
  2318. // Normalize line endings and trim
  2319. lines := strings.Split(strings.ReplaceAll(content, "\r\n", "\n"), "\n")
  2320. var cleaned []string
  2321. for _, l := range lines {
  2322. l = strings.TrimSpace(l)
  2323. if l != "" {
  2324. cleaned = append(cleaned, l)
  2325. }
  2326. }
  2327. if len(cleaned) == 0 {
  2328. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.noResult"))
  2329. return
  2330. }
  2331. // Send in chunks to respect message length; use monospace formatting
  2332. const maxPerMessage = 50
  2333. for i := 0; i < len(cleaned); i += maxPerMessage {
  2334. j := i + maxPerMessage
  2335. if j > len(cleaned) {
  2336. j = len(cleaned)
  2337. }
  2338. chunk := cleaned[i:j]
  2339. msg := t.I18nBot("subscription.individualLinks") + ":\r\n"
  2340. for _, link := range chunk {
  2341. // wrap each link in <code>
  2342. msg += "<code>" + link + "</code>\r\n"
  2343. }
  2344. t.SendMsgToTgbot(chatId, msg)
  2345. }
  2346. }
  2347. // sendClientQRLinks generates QR images for subscription URL, JSON URL, and a few individual links, then sends them
  2348. func (t *Tgbot) sendClientQRLinks(chatId int64, email string) {
  2349. subURL, subJsonURL, err := t.buildSubscriptionURLs(email)
  2350. if err != nil {
  2351. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2352. return
  2353. }
  2354. // Helper to create QR PNG bytes from content
  2355. createQR := func(content string, size int) ([]byte, error) {
  2356. if size <= 0 {
  2357. size = 256
  2358. }
  2359. return qrcode.Encode(content, qrcode.Medium, size)
  2360. }
  2361. // Inform user
  2362. t.SendMsgToTgbot(chatId, "QRCode"+":")
  2363. // Send sub URL QR (filename: sub.png)
  2364. if png, err := createQR(subURL, 320); err == nil {
  2365. document := tu.Document(
  2366. tu.ID(chatId),
  2367. tu.FileFromBytes(png, "sub.png"),
  2368. )
  2369. _, _ = bot.SendDocument(context.Background(), document)
  2370. } else {
  2371. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2372. }
  2373. // Send JSON URL QR (filename: subjson.png) when available
  2374. if subJsonURL != "" {
  2375. if png, err := createQR(subJsonURL, 320); err == nil {
  2376. document := tu.Document(
  2377. tu.ID(chatId),
  2378. tu.FileFromBytes(png, "subjson.png"),
  2379. )
  2380. _, _ = bot.SendDocument(context.Background(), document)
  2381. } else {
  2382. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation")+"\r\n"+err.Error())
  2383. }
  2384. }
  2385. // Also generate a few individual links' QRs (first up to 5)
  2386. subPageURL := subURL
  2387. req, err := http.NewRequest("GET", subPageURL, nil)
  2388. if err == nil {
  2389. req.Header.Set("Accept", "text/plain, */*;q=0.1")
  2390. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  2391. defer cancel()
  2392. req = req.WithContext(ctx)
  2393. if resp, err := optimizedHTTPClient.Do(req); err == nil {
  2394. body, _ := io.ReadAll(resp.Body)
  2395. _ = resp.Body.Close()
  2396. encoded, _ := t.settingService.GetSubEncrypt()
  2397. var content string
  2398. if encoded {
  2399. if dec, err := base64.StdEncoding.DecodeString(string(body)); err == nil {
  2400. content = string(dec)
  2401. } else {
  2402. content = string(body)
  2403. }
  2404. } else {
  2405. content = string(body)
  2406. }
  2407. lines := strings.Split(strings.ReplaceAll(content, "\r\n", "\n"), "\n")
  2408. var cleaned []string
  2409. for _, l := range lines {
  2410. l = strings.TrimSpace(l)
  2411. if l != "" {
  2412. cleaned = append(cleaned, l)
  2413. }
  2414. }
  2415. if len(cleaned) > 0 {
  2416. max := min(len(cleaned), 5)
  2417. for i := range max {
  2418. if png, err := createQR(cleaned[i], 320); err == nil {
  2419. // Use the email as filename for individual link QR
  2420. filename := email + ".png"
  2421. document := tu.Document(
  2422. tu.ID(chatId),
  2423. tu.FileFromBytes(png, filename),
  2424. )
  2425. _, _ = bot.SendDocument(context.Background(), document)
  2426. // Reduced delay for better performance
  2427. if i < max-1 { // Only delay between documents, not after the last one
  2428. time.Sleep(50 * time.Millisecond)
  2429. }
  2430. }
  2431. }
  2432. }
  2433. }
  2434. }
  2435. }
  2436. // SendMsgToTgbotAdmins sends a message to all admin Telegram chats.
  2437. func (t *Tgbot) SendMsgToTgbotAdmins(msg string, replyMarkup ...telego.ReplyMarkup) {
  2438. if len(replyMarkup) > 0 {
  2439. for _, adminId := range adminIds {
  2440. t.SendMsgToTgbot(adminId, msg, replyMarkup[0])
  2441. }
  2442. } else {
  2443. for _, adminId := range adminIds {
  2444. t.SendMsgToTgbot(adminId, msg)
  2445. }
  2446. }
  2447. }
  2448. // SendReport sends a periodic report to admin chats.
  2449. func (t *Tgbot) SendReport() {
  2450. runTime, err := t.settingService.GetTgbotRuntime()
  2451. if err == nil && len(runTime) > 0 {
  2452. msg := ""
  2453. msg += t.I18nBot("tgbot.messages.report", "RunTime=="+runTime)
  2454. msg += t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  2455. t.SendMsgToTgbotAdmins(msg)
  2456. }
  2457. info := t.sendServerUsage()
  2458. t.SendMsgToTgbotAdmins(info)
  2459. t.sendExhaustedToAdmins()
  2460. t.notifyExhausted()
  2461. backupEnable, err := t.settingService.GetTgBotBackup()
  2462. if err == nil && backupEnable {
  2463. t.SendBackupToAdmins()
  2464. }
  2465. }
  2466. // SendBackupToAdmins sends a database backup to admin chats.
  2467. func (t *Tgbot) SendBackupToAdmins() {
  2468. if !t.IsRunning() {
  2469. return
  2470. }
  2471. for i, adminId := range adminIds {
  2472. t.sendBackup(int64(adminId))
  2473. // Add delay between sends to avoid Telegram rate limits
  2474. if i < len(adminIds)-1 {
  2475. time.Sleep(1 * time.Second)
  2476. }
  2477. }
  2478. }
  2479. // sendExhaustedToAdmins sends notifications about exhausted clients to admins.
  2480. func (t *Tgbot) sendExhaustedToAdmins() {
  2481. if !t.IsRunning() {
  2482. return
  2483. }
  2484. for _, adminId := range adminIds {
  2485. t.getExhausted(int64(adminId))
  2486. }
  2487. }
  2488. // getServerUsage retrieves and formats server usage information.
  2489. func (t *Tgbot) getServerUsage(chatId int64, messageID ...int) string {
  2490. info := t.prepareServerUsageInfo()
  2491. keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
  2492. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("usage_refresh"))))
  2493. if len(messageID) > 0 {
  2494. t.editMessageTgBot(chatId, messageID[0], info, keyboard)
  2495. } else {
  2496. t.SendMsgToTgbot(chatId, info, keyboard)
  2497. }
  2498. return info
  2499. }
  2500. // Send server usage without an inline keyboard
  2501. func (t *Tgbot) sendServerUsage() string {
  2502. info := t.prepareServerUsageInfo()
  2503. return info
  2504. }
  2505. // prepareServerUsageInfo prepares the server usage information string.
  2506. func (t *Tgbot) prepareServerUsageInfo() string {
  2507. // Check if we have cached data first
  2508. if cachedStats, found := t.getCachedServerStats(); found {
  2509. return cachedStats
  2510. }
  2511. info, ipv4, ipv6 := "", "", ""
  2512. // get latest status of server with caching
  2513. if cachedStatus, found := t.getCachedStatus(); found {
  2514. t.lastStatus = cachedStatus
  2515. } else {
  2516. t.lastStatus = t.serverService.GetStatus(t.lastStatus)
  2517. t.setCachedStatus(t.lastStatus)
  2518. }
  2519. onlines := p.GetOnlineClients()
  2520. info += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  2521. info += t.I18nBot("tgbot.messages.version", "Version=="+config.GetVersion())
  2522. info += t.I18nBot("tgbot.messages.xrayVersion", "XrayVersion=="+fmt.Sprint(t.lastStatus.Xray.Version))
  2523. // get ip address
  2524. netInterfaces, err := net.Interfaces()
  2525. if err != nil {
  2526. logger.Error("net.Interfaces failed, err: ", err.Error())
  2527. info += t.I18nBot("tgbot.messages.ip", "IP=="+t.I18nBot("tgbot.unknown"))
  2528. info += "\r\n"
  2529. } else {
  2530. for i := range netInterfaces {
  2531. if (netInterfaces[i].Flags & net.FlagUp) != 0 {
  2532. addrs, _ := netInterfaces[i].Addrs()
  2533. for _, address := range addrs {
  2534. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  2535. if ipnet.IP.To4() != nil {
  2536. ipv4 += ipnet.IP.String() + " "
  2537. } else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() {
  2538. ipv6 += ipnet.IP.String() + " "
  2539. }
  2540. }
  2541. }
  2542. }
  2543. }
  2544. info += t.I18nBot("tgbot.messages.ipv4", "IPv4=="+ipv4)
  2545. info += t.I18nBot("tgbot.messages.ipv6", "IPv6=="+ipv6)
  2546. }
  2547. info += t.I18nBot("tgbot.messages.serverUpTime", "UpTime=="+strconv.FormatUint(t.lastStatus.Uptime/86400, 10), "Unit=="+t.I18nBot("tgbot.days"))
  2548. info += t.I18nBot("tgbot.messages.serverLoad", "Load1=="+strconv.FormatFloat(t.lastStatus.Loads[0], 'f', 2, 64), "Load2=="+strconv.FormatFloat(t.lastStatus.Loads[1], 'f', 2, 64), "Load3=="+strconv.FormatFloat(t.lastStatus.Loads[2], 'f', 2, 64))
  2549. info += t.I18nBot("tgbot.messages.serverMemory", "Current=="+common.FormatTraffic(int64(t.lastStatus.Mem.Current)), "Total=="+common.FormatTraffic(int64(t.lastStatus.Mem.Total)))
  2550. info += t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(len(onlines)))
  2551. info += t.I18nBot("tgbot.messages.tcpCount", "Count=="+strconv.Itoa(t.lastStatus.TcpCount))
  2552. info += t.I18nBot("tgbot.messages.udpCount", "Count=="+strconv.Itoa(t.lastStatus.UdpCount))
  2553. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent+t.lastStatus.NetTraffic.Recv)), "Upload=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent)), "Download=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Recv)))
  2554. info += t.I18nBot("tgbot.messages.xrayStatus", "State=="+fmt.Sprint(t.lastStatus.Xray.State))
  2555. // Cache the complete server stats
  2556. t.setCachedServerStats(info)
  2557. return info
  2558. }
  2559. // UserLoginNotify sends a notification about user login attempts to admins.
  2560. func (t *Tgbot) UserLoginNotify(username string, password string, ip string, time string, status LoginStatus) {
  2561. if !t.IsRunning() {
  2562. return
  2563. }
  2564. if username == "" || ip == "" || time == "" {
  2565. logger.Warning("UserLoginNotify failed, invalid info!")
  2566. return
  2567. }
  2568. loginNotifyEnabled, err := t.settingService.GetTgBotLoginNotify()
  2569. if err != nil || !loginNotifyEnabled {
  2570. return
  2571. }
  2572. msg := ""
  2573. switch status {
  2574. case LoginSuccess:
  2575. msg += t.I18nBot("tgbot.messages.loginSuccess")
  2576. msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  2577. case LoginFail:
  2578. msg += t.I18nBot("tgbot.messages.loginFailed")
  2579. msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  2580. msg += t.I18nBot("tgbot.messages.password", "Password=="+password)
  2581. }
  2582. msg += t.I18nBot("tgbot.messages.username", "Username=="+username)
  2583. msg += t.I18nBot("tgbot.messages.ip", "IP=="+ip)
  2584. msg += t.I18nBot("tgbot.messages.time", "Time=="+time)
  2585. t.SendMsgToTgbotAdmins(msg)
  2586. }
  2587. // getInboundUsages retrieves and formats inbound usage information.
  2588. func (t *Tgbot) getInboundUsages() string {
  2589. var info strings.Builder
  2590. // get traffic
  2591. inbounds, err := t.inboundService.GetAllInbounds()
  2592. if err != nil {
  2593. logger.Warning("GetAllInbounds run failed:", err)
  2594. info.WriteString(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2595. } else {
  2596. // NOTE:If there no any sessions here,need to notify here
  2597. // TODO:Sub-node push, automatic conversion format
  2598. for _, inbound := range inbounds {
  2599. info.WriteString(t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark))
  2600. info.WriteString(t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port)))
  2601. info.WriteString(t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down)))
  2602. if inbound.ExpiryTime == 0 {
  2603. info.WriteString(t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited")))
  2604. } else {
  2605. info.WriteString(t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05")))
  2606. }
  2607. info.WriteString("\r\n")
  2608. }
  2609. }
  2610. return info.String()
  2611. }
  2612. // getInbounds creates an inline keyboard with all inbounds.
  2613. func (t *Tgbot) getInbounds() (*telego.InlineKeyboardMarkup, error) {
  2614. inbounds, err := t.inboundService.GetAllInbounds()
  2615. if err != nil {
  2616. logger.Warning("GetAllInbounds run failed:", err)
  2617. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2618. }
  2619. if len(inbounds) == 0 {
  2620. logger.Warning("No inbounds found")
  2621. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2622. }
  2623. var buttons []telego.InlineKeyboardButton
  2624. for _, inbound := range inbounds {
  2625. status := "❌"
  2626. if inbound.Enable {
  2627. status = "✅"
  2628. }
  2629. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", "get_clients", inbound.Id))
  2630. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  2631. }
  2632. cols := 1
  2633. if len(buttons) >= 6 {
  2634. cols = 2
  2635. }
  2636. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  2637. return keyboard, nil
  2638. }
  2639. // getInboundsFor builds an inline keyboard of inbounds for a custom next action.
  2640. func (t *Tgbot) getInboundsFor(nextAction string) (*telego.InlineKeyboardMarkup, error) {
  2641. inbounds, err := t.inboundService.GetAllInbounds()
  2642. if err != nil {
  2643. logger.Warning("GetAllInbounds run failed:", err)
  2644. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2645. }
  2646. if len(inbounds) == 0 {
  2647. logger.Warning("No inbounds found")
  2648. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2649. }
  2650. var buttons []telego.InlineKeyboardButton
  2651. for _, inbound := range inbounds {
  2652. status := "❌"
  2653. if inbound.Enable {
  2654. status = "✅"
  2655. }
  2656. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", nextAction, inbound.Id))
  2657. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  2658. }
  2659. cols := 1
  2660. if len(buttons) >= 6 {
  2661. cols = 2
  2662. }
  2663. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  2664. return keyboard, nil
  2665. }
  2666. // getInboundClientsFor lists clients of an inbound with a specific action prefix to be appended with email
  2667. func (t *Tgbot) getInboundClientsFor(inboundID int, action string) (*telego.InlineKeyboardMarkup, error) {
  2668. inbound, err := t.inboundService.GetInbound(inboundID)
  2669. if err != nil {
  2670. logger.Warning("getInboundClientsFor run failed:", err)
  2671. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2672. }
  2673. clients, err := t.inboundService.GetClients(inbound)
  2674. var buttons []telego.InlineKeyboardButton
  2675. if err != nil {
  2676. logger.Warning("GetInboundClients run failed:", err)
  2677. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2678. } else {
  2679. if len(clients) > 0 {
  2680. for _, client := range clients {
  2681. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery(action+" "+client.Email)))
  2682. }
  2683. } else {
  2684. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  2685. }
  2686. }
  2687. cols := 0
  2688. if len(buttons) < 6 {
  2689. cols = 3
  2690. } else {
  2691. cols = 2
  2692. }
  2693. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  2694. return keyboard, nil
  2695. }
  2696. // getInboundsAddClient creates an inline keyboard for adding clients to inbounds.
  2697. func (t *Tgbot) getInboundsAddClient() (*telego.InlineKeyboardMarkup, error) {
  2698. inbounds, err := t.inboundService.GetAllInbounds()
  2699. if err != nil {
  2700. logger.Warning("GetAllInbounds run failed:", err)
  2701. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2702. }
  2703. if len(inbounds) == 0 {
  2704. logger.Warning("No inbounds found")
  2705. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2706. }
  2707. excludedProtocols := map[model.Protocol]bool{
  2708. model.Tunnel: true,
  2709. model.Mixed: true,
  2710. model.WireGuard: true,
  2711. model.HTTP: true,
  2712. }
  2713. var buttons []telego.InlineKeyboardButton
  2714. for _, inbound := range inbounds {
  2715. if excludedProtocols[inbound.Protocol] {
  2716. continue
  2717. }
  2718. status := "❌"
  2719. if inbound.Enable {
  2720. status = "✅"
  2721. }
  2722. callbackData := t.encodeQuery(fmt.Sprintf("%s %d", "add_client_to", inbound.Id))
  2723. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(callbackData))
  2724. }
  2725. cols := 1
  2726. if len(buttons) >= 6 {
  2727. cols = 2
  2728. }
  2729. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  2730. return keyboard, nil
  2731. }
  2732. // getInboundClients creates an inline keyboard with clients of a specific inbound.
  2733. func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) {
  2734. inbound, err := t.inboundService.GetInbound(id)
  2735. if err != nil {
  2736. logger.Warning("getIboundClients run failed:", err)
  2737. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2738. }
  2739. clients, err := t.inboundService.GetClients(inbound)
  2740. var buttons []telego.InlineKeyboardButton
  2741. if err != nil {
  2742. logger.Warning("GetInboundClients run failed:", err)
  2743. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  2744. } else {
  2745. if len(clients) > 0 {
  2746. for _, client := range clients {
  2747. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email)))
  2748. }
  2749. } else {
  2750. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  2751. }
  2752. }
  2753. cols := 0
  2754. if len(buttons) < 6 {
  2755. cols = 3
  2756. } else {
  2757. cols = 2
  2758. }
  2759. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  2760. return keyboard, nil
  2761. }
  2762. // clientInfoMsg formats client information message based on traffic and flags.
  2763. func (t *Tgbot) clientInfoMsg(
  2764. traffic *xray.ClientTraffic,
  2765. printEnabled bool,
  2766. printOnline bool,
  2767. printActive bool,
  2768. printDate bool,
  2769. printTraffic bool,
  2770. printRefreshed bool,
  2771. ) string {
  2772. now := time.Now().Unix()
  2773. expiryTime := ""
  2774. flag := false
  2775. diff := traffic.ExpiryTime/1000 - now
  2776. if traffic.ExpiryTime == 0 {
  2777. expiryTime = t.I18nBot("tgbot.unlimited")
  2778. } else if diff > 172800 || !traffic.Enable {
  2779. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  2780. if diff > 0 {
  2781. days := diff / 86400
  2782. hours := (diff % 86400) / 3600
  2783. minutes := (diff % 3600) / 60
  2784. remainingTime := ""
  2785. if days > 0 {
  2786. remainingTime += fmt.Sprintf("%d %s ", days, t.I18nBot("tgbot.days"))
  2787. }
  2788. if hours > 0 {
  2789. remainingTime += fmt.Sprintf("%d %s ", hours, t.I18nBot("tgbot.hours"))
  2790. }
  2791. if minutes > 0 {
  2792. remainingTime += fmt.Sprintf("%d %s", minutes, t.I18nBot("tgbot.minutes"))
  2793. }
  2794. expiryTime += fmt.Sprintf(" (%s)", remainingTime)
  2795. }
  2796. } else if traffic.ExpiryTime < 0 {
  2797. expiryTime = fmt.Sprintf("%d %s", traffic.ExpiryTime/-86400000, t.I18nBot("tgbot.days"))
  2798. flag = true
  2799. } else {
  2800. expiryTime = fmt.Sprintf("%d %s", diff/3600, t.I18nBot("tgbot.hours"))
  2801. flag = true
  2802. }
  2803. total := ""
  2804. if traffic.Total == 0 {
  2805. total = t.I18nBot("tgbot.unlimited")
  2806. } else {
  2807. total = common.FormatTraffic((traffic.Total))
  2808. }
  2809. enabled := ""
  2810. isEnabled, err := t.inboundService.checkIsEnabledByEmail(traffic.Email)
  2811. if err != nil {
  2812. logger.Warning(err)
  2813. enabled = t.I18nBot("tgbot.wentWrong")
  2814. } else if isEnabled {
  2815. enabled = t.I18nBot("tgbot.messages.yes")
  2816. } else {
  2817. enabled = t.I18nBot("tgbot.messages.no")
  2818. }
  2819. active := ""
  2820. if traffic.Enable {
  2821. active = t.I18nBot("tgbot.messages.yes")
  2822. } else {
  2823. active = t.I18nBot("tgbot.messages.no")
  2824. }
  2825. status := t.I18nBot("tgbot.offline")
  2826. isOnline := false
  2827. if p.IsRunning() {
  2828. if slices.Contains(p.GetOnlineClients(), traffic.Email) {
  2829. status = t.I18nBot("tgbot.online")
  2830. isOnline = true
  2831. }
  2832. }
  2833. output := ""
  2834. output += t.I18nBot("tgbot.messages.email", "Email=="+traffic.Email)
  2835. if printEnabled {
  2836. output += t.I18nBot("tgbot.messages.enabled", "Enable=="+enabled)
  2837. }
  2838. if printOnline {
  2839. output += t.I18nBot("tgbot.messages.online", "Status=="+status)
  2840. if !isOnline && traffic.LastOnline > 0 {
  2841. output += t.I18nBot("tgbot.messages.lastOnline", "Time=="+time.UnixMilli(traffic.LastOnline).Format("2006-01-02 15:04:05"))
  2842. }
  2843. }
  2844. if printActive {
  2845. output += t.I18nBot("tgbot.messages.active", "Enable=="+active)
  2846. }
  2847. if printDate {
  2848. if flag {
  2849. output += t.I18nBot("tgbot.messages.expireIn", "Time=="+expiryTime)
  2850. } else {
  2851. output += t.I18nBot("tgbot.messages.expire", "Time=="+expiryTime)
  2852. }
  2853. }
  2854. if printTraffic {
  2855. output += t.I18nBot("tgbot.messages.upload", "Upload=="+common.FormatTraffic(traffic.Up))
  2856. output += t.I18nBot("tgbot.messages.download", "Download=="+common.FormatTraffic(traffic.Down))
  2857. output += t.I18nBot("tgbot.messages.total", "UpDown=="+common.FormatTraffic((traffic.Up+traffic.Down)), "Total=="+total)
  2858. }
  2859. if printRefreshed {
  2860. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  2861. }
  2862. return output
  2863. }
  2864. // getClientUsage retrieves and sends client usage information to the chat.
  2865. func (t *Tgbot) getClientUsage(chatId int64, tgUserID int64, email ...string) {
  2866. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
  2867. if err != nil {
  2868. logger.Warning(err)
  2869. msg := t.I18nBot("tgbot.wentWrong")
  2870. t.SendMsgToTgbot(chatId, msg)
  2871. return
  2872. }
  2873. if len(traffics) == 0 {
  2874. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
  2875. return
  2876. }
  2877. output := ""
  2878. if len(traffics) > 0 {
  2879. if len(email) > 0 {
  2880. for _, traffic := range traffics {
  2881. if traffic.Email == email[0] {
  2882. output := t.clientInfoMsg(traffic, true, true, true, true, true, true)
  2883. t.SendMsgToTgbot(chatId, output)
  2884. return
  2885. }
  2886. }
  2887. msg := t.I18nBot("tgbot.noResult")
  2888. t.SendMsgToTgbot(chatId, msg)
  2889. return
  2890. } else {
  2891. for _, traffic := range traffics {
  2892. output += t.clientInfoMsg(traffic, true, true, true, true, true, false)
  2893. output += "\r\n"
  2894. }
  2895. }
  2896. }
  2897. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  2898. t.SendMsgToTgbot(chatId, output)
  2899. output = t.I18nBot("tgbot.commands.pleaseChoose")
  2900. t.SendAnswer(chatId, output, false)
  2901. }
  2902. // searchClientIps searches and sends client IP addresses for the given email.
  2903. func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) {
  2904. ips, err := t.inboundService.GetInboundClientIps(email)
  2905. if err != nil || len(ips) == 0 {
  2906. ips = t.I18nBot("tgbot.noIpRecord")
  2907. }
  2908. formattedIps := ips
  2909. if err == nil && len(ips) > 0 {
  2910. type ipWithTimestamp struct {
  2911. IP string `json:"ip"`
  2912. Timestamp int64 `json:"timestamp"`
  2913. }
  2914. var ipsWithTime []ipWithTimestamp
  2915. if json.Unmarshal([]byte(ips), &ipsWithTime) == nil && len(ipsWithTime) > 0 {
  2916. lines := make([]string, 0, len(ipsWithTime))
  2917. for _, item := range ipsWithTime {
  2918. if item.IP == "" {
  2919. continue
  2920. }
  2921. if item.Timestamp > 0 {
  2922. ts := time.Unix(item.Timestamp, 0).Format("2006-01-02 15:04:05")
  2923. lines = append(lines, fmt.Sprintf("%s (%s)", item.IP, ts))
  2924. continue
  2925. }
  2926. lines = append(lines, item.IP)
  2927. }
  2928. if len(lines) > 0 {
  2929. formattedIps = strings.Join(lines, "\n")
  2930. }
  2931. } else {
  2932. var oldIps []string
  2933. if json.Unmarshal([]byte(ips), &oldIps) == nil && len(oldIps) > 0 {
  2934. formattedIps = strings.Join(oldIps, "\n")
  2935. }
  2936. }
  2937. }
  2938. output := ""
  2939. output += t.I18nBot("tgbot.messages.email", "Email=="+email)
  2940. output += t.I18nBot("tgbot.messages.ips", "IPs=="+formattedIps)
  2941. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  2942. inlineKeyboard := tu.InlineKeyboard(
  2943. tu.InlineKeyboardRow(
  2944. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("ips_refresh "+email)),
  2945. ),
  2946. tu.InlineKeyboardRow(
  2947. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clearIPs")).WithCallbackData(t.encodeQuery("clear_ips "+email)),
  2948. ),
  2949. )
  2950. if len(messageID) > 0 {
  2951. t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
  2952. } else {
  2953. t.SendMsgToTgbot(chatId, output, inlineKeyboard)
  2954. }
  2955. }
  2956. // clientTelegramUserInfo retrieves and sends Telegram user info for the client.
  2957. func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string, messageID ...int) {
  2958. traffic, client, err := t.inboundService.GetClientByEmail(email)
  2959. if err != nil {
  2960. logger.Warning(err)
  2961. msg := t.I18nBot("tgbot.wentWrong")
  2962. t.SendMsgToTgbot(chatId, msg)
  2963. return
  2964. }
  2965. if client == nil {
  2966. msg := t.I18nBot("tgbot.noResult")
  2967. t.SendMsgToTgbot(chatId, msg)
  2968. return
  2969. }
  2970. tgId := "None"
  2971. if client.TgID != 0 {
  2972. tgId = strconv.FormatInt(client.TgID, 10)
  2973. }
  2974. output := ""
  2975. output += t.I18nBot("tgbot.messages.email", "Email=="+email)
  2976. output += t.I18nBot("tgbot.messages.TGUser", "TelegramID=="+tgId)
  2977. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  2978. inlineKeyboard := tu.InlineKeyboard(
  2979. tu.InlineKeyboardRow(
  2980. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("tgid_refresh "+email)),
  2981. ),
  2982. tu.InlineKeyboardRow(
  2983. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.removeTGUser")).WithCallbackData(t.encodeQuery("tgid_remove "+email)),
  2984. ),
  2985. )
  2986. if len(messageID) > 0 {
  2987. t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
  2988. } else {
  2989. t.SendMsgToTgbot(chatId, output, inlineKeyboard)
  2990. requestUser := telego.KeyboardButtonRequestUsers{
  2991. RequestID: int32(traffic.Id),
  2992. UserIsBot: new(bool),
  2993. }
  2994. keyboard := tu.Keyboard(
  2995. tu.KeyboardRow(
  2996. tu.KeyboardButton(t.I18nBot("tgbot.buttons.selectTGUser")).WithRequestUsers(&requestUser),
  2997. ),
  2998. tu.KeyboardRow(
  2999. tu.KeyboardButton(t.I18nBot("tgbot.buttons.closeKeyboard")),
  3000. ),
  3001. ).WithIsPersistent().WithResizeKeyboard()
  3002. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.buttons.selectOneTGUser"), keyboard)
  3003. }
  3004. }
  3005. // searchClient searches for a client by email and sends the information.
  3006. func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) {
  3007. traffic, err := t.inboundService.GetClientTrafficByEmail(email)
  3008. if err != nil {
  3009. logger.Warning(err)
  3010. msg := t.I18nBot("tgbot.wentWrong")
  3011. t.SendMsgToTgbot(chatId, msg)
  3012. return
  3013. }
  3014. if traffic == nil {
  3015. msg := t.I18nBot("tgbot.noResult")
  3016. t.SendMsgToTgbot(chatId, msg)
  3017. return
  3018. }
  3019. output := t.clientInfoMsg(traffic, true, true, true, true, true, true)
  3020. inlineKeyboard := tu.InlineKeyboard(
  3021. tu.InlineKeyboardRow(
  3022. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("client_refresh "+email)),
  3023. ),
  3024. tu.InlineKeyboardRow(
  3025. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic "+email)),
  3026. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.limitTraffic")).WithCallbackData(t.encodeQuery("limit_traffic "+email)),
  3027. ),
  3028. tu.InlineKeyboardRow(
  3029. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetExpire")).WithCallbackData(t.encodeQuery("reset_exp "+email)),
  3030. ),
  3031. tu.InlineKeyboardRow(
  3032. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLog")).WithCallbackData(t.encodeQuery("ip_log "+email)),
  3033. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLimit")).WithCallbackData(t.encodeQuery("ip_limit "+email)),
  3034. ),
  3035. tu.InlineKeyboardRow(
  3036. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.setTGUser")).WithCallbackData(t.encodeQuery("tg_user "+email)),
  3037. ),
  3038. tu.InlineKeyboardRow(
  3039. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.toggle")).WithCallbackData(t.encodeQuery("toggle_enable "+email)),
  3040. ),
  3041. )
  3042. if len(messageID) > 0 {
  3043. t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
  3044. } else {
  3045. t.SendMsgToTgbot(chatId, output, inlineKeyboard)
  3046. }
  3047. }
  3048. // addClient handles the process of adding a new client to an inbound.
  3049. func (t *Tgbot) addClient(chatId int64, msg string, messageID ...int) {
  3050. inbound, err := t.inboundService.GetInbound(receiver_inbound_ID)
  3051. if err != nil {
  3052. t.SendMsgToTgbot(chatId, err.Error())
  3053. return
  3054. }
  3055. protocol := inbound.Protocol
  3056. switch protocol {
  3057. case model.VMESS, model.VLESS:
  3058. inlineKeyboard := tu.InlineKeyboard(
  3059. tu.InlineKeyboardRow(
  3060. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_email")).WithCallbackData("add_client_ch_default_email"),
  3061. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_id")).WithCallbackData("add_client_ch_default_id"),
  3062. ),
  3063. tu.InlineKeyboardRow(
  3064. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.limitTraffic")).WithCallbackData("add_client_ch_default_traffic"),
  3065. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetExpire")).WithCallbackData("add_client_ch_default_exp"),
  3066. ),
  3067. tu.InlineKeyboardRow(
  3068. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_comment")).WithCallbackData("add_client_ch_default_comment"),
  3069. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLimit")).WithCallbackData("add_client_ch_default_ip_limit"),
  3070. ),
  3071. tu.InlineKeyboardRow(
  3072. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.submitDisable")).WithCallbackData("add_client_submit_disable"),
  3073. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.submitEnable")).WithCallbackData("add_client_submit_enable"),
  3074. ),
  3075. tu.InlineKeyboardRow(
  3076. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData("add_client_cancel"),
  3077. ),
  3078. )
  3079. if len(messageID) > 0 {
  3080. t.editMessageTgBot(chatId, messageID[0], msg, inlineKeyboard)
  3081. } else {
  3082. t.SendMsgToTgbot(chatId, msg, inlineKeyboard)
  3083. }
  3084. case model.Trojan:
  3085. inlineKeyboard := tu.InlineKeyboard(
  3086. tu.InlineKeyboardRow(
  3087. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_email")).WithCallbackData("add_client_ch_default_email"),
  3088. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_password")).WithCallbackData("add_client_ch_default_pass_tr"),
  3089. ),
  3090. tu.InlineKeyboardRow(
  3091. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.limitTraffic")).WithCallbackData("add_client_ch_default_traffic"),
  3092. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetExpire")).WithCallbackData("add_client_ch_default_exp"),
  3093. ),
  3094. tu.InlineKeyboardRow(
  3095. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_comment")).WithCallbackData("add_client_ch_default_comment"),
  3096. tu.InlineKeyboardButton("ip limit").WithCallbackData("add_client_ch_default_ip_limit"),
  3097. ),
  3098. tu.InlineKeyboardRow(
  3099. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.submitDisable")).WithCallbackData("add_client_submit_disable"),
  3100. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.submitEnable")).WithCallbackData("add_client_submit_enable"),
  3101. ),
  3102. tu.InlineKeyboardRow(
  3103. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData("add_client_cancel"),
  3104. ),
  3105. )
  3106. if len(messageID) > 0 {
  3107. t.editMessageTgBot(chatId, messageID[0], msg, inlineKeyboard)
  3108. } else {
  3109. t.SendMsgToTgbot(chatId, msg, inlineKeyboard)
  3110. }
  3111. case model.Shadowsocks:
  3112. inlineKeyboard := tu.InlineKeyboard(
  3113. tu.InlineKeyboardRow(
  3114. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_email")).WithCallbackData("add_client_ch_default_email"),
  3115. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_password")).WithCallbackData("add_client_ch_default_pass_sh"),
  3116. ),
  3117. tu.InlineKeyboardRow(
  3118. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.limitTraffic")).WithCallbackData("add_client_ch_default_traffic"),
  3119. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetExpire")).WithCallbackData("add_client_ch_default_exp"),
  3120. ),
  3121. tu.InlineKeyboardRow(
  3122. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.change_comment")).WithCallbackData("add_client_ch_default_comment"),
  3123. tu.InlineKeyboardButton("ip limit").WithCallbackData("add_client_ch_default_ip_limit"),
  3124. ),
  3125. tu.InlineKeyboardRow(
  3126. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.submitDisable")).WithCallbackData("add_client_submit_disable"),
  3127. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.submitEnable")).WithCallbackData("add_client_submit_enable"),
  3128. ),
  3129. tu.InlineKeyboardRow(
  3130. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData("add_client_cancel"),
  3131. ),
  3132. )
  3133. if len(messageID) > 0 {
  3134. t.editMessageTgBot(chatId, messageID[0], msg, inlineKeyboard)
  3135. } else {
  3136. t.SendMsgToTgbot(chatId, msg, inlineKeyboard)
  3137. }
  3138. }
  3139. }
  3140. // searchInbound searches for inbounds by remark and sends the results.
  3141. func (t *Tgbot) searchInbound(chatId int64, remark string) {
  3142. inbounds, err := t.inboundService.SearchInbounds(remark)
  3143. if err != nil {
  3144. logger.Warning(err)
  3145. msg := t.I18nBot("tgbot.wentWrong")
  3146. t.SendMsgToTgbot(chatId, msg)
  3147. return
  3148. }
  3149. if len(inbounds) == 0 {
  3150. msg := t.I18nBot("tgbot.noInbounds")
  3151. t.SendMsgToTgbot(chatId, msg)
  3152. return
  3153. }
  3154. for _, inbound := range inbounds {
  3155. info := ""
  3156. info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  3157. info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  3158. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  3159. if inbound.ExpiryTime == 0 {
  3160. info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  3161. } else {
  3162. info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  3163. }
  3164. t.SendMsgToTgbot(chatId, info)
  3165. if len(inbound.ClientStats) > 0 {
  3166. var output strings.Builder
  3167. for _, traffic := range inbound.ClientStats {
  3168. output.WriteString(t.clientInfoMsg(&traffic, true, true, true, true, true, true))
  3169. }
  3170. t.SendMsgToTgbot(chatId, output.String())
  3171. }
  3172. }
  3173. }
  3174. // getExhausted retrieves and sends information about exhausted clients.
  3175. func (t *Tgbot) getExhausted(chatId int64) {
  3176. trDiff := int64(0)
  3177. exDiff := int64(0)
  3178. now := time.Now().Unix() * 1000
  3179. var exhaustedInbounds []model.Inbound
  3180. var exhaustedClients []xray.ClientTraffic
  3181. var disabledInbounds []model.Inbound
  3182. var disabledClients []xray.ClientTraffic
  3183. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  3184. if err == nil && TrafficThreshold > 0 {
  3185. trDiff = int64(TrafficThreshold) * 1073741824
  3186. }
  3187. ExpireThreshold, err := t.settingService.GetExpireDiff()
  3188. if err == nil && ExpireThreshold > 0 {
  3189. exDiff = int64(ExpireThreshold) * 86400000
  3190. }
  3191. inbounds, err := t.inboundService.GetAllInbounds()
  3192. if err != nil {
  3193. logger.Warning("Unable to load Inbounds", err)
  3194. }
  3195. for _, inbound := range inbounds {
  3196. if inbound.Enable {
  3197. if (inbound.ExpiryTime > 0 && (inbound.ExpiryTime-now < exDiff)) ||
  3198. (inbound.Total > 0 && (inbound.Total-(inbound.Up+inbound.Down) < trDiff)) {
  3199. exhaustedInbounds = append(exhaustedInbounds, *inbound)
  3200. }
  3201. if len(inbound.ClientStats) > 0 {
  3202. for _, client := range inbound.ClientStats {
  3203. if client.Enable {
  3204. if (client.ExpiryTime > 0 && (client.ExpiryTime-now < exDiff)) ||
  3205. (client.Total > 0 && (client.Total-(client.Up+client.Down) < trDiff)) {
  3206. exhaustedClients = append(exhaustedClients, client)
  3207. }
  3208. } else {
  3209. disabledClients = append(disabledClients, client)
  3210. }
  3211. }
  3212. }
  3213. } else {
  3214. disabledInbounds = append(disabledInbounds, *inbound)
  3215. }
  3216. }
  3217. // Inbounds
  3218. output := ""
  3219. output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.inbounds"))
  3220. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledInbounds)))
  3221. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedInbounds)))
  3222. if len(exhaustedInbounds) > 0 {
  3223. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.inbounds"))
  3224. for _, inbound := range exhaustedInbounds {
  3225. output += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  3226. output += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  3227. output += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  3228. if inbound.ExpiryTime == 0 {
  3229. output += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  3230. } else {
  3231. output += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  3232. }
  3233. output += "\r\n"
  3234. }
  3235. }
  3236. // Clients
  3237. exhaustedCC := len(exhaustedClients)
  3238. output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients"))
  3239. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients)))
  3240. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(exhaustedCC))
  3241. if exhaustedCC > 0 {
  3242. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.clients"))
  3243. var buttons []telego.InlineKeyboardButton
  3244. for _, traffic := range exhaustedClients {
  3245. output += t.clientInfoMsg(&traffic, true, false, false, true, true, false)
  3246. output += "\r\n"
  3247. buttons = append(buttons, tu.InlineKeyboardButton(traffic.Email).WithCallbackData(t.encodeQuery("client_get_usage "+traffic.Email)))
  3248. }
  3249. cols := 0
  3250. if exhaustedCC < 11 {
  3251. cols = 1
  3252. } else {
  3253. cols = 2
  3254. }
  3255. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  3256. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  3257. t.SendMsgToTgbot(chatId, output, keyboard)
  3258. } else {
  3259. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  3260. t.SendMsgToTgbot(chatId, output)
  3261. }
  3262. }
  3263. // notifyExhausted sends notifications for exhausted clients.
  3264. func (t *Tgbot) notifyExhausted() {
  3265. trDiff := int64(0)
  3266. exDiff := int64(0)
  3267. now := time.Now().Unix() * 1000
  3268. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  3269. if err == nil && TrafficThreshold > 0 {
  3270. trDiff = int64(TrafficThreshold) * 1073741824
  3271. }
  3272. ExpireThreshold, err := t.settingService.GetExpireDiff()
  3273. if err == nil && ExpireThreshold > 0 {
  3274. exDiff = int64(ExpireThreshold) * 86400000
  3275. }
  3276. inbounds, err := t.inboundService.GetAllInbounds()
  3277. if err != nil {
  3278. logger.Warning("Unable to load Inbounds", err)
  3279. }
  3280. var chatIDsDone []int64
  3281. for _, inbound := range inbounds {
  3282. if inbound.Enable {
  3283. if len(inbound.ClientStats) > 0 {
  3284. clients, err := t.inboundService.GetClients(inbound)
  3285. if err == nil {
  3286. for _, client := range clients {
  3287. if client.TgID != 0 {
  3288. chatID := client.TgID
  3289. if !int64Contains(chatIDsDone, chatID) && !checkAdmin(chatID) {
  3290. var disabledClients []xray.ClientTraffic
  3291. var exhaustedClients []xray.ClientTraffic
  3292. traffics, err := t.inboundService.GetClientTrafficTgBot(client.TgID)
  3293. if err == nil && len(traffics) > 0 {
  3294. output := t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients"))
  3295. for _, traffic := range traffics {
  3296. if traffic.Enable {
  3297. if (traffic.ExpiryTime > 0 && (traffic.ExpiryTime-now < exDiff)) ||
  3298. (traffic.Total > 0 && (traffic.Total-(traffic.Up+traffic.Down) < trDiff)) {
  3299. exhaustedClients = append(exhaustedClients, *traffic)
  3300. }
  3301. } else {
  3302. disabledClients = append(disabledClients, *traffic)
  3303. }
  3304. }
  3305. if len(exhaustedClients) > 0 {
  3306. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients)))
  3307. if len(disabledClients) > 0 {
  3308. output += t.I18nBot("tgbot.clients") + ":\r\n"
  3309. for _, traffic := range disabledClients {
  3310. output += " " + traffic.Email
  3311. }
  3312. output += "\r\n"
  3313. }
  3314. output += "\r\n"
  3315. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedClients)))
  3316. for _, traffic := range exhaustedClients {
  3317. output += t.clientInfoMsg(&traffic, true, false, false, true, true, false)
  3318. output += "\r\n"
  3319. }
  3320. t.SendMsgToTgbot(chatID, output)
  3321. }
  3322. chatIDsDone = append(chatIDsDone, chatID)
  3323. }
  3324. }
  3325. }
  3326. }
  3327. }
  3328. }
  3329. }
  3330. }
  3331. }
  3332. // int64Contains checks if an int64 slice contains a specific item.
  3333. func int64Contains(slice []int64, item int64) bool {
  3334. for _, s := range slice {
  3335. if s == item {
  3336. return true
  3337. }
  3338. }
  3339. return false
  3340. }
  3341. // onlineClients retrieves and sends information about online clients.
  3342. func (t *Tgbot) onlineClients(chatId int64, messageID ...int) {
  3343. if !p.IsRunning() {
  3344. return
  3345. }
  3346. onlines := p.GetOnlineClients()
  3347. onlinesCount := len(onlines)
  3348. output := t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(onlinesCount))
  3349. keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
  3350. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("onlines_refresh"))))
  3351. if onlinesCount > 0 {
  3352. var buttons []telego.InlineKeyboardButton
  3353. for _, online := range onlines {
  3354. buttons = append(buttons, tu.InlineKeyboardButton(online).WithCallbackData(t.encodeQuery("client_get_usage "+online)))
  3355. }
  3356. cols := 0
  3357. if onlinesCount < 21 {
  3358. cols = 2
  3359. } else if onlinesCount < 61 {
  3360. cols = 3
  3361. } else {
  3362. cols = 4
  3363. }
  3364. keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, tu.InlineKeyboardCols(cols, buttons...)...)
  3365. }
  3366. if len(messageID) > 0 {
  3367. t.editMessageTgBot(chatId, messageID[0], output, keyboard)
  3368. } else {
  3369. t.SendMsgToTgbot(chatId, output, keyboard)
  3370. }
  3371. }
  3372. // sendBackup sends a backup of the database and configuration files.
  3373. func (t *Tgbot) sendBackup(chatId int64) {
  3374. output := t.I18nBot("tgbot.messages.backupTime", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  3375. t.SendMsgToTgbot(chatId, output)
  3376. // Update by manually trigger a checkpoint operation
  3377. err := database.Checkpoint()
  3378. if err != nil {
  3379. logger.Error("Error in trigger a checkpoint operation: ", err)
  3380. }
  3381. // Send database backup
  3382. file, err := os.Open(config.GetDBPath())
  3383. if err == nil {
  3384. defer file.Close()
  3385. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  3386. defer cancel()
  3387. document := tu.Document(
  3388. tu.ID(chatId),
  3389. tu.File(file),
  3390. )
  3391. _, err = bot.SendDocument(ctx, document)
  3392. if err != nil {
  3393. logger.Error("Error in uploading backup: ", err)
  3394. }
  3395. } else {
  3396. logger.Error("Error in opening db file for backup: ", err)
  3397. }
  3398. // Small delay between file sends
  3399. time.Sleep(500 * time.Millisecond)
  3400. // Send config.json backup
  3401. file, err = os.Open(xray.GetConfigPath())
  3402. if err == nil {
  3403. defer file.Close()
  3404. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  3405. defer cancel()
  3406. document := tu.Document(
  3407. tu.ID(chatId),
  3408. tu.File(file),
  3409. )
  3410. _, err = bot.SendDocument(ctx, document)
  3411. if err != nil {
  3412. logger.Error("Error in uploading config.json: ", err)
  3413. }
  3414. } else {
  3415. logger.Error("Error in opening config.json file for backup: ", err)
  3416. }
  3417. }
  3418. // sendBanLogs sends the ban logs to the specified chat.
  3419. func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
  3420. if dt {
  3421. output := t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  3422. t.SendMsgToTgbot(chatId, output)
  3423. }
  3424. file, err := os.Open(xray.GetIPLimitBannedPrevLogPath())
  3425. if err == nil {
  3426. // Check if the file is non-empty before attempting to upload
  3427. fileInfo, _ := file.Stat()
  3428. if fileInfo.Size() > 0 {
  3429. document := tu.Document(
  3430. tu.ID(chatId),
  3431. tu.File(file),
  3432. )
  3433. _, err = bot.SendDocument(context.Background(), document)
  3434. if err != nil {
  3435. logger.Error("Error in uploading IPLimitBannedPrevLog: ", err)
  3436. }
  3437. } else {
  3438. logger.Warning("IPLimitBannedPrevLog file is empty, not uploading.")
  3439. }
  3440. file.Close()
  3441. } else {
  3442. logger.Error("Error in opening IPLimitBannedPrevLog file for backup: ", err)
  3443. }
  3444. file, err = os.Open(xray.GetIPLimitBannedLogPath())
  3445. if err == nil {
  3446. // Check if the file is non-empty before attempting to upload
  3447. fileInfo, _ := file.Stat()
  3448. if fileInfo.Size() > 0 {
  3449. document := tu.Document(
  3450. tu.ID(chatId),
  3451. tu.File(file),
  3452. )
  3453. _, err = bot.SendDocument(context.Background(), document)
  3454. if err != nil {
  3455. logger.Error("Error in uploading IPLimitBannedLog: ", err)
  3456. }
  3457. } else {
  3458. logger.Warning("IPLimitBannedLog file is empty, not uploading.")
  3459. }
  3460. file.Close()
  3461. } else {
  3462. logger.Error("Error in opening IPLimitBannedLog file for backup: ", err)
  3463. }
  3464. }
  3465. // sendCallbackAnswerTgBot answers a callback query with a message.
  3466. func (t *Tgbot) sendCallbackAnswerTgBot(id string, message string) {
  3467. params := telego.AnswerCallbackQueryParams{
  3468. CallbackQueryID: id,
  3469. Text: message,
  3470. }
  3471. if err := bot.AnswerCallbackQuery(context.Background(), &params); err != nil {
  3472. logger.Warning(err)
  3473. }
  3474. }
  3475. // editMessageCallbackTgBot edits the reply markup of a message.
  3476. func (t *Tgbot) editMessageCallbackTgBot(chatId int64, messageID int, inlineKeyboard *telego.InlineKeyboardMarkup) {
  3477. params := telego.EditMessageReplyMarkupParams{
  3478. ChatID: tu.ID(chatId),
  3479. MessageID: messageID,
  3480. ReplyMarkup: inlineKeyboard,
  3481. }
  3482. if _, err := bot.EditMessageReplyMarkup(context.Background(), &params); err != nil {
  3483. logger.Warning(err)
  3484. }
  3485. }
  3486. // editMessageTgBot edits the text and reply markup of a message.
  3487. func (t *Tgbot) editMessageTgBot(chatId int64, messageID int, text string, inlineKeyboard ...*telego.InlineKeyboardMarkup) {
  3488. params := telego.EditMessageTextParams{
  3489. ChatID: tu.ID(chatId),
  3490. MessageID: messageID,
  3491. Text: text,
  3492. ParseMode: "HTML",
  3493. }
  3494. if len(inlineKeyboard) > 0 {
  3495. params.ReplyMarkup = inlineKeyboard[0]
  3496. }
  3497. if _, err := bot.EditMessageText(context.Background(), &params); err != nil {
  3498. logger.Warning(err)
  3499. }
  3500. }
  3501. // SendMsgToTgbotDeleteAfter sends a message and deletes it after a specified delay.
  3502. func (t *Tgbot) SendMsgToTgbotDeleteAfter(chatId int64, msg string, delayInSeconds int, replyMarkup ...telego.ReplyMarkup) {
  3503. // Determine if replyMarkup was passed; otherwise, set it to nil
  3504. var replyMarkupParam telego.ReplyMarkup
  3505. if len(replyMarkup) > 0 {
  3506. replyMarkupParam = replyMarkup[0] // Use the first element
  3507. }
  3508. // Send the message
  3509. sentMsg, err := bot.SendMessage(context.Background(), &telego.SendMessageParams{
  3510. ChatID: tu.ID(chatId),
  3511. Text: msg,
  3512. ReplyMarkup: replyMarkupParam, // Use the correct replyMarkup value
  3513. })
  3514. if err != nil {
  3515. logger.Warning("Failed to send message:", err)
  3516. return
  3517. }
  3518. // Delete the sent message after the specified number of seconds
  3519. go func() {
  3520. time.Sleep(time.Duration(delayInSeconds) * time.Second) // Wait for the specified delay
  3521. t.deleteMessageTgBot(chatId, sentMsg.MessageID) // Delete the message
  3522. delete(userStates, chatId)
  3523. }()
  3524. }
  3525. // deleteMessageTgBot deletes a message from the chat.
  3526. func (t *Tgbot) deleteMessageTgBot(chatId int64, messageID int) {
  3527. params := telego.DeleteMessageParams{
  3528. ChatID: tu.ID(chatId),
  3529. MessageID: messageID,
  3530. }
  3531. if err := bot.DeleteMessage(context.Background(), &params); err != nil {
  3532. logger.Warning("Failed to delete message:", err)
  3533. } else {
  3534. logger.Info("Message deleted successfully")
  3535. }
  3536. }
  3537. // isSingleWord checks if the text contains only a single word.
  3538. func (t *Tgbot) isSingleWord(text string) bool {
  3539. text = strings.TrimSpace(text)
  3540. re := regexp.MustCompile(`\s+`)
  3541. return re.MatchString(text)
  3542. }