initial commit
This commit is contained in:
commit
b1eb82e1b9
|
@ -0,0 +1,366 @@
|
|||
# Created by https://www.gitignore.io/api/intellij,go,linux,osx,windows,node,python,executable,jetbrains+all,visualstudiocode,compressedarchive,git
|
||||
# Edit at https://www.gitignore.io/?templates=intellij,go,linux,osx,windows,node,python,executable,jetbrains+all,visualstudiocode,compressedarchive,git
|
||||
|
||||
### CompressedArchive ###
|
||||
|
||||
### Mostly from https://en.wikipedia.org/wiki/List_of_archive_formats
|
||||
|
||||
## Archiving and compression
|
||||
# Open source file format. Used by 7-Zip.
|
||||
*.7z
|
||||
# Mac OS X, restoration on different platforms is possible although not immediate Yes Based on 7z. Preserves Spotlight metadata, resource forks, owner/group information, dates and other data which would be otherwise lost with compression.
|
||||
*.s7z
|
||||
# Old archive versions only Proprietary format
|
||||
*.ace
|
||||
# A format that compresses and doubly encrypt the data (AES256 and CAS256) avoiding brute force attacks, also hide files in an AFA file. It has two ways to safeguard data integrity and subsequent repair of the file if has an error (repair with AstroA2P (online) or Astrotite (offline)).
|
||||
*.afa
|
||||
# A mainly Korean format designed for very large archives.
|
||||
*.alz
|
||||
# Android application package (variant of JAR file format).
|
||||
*.apk
|
||||
# ??
|
||||
*.arc
|
||||
# Originally DOS, now multiple
|
||||
*.arj
|
||||
# Open archive format, used by B1 Free Archiver (http://dev.b1.org/standard/archive-format.html)
|
||||
*.b1
|
||||
# Binary Archive with external header
|
||||
*.ba
|
||||
# Proprietary format from the ZipTV Compression Components
|
||||
*.bh
|
||||
# The Microsoft Windows native archive format, which is also used by many commercial installers such as InstallShield and WISE.
|
||||
*.cab
|
||||
# Originally DOS, now DOS and Windows Created by Yaakov Gringeler; released last in 2003 (Compressia 1.0.0.1 beta), now apparently defunct. Free trial of 30 days lets user create and extract archives; after that it is possible to extract, but not to create.
|
||||
*.car
|
||||
# Open source file format.
|
||||
*.cfs
|
||||
# Compact Pro archive, a common archiver used on Mac platforms until about Mac OS 7.5.x. Competed with StuffIt; now obsolete.
|
||||
*.cpt
|
||||
# Windows, Unix-like, Mac OS X Open source file format. Files are compressed individually with either gzip, bzip2 or lzo.
|
||||
*.dar
|
||||
# DiskDoubler Mac OS obsolete
|
||||
*.dd
|
||||
# ??
|
||||
*.dgc
|
||||
# Apple Disk Image upports "Internet-enabled" disk images, which, once downloaded, are automatically decompressed, mounted, have the contents extracted, and thrown away. Currently, Safari is the only browser that supports this form of extraction; however, the images can be manually extracted as well. This format can also be password-protected or encrypted with 128-bit or 256-bit AES encryption.
|
||||
*.dmg
|
||||
# Enterprise Java Archive archive
|
||||
*.ear
|
||||
# ETSoft compressed archive
|
||||
*.egg
|
||||
# The predecessor of DGCA.
|
||||
*.gca
|
||||
# Originally DOS Yes, but may be covered by patents DOS era format; uses arithmetic/Markov coding
|
||||
*.ha
|
||||
# MS Windows HKI
|
||||
*.hki
|
||||
# Produced by ICEOWS program. Excels at text file compression.
|
||||
*.ice
|
||||
# Java archive, compatible with ZIP files
|
||||
*.jar
|
||||
# Open sourced archiver with compression using the PAQ family of algorithms and optional encryption.
|
||||
*.kgb
|
||||
# Originally DOS, now multiple Multiple Yes The standard format on Amiga.
|
||||
*.lzh
|
||||
*.lha
|
||||
# Archiver originally used on The Amiga. Now copied by Microsoft to use in their .cab and .chm files.
|
||||
*.lzx
|
||||
# file format from NoGate Consultings, a rival from ARC-Compressor.
|
||||
*.pak
|
||||
# A disk image archive format that supports several compression methods as well as splitting the archive into smaller pieces.
|
||||
*.partimg
|
||||
# An experimental open source packager (http://mattmahoney.net/dc)
|
||||
*.paq*
|
||||
# Open source archiver supporting authenticated encryption, volume spanning, customizable object level and volume level integrity checks (form CRCs to SHA-512 and Whirlpool hashes), fast deflate based compression
|
||||
*.pea
|
||||
# The format from the PIM - a freeware compression tool by Ilia Muraviev. It uses an LZP-based compression algorithm with set of filters for executable, image and audio files.
|
||||
*.pim
|
||||
# PackIt Mac OS obsolete
|
||||
*.pit
|
||||
# Used for data in games written using the Quadruple D library for Delphi. Uses byte pair compression.
|
||||
*.qda
|
||||
# A proprietary archive format, second in popularity to .zip files.
|
||||
*.rar
|
||||
# The format from a commercial archiving package. Odd among commercial packages in that they focus on incorporating experimental algorithms with the highest possible compression (at the expense of speed and memory), such as PAQ, PPMD and PPMZ (PPMD with unlimited-length strings), as well as a proprietary algorithms.
|
||||
*.rk
|
||||
# Self Dissolving ARChive Commodore 64, Commodore 128 Commodore 64, Commodore 128 Yes SDAs refer to Self Dissolving ARC files, and are based on the Commodore 64 and Commodore 128 versions of ARC, originally written by Chris Smeets. While the files share the same extension, they are not compatible between platforms. That is, an SDA created on a Commodore 64 but run on a Commodore 128 in Commodore 128 mode will crash the machine, and vice versa. The intended successor to SDA is SFX.
|
||||
*.sda
|
||||
# A pre-Mac OS X Self-Extracting Archive format. StuffIt, Compact Pro, Disk Doubler and others could create .sea files, though the StuffIt versions were the most common.
|
||||
*.sea
|
||||
# Scifer Archive with internal header
|
||||
*.sen
|
||||
# Commodore 64, Commodore 128 SFX is a Self Extracting Archive which uses the LHArc compression algorithm. It was originally developed by Chris Smeets on the Commodore platform, and runs primarily using the CS-DOS extension for the Commodore 128. Unlike its predecessor SDA, SFX files will run on both the Commodore 64 and Commodore 128 regardless of which machine they were created on.
|
||||
*.sfx
|
||||
# An archive format designed for the Apple II series of computers. The canonical implementation is ShrinkIt, which can operate on disk images as well as files. Preferred compression algorithm is a combination of RLE and 12-bit LZW. Archives can be manipulated with the command-line NuLib tool, or the Windows-based CiderPress.
|
||||
*.shk
|
||||
# A compression format common on Apple Macintosh computers. The free StuffIt Expander is available for Windows and OS X.
|
||||
*.sit
|
||||
# The replacement for the .sit format that supports more compression methods, UNIX file permissions, long file names, very large files, more encryption options, data specific compressors (JPEG, Zip, PDF, 24-bit image, MP3). The free StuffIt Expander is available for Windows and OS X.
|
||||
*.sitx
|
||||
# A royalty-free compressing format
|
||||
*.sqx
|
||||
# The "tarball" format combines tar archives with a file-based compression scheme (usually gzip). Commonly used for source and binary distribution on Unix-like platforms, widely available elsewhere.
|
||||
*.tar.gz
|
||||
*.tgz
|
||||
*.tar.Z
|
||||
*.tar.bz2
|
||||
*.tbz2
|
||||
*.tar.lzma
|
||||
*.tlz
|
||||
# UltraCompressor 2.3 was developed to act as an alternative to the then popular PKZIP application. The main feature of the application is its ability to create large archives. This means that compressed archives with the UC2 file extension can hold almost 1 million files.
|
||||
*.uc
|
||||
*.uc0
|
||||
*.uc2
|
||||
*.ucn
|
||||
*.ur2
|
||||
*.ue2
|
||||
# Based on PAQ, RZM, CSC, CCM, and 7zip. The format consists of a PAQ, RZM, CSC, or CCM compressed file and a manifest with compression settings stored in a 7z archive.
|
||||
*.uca
|
||||
# A high compression rate archive format originally for DOS.
|
||||
*.uha
|
||||
# Web Application archive (Java-based web app)
|
||||
*.war
|
||||
# File-based disk image format developed to deploy Microsoft Windows.
|
||||
*.wim
|
||||
# XAR
|
||||
*.xar
|
||||
# Native format of the Open Source KiriKiri Visual Novel engine. Uses combination of block splitting and zlib compression. The filenames and pathes are stored in UTF-16 format. For integrity check, the Adler-32 hashsum is used. For many commercial games, the files are encrypted (and decoded on runtime) via so-called "cxdec" module, which implements xor-based encryption.
|
||||
*.xp3
|
||||
# Yamazaki zipper archive. Compression format used in DeepFreezer archiver utility created by Yamazaki Satoshi. Read and write support exists in TUGZip, IZArc and ZipZag
|
||||
*.yz1
|
||||
# The most widely used compression format on Microsoft Windows. Commonly used on Macintosh and Unix systems as well.
|
||||
*.zip
|
||||
*.zipx
|
||||
# application/x-zoo zoo Multiple Multiple Yes
|
||||
*.zoo
|
||||
# Journaling (append-only) archive format with rollback capability. Supports deduplication and incremental update based on last-modified dates. Multi-threaded. Compresses in LZ77, BWT, and context mixing formats. Open source.
|
||||
*.zpaq
|
||||
# Archiver with a compression algorithm based on the Burrows-Wheeler transform method.
|
||||
*.zz
|
||||
|
||||
|
||||
### Executable ###
|
||||
*.app
|
||||
*.bat
|
||||
*.cgi
|
||||
*.com
|
||||
*.exe
|
||||
*.gadget
|
||||
*.pif
|
||||
*.vb
|
||||
*.wsf
|
||||
|
||||
### Git ###
|
||||
# Created by git for backups. To disable backups in Git:
|
||||
# $ git config --global mergetool.keepBackup false
|
||||
*.orig
|
||||
|
||||
# Created by git when using merge tools for conflicts
|
||||
*.BACKUP.*
|
||||
*.BASE.*
|
||||
*.LOCAL.*
|
||||
*.REMOTE.*
|
||||
*_BACKUP_*.txt
|
||||
*_BASE_*.txt
|
||||
*_LOCAL_*.txt
|
||||
*_REMOTE_*.txt
|
||||
|
||||
### Go ###
|
||||
# Binaries for programs and plugins
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
### Go Patch ###
|
||||
/vendor/
|
||||
/Godeps/
|
||||
|
||||
### Intellij ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Intellij Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
.idea/sonarlint
|
||||
|
||||
### JetBrains+all Patch ###
|
||||
# Ignores the whole .idea folder and all .iml files
|
||||
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
|
||||
|
||||
.idea/
|
||||
|
||||
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
|
||||
|
||||
*.iml
|
||||
modules.xml
|
||||
.idea/misc.xml
|
||||
*.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
|
||||
### Linux ###
|
||||
*~
|
||||
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
### OSX ###
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
### VisualStudioCode Patch ###
|
||||
# Ignore all local history of files
|
||||
.history
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# End of https://www.gitignore.io/api/intellij,go,linux,osx,windows,node,python,executable,jetbrains+all,visualstudiocode,compressedarchive,git
|
||||
|
||||
go.sum
|
||||
/vendor
|
|
@ -0,0 +1,22 @@
|
|||
The BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2022 Sangbum Kim.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
||||
the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
|
||||
promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -0,0 +1,24 @@
|
|||
# eighty
|
||||
|
||||
[![GoDoc](https://godoc.org/amuz.es/src/go/eighty?status.png)](http://godoc.org/amuz.es/src/go/eighty)
|
||||
[![Go Report](https://goreportcard.com/badge/spi-ca/eighty)](http://goreportcard.com/report/spi-ca/eighty)
|
||||
|
||||
## Description
|
||||
|
||||
net.http and fasthttp related utility functions.
|
||||
|
||||
## Requirements
|
||||
|
||||
Go 1.5 or above.
|
||||
|
||||
## Installation
|
||||
|
||||
Run the following command to install the package:
|
||||
|
||||
```
|
||||
go get amuz.es/src/go/eighty
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
|
||||
TBD
|
|
@ -0,0 +1,76 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
httpCannotRedirectError = errors.New("this client cannot redirect")
|
||||
disableRedirect = func(_ *http.Request, _ []*http.Request) error {
|
||||
return httpCannotRedirectError
|
||||
}
|
||||
limitedRedirect = func(_ *http.Request, via []*http.Request) error {
|
||||
if len(via) >= 10 {
|
||||
return errors.New("stopped after 10 redirects")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
)
|
||||
|
||||
// Client is an http.Client with some tunable parameters.
|
||||
type Client interface {
|
||||
http.RoundTripper
|
||||
HttpClient() *http.Client
|
||||
roundTripper() http.RoundTripper
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
Get(url string) (resp *http.Response, err error)
|
||||
Head(url string) (resp *http.Response, err error)
|
||||
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
|
||||
PostForm(url string, data url.Values) (resp *http.Response, err error)
|
||||
}
|
||||
|
||||
type wrappedClient struct {
|
||||
http.Client
|
||||
}
|
||||
|
||||
func (cli *wrappedClient) HttpClient() *http.Client {
|
||||
return &cli.Client
|
||||
}
|
||||
func (cli *wrappedClient) roundTripper() http.RoundTripper {
|
||||
return cli.Transport
|
||||
}
|
||||
|
||||
func (cli *wrappedClient) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return cli.Client.Transport.RoundTrip(req)
|
||||
}
|
||||
|
||||
// NewClient returns a Client interface that has some tunable parameters.
|
||||
func NewClient(
|
||||
keepaliveDuration time.Duration,
|
||||
connectTimeout time.Duration,
|
||||
responseHeaderTimeout time.Duration,
|
||||
idleConnectionTimeout time.Duration,
|
||||
maxIdleConnections int,
|
||||
redirectSupport bool,
|
||||
serverName string,
|
||||
) Client {
|
||||
|
||||
var redirectChecker func(*http.Request, []*http.Request) error
|
||||
if redirectSupport {
|
||||
redirectChecker = limitedRedirect
|
||||
} else {
|
||||
redirectChecker = disableRedirect
|
||||
}
|
||||
|
||||
return &wrappedClient{
|
||||
Client: http.Client{
|
||||
Transport: NewRoundTripper(keepaliveDuration, connectTimeout, responseHeaderTimeout, idleConnectionTimeout, maxIdleConnections, serverName),
|
||||
CheckRedirect: redirectChecker,
|
||||
Jar: nil,
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// Package client provides some customizable http client.
|
||||
package client
|
|
@ -0,0 +1,53 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
userAgentHeader = "User-Agent"
|
||||
)
|
||||
|
||||
type predefinedHeaderTransport struct {
|
||||
useragentName string
|
||||
http.Transport
|
||||
}
|
||||
|
||||
func (pht *predefinedHeaderTransport) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
||||
req.Close = pht.DisableKeepAlives
|
||||
req.Header.Set(userAgentHeader, pht.useragentName)
|
||||
res, err = pht.Transport.RoundTrip(req)
|
||||
return
|
||||
}
|
||||
|
||||
// NewRoundTripper returns a http.RoundTripper that has some tunable parameters.
|
||||
func NewRoundTripper(
|
||||
keepaliveDuration time.Duration,
|
||||
connectTimeout time.Duration,
|
||||
responseHeaderTimeout time.Duration,
|
||||
idleConnectionTimeout time.Duration,
|
||||
maxIdleConnections int,
|
||||
serverName string,
|
||||
) http.RoundTripper {
|
||||
|
||||
keepaliveDisabled := keepaliveDuration == 0
|
||||
dialer := &net.Dialer{
|
||||
Timeout: connectTimeout,
|
||||
KeepAlive: keepaliveDuration,
|
||||
}
|
||||
|
||||
return &predefinedHeaderTransport{
|
||||
useragentName: serverName,
|
||||
Transport: http.Transport{
|
||||
DisableKeepAlives: keepaliveDisabled,
|
||||
DisableCompression: true,
|
||||
MaxIdleConnsPerHost: maxIdleConnections,
|
||||
DialContext: dialer.DialContext,
|
||||
MaxIdleConns: maxIdleConnections,
|
||||
IdleConnTimeout: idleConnectionTimeout,
|
||||
ResponseHeaderTimeout: responseHeaderTimeout,
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package eighty
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
oldTime = time.Unix(0, 0)
|
||||
)
|
||||
|
||||
// SetCookieValue is a useful cookie generator for net.http.
|
||||
func SetCookieValue(key string, expireDuration time.Duration, sessionSecure bool) func(http.ResponseWriter, string, string) {
|
||||
return func(w http.ResponseWriter, host, newCookieValue string) {
|
||||
if len(newCookieValue) > 0 {
|
||||
http.SetCookie(w,
|
||||
&http.Cookie{
|
||||
Name: key,
|
||||
Value: newCookieValue,
|
||||
Path: "/",
|
||||
Domain: host,
|
||||
Expires: time.Now().Add(expireDuration),
|
||||
Secure: sessionSecure,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
MaxAge: int(expireDuration.Seconds()),
|
||||
HttpOnly: true,
|
||||
},
|
||||
)
|
||||
} else {
|
||||
http.SetCookie(w,
|
||||
&http.Cookie{
|
||||
Name: key,
|
||||
Value: "_",
|
||||
Path: "/",
|
||||
Domain: host,
|
||||
Expires: oldTime,
|
||||
Secure: sessionSecure,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
MaxAge: -1,
|
||||
HttpOnly: true,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetCookieValue is the simple cookie getter.
|
||||
func GetCookieValue(req *http.Request, name string) (cookieValue string) {
|
||||
if cookie, _ := req.Cookie(name); cookie != nil {
|
||||
cookieValue = cookie.Value
|
||||
}
|
||||
return
|
||||
}
|
|
@ -0,0 +1,165 @@
|
|||
package eighty
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/misc/networking"
|
||||
"amuz.es/src/go/misc/strutil"
|
||||
"github.com/valyala/fasthttp"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
cookieWriterFasthttpImpl struct {
|
||||
key string
|
||||
expireDuration time.Duration
|
||||
sessionSecure bool
|
||||
}
|
||||
|
||||
CookieWriterFasthttp func(*fasthttp.Response, []byte, string)
|
||||
)
|
||||
|
||||
// NewCookieWriter is a useful cookie generator for fasthttp.
|
||||
func NewCookieWriter(key string, expireDuration time.Duration, secured bool) CookieWriterFasthttp {
|
||||
return (&cookieWriterFasthttpImpl{
|
||||
key: key,
|
||||
expireDuration: expireDuration,
|
||||
sessionSecure: secured,
|
||||
}).Write
|
||||
}
|
||||
func (cw *cookieWriterFasthttpImpl) validateCookiePathByte(b byte) bool {
|
||||
return 0x20 <= b && b < 0x7f && b != ';'
|
||||
}
|
||||
|
||||
func (cw *cookieWriterFasthttpImpl) validateCookieValueByte(b byte) bool {
|
||||
return 0x20 <= b && b < 0x7f && b != '"' && b != ';' && b != '\\'
|
||||
}
|
||||
|
||||
func (cw *cookieWriterFasthttpImpl) validateCookieDomain(v []byte) (valid bool) {
|
||||
// isCookieDomainName
|
||||
if len(v) == 0 {
|
||||
return false
|
||||
}
|
||||
if len(v) > 255 {
|
||||
return false
|
||||
}
|
||||
|
||||
if v[0] == '.' {
|
||||
// A cookie a domain attribute may start with a leading dot.
|
||||
v = v[1:]
|
||||
}
|
||||
var last byte = '.'
|
||||
partlen := 0
|
||||
for i := 0; i < len(v); i++ {
|
||||
c := v[i]
|
||||
switch {
|
||||
default:
|
||||
return false
|
||||
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
|
||||
// No '_' allowed here (in contrast to package net).
|
||||
valid = true
|
||||
partlen++
|
||||
case '0' <= c && c <= '9':
|
||||
// fine
|
||||
partlen++
|
||||
case c == '-':
|
||||
// Byte before dash cannot be dot.
|
||||
if last == '.' {
|
||||
return false
|
||||
}
|
||||
partlen++
|
||||
case c == '.':
|
||||
// Byte before dot cannot be dot, dash.
|
||||
if last == '.' || last == '-' {
|
||||
return false
|
||||
}
|
||||
if partlen > 63 || partlen == 0 {
|
||||
return false
|
||||
}
|
||||
partlen = 0
|
||||
}
|
||||
last = c
|
||||
}
|
||||
|
||||
if last == '-' || partlen > 63 {
|
||||
return false
|
||||
} else if valid {
|
||||
// isCookieDomainName
|
||||
return
|
||||
}
|
||||
// isCookieValidIp
|
||||
addr := networking.ParseIPv4(v)
|
||||
return addr != nil &&
|
||||
!addr.Equal(net.IPv4bcast) &&
|
||||
!addr.IsUnspecified() &&
|
||||
!addr.IsMulticast() &&
|
||||
!addr.IsLinkLocalUnicast()
|
||||
}
|
||||
|
||||
func (cw *cookieWriterFasthttpImpl) sanitizeOrWarn(fieldName string, valid func(byte) bool, v string) string {
|
||||
ok := true
|
||||
for i := 0; i < len(v); i++ {
|
||||
if valid(v[i]) {
|
||||
continue
|
||||
}
|
||||
log.Printf("invalid byte %q in %s; dropping invalid bytes", v[i], fieldName)
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
if ok {
|
||||
return v
|
||||
}
|
||||
var build strings.Builder
|
||||
for i := 0; i < len(v); i++ {
|
||||
if b := v[i]; valid(b) {
|
||||
build.WriteByte(b)
|
||||
}
|
||||
}
|
||||
return build.String()
|
||||
}
|
||||
|
||||
func (cw *cookieWriterFasthttpImpl) sanitizeCookiePath(v string) string {
|
||||
return cw.sanitizeOrWarn("Cookie.Path", cw.validateCookiePathByte, v)
|
||||
}
|
||||
|
||||
func (cw *cookieWriterFasthttpImpl) sanitizeCookieValue(v string) string {
|
||||
v = cw.sanitizeOrWarn("Cookie.Value", cw.validateCookieValueByte, v)
|
||||
if len(v) == 0 {
|
||||
return v
|
||||
}
|
||||
if strings.IndexByte(v, ' ') >= 0 || strings.IndexByte(v, ',') >= 0 {
|
||||
return `"` + v + `"`
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (cw *cookieWriterFasthttpImpl) Write(w *fasthttp.Response, host []byte, newCookieValue string) {
|
||||
cookie := fasthttp.AcquireCookie()
|
||||
defer fasthttp.ReleaseCookie(cookie)
|
||||
|
||||
cookie.SetKey(cw.key)
|
||||
cookie.SetPath(cw.sanitizeCookiePath("/"))
|
||||
|
||||
if len(host) > 0 {
|
||||
if cw.validateCookieDomain(host) {
|
||||
cookie.SetDomainBytes(host)
|
||||
} else {
|
||||
log.Printf("invalid Cookie.Domain %s; dropping domain attribute", strutil.B2S(host))
|
||||
}
|
||||
}
|
||||
cookie.SetSecure(cw.sessionSecure)
|
||||
cookie.SetSameSite(fasthttp.CookieSameSiteLaxMode)
|
||||
cookie.SetHTTPOnly(true)
|
||||
|
||||
if len(newCookieValue) > 0 {
|
||||
cookie.SetValue(cw.sanitizeCookieValue(newCookieValue))
|
||||
cookie.SetExpire(time.Now().Add(cw.expireDuration))
|
||||
cookie.SetMaxAge(int(cw.expireDuration.Seconds()))
|
||||
} else {
|
||||
cookie.SetValue("-")
|
||||
cookie.SetExpire(oldTime)
|
||||
cookie.SetMaxAge(-1)
|
||||
}
|
||||
w.Header.SetCookie(cookie)
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package eighty
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/misc/strutil"
|
||||
"github.com/valyala/fasthttp"
|
||||
"mime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Collection of predefined request header names.
|
||||
const (
|
||||
ContentTypeHeader = "Content-Type"
|
||||
ContentLengthHeader = "Content-Length"
|
||||
EtagHeader = "Etag"
|
||||
UserAgentHeader = "User-Agent"
|
||||
LastModifiedHeader = "Last-Modified"
|
||||
ExpiresHeader = "Expires"
|
||||
CacheControlHeader = "Cache-Control"
|
||||
IfModifiedSince = "If-Modified-Since"
|
||||
IfNoneMatch = "If-None-Match"
|
||||
Server = "Server"
|
||||
VaryHeader = "Vary"
|
||||
ForwardedForIPHeader = "X-Forwarded-For"
|
||||
)
|
||||
|
||||
// Collection of predefined response header names.
|
||||
const (
|
||||
RetryAfterHeader = "Retry-After"
|
||||
LocationHeader = "Location"
|
||||
FrameOptionHeader = "X-Frame-Options"
|
||||
ContentTypeOptionHeader = "X-Content-Type-Options"
|
||||
XssProtectionHeader = "X-XSS-Protection"
|
||||
XCsrfToken = "X-CSRF-Token"
|
||||
XForwardedProto = "X-Forwarded-Proto"
|
||||
)
|
||||
|
||||
// Collection of predefined cache header values.
|
||||
const (
|
||||
CacheControlNoCache = "private, no-cache, no-store, no-transform, max-age=0, must-revalidate"
|
||||
ExpiresNone = "0"
|
||||
)
|
||||
|
||||
// Collection of predefined mime types.
|
||||
var (
|
||||
HtmlContentUTF8Type = []string{"text/html; charset=utf-8"}
|
||||
HtmlContentType = []string{"text/html"}
|
||||
TextContentType = []string{"text/text"}
|
||||
TextContentUTF8Type = []string{"text/text; charset=utf-8"}
|
||||
UrlencodeContentUTF8Type = []string{"application/x-www-form-urlencoded; charset=utf-8"}
|
||||
UrlencodeContentType = []string{"application/x-www-form-urlencoded"}
|
||||
JsonContentUTF8Type = []string{"application/json; charset=utf-8"}
|
||||
JsonContentType = []string{"application/json"}
|
||||
)
|
||||
|
||||
// Collection of predefined CSRF header values.
|
||||
var (
|
||||
FrameOptionDeny = []string{"DENY"}
|
||||
FrameOptionSameOrigin = []string{"SAMEORIGIN"}
|
||||
|
||||
ContentTypeOptionNoSniffing = []string{"nosniff"}
|
||||
XssProtectionBlocking = []string{"1; mode=block"}
|
||||
)
|
||||
|
||||
// Collection of predefined http method names.
|
||||
var (
|
||||
MethodHEAD = []byte("HEAD")
|
||||
MethodGET = []byte("GET")
|
||||
MethodPOST = []byte("POST")
|
||||
)
|
||||
|
||||
// HasContentTypeFasthttp is a simple checker that checks if an incoming request satisfies a given mime-type.
|
||||
func HasContentTypeFasthttp(r *fasthttp.Request, mimetype string) bool {
|
||||
contentType := strutil.B2S(r.Header.ContentType())
|
||||
if len(contentType) == 0 {
|
||||
return mimetype == "application/octet-stream"
|
||||
}
|
||||
|
||||
for _, v := range strings.Split(contentType, ",") {
|
||||
t, _, err := mime.ParseMediaType(v)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if t == mimetype {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
module amuz.es/src/go/eighty
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
amuz.es/src/go/logging v1.0.0
|
||||
amuz.es/src/go/misc v1.0.1
|
||||
github.com/fasthttp/router v1.4.6
|
||||
github.com/valyala/fasthttp v1.34.0
|
||||
gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.15.0 // indirect
|
||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
|
||||
github.com/lestrrat-go/strftime v1.0.5 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20211223103454-d0aaa54c5899 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.21.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
||||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
// Package eighty is net.http and fasthttp related utility functions.
|
||||
package eighty // import "amuz.es/src/go/eighty"
|
|
@ -0,0 +1,25 @@
|
|||
package eighty
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/misc"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
const (
|
||||
jsonMimeType = "application/json; charset=utf-8"
|
||||
)
|
||||
|
||||
// DumpJSONFasthttp is a simple JSON renderer for the fasthttp.
|
||||
func DumpJSONFasthttp(ctx *fasthttp.RequestCtx, code int, serializable any) {
|
||||
stream := misc.JSONCodec.BorrowStream(nil)
|
||||
defer misc.JSONCodec.ReturnStream(stream)
|
||||
|
||||
if stream.WriteVal(serializable); stream.Error != nil {
|
||||
panic(stream.Error)
|
||||
} else if _, err := ctx.Write(stream.Buffer()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ctx.SetContentType(jsonMimeType)
|
||||
ctx.SetStatusCode(code)
|
||||
}
|
|
@ -0,0 +1,325 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/eighty"
|
||||
"amuz.es/src/go/eighty/routing"
|
||||
"amuz.es/src/go/logging"
|
||||
"amuz.es/src/go/misc/q"
|
||||
"amuz.es/src/go/misc/strutil"
|
||||
"bytes"
|
||||
"context"
|
||||
"github.com/valyala/fasthttp"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
dateFormat = "02/Jan/2006:15:04:05 -0700"
|
||||
)
|
||||
|
||||
type accessLogMiddleware struct {
|
||||
writer io.WriteCloser
|
||||
|
||||
apiUrlPrefix string
|
||||
|
||||
logInChan chan<- string
|
||||
logOutChan <-chan string
|
||||
|
||||
logger logging.Logger
|
||||
logWaiter sync.Mutex
|
||||
waitGroup sync.WaitGroup
|
||||
ctx context.Context
|
||||
closer func()
|
||||
|
||||
errorViewTemplateRenderer eighty.PageRenderer
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) Handle(h routing.Router) routing.Router {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
m.waitGroup.Add(1)
|
||||
defer m.waitGroup.Done()
|
||||
// access log 기록
|
||||
defer m.recordAccess()(ctx)
|
||||
// 내부 panic 해소
|
||||
defer m.handlePanic(ctx)
|
||||
m.writeBasicHeader(&ctx.Response)
|
||||
h(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// source returns a space-trimmed slice of the n'th line.
|
||||
func (m *accessLogMiddleware) source(buf *strings.Builder, lines [][]byte, n int) {
|
||||
n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
|
||||
if n < 0 || n >= len(lines) {
|
||||
buf.WriteString("???")
|
||||
} else {
|
||||
buf.Write(lines[n])
|
||||
}
|
||||
}
|
||||
|
||||
// function returns, if possible, the name of the function containing the PC.
|
||||
func (m *accessLogMiddleware) function(buf *strings.Builder, pc uintptr) {
|
||||
fn := runtime.FuncForPC(pc)
|
||||
if fn == nil {
|
||||
buf.WriteString("???")
|
||||
}
|
||||
name := fn.Name()
|
||||
// The name include the path name to the package, which is unnecessary
|
||||
// since the file name is already included. Plus, it has center dots.
|
||||
// That is, we see
|
||||
// runtime/debug.*T·ptrmethod
|
||||
// and want
|
||||
// *T.ptrmethod
|
||||
// Also the package path might contains dot (e.g. code.google.com/...),
|
||||
// so first eliminate the path prefix
|
||||
if lastslash := strings.LastIndexByte(name, '/'); lastslash >= 0 {
|
||||
name = name[lastslash+1:]
|
||||
}
|
||||
if period := strings.IndexByte(name, '.'); period >= 0 {
|
||||
name = name[period+1:]
|
||||
}
|
||||
buf.WriteString(strings.Replace(name, "·", ".", -1))
|
||||
}
|
||||
|
||||
// 라우팅 로직에서 panic이 발생 했을경우 해당 스택을 보여준다.
|
||||
// stack returns a nicely formated stack frame, skipping skip frames
|
||||
func (m *accessLogMiddleware) getStack(buf *strings.Builder, skip int) {
|
||||
// As we loop, we open files and read them. These variables record the currently
|
||||
// loaded file.
|
||||
var lines [][]byte
|
||||
var lastFile string
|
||||
for i := skip; ; i++ {
|
||||
// Skip the expected number of frames
|
||||
pc, file, line, ok := runtime.Caller(i)
|
||||
if !ok {
|
||||
break
|
||||
} else if i > skip {
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
if paths := strings.SplitN(file, "src/", 2); len(paths) == 1 {
|
||||
// Print this much at least. If we can't find the source, it won't show.
|
||||
//_, _ = fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
|
||||
buf.WriteString(file)
|
||||
} else if vendors := strings.SplitN(paths[1], "vendor/", 2); len(vendors) == 1 {
|
||||
// Print this much at least. If we can't find the source, it won't show.
|
||||
//_, _ = fmt.Fprintf(buf, "%s:%d (0x%x)\n", paths[1], line, pc)
|
||||
buf.WriteString(paths[1])
|
||||
} else {
|
||||
// Print this much at least. If we can't find the source, it won't show.
|
||||
//_, _ = fmt.Fprintf(buf, "%s:%d (0x%x)\n", vendors[1], line, pc)
|
||||
buf.WriteString(vendors[1])
|
||||
}
|
||||
buf.WriteByte(':')
|
||||
buf.WriteString(strconv.FormatInt(int64(line), 10))
|
||||
buf.WriteString(" (0x")
|
||||
buf.WriteString(strconv.FormatInt(int64(pc), 16))
|
||||
buf.WriteString(")\n")
|
||||
|
||||
// -----
|
||||
//_, _ = fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
|
||||
buf.WriteByte('\t')
|
||||
m.function(buf, pc)
|
||||
buf.WriteString(": ")
|
||||
if file == lastFile {
|
||||
buf.WriteString("???")
|
||||
} else if data, err := ioutil.ReadFile(file); err != nil {
|
||||
buf.WriteString("???")
|
||||
} else {
|
||||
lines = bytes.Split(data, []byte{'\n'})
|
||||
lastFile = file
|
||||
m.source(buf, lines, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) handlePanic(ctx *fasthttp.RequestCtx) {
|
||||
panicObj := recover()
|
||||
if panicObj == nil {
|
||||
return
|
||||
}
|
||||
errorType, err := eighty.WrapHandledError(panicObj)
|
||||
if err != nil {
|
||||
var buf strings.Builder
|
||||
buf.WriteString("PANIC! ")
|
||||
buf.WriteString(err.Error())
|
||||
buf.WriteString("\n--------\nREQUEST\n")
|
||||
_, _ = ctx.Request.WriteTo(&buf)
|
||||
buf.WriteString("\n--------\nSTACK\n")
|
||||
m.getStack(&buf, 3)
|
||||
buf.WriteString("\n--------")
|
||||
m.logger.Error(buf.String())
|
||||
}
|
||||
isAPI := bytes.HasPrefix(ctx.RequestURI(), []byte(m.apiUrlPrefix))
|
||||
if isAPI {
|
||||
errorType.RenderAPI(ctx, err)
|
||||
} else {
|
||||
errorType.RenderPage(ctx, m.errorViewTemplateRenderer, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) writeBasicHeader(w *fasthttp.Response) {
|
||||
w.Header.Set(eighty.FrameOptionHeader, eighty.FrameOptionSameOrigin[0])
|
||||
w.Header.Set(eighty.ContentTypeOptionHeader, eighty.ContentTypeOptionNoSniffing[0])
|
||||
w.Header.Set(eighty.XssProtectionHeader, eighty.XssProtectionBlocking[0])
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) recordAccess() routing.Router {
|
||||
now := time.Now()
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
var (
|
||||
dur = time.Since(now)
|
||||
builder strings.Builder
|
||||
)
|
||||
_, _ = builder.Write(m.remoteAddr(ctx.RemoteAddr(), &ctx.Request))
|
||||
_, _ = builder.WriteString(` - - [`)
|
||||
_, _ = builder.WriteString(now.Format(dateFormat))
|
||||
_, _ = builder.WriteString(`] "`)
|
||||
_, _ = builder.Write(ctx.Method())
|
||||
_ = builder.WriteByte(' ')
|
||||
_, _ = builder.Write(ctx.RequestURI())
|
||||
_ = builder.WriteByte(' ')
|
||||
if ctx.Request.Header.IsHTTP11() {
|
||||
_, _ = builder.WriteString("HTTP/1.1")
|
||||
} else {
|
||||
_, _ = builder.WriteString("HTTP/1.0")
|
||||
}
|
||||
_, _ = builder.WriteString(`" `)
|
||||
_, _ = builder.Write(strutil.FormatIntToBytes(ctx.Response.StatusCode()))
|
||||
_ = builder.WriteByte(' ')
|
||||
_, _ = builder.Write(strutil.FormatIntToBytes(ctx.Response.Header.ContentLength()))
|
||||
_, _ = builder.WriteString(` "`)
|
||||
_, _ = builder.Write(ctx.Request.Header.Referer())
|
||||
_, _ = builder.WriteString(`" "`)
|
||||
_, _ = builder.Write(ctx.Request.Header.UserAgent())
|
||||
_, _ = builder.WriteString(`" `)
|
||||
_, _ = builder.Write(strutil.FormatIntToBytes(int(dur.Nanoseconds() / time.Millisecond.Nanoseconds())))
|
||||
_ = builder.WriteByte(' ')
|
||||
_, _ = builder.Write(ctx.Request.Host())
|
||||
_ = builder.WriteByte('\n')
|
||||
|
||||
select {
|
||||
case <-m.ctx.Done():
|
||||
m.logger.Error("cannot accesslog record: ", builder.String())
|
||||
default:
|
||||
m.logInChan <- builder.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) Close() {
|
||||
defer m.writer.Close()
|
||||
m.closer()
|
||||
close(m.logInChan)
|
||||
m.logWaiter.Lock()
|
||||
defer m.logWaiter.Unlock()
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) lineByLineWriter() {
|
||||
var (
|
||||
ticker = time.NewTicker(200 * time.Millisecond)
|
||||
maxsz = 1024 * 1024
|
||||
sz = 0
|
||||
rcvsz = 0
|
||||
buf = make([]byte, maxsz)
|
||||
flusher = func() {
|
||||
if sz > 0 {
|
||||
if _, err := m.writer.Write(buf[:sz]); err != nil {
|
||||
m.logger.Error("cannot write accesslog chunk : ", err)
|
||||
}
|
||||
//reset
|
||||
sz = 0
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
m.logWaiter.Lock()
|
||||
defer func() {
|
||||
defer m.logWaiter.Unlock()
|
||||
ticker.Stop()
|
||||
flusher()
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case logItem, ok := <-m.logOutChan:
|
||||
//or do the next job
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
rcvsz = len(logItem)
|
||||
if maxsz < sz+rcvsz {
|
||||
flusher()
|
||||
}
|
||||
if rcvsz > 0 {
|
||||
// append
|
||||
copy(buf[sz:], logItem)
|
||||
sz += rcvsz
|
||||
}
|
||||
case <-ticker.C:
|
||||
// if deadline exceeded write
|
||||
flusher()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// strip port from addresses with hostname, ipv4 or ipv6
|
||||
func (m *accessLogMiddleware) stripPort(address string) string {
|
||||
if h, _, err := net.SplitHostPort(address); err == nil {
|
||||
return h
|
||||
}
|
||||
|
||||
return address
|
||||
}
|
||||
|
||||
// The remote address of the client. When the 'X-Forwarded-For'
|
||||
// header is set, then it is used instead.
|
||||
func (m *accessLogMiddleware) remoteAddr(remoteAddr net.Addr, r *fasthttp.Request) (ret []byte) {
|
||||
if ret = r.Header.Peek(eighty.ForwardedForIPHeader); ret == nil {
|
||||
ret = []byte(remoteAddr.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *accessLogMiddleware) remoteHost(remoteAddr net.Addr, r *fasthttp.Request) string {
|
||||
a := m.remoteAddr(remoteAddr, r)
|
||||
h := m.stripPort(strutil.B2S(a))
|
||||
if h != "" {
|
||||
return h
|
||||
}
|
||||
|
||||
return "-"
|
||||
}
|
||||
|
||||
// AccessLogMiddleware returns a routing.Middleware that handles error handling and access logging.
|
||||
func AccessLogMiddleware(
|
||||
apiUrlPrefix string,
|
||||
logWriter io.WriteCloser,
|
||||
templateRenderer eighty.PageRenderer,
|
||||
logger logging.Logger) (handler routing.Middleware, closer func(), err error) {
|
||||
ctx, canceler := context.WithCancel(context.Background())
|
||||
inchan, outchan := q.NewStringQueue()
|
||||
impl := &accessLogMiddleware{
|
||||
writer: logWriter,
|
||||
apiUrlPrefix: apiUrlPrefix,
|
||||
logInChan: inchan,
|
||||
logOutChan: outchan,
|
||||
logger: logger,
|
||||
ctx: ctx,
|
||||
errorViewTemplateRenderer: templateRenderer,
|
||||
}
|
||||
impl.closer = func() {
|
||||
if ctx.Err() == nil {
|
||||
canceler()
|
||||
}
|
||||
impl.waitGroup.Wait()
|
||||
}
|
||||
|
||||
go impl.lineByLineWriter()
|
||||
|
||||
return impl.Handle, impl.Close, nil
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/eighty"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CacheControlFunc returns a func(next http.Handler) http.Handler that handles cache control header.
|
||||
func CacheControlFunc(debug bool, startupTime time.Time) func(next http.Handler) http.Handler {
|
||||
baseVersion := startupTime.Unix()
|
||||
return func(next http.Handler) http.Handler {
|
||||
if debug {
|
||||
return next
|
||||
} else {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
defer next.ServeHTTP(w, r)
|
||||
if receivedVersion, err := strconv.ParseInt(r.URL.Query().Get("v"), 10, 64); err == nil && receivedVersion >= baseVersion {
|
||||
var cacheDuration int64 = 2592000
|
||||
if pushedDuration, err := strconv.ParseInt(r.URL.Query().Get("d"), 10, 64); err == nil && pushedDuration > cacheDuration {
|
||||
cacheDuration = pushedDuration
|
||||
}
|
||||
//add header
|
||||
cacheDurationStr := strconv.FormatInt(cacheDuration, 10)
|
||||
w.Header().Add(eighty.CacheControlHeader, "public, max-age="+cacheDurationStr)
|
||||
w.Header().Add(eighty.ExpiresHeader, cacheDurationStr)
|
||||
}
|
||||
w.Header().Add(eighty.VaryHeader, "User-Agent")
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/eighty"
|
||||
"amuz.es/src/go/eighty/routing"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"github.com/valyala/fasthttp"
|
||||
"gitlab.com/NebulousLabs/fastrand"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// the name of CSRF cookie
|
||||
CsrfCookieName = "csrf_token"
|
||||
|
||||
// the name of CSRF header
|
||||
csrfContextKey = "csrf"
|
||||
|
||||
csrfTokenLength = 32
|
||||
)
|
||||
|
||||
// reasons for CSRF check failures
|
||||
var (
|
||||
csrfSafeMethods = [][]byte{
|
||||
[]byte(http.MethodGet),
|
||||
[]byte(http.MethodHead),
|
||||
[]byte(http.MethodOptions),
|
||||
[]byte(http.MethodTrace),
|
||||
}
|
||||
mockCSRFRouterMiddleware = func(next routing.Router) routing.Router { return next }
|
||||
)
|
||||
|
||||
type (
|
||||
csrfToken struct {
|
||||
payload string
|
||||
}
|
||||
csrfMiddleware struct {
|
||||
writer eighty.CookieWriterFasthttp
|
||||
}
|
||||
)
|
||||
|
||||
// CSRFToken returns a CSRF token in the current request context.
|
||||
// If the token was not found in the request, zero-value returned.
|
||||
func CSRFToken(ctx *fasthttp.RequestCtx) (token string) {
|
||||
if ctx, ok := ctx.UserValue(csrfContextKey).(*csrfToken); ok && ctx != nil {
|
||||
token = ctx.payload
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Masks/unmasks the given data *in place*
|
||||
// with the given key
|
||||
// Slices must be of the same length, or csrfOneTimePad will panic
|
||||
func (m *csrfMiddleware) csrfOneTimePad(data, key []byte) {
|
||||
n := len(data)
|
||||
if n != len(key) {
|
||||
panic("Lengths of slices are not equal")
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
data[i] ^= key[i]
|
||||
}
|
||||
}
|
||||
|
||||
func (m *csrfMiddleware) isMethodSafe(s []byte) (safe bool) {
|
||||
// checks if the given slice contains the given string
|
||||
for _, v := range csrfSafeMethods {
|
||||
if safe = subtle.ConstantTimeCompare(v, s) == 1; safe {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// A token is generated by returning csrfTokenLength bytes
|
||||
// from crypto/rand
|
||||
func (m *csrfMiddleware) generateToken() []byte {
|
||||
bytes := make([]byte, csrfTokenLength)
|
||||
|
||||
if _, err := io.ReadFull(rand.Reader, bytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return bytes
|
||||
}
|
||||
|
||||
func (m *csrfMiddleware) tokenSerializer(data []byte, mask bool) (encoded string) {
|
||||
if !mask || len(data) != csrfTokenLength {
|
||||
return
|
||||
}
|
||||
|
||||
// csrfTokenLength*2 == len(enckey + token)
|
||||
result := make([]byte, 2*csrfTokenLength)
|
||||
// the first half of the result is the OTP
|
||||
// the second half is the masked token itself
|
||||
key := result[:csrfTokenLength]
|
||||
token := result[csrfTokenLength:]
|
||||
copy(token, data)
|
||||
|
||||
// generate the random token
|
||||
if _, err := io.ReadFull(fastrand.Reader, key); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
m.csrfOneTimePad(token, key)
|
||||
|
||||
return base64.StdEncoding.EncodeToString(result)
|
||||
}
|
||||
|
||||
func (m *csrfMiddleware) tokenDeserializer(data []byte, unmask bool) (decoded []byte) {
|
||||
payloadSize := base64.StdEncoding.DecodedLen(len(data))
|
||||
if payloadSize != csrfTokenLength*2 {
|
||||
return
|
||||
}
|
||||
|
||||
decoded = make([]byte, payloadSize)
|
||||
n, err := base64.StdEncoding.Decode(decoded, data)
|
||||
if err != nil || n < payloadSize {
|
||||
return nil
|
||||
}
|
||||
|
||||
decoded = decoded[:n]
|
||||
if unmask {
|
||||
key := decoded[:csrfTokenLength]
|
||||
decoded = decoded[csrfTokenLength:]
|
||||
m.csrfOneTimePad(decoded, key)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *csrfMiddleware) verifyToken(realToken, sentToken []byte) bool {
|
||||
realN := len(realToken)
|
||||
sentN := len(sentToken)
|
||||
if realN == csrfTokenLength && sentN == csrfTokenLength {
|
||||
return subtle.ConstantTimeCompare(realToken, sentToken) == 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *csrfMiddleware) Handle(h routing.Router) routing.Router {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
var (
|
||||
realToken []byte
|
||||
internalToken csrfToken
|
||||
tokenCreated bool
|
||||
)
|
||||
|
||||
if cookieValue := ctx.Request.Header.Cookie(CsrfCookieName); len(cookieValue) > 0 {
|
||||
realToken = m.tokenDeserializer(cookieValue, false)
|
||||
}
|
||||
tokenCreated = len(realToken) != csrfTokenLength
|
||||
if tokenCreated {
|
||||
realToken = m.generateToken()
|
||||
}
|
||||
internalToken = csrfToken{
|
||||
payload: m.tokenSerializer(realToken, true),
|
||||
}
|
||||
ctx.SetUserValue(csrfContextKey, &internalToken)
|
||||
|
||||
if m.isMethodSafe(ctx.Method()) {
|
||||
h(ctx)
|
||||
} else if sentToken := m.tokenDeserializer(ctx.Request.Header.Peek(eighty.XCsrfToken), true); !m.verifyToken(realToken, sentToken) {
|
||||
panic(eighty.HandledErrorBadRequest)
|
||||
} else {
|
||||
h(ctx)
|
||||
}
|
||||
ctx.Response.Header.Set(eighty.VaryHeader, "Cookie")
|
||||
if tokenCreated {
|
||||
m.writer(&ctx.Response, ctx.Host(), m.tokenSerializer(realToken, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CSRFFunc returns a routing.Middleware that handles CSRF validation logic.
|
||||
func CSRFFunc(isDebug bool, expire time.Duration, secure bool) (w routing.Middleware) {
|
||||
if isDebug {
|
||||
return mockCSRFRouterMiddleware
|
||||
}
|
||||
return (&csrfMiddleware{
|
||||
writer: eighty.NewCookieWriter(CsrfCookieName, expire, secure),
|
||||
}).Handle
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// Package middleware provides the collection of processing filters for the http request.
|
||||
package middleware
|
|
@ -0,0 +1,56 @@
|
|||
package internal
|
||||
|
||||
//
|
||||
//import (
|
||||
// "amuz.es/src/mercury/endpoint/misc"
|
||||
// "encoding/base64"
|
||||
// "github.com/tinylib/msgp/msgp"
|
||||
// "io"
|
||||
// "strings"
|
||||
//)
|
||||
//
|
||||
//type (
|
||||
// secretPayload map[string]string
|
||||
// SecretToolboxImpl struct {
|
||||
// EncryptWriter func(dst io.Writer) (io.WriteCloser, error)
|
||||
// DecryptReader func(src io.Reader) (io.Reader, error)
|
||||
// }
|
||||
//)
|
||||
//
|
||||
//// 인터페이스가 실제구현체랑 호환되는가
|
||||
//var _ misc.SecretToolbox = (*SecretToolboxImpl)(nil)
|
||||
//
|
||||
//func (tbx *SecretToolboxImpl) Encrypt(data map[string]string) (encrypted string, err error) {
|
||||
//
|
||||
// var (
|
||||
// payload secretPayload = data
|
||||
// stringWriter strings.Builder
|
||||
// )
|
||||
//
|
||||
// encryptor, err := tbx.EncryptWriter(base64.NewEncoder(base64.URLEncoding, &stringWriter))
|
||||
// if err != nil {
|
||||
// return
|
||||
// } else if err = msgp.Encode(encryptor, payload); err != nil {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// _ = encryptor.Close()
|
||||
//
|
||||
// encrypted = stringWriter.String()
|
||||
// return
|
||||
//}
|
||||
//func (tbx *SecretToolboxImpl) Decrypt(encrypted string) (data map[string]string, err error) {
|
||||
// var (
|
||||
// stringReader = strings.NewReader(encrypted)
|
||||
// payload = secretPayload{}
|
||||
// )
|
||||
//
|
||||
// decryptor, err := tbx.DecryptReader(base64.NewDecoder(base64.URLEncoding, stringReader))
|
||||
// if err != nil {
|
||||
// return
|
||||
// } else if err = msgp.Decode(decryptor, &payload); err != nil {
|
||||
// return
|
||||
// }
|
||||
// data = payload
|
||||
// return
|
||||
//}
|
|
@ -0,0 +1,134 @@
|
|||
package internal
|
||||
|
||||
//
|
||||
//import (
|
||||
// misc2 "amuz.es/src/mercury/endpoint/misc"
|
||||
// "amuz.es/src/mercury/service/models"
|
||||
// "amuz.es/src/go/misc"
|
||||
// "amuz.es/src/mercury/util/mycrypt"
|
||||
// "github.com/pkg/errors"
|
||||
// "io"
|
||||
// "log"
|
||||
// "reflect"
|
||||
//)
|
||||
//
|
||||
//// 인터페이스가 실제구현체랑 호환되는가
|
||||
//var _ misc2.Session = (*SessionImpl)(nil)
|
||||
//
|
||||
//type SessionImpl struct {
|
||||
// FBox mycrypt.SecretBox
|
||||
// FUser models.User
|
||||
// FToken misc.UUID
|
||||
// FData map[string]any
|
||||
// FSyncer func(newdata SessionImpl) error
|
||||
//}
|
||||
//
|
||||
//func (sess *SessionImpl) Id() misc.UUID { return sess.FToken }
|
||||
//func (sess *SessionImpl) User() models.User { return sess.FUser }
|
||||
//func (sess *SessionImpl) SetUser(user models.User) {
|
||||
// sess.Create()
|
||||
// sess.FUser = user
|
||||
//}
|
||||
//func (sess *SessionImpl) Delete() {
|
||||
// if !sess.FToken.IsZero() {
|
||||
// sess.FToken = misc.UUID{}
|
||||
// }
|
||||
//}
|
||||
//func (sess *SessionImpl) Create() {
|
||||
// if sess.FToken.IsZero() {
|
||||
// sess.FToken.Random()
|
||||
// }
|
||||
// if sess.FData == nil {
|
||||
// sess.FData = make(map[string]any)
|
||||
// }
|
||||
//}
|
||||
//func (sess *SessionImpl) Get(key string) any {
|
||||
// if sess.FData == nil {
|
||||
// return nil
|
||||
// }
|
||||
// data, ok := sess.FData[key]
|
||||
// if !ok {
|
||||
// return nil
|
||||
// } else {
|
||||
// return data
|
||||
// }
|
||||
//}
|
||||
//func (sess *SessionImpl) Set(key string, data any) {
|
||||