Compare commits
4 Commits
main
...
conditiona
Author | SHA1 | Date | |
---|---|---|---|
58bb2432fb | |||
b5fdfcb49c | |||
3fcc46c4f9 | |||
6d386bf591 |
@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.16
|
FROM golang:1.18
|
||||||
|
|
||||||
WORKDIR /go/src/app
|
WORKDIR /go/src/app
|
||||||
COPY . .
|
COPY . .
|
||||||
|
@ -9,3 +9,4 @@ services:
|
|||||||
redis:
|
redis:
|
||||||
image: redis:7
|
image: redis:7
|
||||||
volumes: [ "./redis:/data" ]
|
volumes: [ "./redis:/data" ]
|
||||||
|
ports: [ "6379:6379" ]
|
||||||
|
120
main.go
120
main.go
@ -24,6 +24,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@ -72,17 +75,27 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
command := r.PostForm.Get("Command")
|
command := r.PostForm.Get("Command")
|
||||||
shortlink := r.PostForm.Get("Shortlink")
|
shortlink := r.PostForm.Get("Shortlink")
|
||||||
value := r.PostForm.Get("Value")
|
redirect := r.PostForm.Get("Redirect")
|
||||||
fmt.Printf("command: %v, shortlink: %v, value: %v\n", command, shortlink, value)
|
alt_redirect := r.PostForm.Get("AltRedirect")
|
||||||
fmt.Println(shortlink)
|
alt_condition := r.PostForm.Get("AltCondition")
|
||||||
fmt.Println(value)
|
fmt.Printf("command: %v, shortlink: %v, redirect: %v, alt_redirect: %v, alt_condition: %v\n",
|
||||||
|
command,
|
||||||
|
shortlink,
|
||||||
|
redirect,
|
||||||
|
alt_redirect,
|
||||||
|
alt_condition,
|
||||||
|
)
|
||||||
|
|
||||||
|
formstring := command + ":" + shortlink + ":" + redirect + ":" + alt_condition + ":" + alt_redirect
|
||||||
|
|
||||||
|
fmt.Println("formstring: " + formstring)
|
||||||
|
|
||||||
signature := r.Header.Get("Signature")
|
signature := r.Header.Get("Signature")
|
||||||
calculatedSignature := fmt.Sprintf(
|
calculatedSignature := fmt.Sprintf(
|
||||||
"SUS-SIGNATURE-%v",
|
"SUS-SIGNATURE-%v",
|
||||||
getSha256HMACSignature(
|
getSha256HMACSignature(
|
||||||
[]byte(SECRET),
|
[]byte(SECRET),
|
||||||
command+":"+shortlink+":"+value,
|
command+":"+shortlink+":"+redirect+":"+alt_condition+":"+alt_redirect,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -100,7 +113,21 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_, err := client.Get(ctx, shortlink).Result()
|
_, err := client.Get(ctx, shortlink).Result()
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
err = client.Set(ctx, shortlink, value, 0).Err()
|
err = client.Set(ctx, shortlink+":redirect", shortlink, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = client.Set(ctx, shortlink+":altcondition", alt_condition, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = client.Set(ctx, shortlink+":altredirect", alt_redirect, 0).Err()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@ -128,13 +155,16 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if command == "delete" {
|
if command == "delete" {
|
||||||
if value != "confirm" {
|
|
||||||
w.WriteHeader(400)
|
|
||||||
w.Write([]byte("400 Bad Request"))
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if err := client.Del(ctx, shortlink).Err(); err != nil {
|
if err := client.Del(ctx, shortlink+":redirect").Err(); err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
}
|
||||||
|
if err := client.Del(ctx, shortlink+":altredirect").Err(); err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
}
|
||||||
|
if err := client.Del(ctx, shortlink+":altcondition").Err(); err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte("500 Internal Server Error"))
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
}
|
}
|
||||||
@ -152,13 +182,13 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
resp := ""
|
resp := ""
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
value, err = client.Get(ctx, key).Result()
|
shortlink, err = client.Get(ctx, key).Result()
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte("500 Internal Server Error"))
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp += key + ":" + value + "\n"
|
resp += key + ":" + shortlink + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
@ -171,7 +201,9 @@ func shortlinkHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Println("shortlinkHandler called")
|
fmt.Println("shortlinkHandler called")
|
||||||
shortlink := string(mux.Vars(r)["shortlink"])
|
shortlink := string(mux.Vars(r)["shortlink"])
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
redirect, err := client.Get(ctx, shortlink).Result()
|
client.Incr(ctx, shortlink + ":hits")
|
||||||
|
|
||||||
|
redirect, err := client.Get(ctx, shortlink+":redirect").Result()
|
||||||
fmt.Printf("shortlink: %v, redirect: %v\n", shortlink, redirect)
|
fmt.Printf("shortlink: %v, redirect: %v\n", shortlink, redirect)
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
@ -179,13 +211,67 @@ func shortlinkHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
fmt.Println(0)
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
w.Write([]byte("500 Internal Server Error"))
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, redirect, 302)
|
altcondition, err := client.Get(ctx, shortlink+":altcondition").Result()
|
||||||
return
|
if err == redis.Nil {
|
||||||
|
http.Redirect(w, r, redirect, 302)
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(1)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
altredirect, err := client.Get(ctx, shortlink+":altredirect").Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
http.Redirect(w, r, redirect, 302)
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(2)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
altcondition_split := strings.Split(altcondition, ",")
|
||||||
|
ac_varname := altcondition_split[0]
|
||||||
|
ac_operator := altcondition_split[1]
|
||||||
|
ac_required_value, _ := strconv.Atoi(altcondition_split[2])
|
||||||
|
|
||||||
|
var ac_varval int
|
||||||
|
|
||||||
|
if ac_varname != "timestamp" {
|
||||||
|
ac_varvalstr, err := client.Get(ctx, shortlink+":"+ac_varname).Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
ac_varval = 0
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(3)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("500 Internal Server Error"))
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
ac_varval, _ = strconv.Atoi(ac_varvalstr)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ac_varval = int(time.Now().Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ac_operator == "eq" && ac_varval == ac_required_value) ||
|
||||||
|
(ac_operator == "gt" && ac_varval > ac_required_value) ||
|
||||||
|
(ac_operator == "lt" && ac_varval < ac_required_value) {
|
||||||
|
http.Redirect(w, r, altredirect, 307)
|
||||||
|
} else {
|
||||||
|
http.Redirect(w, r, redirect, 307)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSha256HMACSignature(secret []byte, data string) string {
|
func getSha256HMACSignature(secret []byte, data string) string {
|
||||||
|
14
readme.md
14
readme.md
@ -7,11 +7,21 @@ sus URL shortener
|
|||||||
|
|
||||||
- creating a new shortlink at https://pls.cx/shortlink
|
- creating a new shortlink at https://pls.cx/shortlink
|
||||||
|
|
||||||
susmng [-s pls.cx] create -l shortlink -v https://example.com
|
susmng [-s pls.cx] create -l shortlink -r https://example.com
|
||||||
|
|
||||||
|
- creating a new shortlink at https://pls.cx/shortlink which redirects to https://example.com/a the first n times, https://example.com/b any other times
|
||||||
|
|
||||||
|
susmng [-s pls.cx] create -l shortlink -r https://example.com/a \
|
||||||
|
-c hits,gt,<n> -a https://example.com/b
|
||||||
|
|
||||||
|
- creating a new shortlink at https://pls.cx/shortlink which redirects to https://example.com/before before unix timestamp n (seconds), https://example.com/after after that
|
||||||
|
|
||||||
|
susmng [-s pls.cx] create -l shortlink -r https://example.com/before \
|
||||||
|
-c timestamp,gt,<n> -a https://example.com/after
|
||||||
|
|
||||||
- deleting the shortlink https://pls.cx/shortlink
|
- deleting the shortlink https://pls.cx/shortlink
|
||||||
|
|
||||||
susmng [-s pls.cx] delete -l shortlink -v confirm
|
susmng [-s pls.cx] delete -l shortlink
|
||||||
|
|
||||||
- listing all shortlinks on the server pls.cx
|
- listing all shortlinks on the server pls.cx
|
||||||
|
|
||||||
|
16
susmng.py
16
susmng.py
@ -18,8 +18,10 @@ def get_args():
|
|||||||
parser.add_argument('command')
|
parser.add_argument('command')
|
||||||
parser.add_argument('-s', '--server', default="")
|
parser.add_argument('-s', '--server', default="")
|
||||||
parser.add_argument('-l', '--shortlink', default="")
|
parser.add_argument('-l', '--shortlink', default="")
|
||||||
parser.add_argument('-v', '--value', default="")
|
parser.add_argument('-r', '--redirect', default="")
|
||||||
parser.add_argument('-c', '--config', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.config/susmng/config.json')))
|
parser.add_argument('-a', '--alt-redirect', default="")
|
||||||
|
parser.add_argument('-c', '--alt-condition', default="")
|
||||||
|
parser.add_argument('--config', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.config/susmng/config.json')))
|
||||||
parser.add_argument('-H', '--http', action='store_true')
|
parser.add_argument('-H', '--http', action='store_true')
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
@ -48,19 +50,21 @@ def main(args):
|
|||||||
|
|
||||||
secret = config['secrets'][server]
|
secret = config['secrets'][server]
|
||||||
|
|
||||||
if args.command == "delete" and args.value != "confirm":
|
formstring = args.command+":"+args.shortlink+":"+args.redirect+":"+args.alt_condition+":"+args.alt_redirect
|
||||||
print("--value not set to 'confirm'... delete operation may fail")
|
print(f"{formstring=}")
|
||||||
|
|
||||||
r = requests.post(f"{'http' if args.http else 'https'}://{server}",
|
r = requests.post(f"{'http' if args.http else 'https'}://{server}",
|
||||||
data = {
|
data = {
|
||||||
'Command': args.command,
|
'Command': args.command,
|
||||||
'Shortlink': args.shortlink,
|
'Shortlink': args.shortlink,
|
||||||
'Value': args.value,
|
'Redirect': args.redirect,
|
||||||
|
'AltCondition': args.alt_condition,
|
||||||
|
'AltRedirect': args.alt_redirect,
|
||||||
},
|
},
|
||||||
headers = {
|
headers = {
|
||||||
'Signature': 'SUS-SIGNATURE-' + hmac.new(
|
'Signature': 'SUS-SIGNATURE-' + hmac.new(
|
||||||
secret.encode("UTF-8"),
|
secret.encode("UTF-8"),
|
||||||
(args.command+":"+args.shortlink+":"+args.value).encode("UTF-8"),
|
formstring.encode("UTF-8"),
|
||||||
hashlib.sha256
|
hashlib.sha256
|
||||||
).hexdigest()
|
).hexdigest()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user