Compare commits
77 commits
d178a478f0
...
589a23198c
Author | SHA1 | Date | |
---|---|---|---|
589a23198c | |||
aef10ffbe2 | |||
498f299889 | |||
42442d63c6 | |||
f02e703943 | |||
4e062482d2 | |||
2442b6aa03 | |||
9d29c55307 | |||
1f378ad416 | |||
770c5a0b13 | |||
c91a228ab4 | |||
dc56b219b5 | |||
52774d95e7 | |||
10ad86d1e9 | |||
dc1524c3d4 | |||
62d56ef860 | |||
07d90bdd3a | |||
|
ef4367ace7 | ||
|
767826f049 | ||
|
44486ba59a | ||
|
c1ce2fed3d | ||
d7b8ffc9f2 | |||
f94fa6cec8 | |||
c553565f2c | |||
359caeab0f | |||
c6039e4ee4 | |||
a9c14cd74c | |||
5f63e0af64 | |||
b7be167dd7 | |||
63cc04e3b6 | |||
c02bce9841 | |||
f30ac50718 | |||
05cd9e3abe | |||
5a26dde806 | |||
a5349d587b | |||
93374ac6aa | |||
8696fc4d1c | |||
9a2ffd9898 | |||
05888973d9 | |||
93958de6a5 | |||
9bf69d2757 | |||
8a88613f89 | |||
849a4ff44e | |||
bc57218bc8 | |||
5c8928d8c9 | |||
095a676dea | |||
bd9b6996b8 | |||
6bf757a733 | |||
24e8d76460 | |||
7d2aa18df9 | |||
2545b7550d | |||
2c712cf793 | |||
bef9118149 | |||
9c8130c6df | |||
36f27f72aa | |||
54faeeda8a | |||
477d8ee34c | |||
c188b73a7f | |||
317f57519d | |||
48071ac77a | |||
234d88edd6 | |||
c00f777935 | |||
8d1295bd23 | |||
b225c24900 | |||
66d49d36a3 | |||
29ec2f78ac | |||
df8a04d57e | |||
3c9d90566f | |||
8ca8bc893e | |||
a1ffff4e5b | |||
a23fe965cc | |||
|
c285e117fe | ||
|
9adcb52174 | ||
|
eff4c30018 | ||
|
36e878c2ac | ||
|
46d13fcd3a | ||
f41c5fde07 |
53 changed files with 2171 additions and 423 deletions
7
.env
7
.env
|
@ -1,7 +1,6 @@
|
|||
PLAKKEN_INTERFACE=0.0.0.0
|
||||
PLAKKEN_PORT=3000
|
||||
PLAKKEN_REDIS_ADDR=localhost:6379
|
||||
PLAKKEN_LISTEN=:5000
|
||||
PLAKKEN_REDIS_ADDRESS=localhost:6379
|
||||
PLAKKEN_REDIS_USER=
|
||||
PLAKKEN_REDIS_PASSWORD=
|
||||
PLAKKEN_REDIS_DB=0
|
||||
PLAKKEN_REDIS_URL_LEN=5
|
||||
PLAKKEN_URL_LENGTH=5
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
PLAKKEN_HOST=0.0.0.0
|
||||
PLAKKEN_PORT=3000
|
||||
PLAKKEN_REDIS_ADDR=localhost:6379
|
||||
PLAKKEN_REDIS_USER=
|
||||
PLAKKEN_REDIS_PASSWORD=
|
||||
PLAKKEN_REDIS_DB=0
|
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,3 +1,7 @@
|
|||
# IDE specific
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
### Go ###
|
||||
*.exe
|
||||
*.exe~
|
||||
|
@ -18,3 +22,5 @@ plakken
|
|||
# Go workspace file
|
||||
go.work
|
||||
.idea/discord.xml
|
||||
|
||||
dist/
|
||||
|
|
25
.golangci.yaml
Normal file
25
.golangci.yaml
Normal file
|
@ -0,0 +1,25 @@
|
|||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
# Deprecated
|
||||
- varcheck
|
||||
- ifshort
|
||||
- interfacer
|
||||
- maligned
|
||||
- deadcode
|
||||
- scopelint
|
||||
- golint
|
||||
- structcheck
|
||||
- exhaustivestruct
|
||||
- nosnakecase
|
||||
# Too extremist/unusable
|
||||
- depguard
|
||||
- varnamelen
|
||||
- exhaustruct
|
||||
- wsl
|
||||
- contextcheck
|
||||
- wrapcheck
|
||||
linters-settings:
|
||||
lll:
|
||||
# Too short byt default
|
||||
line-length: 160
|
67
.goreleaser.yaml
Normal file
67
.goreleaser.yaml
Normal file
|
@ -0,0 +1,67 @@
|
|||
gitea_urls:
|
||||
api: https://git.gnous.eu/api/v1
|
||||
download: https://git.gnous.eu
|
||||
skip_tls_verify: false
|
||||
|
||||
before:
|
||||
hooks:
|
||||
# You may remove this if you don't use go modules.
|
||||
- go mod tidy
|
||||
|
||||
builds:
|
||||
-
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- linux
|
||||
- windows
|
||||
- darwin
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
|
||||
archives:
|
||||
- format: tar.gz
|
||||
format_overrides:
|
||||
-
|
||||
goos: windows
|
||||
format: zip
|
||||
|
||||
nfpms:
|
||||
-
|
||||
id: plakken
|
||||
package_name: plakken
|
||||
file_name_template: "{{ .ConventionalFileName }}"
|
||||
vendor: GnousEU
|
||||
homepage: https://git.gnous.eu/plakken/
|
||||
maintainer: GnousEU <contact@gnous.eu>
|
||||
description: A light paste server
|
||||
license: AGPLv3
|
||||
formats:
|
||||
- deb
|
||||
- rpm
|
||||
- archlinux
|
||||
umask: 0o002
|
||||
provides:
|
||||
- plakken
|
||||
suggests:
|
||||
- redis
|
||||
contents:
|
||||
- src: .env
|
||||
dst: /etc/plakken/env
|
||||
- src: plakken.service
|
||||
dst: /usr/lib/systemd/system/plakken.service
|
||||
scripts:
|
||||
preinstall: "deployement/goreleaser/preinstall.sh"
|
||||
preremove: "deployement/goreleaser/preremove.sh"
|
||||
postremove: "deployement/goreleaser/postremove.sh"
|
||||
|
||||
checksum:
|
||||
algorithm: sha256
|
||||
|
||||
changelog:
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- "^docs:"
|
||||
- "^test:"
|
6
.idea/.gitignore
vendored
6
.idea/.gitignore
vendored
|
@ -1,6 +0,0 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/plakken.iml" filepath="$PROJECT_DIR$/.idea/plakken.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
|
@ -1,10 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="highlight.js" level="application" />
|
||||
</component>
|
||||
</module>
|
|
@ -1,6 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
17
.woodpecker/build.yaml
Normal file
17
.woodpecker/build.yaml
Normal file
|
@ -0,0 +1,17 @@
|
|||
steps:
|
||||
- name: publish_image
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
repo: git.gnous.eu/${CI_REPO_OWNER}/plakken
|
||||
dockerfile: docker/Dockerfile
|
||||
platforms: linux/amd64,linux/arm64/v8
|
||||
registry: https://git.gnous.eu
|
||||
tag: ${CI_COMMIT}
|
||||
username:
|
||||
from_secret: docker_username
|
||||
password:
|
||||
from_secret: docker_password
|
||||
debug: true
|
||||
when:
|
||||
branch: ${CI_REPO_DEFAULT_BRANCH}
|
||||
event: push
|
11
.woodpecker/lint.yml
Normal file
11
.woodpecker/lint.yml
Normal file
|
@ -0,0 +1,11 @@
|
|||
steps:
|
||||
lint:
|
||||
image: golang:1.22
|
||||
commands:
|
||||
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||
- golangci-lint run
|
||||
when:
|
||||
- event: pull_request
|
||||
repo: gnouseu/plakken
|
||||
- event: push
|
||||
branch: main
|
25
.woodpecker/release.yaml
Normal file
25
.woodpecker/release.yaml
Normal file
|
@ -0,0 +1,25 @@
|
|||
steps:
|
||||
- name: Release
|
||||
image: golang:1.22
|
||||
commands:
|
||||
- go install github.com/goreleaser/goreleaser@latest
|
||||
- goreleaser release
|
||||
secrets: [ gitea_token ]
|
||||
when:
|
||||
event: tag
|
||||
- name: publish_image_tag
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
repo: git.gnous.eu/${CI_REPO_OWNER}/plakken
|
||||
dockerfile: docker/Dockerfile
|
||||
platforms: linux/amd64,linux/arm64/v8
|
||||
registry: https://git.gnous.eu
|
||||
tags:
|
||||
- ${CI_COMMIT_TAG##v} # Remove v from tag
|
||||
- stable
|
||||
username:
|
||||
from_secret: docker_username
|
||||
password:
|
||||
from_secret: docker_password
|
||||
when:
|
||||
event: tag
|
56
config.go
56
config.go
|
@ -1,56 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/joho/godotenv"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
host string
|
||||
port string
|
||||
redisAddr string
|
||||
redisUser string
|
||||
redisPassword string
|
||||
redisDB int
|
||||
urlLength int
|
||||
}
|
||||
|
||||
func getConfig() config {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
}
|
||||
|
||||
port := os.Getenv("PLAKKEN_PORT")
|
||||
redisAddr := os.Getenv("PLAKKEN_REDIS_ADDR")
|
||||
db := os.Getenv("PLAKKEN_REDIS_DB")
|
||||
uLen := os.Getenv("PLAKKEN_REDIS_URL_LEN")
|
||||
|
||||
if port == "" || redisAddr == "" {
|
||||
log.Fatal("Missing or invalid PLAKKEN_PORT or PLAKKEN_REDIS_ADDR")
|
||||
}
|
||||
|
||||
redisDB, err := strconv.Atoi(db)
|
||||
if err != nil {
|
||||
log.Fatal("Invalid PLAKKEN_REDIS_DB")
|
||||
}
|
||||
|
||||
urlLen, err := strconv.Atoi(uLen)
|
||||
if err != nil {
|
||||
log.Fatal("Invalid PLAKKEN_REDIS_URL_LEN")
|
||||
}
|
||||
|
||||
conf := config{
|
||||
host: os.Getenv("PLAKKEN_INTERFACE"),
|
||||
port: port,
|
||||
redisAddr: redisAddr,
|
||||
redisUser: os.Getenv("PLAKKEN_REDIS_USER"),
|
||||
redisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
|
||||
redisDB: redisDB,
|
||||
urlLength: urlLen,
|
||||
}
|
||||
|
||||
return conf
|
||||
}
|
52
db.go
52
db.go
|
@ -1,52 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func connectDB() *redis.Client {
|
||||
localDb := redis.NewClient(&redis.Options{
|
||||
Addr: currentConfig.redisAddr,
|
||||
Username: currentConfig.redisUser,
|
||||
Password: currentConfig.redisPassword,
|
||||
DB: currentConfig.redisDB,
|
||||
})
|
||||
return localDb
|
||||
}
|
||||
|
||||
func insertPaste(key string, content string, secret string, ttl time.Duration) {
|
||||
type dbSchema struct {
|
||||
content string
|
||||
secret string
|
||||
}
|
||||
|
||||
hash := dbSchema{
|
||||
content: content,
|
||||
secret: secret,
|
||||
}
|
||||
err := db.HSet(ctx, key, "content", hash.content)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
err = db.HSet(ctx, key, "secret", hash.secret)
|
||||
if ttl > -1 {
|
||||
db.Do(ctx, key, ttl)
|
||||
}
|
||||
}
|
||||
|
||||
func getContent(key string) string {
|
||||
content := db.HGet(ctx, key, "content").Val()
|
||||
return content
|
||||
}
|
||||
|
||||
func deleteContent(key string) {
|
||||
err := db.Del(ctx, key)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
1
deployement/goreleaser/postremove.sh
Normal file
1
deployement/goreleaser/postremove.sh
Normal file
|
@ -0,0 +1 @@
|
|||
userdel -r plakken
|
8
deployement/goreleaser/preinstall.sh
Normal file
8
deployement/goreleaser/preinstall.sh
Normal file
|
@ -0,0 +1,8 @@
|
|||
if ! getent passwd plakken > /dev/null; then
|
||||
useradd -r -d /var/lib/plakken -s /sbin/nologin -U plakken
|
||||
fi
|
||||
if ! test -d /var/lib/plakken; then
|
||||
mkdir -p /var/lib/plakken
|
||||
chmod 0750 /var/lib/plakken
|
||||
chown -R plakken:plakken /var/lib/plakken
|
||||
fi
|
1
deployement/goreleaser/preremove.sh
Normal file
1
deployement/goreleaser/preremove.sh
Normal file
|
@ -0,0 +1 @@
|
|||
systemctl stop plakken
|
28
docker/Dockerfile
Normal file
28
docker/Dockerfile
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Build
|
||||
FROM golang:1.22 AS build
|
||||
LABEL authors="gnousEU"
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY main.go ./
|
||||
COPY internal/ ./internal
|
||||
COPY static/ ./static
|
||||
COPY templates/ ./templates
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-w -s" # Enable static binary, target Linux, remove debug information and strip binary
|
||||
|
||||
# Copy to our image
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=build /build/plakken ./
|
||||
|
||||
ENV PLAKKEN_LISTEN ":3000"
|
||||
|
||||
EXPOSE 3000/tcp
|
||||
|
||||
ENTRYPOINT ["/app/plakken"]
|
31
docker/docker-compose.dev.yaml
Normal file
31
docker/docker-compose.dev.yaml
Normal file
|
@ -0,0 +1,31 @@
|
|||
version: "3"
|
||||
|
||||
networks:
|
||||
plakken:
|
||||
external: false
|
||||
|
||||
services:
|
||||
server:
|
||||
build:
|
||||
context: ../
|
||||
dockerfile: docker/Dockerfile
|
||||
restart: always
|
||||
container_name: plakken
|
||||
networks:
|
||||
- plakken
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- PLAKKEN_REDIS_ADDRESS=redis:6379
|
||||
- POSTGRES_PASSWORD=gitea
|
||||
- PLAKKEN_REDIS_DB=0
|
||||
- PLAKKEN_URL_LENGTH=5
|
||||
depends_on:
|
||||
- redis
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
restart: always
|
||||
healthcheck:
|
||||
test: [ "CMD", "redis-cli", "ping" ]
|
||||
networks:
|
||||
- plakken
|
35
docker/docker-compose.yaml
Normal file
35
docker/docker-compose.yaml
Normal file
|
@ -0,0 +1,35 @@
|
|||
version: "3"
|
||||
|
||||
networks:
|
||||
plakken:
|
||||
external: false
|
||||
|
||||
volumes:
|
||||
redis:
|
||||
driver: local
|
||||
|
||||
services:
|
||||
server:
|
||||
image: git.gnous.eu/gnouseu/plakken:latest
|
||||
restart: always
|
||||
container_name: plakken
|
||||
read_only: true
|
||||
networks:
|
||||
- plakken
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- PLAKKEN_REDIS_ADDRESS=redis:6379
|
||||
- PLAKKEN_REDIS_DB=0
|
||||
- PLAKKEN_URL_LENGTH=5
|
||||
depends_on:
|
||||
- redis
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
restart: always
|
||||
healthcheck:
|
||||
test: [ "CMD", "redis-cli", "ping" ]
|
||||
networks:
|
||||
- plakken
|
||||
volumes:
|
||||
- redis:/data
|
5
front/_colors.scss
Normal file
5
front/_colors.scss
Normal file
|
@ -0,0 +1,5 @@
|
|||
$accent: #d30f45;
|
||||
$background: #0f0f0f;
|
||||
$border: #363636;
|
||||
$text: #e5e5e7;
|
||||
$placeholder: #888;
|
0
front/_inputs.scss
Normal file
0
front/_inputs.scss
Normal file
23
front/_misc.scss
Normal file
23
front/_misc.scss
Normal file
|
@ -0,0 +1,23 @@
|
|||
@use 'colors' as *;
|
||||
|
||||
::selection {
|
||||
background-color: $accent;
|
||||
color: $text;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar {
|
||||
width: 9px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background-color: $background;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background-color: #444;
|
||||
border-radius: 9px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background-color: $text;
|
||||
}
|
65
front/_recents.scss
Normal file
65
front/_recents.scss
Normal file
|
@ -0,0 +1,65 @@
|
|||
@use 'colors' as *;
|
||||
|
||||
section#recent-plaks {
|
||||
bottom: 1rem;
|
||||
display: flex;
|
||||
flex-flow: column wrap;
|
||||
gap: 9px;
|
||||
position: absolute;
|
||||
right: 1rem;
|
||||
transition: .15s ease-in-out;
|
||||
user-select: none;
|
||||
|
||||
.title {
|
||||
align-items: center;
|
||||
gap: 18px;
|
||||
transition: .15s ease-in-out;
|
||||
|
||||
&:hover {
|
||||
border-color: #555;
|
||||
}
|
||||
}
|
||||
|
||||
.recent-plak {
|
||||
align-items: center;
|
||||
border: 3px solid $border;
|
||||
font-weight: 500;
|
||||
justify-content: space-between;
|
||||
outline: none;
|
||||
padding: 8px 18px;
|
||||
text-decoration: none;
|
||||
transition: .15s ease-in-out;
|
||||
width: 12rem;
|
||||
|
||||
&:focus-visible {
|
||||
border: 2px solid #bbb;
|
||||
}
|
||||
|
||||
svg {
|
||||
border-radius: 15px;
|
||||
cursor: pointer;
|
||||
height: 22px;
|
||||
padding: 5px;
|
||||
stroke: $text;
|
||||
stroke-width: 2.25;
|
||||
width: 22px;
|
||||
transition: .1s ease-in-out;
|
||||
|
||||
&:hover {
|
||||
background-color: $border;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.5rem;
|
||||
margin: 0;
|
||||
transition: .15s ease-in-out;
|
||||
width: fit-content;
|
||||
}
|
60
front/index.html
Normal file
60
front/index.html
Normal file
|
@ -0,0 +1,60 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="Plakken, paste, pastebin" name="keywords">
|
||||
<meta content="Plakken is a simple open-source paste service where you can store text for a set period of time."
|
||||
name="description">
|
||||
<meta content="Gnous EU" name="author">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<link href="/static/favicon.svg" rel="icon">
|
||||
<title>New paste</title>
|
||||
<link href="style.css" rel="stylesheet">
|
||||
<script async src="../static/app.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form action="/create/" class="fr" method="post">
|
||||
<div id="line-numbers">1</div>
|
||||
<label for="content"><textarea autofocus id="content" name="content"
|
||||
placeholder="Type your paste here"></textarea></label>
|
||||
|
||||
<div class="menu fr">
|
||||
<label for="filename"><input id="filename" name="filename" placeholder="Filename" type="text"></label>
|
||||
<!--<label for="password"><input id="password" placeholder="Password" type="password"></label>-->
|
||||
<label for="exp"><input id="exp" name="exp" placeholder="Expiration (1d1h1m1s)" type="text"></label>
|
||||
<label for="type">
|
||||
<select id="type" name="type">zz
|
||||
<option value="c">C/C++</option>
|
||||
<option value="csharp">C#</option>
|
||||
<option value="css">CSS</option>
|
||||
<option value="go">Go</option>
|
||||
<option value="java">Java</option>
|
||||
<option value="js">Javascript</option>
|
||||
<option value="html">HTML</option>
|
||||
<option selected value="pt">Plaintext</option>
|
||||
<option value="py">Python</option>
|
||||
<option value="rb">Ruby</option>
|
||||
<option value="rs">Rust</option>
|
||||
<option value="sh">Shell</option>
|
||||
<option value="sql">SQL</option>
|
||||
<option value="ts">Typescript</option>
|
||||
<option value="xml">XML</option>
|
||||
<option value="yml">YAML</option>
|
||||
</select>
|
||||
</label>
|
||||
<button title="Save plak" type="submit">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
<section id="recent-plaks">
|
||||
<div class="title fr">
|
||||
<h3>Recent plaks</h3>
|
||||
<svg height="32" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24" width="32">
|
||||
<polyline points="6 9 12 15 18 9"></polyline>
|
||||
</svg>
|
||||
</div>
|
||||
</section>
|
||||
</body>
|
||||
|
||||
</html>
|
202
front/style.css
Normal file
202
front/style.css
Normal file
|
@ -0,0 +1,202 @@
|
|||
section#recent-plaks {
|
||||
bottom: 1rem;
|
||||
display: flex;
|
||||
flex-flow: column wrap;
|
||||
gap: 9px;
|
||||
position: absolute;
|
||||
right: 1rem;
|
||||
transition: 0.15s ease-in-out;
|
||||
user-select: none;
|
||||
}
|
||||
section#recent-plaks .title {
|
||||
align-items: center;
|
||||
gap: 18px;
|
||||
transition: 0.15s ease-in-out;
|
||||
}
|
||||
section#recent-plaks .title:hover {
|
||||
border-color: #555;
|
||||
}
|
||||
section#recent-plaks .recent-plak {
|
||||
align-items: center;
|
||||
border: 3px solid #363636;
|
||||
font-weight: 500;
|
||||
justify-content: space-between;
|
||||
outline: none;
|
||||
padding: 8px 18px;
|
||||
text-decoration: none;
|
||||
transition: 0.15s ease-in-out;
|
||||
width: 12rem;
|
||||
}
|
||||
section#recent-plaks .recent-plak:focus-visible {
|
||||
border: 2px solid #bbb;
|
||||
}
|
||||
section#recent-plaks .recent-plak svg {
|
||||
border-radius: 15px;
|
||||
cursor: pointer;
|
||||
height: 22px;
|
||||
padding: 5px;
|
||||
stroke: #e5e5e7;
|
||||
stroke-width: 2.25;
|
||||
width: 22px;
|
||||
transition: 0.1s ease-in-out;
|
||||
}
|
||||
section#recent-plaks .recent-plak svg:hover {
|
||||
background-color: #363636;
|
||||
}
|
||||
section#recent-plaks a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.5rem;
|
||||
margin: 0;
|
||||
transition: 0.15s ease-in-out;
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
::selection {
|
||||
background-color: #d30f45;
|
||||
color: #e5e5e7;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar {
|
||||
width: 9px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background-color: #0f0f0f;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background-color: #444;
|
||||
border-radius: 9px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background-color: #e5e5e7;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "Inter";
|
||||
font-style: normal;
|
||||
font-weight: 400 700;
|
||||
font-display: swap;
|
||||
src: url(inter.woff2) format("woff2");
|
||||
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "JetBrains Mono";
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url(jetbrainsmono.woff2) format("woff2");
|
||||
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||
}
|
||||
body {
|
||||
background-color: #0f0f0f;
|
||||
color: #e5e5e7;
|
||||
font: 400 16px/2 "Inter", "system-ui", sans-serif;
|
||||
margin: 0;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
.fr {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
}
|
||||
|
||||
#line-numbers,
|
||||
#content {
|
||||
font: 400 14px/1.6 "JetBrains Mono", monospace;
|
||||
}
|
||||
|
||||
#line-numbers {
|
||||
color: #888;
|
||||
padding: 16px 1px;
|
||||
text-align: center;
|
||||
white-space: pre;
|
||||
width: 30px;
|
||||
}
|
||||
|
||||
#content {
|
||||
color: #e5e5e7;
|
||||
height: 100%;
|
||||
min-height: 90vh;
|
||||
padding: 16px 16px 0 16px;
|
||||
width: calc(100vw - 65px);
|
||||
}
|
||||
|
||||
.menu {
|
||||
gap: 18px;
|
||||
position: fixed;
|
||||
right: 16px;
|
||||
top: 16px;
|
||||
}
|
||||
.menu svg {
|
||||
cursor: pointer;
|
||||
height: 24px;
|
||||
fill: none;
|
||||
margin-bottom: -6px;
|
||||
stroke: #e5e5e7;
|
||||
stroke-width: 2;
|
||||
width: 24px;
|
||||
}
|
||||
|
||||
button,
|
||||
input,
|
||||
select {
|
||||
background-color: #0f0f0f;
|
||||
border: 2px solid #363636;
|
||||
color: #e5e5e7;
|
||||
font: 500 14px/2 "Inter", "system-ui", sans-serif;
|
||||
max-width: 145px;
|
||||
outline: none;
|
||||
padding: 3px 10px;
|
||||
transition: border 0.15s ease;
|
||||
width: min-content;
|
||||
}
|
||||
button::placeholder,
|
||||
input::placeholder,
|
||||
select::placeholder {
|
||||
color: #888;
|
||||
}
|
||||
button:hover,
|
||||
input:hover,
|
||||
select:hover {
|
||||
border-color: #777;
|
||||
}
|
||||
button:focus-visible,
|
||||
input:focus-visible,
|
||||
select:focus-visible {
|
||||
border: 2px solid #bbb;
|
||||
}
|
||||
|
||||
select {
|
||||
padding: 8px 10px;
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
select:focus {
|
||||
background-color: #0f0f0f;
|
||||
transition: none;
|
||||
}
|
||||
|
||||
option {
|
||||
background-color: #0f0f0f;
|
||||
color: #e5e5e7;
|
||||
}
|
||||
option:focus {
|
||||
background-color: #d30f45;
|
||||
color: #0f0f0f;
|
||||
}
|
||||
|
||||
textarea {
|
||||
background-color: inherit;
|
||||
border: none;
|
||||
margin: 0;
|
||||
outline: none;
|
||||
resize: none;
|
||||
}
|
||||
|
||||
/*# sourceMappingURL=style.css.map */
|
130
front/style.scss
Normal file
130
front/style.scss
Normal file
|
@ -0,0 +1,130 @@
|
|||
@use 'colors' as *;
|
||||
|
||||
@use 'inputs';
|
||||
@use 'recents';
|
||||
@use 'misc';
|
||||
|
||||
@font-face {
|
||||
font-family: 'Inter';
|
||||
font-style: normal;
|
||||
font-weight: 400 700;
|
||||
font-display: swap;
|
||||
src: url(inter.woff2) format('woff2');
|
||||
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: 'JetBrains Mono';
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url(jetbrainsmono.woff2) format('woff2');
|
||||
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||
}
|
||||
|
||||
|
||||
body {
|
||||
background-color: $background;
|
||||
color: $text;
|
||||
font: 400 16px/2 "Inter", "system-ui", sans-serif;
|
||||
margin: 0;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
.fr {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
}
|
||||
|
||||
#line-numbers,
|
||||
#content {
|
||||
font: 400 14px/1.6 "JetBrains Mono", monospace;
|
||||
}
|
||||
|
||||
#line-numbers {
|
||||
color: $placeholder;
|
||||
padding: 16px 1px;
|
||||
text-align: center;
|
||||
white-space: pre;
|
||||
width: 30px;
|
||||
}
|
||||
|
||||
#content {
|
||||
color: $text;
|
||||
height: 100%;
|
||||
min-height: 90vh;
|
||||
padding: 16px 16px 0 16px;
|
||||
width: calc(100vw - 65px);
|
||||
}
|
||||
|
||||
.menu {
|
||||
gap: 18px;
|
||||
position: fixed;
|
||||
right: 16px;
|
||||
top: 16px;
|
||||
|
||||
svg {
|
||||
cursor: pointer;
|
||||
height: 24px;
|
||||
fill: none;
|
||||
margin-bottom: -6px;
|
||||
stroke: $text;
|
||||
stroke-width: 2;
|
||||
width: 24px;
|
||||
}
|
||||
}
|
||||
|
||||
button,
|
||||
input,
|
||||
select {
|
||||
background-color: $background;
|
||||
border: 2px solid $border;
|
||||
color: $text;
|
||||
font: 500 14px/2 "Inter", "system-ui", sans-serif;
|
||||
max-width: 145px;
|
||||
outline: none;
|
||||
padding: 3px 10px;
|
||||
transition: border .15s ease;
|
||||
width: min-content;
|
||||
|
||||
|
||||
&::placeholder {
|
||||
color: $placeholder;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
border-color: #777;
|
||||
}
|
||||
|
||||
&:focus-visible {
|
||||
border: 2px solid #bbb;
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
padding: 8px 10px;
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
select:focus {
|
||||
background-color: $background;
|
||||
transition: none;
|
||||
}
|
||||
|
||||
option {
|
||||
background-color: $background;
|
||||
color: $text;
|
||||
|
||||
&:focus {
|
||||
background-color: $accent;
|
||||
color: $background;
|
||||
}
|
||||
}
|
||||
|
||||
textarea {
|
||||
background-color: inherit;
|
||||
border: none;
|
||||
margin: 0;
|
||||
outline: none;
|
||||
resize: none;
|
||||
}
|
10
go.mod
10
go.mod
|
@ -1,13 +1,15 @@
|
|||
module plakken
|
||||
module git.gnous.eu/gnouseu/plakken
|
||||
|
||||
go 1.21
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/redis/go-redis/v9 v9.2.1
|
||||
github.com/redis/go-redis/v9 v9.5.1
|
||||
golang.org/x/crypto v0.22.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
)
|
||||
|
|
16
go.sum
16
go.sum
|
@ -1,8 +1,16 @@
|
|||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
||||
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
|
||||
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
|
|
63
internal/config/config.go
Normal file
63
internal/config/config.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/utils"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// InitConfig Structure for program initialisation.
|
||||
type InitConfig struct {
|
||||
ListenAddress string
|
||||
RedisAddress string
|
||||
RedisUser string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
URLLength uint8
|
||||
}
|
||||
|
||||
// GetConfig Initialise configuration form .env.
|
||||
func GetConfig() InitConfig {
|
||||
if utils.FileExist(".env") {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
listenAddress := os.Getenv("PLAKKEN_LISTEN")
|
||||
redisAddress := os.Getenv("PLAKKEN_REDIS_ADDRESS")
|
||||
db := os.Getenv("PLAKKEN_REDIS_DB")
|
||||
uLen := os.Getenv("PLAKKEN_URL_LENGTH")
|
||||
|
||||
if listenAddress == "" || redisAddress == "" {
|
||||
log.Fatal("Missing or invalid listenAddress or PLAKKEN_REDIS_ADDRESS")
|
||||
}
|
||||
|
||||
redisDB, err := strconv.Atoi(db)
|
||||
if err != nil {
|
||||
log.Fatal("Invalid PLAKKEN_REDIS_DB")
|
||||
}
|
||||
|
||||
urlLength, err := strconv.Atoi(uLen)
|
||||
if err != nil {
|
||||
log.Fatal("Invalid PLAKKEN_URL_LENGTH")
|
||||
}
|
||||
|
||||
if urlLength > constant.MaxURLLength {
|
||||
log.Fatal("PLAKKEN_URL_LENGTH cannot be greater than 255")
|
||||
}
|
||||
|
||||
return InitConfig{
|
||||
ListenAddress: listenAddress,
|
||||
RedisAddress: redisAddress,
|
||||
RedisUser: os.Getenv("PLAKKEN_REDIS_USER"),
|
||||
RedisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
|
||||
RedisDB: redisDB,
|
||||
URLLength: uint8(urlLength),
|
||||
}
|
||||
}
|
18
internal/constant/constants.go
Normal file
18
internal/constant/constants.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package constant
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
HTTPTimeout = 3 * time.Second
|
||||
ExpirationCurlCreate = 604800 * time.Second // Second in one week
|
||||
TokenLength = 32
|
||||
ArgonSaltSize = 16
|
||||
ArgonMemory = 64 * 1024
|
||||
ArgonThreads = 4
|
||||
ArgonKeyLength = 32
|
||||
ArgonIterations = 2
|
||||
MaxURLLength = 255
|
||||
SecondsInDay = 86400
|
||||
SecondsInHour = 3600
|
||||
SecondsInMinute = 60
|
||||
)
|
83
internal/database/db.go
Normal file
83
internal/database/db.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/secret"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type DBConfig struct {
|
||||
DB *redis.Client
|
||||
}
|
||||
|
||||
var ctx = context.Background() //nolint:gochecknoglobals
|
||||
|
||||
// InitDB initialise redis connection settings.
|
||||
func InitDB(addr string, user string, password string, db int) *redis.Options {
|
||||
DBConfig := &redis.Options{
|
||||
Addr: addr,
|
||||
Username: user,
|
||||
Password: password,
|
||||
DB: db,
|
||||
}
|
||||
|
||||
return DBConfig
|
||||
}
|
||||
|
||||
// ConnectDB make new database connection.
|
||||
func ConnectDB(dbConfig *redis.Options) *redis.Client {
|
||||
localDB := redis.NewClient(dbConfig)
|
||||
|
||||
return localDB
|
||||
}
|
||||
|
||||
// Ping test connection to Redis database.
|
||||
func Ping(db *redis.Client) error {
|
||||
status := db.Ping(ctx)
|
||||
if status.String() != "ping: PONG" {
|
||||
return &pingError{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (config DBConfig) InsertPaste(key string, content string, secret string, ttl time.Duration) {
|
||||
type dbSchema struct {
|
||||
content string
|
||||
secret string
|
||||
}
|
||||
|
||||
hash := dbSchema{
|
||||
content: content,
|
||||
secret: secret,
|
||||
}
|
||||
err := config.DB.HSet(ctx, key, "content", hash.content).Err()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
err = config.DB.HSet(ctx, key, "secret", hash.secret).Err()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if ttl > -1 {
|
||||
config.DB.Expire(ctx, key, ttl)
|
||||
}
|
||||
}
|
||||
|
||||
func (config DBConfig) URLExist(url string) bool {
|
||||
return config.DB.Exists(ctx, url).Val() == 1
|
||||
}
|
||||
|
||||
func (config DBConfig) VerifySecret(url string, token string) (bool, error) {
|
||||
storedHash := config.DB.HGet(ctx, url, "secret").Val()
|
||||
|
||||
result, err := secret.VerifyPassword(token, storedHash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
7
internal/database/error.go
Normal file
7
internal/database/error.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package database
|
||||
|
||||
type pingError struct{}
|
||||
|
||||
func (m *pingError) Error() string {
|
||||
return "Connection to redis not work"
|
||||
}
|
67
internal/httpserver/server.go
Normal file
67
internal/httpserver/server.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/web/plak"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type ServerConfig struct {
|
||||
HTTPServer *http.Server
|
||||
URLLength uint8
|
||||
DB *redis.Client
|
||||
Static embed.FS
|
||||
Templates embed.FS
|
||||
}
|
||||
|
||||
func (config ServerConfig) home(w http.ResponseWriter, _ *http.Request) {
|
||||
index, err := config.Static.ReadFile("static/index.html")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
_, err = w.Write(index)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Configure HTTP router.
|
||||
func (config ServerConfig) router() {
|
||||
WebConfig := plak.WebConfig{
|
||||
DB: config.DB,
|
||||
URLLength: config.URLLength,
|
||||
Templates: config.Templates,
|
||||
}
|
||||
staticFiles := http.FS(config.Static)
|
||||
|
||||
http.HandleFunc("GET /{$}", config.home)
|
||||
http.Handle("GET /static/{file}", http.FileServer(staticFiles))
|
||||
http.HandleFunc("GET /{key}/{settings...}", WebConfig.View)
|
||||
http.HandleFunc("POST /{$}", WebConfig.CurlCreate)
|
||||
http.HandleFunc("POST /create/{$}", WebConfig.PostCreate)
|
||||
http.HandleFunc("DELETE /{key}", WebConfig.DeleteRequest)
|
||||
}
|
||||
|
||||
// Config Configure HTTP server.
|
||||
func Config(listenAddress string) *http.Server {
|
||||
server := &http.Server{
|
||||
Addr: listenAddress,
|
||||
ReadTimeout: constant.HTTPTimeout,
|
||||
WriteTimeout: constant.HTTPTimeout,
|
||||
}
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
// Server Start HTTP server.
|
||||
func (config ServerConfig) Server() {
|
||||
log.Println("Listening on " + config.HTTPServer.Addr)
|
||||
|
||||
config.router()
|
||||
|
||||
log.Fatal(config.HTTPServer.ListenAndServe())
|
||||
}
|
161
internal/secret/crypto.go
Normal file
161
internal/secret/crypto.go
Normal file
|
@ -0,0 +1,161 @@
|
|||
// Package secret implement all crypto utils like password hashing and secret generation
|
||||
package secret
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
type argon2idHash struct {
|
||||
salt []byte
|
||||
hash []byte
|
||||
}
|
||||
|
||||
// Argon2id config.
|
||||
type config struct {
|
||||
saltLength uint8
|
||||
memory uint32
|
||||
threads uint8
|
||||
keyLength uint32
|
||||
iterations uint32
|
||||
}
|
||||
|
||||
// generateSecret for password hashing or token generation.
|
||||
func generateSecret(length uint8) ([]byte, error) {
|
||||
secret := make([]byte, length)
|
||||
|
||||
_, err := rand.Read(secret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return secret, err
|
||||
}
|
||||
|
||||
// GenerateToken generate hexadecimal token.
|
||||
func GenerateToken() (string, error) {
|
||||
secret, err := generateSecret(constant.TokenLength)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
token := hex.EncodeToString(secret)
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// generateArgon2ID Generate an argon2id hash from source string and specified salt.
|
||||
func (config config) generateArgon2ID(source string, salt []byte) []byte {
|
||||
hash := argon2.IDKey([]byte(source), salt, config.iterations, config.memory, config.threads, config.keyLength)
|
||||
|
||||
return hash
|
||||
}
|
||||
|
||||
// Password hash a source string with argon2id, return properly encoded hash.
|
||||
func Password(password string) (string, error) {
|
||||
config := config{
|
||||
saltLength: constant.ArgonSaltSize,
|
||||
memory: constant.ArgonMemory,
|
||||
threads: constant.ArgonThreads,
|
||||
keyLength: constant.ArgonKeyLength,
|
||||
iterations: constant.ArgonIterations,
|
||||
}
|
||||
|
||||
salt, err := generateSecret(config.saltLength)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hash := config.generateArgon2ID(password, salt)
|
||||
|
||||
base64Hash := base64.RawStdEncoding.EncodeToString(hash)
|
||||
base64Salt := base64.RawStdEncoding.EncodeToString(salt)
|
||||
|
||||
formatted := fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", argon2.Version, config.memory, config.iterations, config.threads, base64Salt, base64Hash)
|
||||
|
||||
return formatted, nil
|
||||
}
|
||||
|
||||
// VerifyPassword check is source password and stored password is similar, take password and a properly encoded hash.
|
||||
func VerifyPassword(password string, hash string) (bool, error) {
|
||||
argon2Hash, config, err := parseHash(hash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result := config.generateArgon2ID(password, argon2Hash.salt)
|
||||
|
||||
return bytes.Equal(result, argon2Hash.hash), nil
|
||||
}
|
||||
|
||||
// parseHash parse existing encoded argon2id string.
|
||||
func parseHash(source string) (argon2idHash, config, error) {
|
||||
separateItem := strings.Split(source, "$")
|
||||
if len(separateItem) != 6 { //nolint:gomnd
|
||||
return argon2idHash{}, config{}, &parseError{message: "Hash format is not valid"}
|
||||
}
|
||||
|
||||
if separateItem[1] != "argon2id" {
|
||||
return argon2idHash{}, config{}, &parseError{message: "Algorithm is not valid"}
|
||||
}
|
||||
|
||||
separateParam := strings.Split(separateItem[3], ",")
|
||||
if len(separateParam) != 3 { //nolint:gomnd
|
||||
return argon2idHash{}, config{}, &parseError{message: "Hash config is not valid"}
|
||||
}
|
||||
|
||||
salt, err := base64.RawStdEncoding.Strict().DecodeString(separateItem[4])
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
var hash []byte
|
||||
hash, err = base64.RawStdEncoding.Strict().DecodeString(separateItem[5])
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
saltLength := uint8(len(salt))
|
||||
keyLength := uint32(len(hash))
|
||||
|
||||
var memory int
|
||||
memory, err = strconv.Atoi(strings.ReplaceAll(separateParam[0], "m=", ""))
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
var iterations int
|
||||
iterations, err = strconv.Atoi(strings.ReplaceAll(separateParam[1], "t=", ""))
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
var threads int
|
||||
threads, err = strconv.Atoi(strings.ReplaceAll(separateParam[2], "p=", ""))
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
argon2idStruct := argon2idHash{
|
||||
salt: salt,
|
||||
hash: hash,
|
||||
}
|
||||
|
||||
hashConfig := config{
|
||||
saltLength: saltLength,
|
||||
memory: uint32(memory),
|
||||
threads: uint8(threads),
|
||||
iterations: uint32(iterations),
|
||||
keyLength: keyLength,
|
||||
}
|
||||
|
||||
return argon2idStruct, hashConfig, nil
|
||||
}
|
9
internal/secret/error.go
Normal file
9
internal/secret/error.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package secret
|
||||
|
||||
type parseError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (m *parseError) Error() string {
|
||||
return "parseHash: " + m.message
|
||||
}
|
17
internal/utils/error.go
Normal file
17
internal/utils/error.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package utils
|
||||
|
||||
type parseIntBeforeSeparatorError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (m *parseIntBeforeSeparatorError) Error() string {
|
||||
return "parseIntBeforeSeparator: " + m.message
|
||||
}
|
||||
|
||||
type ParseExpirationError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (m *ParseExpirationError) Error() string {
|
||||
return "parseIntBeforeSeparator: " + m.message
|
||||
}
|
117
internal/utils/utils.go
Normal file
117
internal/utils/utils.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
mathrand "math/rand/v2"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
)
|
||||
|
||||
// GenerateURL generate random string for plak url.
|
||||
func GenerateURL(length uint8) string {
|
||||
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
b := make([]rune, length)
|
||||
for i := range b {
|
||||
b[i] = listChars[mathrand.IntN(len(listChars))]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// CheckCharRedundant verify is a character is redundant in a string.
|
||||
func CheckCharRedundant(source string, char string) bool { // Verify if a char is redundant
|
||||
return strings.Count(source, char) > 1
|
||||
}
|
||||
|
||||
func parseIntBeforeSeparator(source *string, sep string) (int, error) { // return 0 & error if error, only accept positive number
|
||||
if CheckCharRedundant(*source, sep) {
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
||||
}
|
||||
var value int
|
||||
var err error
|
||||
if strings.Contains(*source, sep) {
|
||||
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
||||
}
|
||||
if value < 0 { // Only positive value is correct
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": format only take positive value"}
|
||||
}
|
||||
|
||||
if value > 99 { //nolint:gomnd
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": Format only take two number"}
|
||||
}
|
||||
|
||||
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// ParseExpiration Parse "1d1h1m1s" duration format. Return 0 & error if error.
|
||||
func ParseExpiration(source string) (int, error) {
|
||||
var expiration int
|
||||
var tempOutput int
|
||||
var err error
|
||||
if source == "0" {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
source = strings.ToLower(source)
|
||||
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
||||
expiration = tempOutput * constant.SecondsInDay
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
||||
expiration += tempOutput * constant.SecondsInHour
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
||||
expiration += tempOutput * constant.SecondsInMinute
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
||||
expiration += tempOutput * 1
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
|
||||
return expiration, nil
|
||||
}
|
||||
|
||||
// ValidKey Verify if a key is valid (only letter, number, - and _).
|
||||
func ValidKey(key string) bool {
|
||||
result, err := regexp.MatchString("^[a-zA-Z0-9_.-]*$", key)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
log.Println(key, result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FileExist verify if a file exist.
|
||||
func FileExist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
|
||||
return !errors.Is(err, os.ErrNotExist)
|
||||
}
|
18
internal/web/plak/error.go
Normal file
18
internal/web/plak/error.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package plak
|
||||
|
||||
type deletePlakError struct {
|
||||
name string
|
||||
err error
|
||||
}
|
||||
|
||||
func (m *deletePlakError) Error() string {
|
||||
return "Cannot delete: " + m.name + " : " + m.err.Error()
|
||||
}
|
||||
|
||||
type createError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (m *createError) Error() string {
|
||||
return "create: cannot create plak: " + m.message
|
||||
}
|
261
internal/web/plak/plak.go
Normal file
261
internal/web/plak/plak.go
Normal file
|
@ -0,0 +1,261 @@
|
|||
package plak
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/database"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/secret"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/utils"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ctx = context.Background() //nolint:gochecknoglobals
|
||||
|
||||
type WebConfig struct {
|
||||
DB *redis.Client
|
||||
URLLength uint8
|
||||
Templates embed.FS
|
||||
}
|
||||
|
||||
// plak "Object" for plak.
|
||||
type plak struct {
|
||||
Key string
|
||||
Content string
|
||||
Expiration time.Duration
|
||||
DB *redis.Client
|
||||
}
|
||||
|
||||
// create a plak.
|
||||
func (plak plak) create() (string, error) {
|
||||
dbConf := database.DBConfig{
|
||||
DB: plak.DB,
|
||||
}
|
||||
|
||||
token, err := secret.GenerateToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if dbConf.URLExist(plak.Key) {
|
||||
return "", &createError{message: "key already exist"}
|
||||
}
|
||||
|
||||
var hashedSecret string
|
||||
hashedSecret, err = secret.Password(token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
dbConf.InsertPaste(plak.Key, plak.Content, hashedSecret, plak.Expiration)
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// PostCreate manage POST request for create plak.
|
||||
func (config WebConfig) PostCreate(w http.ResponseWriter, r *http.Request) {
|
||||
content := r.FormValue("content")
|
||||
if content == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
filename := r.FormValue("filename")
|
||||
var key string
|
||||
if len(filename) == 0 {
|
||||
key = utils.GenerateURL(config.URLLength)
|
||||
} else {
|
||||
if !utils.ValidKey(filename) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
key = filename
|
||||
}
|
||||
|
||||
plak := plak{
|
||||
Key: key,
|
||||
Content: content,
|
||||
DB: config.DB,
|
||||
}
|
||||
|
||||
rawExpiration := r.FormValue("exp")
|
||||
expiration, err := utils.ParseExpiration(rawExpiration)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if expiration == 0 {
|
||||
plak.Expiration = -1
|
||||
} else {
|
||||
plak.Expiration = time.Duration(expiration * int(time.Second))
|
||||
}
|
||||
|
||||
_, err = plak.create()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/"+key, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// CurlCreate PostCreate plak with minimum param, ideal for curl. Force 7 day expiration.
|
||||
func (config WebConfig) CurlCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if r.ContentLength == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
content, _ := io.ReadAll(r.Body)
|
||||
err := r.Body.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
key := utils.GenerateURL(config.URLLength)
|
||||
|
||||
plak := plak{
|
||||
Key: key,
|
||||
Content: string(content),
|
||||
Expiration: constant.ExpirationCurlCreate,
|
||||
DB: config.DB,
|
||||
}
|
||||
|
||||
var token string
|
||||
token, err = plak.create()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var baseURL string
|
||||
if r.TLS == nil {
|
||||
baseURL = "http://" + r.Host + "/" + key
|
||||
} else {
|
||||
baseURL = "https://" + r.Host + "/" + key
|
||||
}
|
||||
|
||||
message := baseURL + "\n" + "Delete with : 'curl -X DELETE " + baseURL + "?secret\\=" + token + "'" + "\n"
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
_, err = io.WriteString(w, message)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// View for plak.
|
||||
func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
|
||||
dbConf := database.DBConfig{
|
||||
DB: config.DB,
|
||||
}
|
||||
var currentPlak plak
|
||||
key := r.PathValue("key")
|
||||
|
||||
//nolint:nestif
|
||||
if dbConf.URLExist(key) {
|
||||
currentPlak = plak{
|
||||
Key: key,
|
||||
DB: config.DB,
|
||||
}
|
||||
currentPlak = currentPlak.getContent()
|
||||
if r.PathValue("settings") == "raw" {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
_, err := io.WriteString(w, currentPlak.Content)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
t, err := template.ParseFS(config.Templates, "templates/paste.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
err = t.Execute(w, currentPlak)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteRequest manage plak deletion endpoint.
|
||||
func (config WebConfig) DeleteRequest(w http.ResponseWriter, r *http.Request) {
|
||||
dbConf := database.DBConfig{
|
||||
DB: config.DB,
|
||||
}
|
||||
key := r.PathValue("key")
|
||||
var valid bool
|
||||
|
||||
//nolint:nestif
|
||||
if dbConf.URLExist(key) {
|
||||
var err error
|
||||
token := r.URL.Query().Get("secret")
|
||||
|
||||
valid, err = dbConf.VerifySecret(key, token)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
if valid {
|
||||
plak := plak{
|
||||
Key: key,
|
||||
DB: config.DB,
|
||||
}
|
||||
err := plak.delete()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
||||
// delete DeleteRequest plak from database.
|
||||
func (plak plak) delete() error {
|
||||
err := plak.DB.Del(ctx, plak.Key).Err()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return &deletePlakError{name: plak.Key, err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getContent get plak content.
|
||||
func (plak plak) getContent() plak {
|
||||
plak.Content = plak.DB.HGet(ctx, plak.Key, "content").Val()
|
||||
|
||||
return plak
|
||||
}
|
116
main.go
116
main.go
|
@ -1,103 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"html/template"
|
||||
"io"
|
||||
"embed"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/config"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/database"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/httpserver"
|
||||
)
|
||||
|
||||
var currentConfig config
|
||||
var db *redis.Client
|
||||
|
||||
type pasteView struct {
|
||||
Content string
|
||||
Key string
|
||||
}
|
||||
|
||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
if path == "/" {
|
||||
http.ServeFile(w, r, "./static/index.html")
|
||||
|
||||
} else if strings.HasPrefix(path, "/static/") {
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
} else {
|
||||
if urlExist(clearPath) {
|
||||
if strings.HasSuffix(path, "/raw") {
|
||||
pasteContent := getContent(clearPath)
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
_, err := io.WriteString(w, pasteContent)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
pasteContent := getContent(path)
|
||||
s := pasteView{Content: pasteContent, Key: strings.TrimPrefix(path, "/")}
|
||||
t, err := template.ParseFiles("templates/paste.html")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
err = t.Execute(w, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
case "POST":
|
||||
if path == "/" {
|
||||
secret := generateSecret()
|
||||
url := "/" + generateUrl()
|
||||
content := r.FormValue("content")
|
||||
insertPaste(url, content, secret, -1)
|
||||
http.Redirect(w, r, url, http.StatusSeeOther)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
case "DELETE":
|
||||
if strings.HasPrefix(path, "/delete") {
|
||||
urlItem := strings.Split(path, "/")
|
||||
if urlExist("/" + urlItem[2]) {
|
||||
secret := r.URL.Query().Get("secret")
|
||||
if verifySecret("/"+urlItem[2], secret) {
|
||||
deleteContent("/" + urlItem[2])
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
}
|
||||
var (
|
||||
//go:embed templates
|
||||
templates embed.FS
|
||||
//go:embed static
|
||||
static embed.FS
|
||||
)
|
||||
|
||||
func main() {
|
||||
db = connectDB()
|
||||
currentConfig = getConfig()
|
||||
listen := currentConfig.host + ":" + currentConfig.port
|
||||
http.HandleFunc("/", handleRequest)
|
||||
|
||||
if currentConfig.host == "" {
|
||||
fmt.Println("Listening on port " + listen)
|
||||
} else {
|
||||
fmt.Println("Listening on " + listen)
|
||||
}
|
||||
|
||||
err := http.ListenAndServe(listen, nil)
|
||||
initConfig := config.GetConfig()
|
||||
dbConfig := database.InitDB(initConfig.RedisAddress, initConfig.RedisUser, initConfig.RedisPassword, initConfig.RedisDB)
|
||||
db := database.ConnectDB(dbConfig)
|
||||
err := database.Ping(db)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
serverConfig := httpserver.ServerConfig{
|
||||
HTTPServer: httpserver.Config(initConfig.ListenAddress),
|
||||
URLLength: initConfig.URLLength,
|
||||
DB: db,
|
||||
Static: static,
|
||||
Templates: templates,
|
||||
}
|
||||
|
||||
serverConfig.Server()
|
||||
}
|
||||
|
|
54
plakken.service
Normal file
54
plakken.service
Normal file
|
@ -0,0 +1,54 @@
|
|||
# /usr/lib/systemd/system/plakken.service
|
||||
[Unit]
|
||||
Description=A paste server
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=plakken
|
||||
|
||||
ExecStart=/usr/bin/plakken
|
||||
|
||||
|
||||
EnvironmentFile=/etc/plakken/env
|
||||
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
NoNewPrivileges=yes
|
||||
RestrictNamespaces=true
|
||||
PrivateTmp=true
|
||||
PrivateDevices=true
|
||||
PrivateUsers=true
|
||||
ProtectClock=true
|
||||
ProtectControlGroups=true
|
||||
ProtectKernelTunables=true
|
||||
ProtectKernelLogs=true
|
||||
ProtectKernelModules=true
|
||||
LockPersonality=true
|
||||
RestrictSUIDSGID=true
|
||||
RemoveIPC=true
|
||||
RestrictRealtime=true
|
||||
SystemCallArchitectures=native
|
||||
MemoryDenyWriteExecute=true
|
||||
UMask=177
|
||||
ProtectProc=invisible
|
||||
CapabilityBoundingSet=
|
||||
ProtectHostname=true
|
||||
RestrictAddressFamilies=~AF_(INET|INET6)
|
||||
RestrictAddressFamilies=~…
|
||||
RestrictAddressFamilies=~AF_UNIX
|
||||
RestrictAddressFamilies=~AF_NETLINK
|
||||
RestrictAddressFamilies=~AF_PACKET
|
||||
SystemCallFilter=~@reboot
|
||||
SystemCallFilter=~@obsolete
|
||||
SystemCallFilter=~@mount
|
||||
SystemCallFilter=~@module
|
||||
SystemCallFilter=~@debug
|
||||
SystemCallFilter=~@cpu-emulation
|
||||
SystemCallFilter=~@clock
|
||||
SystemCallFilter=~@swap
|
||||
SystemCallFilter=~@privileged
|
||||
ProcSubset=pid
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
3
renovate.json
Normal file
3
renovate.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
const editor = document.getElementById('content');
|
||||
const filenameSelector = document.getElementById('filename');
|
||||
const recentPlaksDiv = document.getElementById('recent-plaks');
|
||||
const lineNumbersDiv = document.getElementById('line-numbers');
|
||||
|
||||
function updateLn() {
|
||||
const lines = editor.value.split('\n').length;
|
||||
const lineNumbers = Array.from({ length: lines }, (_, i) => i + 1).join('\n');
|
||||
|
||||
lineNumbersDiv.textContent = lineNumbers;
|
||||
}
|
||||
|
||||
function updateTitle() {
|
||||
document.title = filenameSelector.value == '' ? 'New paste' : ` ${filenameSelector.value} (Unsaved) - Plakken`;
|
||||
}
|
||||
|
||||
function getRecentPlaksFromStorage() {
|
||||
return new Set(JSON.parse(localStorage.getItem('recentPlaks')) || []);
|
||||
}
|
||||
|
||||
function updateLocalStorage() {
|
||||
localStorage.setItem('recentPlaks', JSON.stringify(Array.from(recentPlaks)));
|
||||
}
|
||||
|
||||
function addRecentPlak(plakId) {
|
||||
recentPlaks.add(plakId);
|
||||
updateLocalStorage();
|
||||
}
|
||||
|
||||
function deleteRecentPlak(plakId) {
|
||||
const plak = document.querySelector(`[href="/${plakId}/"]`).parentElement;
|
||||
|
||||
plak.style.transform = 'translateX(150%)';
|
||||
setTimeout(() => plak.remove(), 250);
|
||||
|
||||
recentPlaks.delete(plakId);
|
||||
updateLocalStorage();
|
||||
}
|
||||
|
||||
function createRecentPlakComponent(plak) {
|
||||
const div = document.createElement('div');
|
||||
div.classList.add('recent-plak', 'fr');
|
||||
|
||||
const a = document.createElement('a');
|
||||
a.href = `/${plak}/`;
|
||||
a.textContent = plak;
|
||||
|
||||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
svg.innerHTML = `<line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line>`;
|
||||
svg.id = 'cross';
|
||||
svg.setAttribute('viewBox', '0 0 24 24');
|
||||
svg.onclick = () => deleteRecentPlak(plak);
|
||||
|
||||
div.appendChild(a);
|
||||
div.appendChild(svg);
|
||||
|
||||
return div;
|
||||
}
|
||||
|
||||
filenameSelector.addEventListener('input', updateTitle);
|
||||
editor.addEventListener('input', updateLn);
|
||||
|
||||
let recentPlaks = getRecentPlaksFromStorage();
|
||||
|
||||
if (recentPlaks === null) {
|
||||
recentPlaks = [];
|
||||
localStorage.setItem('recentPlaks', JSON.stringify(recentPlaks));
|
||||
} else {
|
||||
for (const plak of recentPlaks)
|
||||
recentPlaksDiv.appendChild(createRecentPlakComponent(plak));
|
||||
}
|
BIN
static/favicon.ico
Normal file
BIN
static/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
1
static/favicon.svg
Normal file
1
static/favicon.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path class="b" d="m777.387 609.18c-5.427 2.039-9.716 6.229-11.803 11.531l-80.287 203.948c-6.755 17.159-31.604 17.16-38.359 0l-80.287-203.948c-2.087-5.302-6.376-9.492-11.803-11.531l-208.746-78.442c-17.564-6.599-17.564-30.877 0-37.476l208.746-78.442c5.427-2.039 9.716-6.229 11.803-11.531l80.287-203.948c6.755-17.159 31.604-17.159 38.359 0l80.287 203.948c2.087 5.302 6.376 9.492 11.803 11.531l208.747 78.442c17.566 6.599 17.566 30.877 0 37.477z"/><g class="s"><path d="m192.121 342.82c-6.756 17.16-31.604 17.16-38.359 0l-28.915-73.449c-2.087-5.302-6.376-9.492-11.803-11.532l-75.1777-28.25c-17.5631-6.599-17.5631-30.877 0-37.477l75.1777-28.249c5.427-2.04 9.716-6.23 11.803-11.532l28.915-73.4498c6.755-17.1594 31.603-17.1593 38.359.0001l28.914 73.4497c2.087 5.302 6.376 9.492 11.803 11.532l75.178 28.249c17.563 6.6 17.563 30.878 0 37.477l-75.178 28.25c-5.427 2.04-9.716 6.23-11.803 11.532z"/><path d="m356.132 860.138c-5.427 2.039-9.715 6.229-11.803 11.531l-28.914 73.45c-6.755 17.159-31.604 17.159-38.359 0l-28.915-73.45c-2.087-5.302-6.376-9.492-11.802-11.531l-75.179-28.25c-17.563-6.6-17.563-30.877 0-37.477l75.179-28.25c5.426-2.039 9.715-6.229 11.802-11.531l28.915-73.45c6.755-17.16 31.604-17.16 38.359 0l28.914 73.45c2.088 5.302 6.376 9.492 11.803 11.531l75.178 28.25c17.563 6.6 17.563 30.877 0 37.477z"/></g><style xmlns="http://www.w3.org/2000/svg">.b{fill:#ff9500}.s{fill:#ffBf00}@media(prefers-color-scheme:dark){.b{fill:#ffbf00}.s{fill:#ff9500}}</style></svg>
|
After Width: | Height: | Size: 1.5 KiB |
|
@ -1,58 +1,60 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="Plakken, paste, pastebin" name="keywords">
|
||||
<meta content="Plakken is a simple open-source paste service where you can store store text for a set period of time."
|
||||
<meta content="Plakken is a simple open-source paste service where you can store text for a set period of time."
|
||||
name="description">
|
||||
<meta content="Plakken" name="author">
|
||||
<meta content="Gnous EU" name="author">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>New plak • Plakken</title>
|
||||
<link href="/static/favicon.svg" rel="icon">
|
||||
<title>New paste</title>
|
||||
<link href="/static/style.css" rel="stylesheet">
|
||||
<script async src="/static/app.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form method="post">
|
||||
<label for="content"></label>
|
||||
<textarea autofocus id="content" name="content" placeholder="Your paste here"></textarea>
|
||||
<nav>
|
||||
<ul>
|
||||
<li>
|
||||
<label for="password">Password?</label>
|
||||
<input id="password" type="text">
|
||||
</li>
|
||||
<li><label for="exp">Expiration?</label>
|
||||
<input id="exp" type="date"></li>
|
||||
<li>
|
||||
<label for="type">Type</label>
|
||||
<select id="type" name="type">
|
||||
<option value="auto">Auto Detect</option>
|
||||
<option value="c">C</option>
|
||||
<option value="cpp">C++</option>
|
||||
<option value="csharp">C#</option>
|
||||
<option value="css">CSS</option>
|
||||
<option value="go">Go</option>
|
||||
<option value="java">Java</option>
|
||||
<option value="js">Javascript</option>
|
||||
<option value="html">HTML</option>
|
||||
<option selected value="plain">Plaintext</option>
|
||||
<option value="python">Python</option>
|
||||
<option value="rb">Ruby</option>
|
||||
<option value="rs">Rust</option>
|
||||
<option value="sh">Shell</option>
|
||||
<option value="sql">SQL</option>
|
||||
<option value="ts">Typescript</option>
|
||||
<option value="xml">XML</option>
|
||||
<option value="yml">YAML</option>
|
||||
</select>
|
||||
</li>
|
||||
</ul>
|
||||
<button type="submit" title="Save plak">
|
||||
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
||||
<polyline points="9 11 12 14 22 4"></polyline>
|
||||
<path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"></path>
|
||||
</svg>
|
||||
</button>
|
||||
</nav>
|
||||
<form action="/create/" class="fr" method="post">
|
||||
<div id="line-numbers">1</div>
|
||||
<label for="content"><textarea autofocus id="content" name="content"
|
||||
placeholder="Type your paste here"></textarea></label>
|
||||
|
||||
<div class="menu fr">
|
||||
<label for="filename"><input id="filename" name="filename" placeholder="Filename" type="text"></label>
|
||||
<!--<label for="password"><input id="password" placeholder="Password" type="password"></label>-->
|
||||
<label for="exp"><input id="exp" name="exp" placeholder="Expiration (1d1h1m1s)" type="text"></label>
|
||||
<label for="type">
|
||||
<select id="type" name="type">zz
|
||||
<option value="c">C/C++</option>
|
||||
<option value="csharp">C#</option>
|
||||
<option value="css">CSS</option>
|
||||
<option value="go">Go</option>
|
||||
<option value="java">Java</option>
|
||||
<option value="js">Javascript</option>
|
||||
<option value="html">HTML</option>
|
||||
<option selected value="pt">Plaintext</option>
|
||||
<option value="py">Python</option>
|
||||
<option value="rb">Ruby</option>
|
||||
<option value="rs">Rust</option>
|
||||
<option value="sh">Shell</option>
|
||||
<option value="sql">SQL</option>
|
||||
<option value="ts">Typescript</option>
|
||||
<option value="xml">XML</option>
|
||||
<option value="yml">YAML</option>
|
||||
</select>
|
||||
</label>
|
||||
<button title="Save plak" type="submit">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
<section id="recent-plaks">
|
||||
<div class="title fr">
|
||||
<h3>Recent plaks</h3>
|
||||
<svg height="32" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24" width="32">
|
||||
<polyline points="6 9 12 15 18 9"></polyline>
|
||||
</svg>
|
||||
</div>
|
||||
</section>
|
||||
</body>
|
||||
|
||||
</html>
|
BIN
static/inter.woff2
Normal file
BIN
static/inter.woff2
Normal file
Binary file not shown.
BIN
static/jetbrainsmono.woff2
Normal file
BIN
static/jetbrainsmono.woff2
Normal file
Binary file not shown.
286
static/style.css
286
static/style.css
|
@ -1,96 +1,200 @@
|
|||
:root {
|
||||
--accent: #be0560;
|
||||
--background: #141414;
|
||||
--border: #333;
|
||||
--text: #e2e2e2;
|
||||
section#recent-plaks {
|
||||
bottom: 1rem;
|
||||
display: flex;
|
||||
flex-flow: column wrap;
|
||||
gap: 9px;
|
||||
position: absolute;
|
||||
right: 1rem;
|
||||
transition: 0.15s ease-in-out;
|
||||
user-select: none;
|
||||
}
|
||||
section#recent-plaks .title {
|
||||
align-items: center;
|
||||
gap: 18px;
|
||||
transition: 0.15s ease-in-out;
|
||||
}
|
||||
section#recent-plaks .title:hover {
|
||||
border-color: #555;
|
||||
}
|
||||
section#recent-plaks .recent-plak {
|
||||
align-items: center;
|
||||
border: 3px solid #363636;
|
||||
font-weight: 500;
|
||||
justify-content: space-between;
|
||||
outline: none;
|
||||
padding: 8px 18px;
|
||||
text-decoration: none;
|
||||
transition: 0.15s ease-in-out;
|
||||
width: 12rem;
|
||||
}
|
||||
section#recent-plaks .recent-plak:focus-visible {
|
||||
border: 2px solid #bbb;
|
||||
}
|
||||
section#recent-plaks .recent-plak svg {
|
||||
border-radius: 15px;
|
||||
cursor: pointer;
|
||||
height: 22px;
|
||||
padding: 5px;
|
||||
stroke: #e5e5e7;
|
||||
stroke-width: 2.25;
|
||||
width: 22px;
|
||||
transition: 0.1s ease-in-out;
|
||||
}
|
||||
section#recent-plaks .recent-plak svg:hover {
|
||||
background-color: #363636;
|
||||
}
|
||||
section#recent-plaks a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: var(--background);
|
||||
color: var(--text);
|
||||
font: 400 15px/2 "system-ui", monospace;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
button, textarea {
|
||||
background-color: inherit;
|
||||
border: none;
|
||||
}
|
||||
|
||||
textarea {
|
||||
color: var(--text);
|
||||
font: 14px/1.6 "JetBrains Mono", monospace;
|
||||
height: calc(100vh - 3rem);
|
||||
outline: none;
|
||||
padding: 1rem;
|
||||
resize: none;
|
||||
width: calc(100vw - 2rem);
|
||||
}
|
||||
|
||||
pre {
|
||||
margin: 15px;
|
||||
}
|
||||
|
||||
nav {
|
||||
align-items: end;
|
||||
bottom: 1rem;
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
position: absolute;
|
||||
right: 1rem;
|
||||
}
|
||||
|
||||
ul {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
gap: 2.6rem;
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0 1.9rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
}
|
||||
|
||||
input, select {
|
||||
background-color: var(--background);
|
||||
border: 2px solid var(--border);
|
||||
border-radius: 2px;
|
||||
color: var(--text);
|
||||
font-size: 15px;
|
||||
outline: none;
|
||||
padding: 5px 6px;
|
||||
transition: border .15s ease;
|
||||
}
|
||||
|
||||
input:hover, select:hover {
|
||||
border-color: var(--text);
|
||||
}
|
||||
|
||||
svg {
|
||||
fill: none;
|
||||
height: 26px;
|
||||
stroke: var(--text);
|
||||
stroke-width: 2;
|
||||
stroke-linecap: round;
|
||||
transition: .15s ease;
|
||||
width: 26px;
|
||||
}
|
||||
|
||||
svg:hover {
|
||||
stroke: #fff;
|
||||
}
|
||||
|
||||
input:focus-visible, select:focus-visible {
|
||||
border: 2px solid var(--text);
|
||||
}
|
||||
|
||||
button:focus-visible{
|
||||
outline: none;
|
||||
h3 {
|
||||
font-size: 1.5rem;
|
||||
margin: 0;
|
||||
transition: 0.15s ease-in-out;
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
::selection {
|
||||
background-color: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
background-color: #d30f45;
|
||||
color: #e5e5e7;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar {
|
||||
width: 9px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background-color: #0f0f0f;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background-color: #444;
|
||||
border-radius: 9px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background-color: #e5e5e7;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "Inter";
|
||||
font-style: normal;
|
||||
font-weight: 400 700;
|
||||
font-display: swap;
|
||||
src: url(inter.woff2) format("woff2");
|
||||
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "JetBrains Mono";
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url(jetbrainsmono.woff2) format("woff2");
|
||||
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
|
||||
}
|
||||
body {
|
||||
background-color: #0f0f0f;
|
||||
color: #e5e5e7;
|
||||
font: 400 16px/2 "Inter", "system-ui", sans-serif;
|
||||
margin: 0;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
.fr {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
}
|
||||
|
||||
#line-numbers,
|
||||
#content {
|
||||
font: 400 14px/1.6 "JetBrains Mono", monospace;
|
||||
}
|
||||
|
||||
#line-numbers {
|
||||
color: #888;
|
||||
padding: 16px 1px;
|
||||
text-align: center;
|
||||
white-space: pre;
|
||||
width: 30px;
|
||||
}
|
||||
|
||||
#content {
|
||||
color: #e5e5e7;
|
||||
height: 100%;
|
||||
min-height: 90vh;
|
||||
padding: 16px 16px 0 16px;
|
||||
width: calc(100vw - 65px);
|
||||
}
|
||||
|
||||
.menu {
|
||||
gap: 18px;
|
||||
position: fixed;
|
||||
right: 16px;
|
||||
top: 16px;
|
||||
}
|
||||
.menu svg {
|
||||
cursor: pointer;
|
||||
height: 24px;
|
||||
fill: none;
|
||||
margin-bottom: -6px;
|
||||
stroke: #e5e5e7;
|
||||
stroke-width: 2;
|
||||
width: 24px;
|
||||
}
|
||||
|
||||
button,
|
||||
input,
|
||||
select {
|
||||
background-color: #0f0f0f;
|
||||
border: 2px solid #363636;
|
||||
color: #e5e5e7;
|
||||
font: 500 14px/2 "Inter", "system-ui", sans-serif;
|
||||
max-width: 145px;
|
||||
outline: none;
|
||||
padding: 3px 10px;
|
||||
transition: border 0.15s ease;
|
||||
width: min-content;
|
||||
}
|
||||
button::placeholder,
|
||||
input::placeholder,
|
||||
select::placeholder {
|
||||
color: #888;
|
||||
}
|
||||
button:hover,
|
||||
input:hover,
|
||||
select:hover {
|
||||
border-color: #777;
|
||||
}
|
||||
button:focus-visible,
|
||||
input:focus-visible,
|
||||
select:focus-visible {
|
||||
border: 2px solid #bbb;
|
||||
}
|
||||
|
||||
select {
|
||||
padding: 8px 10px;
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
select:focus {
|
||||
background-color: #0f0f0f;
|
||||
transition: none;
|
||||
}
|
||||
|
||||
option {
|
||||
background-color: #0f0f0f;
|
||||
color: #e5e5e7;
|
||||
}
|
||||
option:focus {
|
||||
background-color: #d30f45;
|
||||
color: #0f0f0f;
|
||||
}
|
||||
|
||||
textarea {
|
||||
background-color: inherit;
|
||||
border: none;
|
||||
margin: 0;
|
||||
outline: none;
|
||||
resize: none;
|
||||
}
|
||||
|
|
58
test/secret/secret_test.go
Normal file
58
test/secret/secret_test.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package secret_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/secret"
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
func TestSecret(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testPasswordFormat(t)
|
||||
testVerifyPassword(t)
|
||||
testVerifyPasswordInvalid(t)
|
||||
}
|
||||
|
||||
func testPasswordFormat(t *testing.T) {
|
||||
t.Helper()
|
||||
regex := fmt.Sprintf("\\$argon2id\\$v=%d\\$m=%d,t=%d,p=%d\\$[A-Za-z0-9+/]*\\$[A-Za-z0-9+/]*$", argon2.Version, constant.ArgonMemory, constant.ArgonIterations, constant.ArgonThreads) //nolint:lll
|
||||
|
||||
got, err := secret.Password("Password!")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, _ := regexp.MatchString(regex, got)
|
||||
if !result {
|
||||
t.Fatal("Error in Password, format is not valid "+": ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testVerifyPassword(t *testing.T) {
|
||||
t.Helper()
|
||||
result, err := secret.VerifyPassword("Password!", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0") //nolint:lll
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !result {
|
||||
t.Fatal("Error in VerifyPassword, got:", result, "want: ", true)
|
||||
}
|
||||
}
|
||||
|
||||
func testVerifyPasswordInvalid(t *testing.T) {
|
||||
t.Helper()
|
||||
result, err := secret.VerifyPassword("notsamepassword", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0") //nolint:lll
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if result {
|
||||
t.Fatal("Error in VerifyPassword, got:", result, "want: ", false)
|
||||
}
|
||||
}
|
135
test/utils/utils_test.go
Normal file
135
test/utils/utils_test.go
Normal file
|
@ -0,0 +1,135 @@
|
|||
package utils_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/utils"
|
||||
)
|
||||
|
||||
func TestUtils(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCheckCharNotRedundantTrue(t)
|
||||
testCheckCharNotRedundantFalse(t)
|
||||
testParseExpirationFull(t)
|
||||
testParseExpirationMissing(t)
|
||||
testParseExpirationWithCaps(t)
|
||||
testParseExpirationNull(t)
|
||||
testParseExpirationNegative(t)
|
||||
testParseExpirationInvalid(t)
|
||||
testParseExpirationInvalidRedundant(t)
|
||||
testParseExpirationInvalidTooHigh(t)
|
||||
testValidKey(t)
|
||||
testInValidKey(t)
|
||||
}
|
||||
|
||||
func testCheckCharNotRedundantTrue(t *testing.T) { // Test CheckCharRedundant with redundant char
|
||||
t.Helper()
|
||||
want := true
|
||||
got := utils.CheckCharRedundant("2d1h3md4h7s", "h")
|
||||
if got != want {
|
||||
t.Fatal("Error in parseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testCheckCharNotRedundantFalse(t *testing.T) { // Test CheckCharRedundant with not redundant char
|
||||
t.Helper()
|
||||
want := false
|
||||
got := utils.CheckCharRedundant("2d1h3m47s", "h")
|
||||
if got != want {
|
||||
t.Fatal("Error in parseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
result, _ := utils.ParseExpiration("2d1h3m47s")
|
||||
correctValue := 176627
|
||||
if result != correctValue {
|
||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
result, _ := utils.ParseExpiration("1h47s")
|
||||
correctValue := 3647
|
||||
if result != correctValue {
|
||||
t.Fatal("Error in ParseExpirationFull, want : ", correctValue, "got : ", result)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationWithCaps(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
result, _ := utils.ParseExpiration("2D1h3M47s")
|
||||
correctValue := 176627
|
||||
if result != correctValue {
|
||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationNull(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
result, _ := utils.ParseExpiration("0")
|
||||
correctValue := 0
|
||||
if result != correctValue {
|
||||
t.Fatal("Error in ParseExpirationFull, want: ", correctValue, "got: ", result)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationNegative(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
_, got := utils.ParseExpiration("-42h1m4s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationInvalid(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
_, got := utils.ParseExpiration("8h42h1m1d4s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationInvalidRedundant(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
_, got := utils.ParseExpiration("8h42h1m1h4s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testParseExpirationInvalidTooHigh(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
_, got := utils.ParseExpiration("2d1h3m130s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testValidKey(t *testing.T) { // test ValidKey with a valid key
|
||||
t.Helper()
|
||||
got := utils.ValidKey("ab_a-C42")
|
||||
want := true
|
||||
|
||||
if got != want {
|
||||
t.Fatal("Error in ValidKey, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testInValidKey(t *testing.T) { // test ValidKey with invalid key
|
||||
t.Helper()
|
||||
got := utils.ValidKey("ab_?a-C42")
|
||||
want := false
|
||||
|
||||
if got != want {
|
||||
t.Fatal("Error in ValidKey, want : ", want, "got : ", got)
|
||||
}
|
||||
}
|
40
utils.go
40
utils.go
|
@ -1,40 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
mathrand "math/rand"
|
||||
)
|
||||
|
||||
func generateUrl() string {
|
||||
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
b := make([]rune, currentConfig.urlLength)
|
||||
for i := range b {
|
||||
b[i] = listChars[mathrand.Intn(len(listChars))]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func generateSecret() string {
|
||||
key := make([]byte, 32)
|
||||
_, err := rand.Read(key)
|
||||
if err != nil {
|
||||
log.Printf("Failed to generate secret")
|
||||
}
|
||||
|
||||
return hex.EncodeToString(key)
|
||||
}
|
||||
|
||||
func urlExist(url string) bool {
|
||||
exist := db.Exists(ctx, url).Val()
|
||||
return exist == 1
|
||||
}
|
||||
|
||||
func verifySecret(url string, secret string) bool {
|
||||
if secret == db.HGet(ctx, url, "secret").Val() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Reference in a new issue