diff --git a/cmd/explorer/main.go b/cmd/explorer/main.go index 611a67d974..800f669279 100644 --- a/cmd/explorer/main.go +++ b/cmd/explorer/main.go @@ -377,102 +377,103 @@ func main() { ) router.HandleFunc("/", handlers.Index).Methods("GET") - router.HandleFunc("/latestState", handlers.LatestState).Methods("GET") - router.HandleFunc("/launchMetrics", handlers.SlotVizMetrics).Methods("GET") + router.HandleFunc("/turnstile/verify", handlers.VerifyTurnstile).Methods("GET") + router.HandleFunc("/latestState", utils.Adapt(http.HandlerFunc(handlers.LatestState), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/launchMetrics", utils.Adapt(http.HandlerFunc(handlers.SlotVizMetrics), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/index/data", handlers.IndexPageData).Methods("GET") router.HandleFunc("/slot/{slotOrHash}", handlers.Slot).Methods("GET") - router.HandleFunc("/slot/{slotOrHash}/deposits", handlers.SlotDepositData).Methods("GET") - router.HandleFunc("/slot/{slotOrHash}/votes", handlers.SlotVoteData).Methods("GET") - router.HandleFunc("/slot/{slot}/attestations", handlers.SlotAttestationsData).Methods("GET") - router.HandleFunc("/slot/{slot}/withdrawals", handlers.SlotWithdrawalData).Methods("GET") - router.HandleFunc("/slot/{slot}/blsChange", handlers.SlotBlsChangeData).Methods("GET") + router.HandleFunc("/slot/{slotOrHash}/deposits", utils.Adapt(http.HandlerFunc(handlers.SlotDepositData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/slot/{slotOrHash}/votes", utils.Adapt(http.HandlerFunc(handlers.SlotVoteData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/slot/{slot}/attestations", utils.Adapt(http.HandlerFunc(handlers.SlotAttestationsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/slot/{slot}/withdrawals", utils.Adapt(http.HandlerFunc(handlers.SlotWithdrawalData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/slot/{slot}/blsChange", utils.Adapt(http.HandlerFunc(handlers.SlotBlsChangeData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/slots/finder", handlers.SlotFinder).Methods("GET") router.HandleFunc("/slots", handlers.Slots).Methods("GET") - router.HandleFunc("/slots/data", handlers.SlotsData).Methods("GET") + router.HandleFunc("/slots/data", utils.Adapt(http.HandlerFunc(handlers.SlotsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/blocks", handlers.Eth1Blocks).Methods("GET") - router.HandleFunc("/blocks/data", handlers.Eth1BlocksData).Methods("GET") + router.HandleFunc("/blocks/data", utils.Adapt(http.HandlerFunc(handlers.Eth1BlocksData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/blocks/highest", handlers.Eth1BlocksHighest).Methods("GET") router.HandleFunc("/address/{address}", handlers.Eth1Address).Methods("GET") - router.HandleFunc("/address/{address}/blocks", handlers.Eth1AddressBlocksMined).Methods("GET") - router.HandleFunc("/address/{address}/uncles", handlers.Eth1AddressUnclesMined).Methods("GET") - router.HandleFunc("/address/{address}/withdrawals", handlers.Eth1AddressWithdrawals).Methods("GET") - router.HandleFunc("/address/{address}/transactions", handlers.Eth1AddressTransactions).Methods("GET") - router.HandleFunc("/address/{address}/internalTxns", handlers.Eth1AddressInternalTransactions).Methods("GET") - router.HandleFunc("/address/{address}/blobTxns", handlers.Eth1AddressBlobTransactions).Methods("GET") - router.HandleFunc("/address/{address}/erc20", handlers.Eth1AddressErc20Transactions).Methods("GET") - router.HandleFunc("/address/{address}/erc721", handlers.Eth1AddressErc721Transactions).Methods("GET") - router.HandleFunc("/address/{address}/erc1155", handlers.Eth1AddressErc1155Transactions).Methods("GET") + router.HandleFunc("/address/{address}/blocks", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressBlocksMined), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/uncles", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressUnclesMined), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/withdrawals", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressWithdrawals), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/transactions", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressTransactions), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/internalTxns", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressInternalTransactions), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/blobTxns", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressBlobTransactions), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/erc20", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressErc20Transactions), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/erc721", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressErc721Transactions), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/address/{address}/erc1155", utils.Adapt(http.HandlerFunc(handlers.Eth1AddressErc1155Transactions), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/token/{token}", handlers.Eth1Token).Methods("GET") - router.HandleFunc("/token/{token}/transfers", handlers.Eth1TokenTransfers).Methods("GET") + router.HandleFunc("/token/{token}/transfers", utils.Adapt(http.HandlerFunc(handlers.Eth1TokenTransfers), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/transactions", handlers.Eth1Transactions).Methods("GET") - router.HandleFunc("/transactions/data", handlers.Eth1TransactionsData).Methods("GET") + router.HandleFunc("/transactions/data", utils.Adapt(http.HandlerFunc(handlers.Eth1TransactionsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/block/{block}", handlers.Eth1Block).Methods("GET") - router.HandleFunc("/block/{block}/transactions", handlers.BlockTransactionsData).Methods("GET") + router.HandleFunc("/block/{block}/transactions", utils.Adapt(http.HandlerFunc(handlers.BlockTransactionsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/tx/{hash}", handlers.Eth1TransactionTx).Methods("GET") router.HandleFunc("/mempool", handlers.MempoolView).Methods("GET") router.HandleFunc("/burn", handlers.Burn).Methods("GET") - router.HandleFunc("/burn/data", handlers.BurnPageData).Methods("GET") + router.HandleFunc("/burn/data", utils.Adapt(http.HandlerFunc(handlers.BurnPageData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/gasnow", handlers.GasNow).Methods("GET") - router.HandleFunc("/gasnow/data", handlers.GasNowData).Methods("GET") + router.HandleFunc("/gasnow/data", utils.Adapt(http.HandlerFunc(handlers.GasNowData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/correlations", handlers.Correlations).Methods("GET") - router.HandleFunc("/correlations/data", handlers.CorrelationsData).Methods("POST") + router.HandleFunc("/correlations/data", utils.Adapt(http.HandlerFunc(handlers.CorrelationsData), utils.TurnstileMiddleware).ServeHTTP).Methods("POST") router.HandleFunc("/vis", handlers.Vis).Methods("GET") router.HandleFunc("/charts", handlers.Charts).Methods("GET") router.HandleFunc("/charts/{chart}", handlers.Chart).Methods("GET") - router.HandleFunc("/charts/{chart}/data", handlers.GenericChartData).Methods("GET") - router.HandleFunc("/vis/blocks", handlers.VisBlocks).Methods("GET") + router.HandleFunc("/charts/{chart}/data", utils.Adapt(http.HandlerFunc(handlers.GenericChartData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/vis/blocks", utils.Adapt(http.HandlerFunc(handlers.VisBlocks), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/vis/votes", handlers.VisVotes).Methods("GET") router.HandleFunc("/epoch/{epoch}", handlers.Epoch).Methods("GET") router.HandleFunc("/epochs", handlers.Epochs).Methods("GET") - router.HandleFunc("/epochs/data", handlers.EpochsData).Methods("GET") + router.HandleFunc("/epochs/data", utils.Adapt(http.HandlerFunc(handlers.EpochsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validator/{index}", handlers.Validator).Methods("GET") - router.HandleFunc("/validator/{index}/proposedblocks", handlers.ValidatorProposedBlocks).Methods("GET") - router.HandleFunc("/validator/{index}/attestations", handlers.ValidatorAttestations).Methods("GET") - router.HandleFunc("/validator/{index}/withdrawals", handlers.ValidatorWithdrawals).Methods("GET") - router.HandleFunc("/validator/{index}/sync", handlers.ValidatorSync).Methods("GET") - router.HandleFunc("/validator/{index}/history", handlers.ValidatorHistory).Methods("GET") - router.HandleFunc("/validator/{pubkey}/deposits", handlers.ValidatorDeposits).Methods("GET") - router.HandleFunc("/validator/{index}/slashings", handlers.ValidatorSlashings).Methods("GET") - router.HandleFunc("/validator/{index}/effectiveness", handlers.ValidatorAttestationInclusionEffectiveness).Methods("GET") + router.HandleFunc("/validator/{index}/proposedblocks", utils.Adapt(http.HandlerFunc(handlers.ValidatorProposedBlocks), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{index}/attestations", utils.Adapt(http.HandlerFunc(handlers.ValidatorAttestations), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{index}/withdrawals", utils.Adapt(http.HandlerFunc(handlers.ValidatorWithdrawals), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{index}/sync", utils.Adapt(http.HandlerFunc(handlers.ValidatorSync), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{index}/history", utils.Adapt(http.HandlerFunc(handlers.ValidatorHistory), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{pubkey}/deposits", utils.Adapt(http.HandlerFunc(handlers.ValidatorDeposits), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{index}/slashings", utils.Adapt(http.HandlerFunc(handlers.ValidatorSlashings), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validator/{index}/effectiveness", utils.Adapt(http.HandlerFunc(handlers.ValidatorAttestationInclusionEffectiveness), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validator/{pubkey}/name", handlers.SaveValidatorName).Methods("POST") router.HandleFunc("/watchlist/add", handlers.UsersModalAddValidator).Methods("POST") router.HandleFunc("/validator/{pubkey}/remove", handlers.UserValidatorWatchlistRemove).Methods("POST") router.HandleFunc("/validator/{index}/stats", handlers.ValidatorStatsTable).Methods("GET") router.HandleFunc("/validators", handlers.Validators).Methods("GET") - router.HandleFunc("/validators/data", handlers.ValidatorsData).Methods("GET") + router.HandleFunc("/validators/data", utils.Adapt(http.HandlerFunc(handlers.ValidatorsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validators/slashings", handlers.ValidatorsSlashings).Methods("GET") - router.HandleFunc("/validators/slashings/data", handlers.ValidatorsSlashingsData).Methods("GET") + router.HandleFunc("/validators/slashings/data", utils.Adapt(http.HandlerFunc(handlers.ValidatorsSlashingsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validators/leaderboard", handlers.ValidatorsLeaderboard).Methods("GET") - router.HandleFunc("/validators/leaderboard/data", handlers.ValidatorsLeaderboardData).Methods("GET") + router.HandleFunc("/validators/leaderboard/data", utils.Adapt(http.HandlerFunc(handlers.ValidatorsLeaderboardData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validators/withdrawals", handlers.Withdrawals).Methods("GET") - router.HandleFunc("/validators/withdrawals/data", handlers.WithdrawalsData).Methods("GET") - router.HandleFunc("/validators/withdrawals/bls", handlers.BLSChangeData).Methods("GET") + router.HandleFunc("/validators/withdrawals/data", utils.Adapt(http.HandlerFunc(handlers.WithdrawalsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/validators/withdrawals/bls", utils.Adapt(http.HandlerFunc(handlers.BLSChangeData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validators/deposits", handlers.Deposits).Methods("GET") router.HandleFunc("/validators/initiated-deposits", handlers.Eth1Deposits).Methods("GET") // deprecated, will redirect to /validators/deposits - router.HandleFunc("/validators/initiated-deposits/data", handlers.Eth1DepositsData).Methods("GET") + router.HandleFunc("/validators/initiated-deposits/data", utils.Adapt(http.HandlerFunc(handlers.Eth1DepositsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validators/deposit-leaderboard", handlers.Eth1DepositsLeaderboard).Methods("GET") - router.HandleFunc("/validators/deposit-leaderboard/data", handlers.Eth1DepositsLeaderboardData).Methods("GET") + router.HandleFunc("/validators/deposit-leaderboard/data", utils.Adapt(http.HandlerFunc(handlers.Eth1DepositsLeaderboardData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/validators/included-deposits", handlers.Eth2Deposits).Methods("GET") // deprecated, will redirect to /validators/deposits - router.HandleFunc("/validators/included-deposits/data", handlers.Eth2DepositsData).Methods("GET") + router.HandleFunc("/validators/included-deposits/data", utils.Adapt(http.HandlerFunc(handlers.Eth2DepositsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/heatmap", handlers.Heatmap).Methods("GET") router.HandleFunc("/dashboard", handlers.Dashboard).Methods("GET") router.HandleFunc("/dashboard/save", handlers.UserDashboardWatchlistAdd).Methods("POST") - router.HandleFunc("/dashboard/data/allbalances", handlers.DashboardDataBalanceCombined).Methods("GET") - router.HandleFunc("/dashboard/data/proposals", handlers.DashboardDataProposals).Methods("GET") - router.HandleFunc("/dashboard/data/proposalshistory", handlers.DashboardDataProposalsHistory).Methods("GET") - router.HandleFunc("/dashboard/data/validators", handlers.DashboardDataValidators).Methods("GET") - router.HandleFunc("/dashboard/data/withdrawal", handlers.DashboardDataWithdrawals).Methods("GET") - router.HandleFunc("/dashboard/data/effectiveness", handlers.DashboardDataEffectiveness).Methods("GET") - router.HandleFunc("/dashboard/data/earnings", handlers.DashboardDataEarnings).Methods("GET") + router.HandleFunc("/dashboard/data/allbalances", utils.Adapt(http.HandlerFunc(handlers.DashboardDataBalanceCombined), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/dashboard/data/proposals", utils.Adapt(http.HandlerFunc(handlers.DashboardDataProposals), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/dashboard/data/proposalshistory", utils.Adapt(http.HandlerFunc(handlers.DashboardDataProposalsHistory), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/dashboard/data/validators", utils.Adapt(http.HandlerFunc(handlers.DashboardDataValidators), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/dashboard/data/withdrawal", utils.Adapt(http.HandlerFunc(handlers.DashboardDataWithdrawals), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/dashboard/data/effectiveness", utils.Adapt(http.HandlerFunc(handlers.DashboardDataEffectiveness), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/dashboard/data/earnings", utils.Adapt(http.HandlerFunc(handlers.DashboardDataEarnings), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/graffitiwall", handlers.Graffitiwall).Methods("GET") router.HandleFunc("/calculator", handlers.StakingCalculator).Methods("GET") router.HandleFunc("/search", handlers.Search).Methods("POST") - router.HandleFunc("/search/{type}/{search}", handlers.SearchAhead).Methods("GET") + router.HandleFunc("/search/{type}/{search}", utils.Adapt(http.HandlerFunc(handlers.SearchAhead), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/imprint", handlers.Imprint).Methods("GET") router.HandleFunc("/mobile", handlers.MobilePage).Methods("GET") router.HandleFunc("/tools/unitConverter", handlers.UnitConverter).Methods("GET") @@ -492,19 +493,19 @@ func main() { router.HandleFunc("/pools", handlers.Pools).Methods("GET") router.HandleFunc("/relays", handlers.Relays).Methods("GET") router.HandleFunc("/pools/rocketpool", handlers.PoolsRocketpool).Methods("GET") - router.HandleFunc("/pools/rocketpool/data/minipools", handlers.PoolsRocketpoolDataMinipools).Methods("GET") - router.HandleFunc("/pools/rocketpool/data/nodes", handlers.PoolsRocketpoolDataNodes).Methods("GET") - router.HandleFunc("/pools/rocketpool/data/dao_proposals", handlers.PoolsRocketpoolDataDAOProposals).Methods("GET") - router.HandleFunc("/pools/rocketpool/data/dao_members", handlers.PoolsRocketpoolDataDAOMembers).Methods("GET") + router.HandleFunc("/pools/rocketpool/data/minipools", utils.Adapt(http.HandlerFunc(handlers.PoolsRocketpoolDataMinipools), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/pools/rocketpool/data/nodes", utils.Adapt(http.HandlerFunc(handlers.PoolsRocketpoolDataNodes), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/pools/rocketpool/data/dao_proposals", utils.Adapt(http.HandlerFunc(handlers.PoolsRocketpoolDataDAOProposals), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") + router.HandleFunc("/pools/rocketpool/data/dao_members", utils.Adapt(http.HandlerFunc(handlers.PoolsRocketpoolDataDAOMembers), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/advertisewithus", handlers.AdvertiseWithUs).Methods("GET") router.HandleFunc("/advertisewithus", handlers.AdvertiseWithUsPost).Methods("POST") // confirming the email update should not require auth router.HandleFunc("/settings/email/{hash}", handlers.UserConfirmUpdateEmail).Methods("GET") - router.HandleFunc("/gitcoinfeed", handlers.GitcoinFeed).Methods("GET") + router.HandleFunc("/gitcoinfeed", utils.Adapt(http.HandlerFunc(handlers.GitcoinFeed), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/rewards", handlers.ValidatorRewards).Methods("GET") - router.HandleFunc("/rewards/hist", handlers.RewardsHistoricalData).Methods("GET") + router.HandleFunc("/rewards/hist", utils.Adapt(http.HandlerFunc(handlers.RewardsHistoricalData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") router.HandleFunc("/rewards/hist/download", handlers.DownloadRewardsHistoricalData).Methods("GET") router.HandleFunc("/notifications/unsubscribe", handlers.UserNotificationsUnsubscribeByHash).Methods("GET") @@ -545,7 +546,7 @@ func main() { authRouter.HandleFunc("/settings/email", handlers.UserUpdateEmailPost).Methods("POST") authRouter.HandleFunc("/notifications", handlers.UserNotificationsCenter).Methods("GET") authRouter.HandleFunc("/notifications/channels", handlers.UsersNotificationChannels).Methods("POST") - authRouter.HandleFunc("/notifications/data", handlers.UserNotificationsData).Methods("GET") + authRouter.HandleFunc("/notifications/data", utils.Adapt(http.HandlerFunc(handlers.UserNotificationsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") authRouter.HandleFunc("/notifications/subscribe", handlers.UserNotificationsSubscribe).Methods("POST") authRouter.HandleFunc("/notifications/network/update", handlers.UserModalAddNetworkEvent).Methods("POST") authRouter.HandleFunc("/watchlist/add", handlers.UsersModalAddValidator).Methods("POST") @@ -564,7 +565,7 @@ func main() { authRouter.HandleFunc("/notifications-center", handlers.UserNotificationsCenter).Methods("GET") authRouter.HandleFunc("/notifications-center/removeall", handlers.RemoveAllValidatorsAndUnsubscribe).Methods("POST") - authRouter.HandleFunc("/subscriptions/data", handlers.UserSubscriptionsData).Methods("GET") + authRouter.HandleFunc("/subscriptions/data", utils.Adapt(http.HandlerFunc(handlers.UserSubscriptionsData), utils.TurnstileMiddleware).ServeHTTP).Methods("GET") authRouter.HandleFunc("/generateKey", handlers.GenerateAPIKey).Methods("POST") authRouter.HandleFunc("/ethClients", handlers.EthClientsServices).Methods("GET") authRouter.HandleFunc("/rewards", handlers.ValidatorRewards).Methods("GET") diff --git a/handlers/index.go b/handlers/index.go index c80810038b..4379bd8268 100644 --- a/handlers/index.go +++ b/handlers/index.go @@ -9,6 +9,7 @@ import ( "fmt" "math" "net/http" + "time" ) // Index will return the main "index" page using a go template @@ -112,3 +113,38 @@ func calculateChurn(page *types.IndexPageData) { page.ValidatorsPerEpoch = *limit page.ValidatorsPerDay = limit_per_day } + +func VerifyTurnstile(w http.ResponseWriter, r *http.Request) { + + if utils.Config.Frontend.Turnstile.Enabled { + + err := utils.VerifyTurnstileToken(r) + + cookie := http.Cookie{ + Name: "turnstile", + Value: "verified", + Path: "/", + MaxAge: int(utils.Config.Frontend.Turnstile.CookieMaxAge), + HttpOnly: false, + Secure: true, + SameSite: http.SameSiteLaxMode, + } + + if err != nil { + fmt.Println(err) + // clear cookie + cookie.MaxAge = -1 + http.SetCookie(w, &cookie) + http.Error(w, "Turnstile challenge failed", http.StatusServiceUnavailable) + return + } + + validuntil := time.Now().Add(time.Duration(utils.Config.Frontend.Turnstile.SessionMaxAge) * time.Second).Format(time.RFC3339) + + utils.SessionStore.SCS.Put(r.Context(), "TURNSTILE::VALIDUNTIL", validuntil) + http.SetCookie(w, &cookie) + w.Write([]byte("Success")) + } else { + w.Write([]byte("Turnstile not enabled")) + } +} diff --git a/handlers/pageData.go b/handlers/pageData.go index 81f7a3d25b..41d4d0b58f 100644 --- a/handlers/pageData.go +++ b/handlers/pageData.go @@ -66,6 +66,7 @@ func InitPageData(w http.ResponseWriter, r *http.Request, active, path, title st MainMenuItems: createMenuItems(active, isMainnet), TermsOfServiceUrl: utils.Config.Frontend.Legal.TermsOfServiceUrl, PrivacyPolicyUrl: utils.Config.Frontend.Legal.PrivacyPolicyUrl, + TurnstileSiteKey: utils.Config.Frontend.Turnstile.SiteKey, } adConfigurations, err := db.GetAdConfigurationsForTemplate(mainTemplates, data.NoAds) diff --git a/static/js/banner.js b/static/js/banner.js index 514b4f7a89..7e0761e619 100644 --- a/static/js/banner.js +++ b/static/js/banner.js @@ -28,101 +28,103 @@ function getCookie(cname) { } function updateBanner() { - fetch("/latestState") - .then(function (res) { - return res.json() - }) - .then(function (data) { - // always visible - var epochHandle = document.getElementById("banner-epoch-data") - - if (data.currentEpoch) { - epochHandle.innerHTML = addCommas(data.currentEpoch) - epochHandle.setAttribute("href", "/epoch/" + data.currentEpoch) - } - - var slotHandle = document.getElementById("banner-slot-data") - if (data.currentSlot) { - slotHandle.innerHTML = addCommas(data.currentSlot) - slotHandle.setAttribute("href", "/slot/" + data.currentSlot) - } - - var ethPriceHandle = document.getElementById("banner-eth-price-data") - - try { - let userCurrency = getCookie("currency") - if (userCurrency == data.rates.mainCurrency) userCurrency = data.rates.tickerCurrency - var price = data.rates.mainCurrencyPrices[userCurrency] - ethPriceHandle.innerHTML = `${price.symbol} ${price.truncPrice}${addCommas(price.roundPrice)}` - } catch (err) { - console.error("failed updating banner-price:", err) - } - - var finDelayDataHandle = document.getElementById("banner-fin-data") - finDelayHtml = ` -