forked from Shiloh/githaven
6433ba0ec3
Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
278 lines
4.6 KiB
Go
Vendored
278 lines
4.6 KiB
Go
Vendored
// Copyright 2013 Beego Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
// not use this file except in compliance with the License. You may obtain
|
|
// a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
// License for the specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package captcha
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"io"
|
|
"sync"
|
|
)
|
|
|
|
// siprng is PRNG based on SipHash-2-4.
|
|
type siprng struct {
|
|
mu sync.Mutex
|
|
k0, k1, ctr uint64
|
|
}
|
|
|
|
// siphash implements SipHash-2-4, accepting a uint64 as a message.
|
|
func siphash(k0, k1, m uint64) uint64 {
|
|
// Initialization.
|
|
v0 := k0 ^ 0x736f6d6570736575
|
|
v1 := k1 ^ 0x646f72616e646f6d
|
|
v2 := k0 ^ 0x6c7967656e657261
|
|
v3 := k1 ^ 0x7465646279746573
|
|
t := uint64(8) << 56
|
|
|
|
// Compression.
|
|
v3 ^= m
|
|
|
|
// Round 1.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
// Round 2.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
v0 ^= m
|
|
|
|
// Compress last block.
|
|
v3 ^= t
|
|
|
|
// Round 1.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
// Round 2.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
v0 ^= t
|
|
|
|
// Finalization.
|
|
v2 ^= 0xff
|
|
|
|
// Round 1.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
// Round 2.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
// Round 3.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
// Round 4.
|
|
v0 += v1
|
|
v1 = v1<<13 | v1>>(64-13)
|
|
v1 ^= v0
|
|
v0 = v0<<32 | v0>>(64-32)
|
|
|
|
v2 += v3
|
|
v3 = v3<<16 | v3>>(64-16)
|
|
v3 ^= v2
|
|
|
|
v0 += v3
|
|
v3 = v3<<21 | v3>>(64-21)
|
|
v3 ^= v0
|
|
|
|
v2 += v1
|
|
v1 = v1<<17 | v1>>(64-17)
|
|
v1 ^= v2
|
|
v2 = v2<<32 | v2>>(64-32)
|
|
|
|
return v0 ^ v1 ^ v2 ^ v3
|
|
}
|
|
|
|
// rekey sets a new PRNG key, which is read from crypto/rand.
|
|
func (p *siprng) rekey() {
|
|
var k [16]byte
|
|
if _, err := io.ReadFull(rand.Reader, k[:]); err != nil {
|
|
panic(err.Error())
|
|
}
|
|
p.k0 = binary.LittleEndian.Uint64(k[0:8])
|
|
p.k1 = binary.LittleEndian.Uint64(k[8:16])
|
|
p.ctr = 1
|
|
}
|
|
|
|
// Uint64 returns a new pseudorandom uint64.
|
|
// It rekeys PRNG on the first call and every 64 MB of generated data.
|
|
func (p *siprng) Uint64() uint64 {
|
|
p.mu.Lock()
|
|
if p.ctr == 0 || p.ctr > 8*1024*1024 {
|
|
p.rekey()
|
|
}
|
|
v := siphash(p.k0, p.k1, p.ctr)
|
|
p.ctr++
|
|
p.mu.Unlock()
|
|
return v
|
|
}
|
|
|
|
func (p *siprng) Int63() int64 {
|
|
return int64(p.Uint64() & 0x7fffffffffffffff)
|
|
}
|
|
|
|
func (p *siprng) Uint32() uint32 {
|
|
return uint32(p.Uint64())
|
|
}
|
|
|
|
func (p *siprng) Int31() int32 {
|
|
return int32(p.Uint32() & 0x7fffffff)
|
|
}
|
|
|
|
func (p *siprng) Intn(n int) int {
|
|
if n <= 0 {
|
|
panic("invalid argument to Intn")
|
|
}
|
|
if n <= 1<<31-1 {
|
|
return int(p.Int31n(int32(n)))
|
|
}
|
|
return int(p.Int63n(int64(n)))
|
|
}
|
|
|
|
func (p *siprng) Int63n(n int64) int64 {
|
|
if n <= 0 {
|
|
panic("invalid argument to Int63n")
|
|
}
|
|
max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
|
|
v := p.Int63()
|
|
for v > max {
|
|
v = p.Int63()
|
|
}
|
|
return v % n
|
|
}
|
|
|
|
func (p *siprng) Int31n(n int32) int32 {
|
|
if n <= 0 {
|
|
panic("invalid argument to Int31n")
|
|
}
|
|
max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
|
|
v := p.Int31()
|
|
for v > max {
|
|
v = p.Int31()
|
|
}
|
|
return v % n
|
|
}
|
|
|
|
func (p *siprng) Float64() float64 { return float64(p.Int63()) / (1 << 63) }
|