pass alt redirect and alt condition and alt redirect

This commit is contained in:
Akbar Rahman 2022-09-20 22:16:20 +01:00
parent 1f2b1dd8e5
commit 6d386bf591
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
3 changed files with 29 additions and 19 deletions

View File

@ -9,3 +9,4 @@ services:
redis:
image: redis:7
volumes: [ "./redis:/data" ]
ports: [ "6379:6379" ]

31
main.go
View File

@ -72,17 +72,27 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
command := r.PostForm.Get("Command")
shortlink := r.PostForm.Get("Shortlink")
value := r.PostForm.Get("Value")
fmt.Printf("command: %v, shortlink: %v, value: %v\n", command, shortlink, value)
fmt.Println(shortlink)
fmt.Println(value)
redirect := r.PostForm.Get("Redirect")
alt_redirect := r.PostForm.Get("AltRedirect")
alt_condition := r.PostForm.Get("AltCondition")
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")
calculatedSignature := fmt.Sprintf(
"SUS-SIGNATURE-%v",
getSha256HMACSignature(
[]byte(SECRET),
command+":"+shortlink+":"+value,
command+":"+shortlink+":"+redirect+":"+alt_condition+":"+alt_redirect,
),
)
@ -100,7 +110,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
_, err := client.Get(ctx, shortlink).Result()
if err == redis.Nil {
err = client.Set(ctx, shortlink, value, 0).Err()
err = client.Set(ctx, shortlink, shortlink, 0).Err()
if err != nil {
fmt.Println(err)
@ -128,11 +138,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
}
if command == "delete" {
if value != "confirm" {
w.WriteHeader(400)
w.Write([]byte("400 Bad Request"))
}
ctx := context.Background()
if err := client.Del(ctx, shortlink).Err(); err != nil {
w.WriteHeader(500)
@ -152,13 +157,13 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
resp := ""
for _, key := range keys {
value, err = client.Get(ctx, key).Result()
shortlink, err = client.Get(ctx, key).Result()
if err == redis.Nil {
w.WriteHeader(500)
w.Write([]byte("500 Internal Server Error"))
return
}
resp += key + ":" + value + "\n"
resp += key + ":" + shortlink + "\n"
}
w.WriteHeader(200)

View File

@ -18,8 +18,10 @@ def get_args():
parser.add_argument('command')
parser.add_argument('-s', '--server', default="")
parser.add_argument('-l', '--shortlink', default="")
parser.add_argument('-v', '--value', default="")
parser.add_argument('-c', '--config', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.config/susmng/config.json')))
parser.add_argument('-r', '--redirect', default="")
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')
return parser.parse_args()
@ -48,19 +50,21 @@ def main(args):
secret = config['secrets'][server]
if args.command == "delete" and args.value != "confirm":
print("--value not set to 'confirm'... delete operation may fail")
formstring = args.command+":"+args.shortlink+":"+args.redirect+":"+args.alt_condition+":"+args.alt_redirect
print(f"{formstring=}")
r = requests.post(f"{'http' if args.http else 'https'}://{server}",
data = {
'Command': args.command,
'Shortlink': args.shortlink,
'Value': args.value,
'Redirect': args.redirect,
'AltCondition': args.alt_condition,
'AltRedirect': args.alt_redirect,
},
headers = {
'Signature': 'SUS-SIGNATURE-' + hmac.new(
secret.encode("UTF-8"),
(args.command+":"+args.shortlink+":"+args.value).encode("UTF-8"),
formstring.encode("UTF-8"),
hashlib.sha256
).hexdigest()
}