Skip to content

Commit

Permalink
changed according to suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
mawe committed Dec 29, 2023
1 parent 0814cd9 commit c0c053f
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 205 deletions.
26 changes: 7 additions & 19 deletions handlers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,19 @@ func VerifyTurnstile(w http.ResponseWriter, r *http.Request) {

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)
utils.LogError(err, "Turnstile challenge failed", 0)
utils.ClearTurnstileVerifiedCookie(w)
http.Error(w, "Turnstile challenge failed", http.StatusBadRequest)
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)
validsince := time.Now().Unix()
utils.SessionStore.SCS.Put(r.Context(), "TURNSTILE::VALIDSINCE", validsince)
utils.SetTurnstileVerifiedCookie(w, int(utils.Config.Frontend.Turnstile.CookieMaxAge))
w.Write([]byte("Success"))
} else {
w.Write([]byte("Turnstile not enabled"))
}

}
124 changes: 37 additions & 87 deletions static/js/banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ function getCookie(cname) {
function updateBanner() {
waitForTurnstileToken(() => {
fetch("/latestState", { headers: { "X-TURNSTILE-TOKEN": window.turnstileToken } })
.then(function (res) {
return res.json()
})
.then(function (data) {
// always visible
var epochHandle = document.getElementById("banner-epoch-data")
.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)
Expand Down Expand Up @@ -92,89 +92,39 @@ function updateBanner() {
const icon = icons[i]
icon.setAttribute("data-original-title", `The last finalized epoch was ${data.finalityDelay} epochs ago.`)
}

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 = `<span class='currency-symbol'>${price.symbol} </span><span class='k-formatted-price'>${price.truncPrice}</span><span class='price'>${addCommas(price.roundPrice)}</span>`
} catch (err) {
console.error("failed updating banner-price:", err)
}

var finDelayDataHandle = document.getElementById("banner-fin-data")
finDelayHtml = `
<div id="banner-fin" class="info-item d-flex mr-3">
<div class="info-item-header mr-1 text-warning">
<span class="item-icon">
<i class="fas fa-exclamation-triangle" data-toggle="tooltip" title="" data-original-title="The last finalized epoch was ${data.finalityDelay} epochs ago."></i>
</span>
<span class="item-text">
Finality
</span>
</div>
<div class="info-item-body text-warning">
<span id="banner-fin-data">${data.finalityDelay}</span>
<i class="fas fa-exclamation-triangle item-text" data-toggle="tooltip" title="" data-original-title="The last finalized epoch was ${data.finalityDelay} epochs ago."></i>
</div>
</div>
`

if (!finDelayDataHandle && data.finalityDelay > 3 && !data.syncing) {
// create fin delay node
document.getElementById("banner-slot").insertAdjacentHTML("afterend", finDelayHtml)
$("#banner-fin i").each(function () {
$(this).tooltip("update")
})
} else if (finDelayDataHandle && data.finalityDelay > 3 && !data.syncing) {
// update fin delay node
finDelayDataHandle.textContent = data.finalityDelay
var icons = document.querySelectorAll("#banner-fin i")
for (let i = 0; i < icons.length; i++) {
const icon = icons[i]
icon.setAttribute("data-original-title", `The last finalized epoch was ${data.finalityDelay} epochs ago.`)
}
$("#banner-fin i").each(function () {
$(this).tooltip("update")
})
} else {
// delete fin delay node if it exists
let findDelayHandle = document.getElementById("banner-fin")
if (findDelayHandle) findDelayHandle.remove()
}
if (data.syncing) {
// remove fin delay if we are still syncing
let findDelayHandle = document.getElementById("banner-fin")
if (findDelayHandle) findDelayHandle.remove()

var bannerHandle = document.getElementById("banner-status")
if (!bannerHandle) {
var statusHtml = `
<a data-toggle="tooltip" title="The explorer is currently syncing with the network" id="banner-status" style="white-space: nowrap;" class="mr-2" href="/"><i class="fas fa-sync"></i> <span>|</span></a>
`
document.getElementById("banner-home").remove()
document.getElementById("banner-stats").insertAdjacentHTML("afterbegin", statusHtml)
}
} else {
// delete sync if it exists otherwise do nothing
var statusHandle = document.getElementById("banner-status")
if (statusHandle) {
var homeHtml = `
<a id="banner-home" style="white-space: nowrap;" class="mr-2" href="/"><i class="fas fa-home"></i> <span>|</span></a>
$("#banner-fin i").each(function () {
$(this).tooltip("update")
})
} else {
// delete fin delay node if it exists
let findDelayHandle = document.getElementById("banner-fin")
if (findDelayHandle) findDelayHandle.remove()
}
if (data.syncing) {
// remove fin delay if we are still syncing
let findDelayHandle = document.getElementById("banner-fin")
if (findDelayHandle) findDelayHandle.remove()

var bannerHandle = document.getElementById("banner-status")
if (!bannerHandle) {
var statusHtml = `
<a data-toggle="tooltip" title="The explorer is currently syncing with the network" id="banner-status" style="white-space: nowrap;" class="mr-2" href="/"><i class="fas fa-sync"></i> <span>|</span></a>
`
statusHandle.remove()
document.getElementById("banner-stats").insertAdjacentHTML("afterbegin", homeHtml)
}
document.getElementById("banner-home").remove()
document.getElementById("banner-stats").insertAdjacentHTML("afterbegin", statusHtml)
}
})
} else {
// delete sync if it exists otherwise do nothing
var statusHandle = document.getElementById("banner-status")
if (statusHandle) {
var homeHtml = `
<a id="banner-home" style="white-space: nowrap;" class="mr-2" href="/"><i class="fas fa-home"></i> <span>|</span></a>
`
statusHandle.remove()
document.getElementById("banner-stats").insertAdjacentHTML("afterbegin", homeHtml)
}
}
})
})
}

Expand Down
41 changes: 12 additions & 29 deletions static/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,26 +323,20 @@ function showProposedHistoryTable() {
waitForTurnstileToken(() => {
fetch("/dashboard/data/proposalshistory" + getValidatorQueryString(), {
method: "GET",
headers: { "X-TURNSTILE-TOKEN": window.turnstileToken }
}).then((res) => {
res.json().then(function (data) {
let proposedHistTableData = []
for (let item of data.data) {
proposedHistTableData.push([item[0], item[1], [item[2], item[3], item[4]]])
}
renderProposedHistoryTable(proposedHistTableData)
})
.then((res) => {
res.json().then(function (data) {
res
.json()
.then(function (data) {
let proposedHistTableData = []
for (let item of data) {
for (let item of data.data) {
proposedHistTableData.push([item[0], item[1], [item[2], item[3], item[4]]])
}
renderProposedHistoryTable(proposedHistTableData)
})
})
.finally(() => {
resetTurnstileToken()
})
.finally(() => {
resetTurnstileToken()
})
})
})
}
Expand Down Expand Up @@ -734,17 +728,6 @@ $(document).ready(function () {
}
create_validators_typeahead("input[aria-controls='validators']", "#validators")

function prepare(query, settings) {
settings.url = settings.url.replace("%QUERY", encodeURIComponent(query))
settings.beforeSend = function (jqXHR) {
jqXHR.setRequestHeader("X-TURNSTILE-TOKEN", window.turnstileToken)
}
settings.complete = function (jqXHR) {
resetTurnstileToken()
}
return settings
}

var timeWait = 0
var debounce = function (context, func) {
var timeout, result
Expand Down Expand Up @@ -797,7 +780,7 @@ $(document).ready(function () {
},
remote: {
url: "/search/validators_by_pubkey/%QUERY",
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhPubkey.remote.transport._get = debounce(bhPubkey.remote.transport, bhPubkey.remote.transport._get)
Expand All @@ -809,7 +792,7 @@ $(document).ready(function () {
},
remote: {
url: "/search/indexed_validators_by_eth1_addresses/%QUERY",
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhEth1Addresses.remote.transport._get = debounce(bhEth1Addresses.remote.transport, bhEth1Addresses.remote.transport._get)
Expand All @@ -821,7 +804,7 @@ $(document).ready(function () {
},
remote: {
url: "/search/indexed_validators_by_name/%QUERY",
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhName.remote.transport._get = debounce(bhName.remote.transport, bhName.remote.transport._get)
Expand All @@ -833,7 +816,7 @@ $(document).ready(function () {
},
remote: {
url: "/search/indexed_validators_by_graffiti/%QUERY",
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhGraffiti.remote.transport._get = debounce(bhGraffiti.remote.transport, bhGraffiti.remote.transport._get)
Expand Down
29 changes: 9 additions & 20 deletions static/js/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,6 @@ $(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip()
}

function prepare(query, settings) {
settings.url = settings.url.replace("%QUERY", encodeURIComponent(query))
settings.beforeSend = function (jqXHR) {
jqXHR.setRequestHeader("X-TURNSTILE-TOKEN", window.turnstileToken)
}
settings.complete = function () {
resetTurnstileToken()
}
return settings
}

// set maxParallelRequests to number of datasets queried in each search
// make sure this is set in every one bloodhound object
let requestNum = 10
Expand Down Expand Up @@ -301,7 +290,7 @@ $(document).ready(function () {
transform: function (data) {
return data?.address && data?.domain ? { data: { ...data } } : null
},
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhEns.remote.transport._get = debounce(bhEns.remote.transport, bhEns.remote.transport._get)
Expand All @@ -315,7 +304,7 @@ $(document).ready(function () {
remote: {
url: "/search/slots/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhSlots.remote.transport._get = debounce(bhSlots.remote.transport, bhSlots.remote.transport._get)
Expand All @@ -329,7 +318,7 @@ $(document).ready(function () {
remote: {
url: "/search/blocks/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhBlocks.remote.transport._get = debounce(bhBlocks.remote.transport, bhBlocks.remote.transport._get)
Expand All @@ -343,7 +332,7 @@ $(document).ready(function () {
remote: {
url: "/search/transactions/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhTransactions.remote.transport._get = debounce(bhTransactions.remote.transport, bhTransactions.remote.transport._get)
Expand All @@ -357,7 +346,7 @@ $(document).ready(function () {
remote: {
url: "/search/graffiti/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhGraffiti.remote.transport._get = debounce(bhGraffiti.remote.transport, bhGraffiti.remote.transport._get)
Expand All @@ -371,7 +360,7 @@ $(document).ready(function () {
remote: {
url: "/search/epochs/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhEpochs.remote.transport._get = debounce(bhEpochs.remote.transport, bhEpochs.remote.transport._get)
Expand All @@ -385,7 +374,7 @@ $(document).ready(function () {
remote: {
url: "/search/eth1_addresses/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhEth1Accounts.remote.transport._get = debounce(bhEth1Accounts.remote.transport, bhEth1Accounts.remote.transport._get)
Expand All @@ -399,7 +388,7 @@ $(document).ready(function () {
remote: {
url: "/search/count_indexed_validators_by_eth1_address/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhValidatorsByAddress.remote.transport._get = debounce(bhValidatorsByAddress.remote.transport, bhValidatorsByAddress.remote.transport._get)
Expand All @@ -413,7 +402,7 @@ $(document).ready(function () {
remote: {
url: "/search/validators_by_pubkey/%QUERY",
maxPendingRequests: requestNum,
prepare: prepare,
prepare: prepareBloodhound,
},
})
bhPubkey.remote.transport._get = debounce(bhPubkey.remote.transport, bhPubkey.remote.transport._get)
Expand Down
Loading

0 comments on commit c0c053f

Please sign in to comment.