Skip to content

Commit

Permalink
feat(hashing): add hash to generated links
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Nov 27, 2023
1 parent 75dc8a7 commit 355cef5
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ In addition to the mandatory variables, you can also set the following optional

- `HOST` : A Fully Qualified Domain Name if present or use your server IP. (eg. `https://example.com` or `http://14.1.154.2:8080`)

- `HASH_LENGTH` : This is the custom hash length for generated URLs. The hash length must be greater than 5 and less than or equal to 32. The default value is 6.

## Contributing

Expand Down
21 changes: 18 additions & 3 deletions commands/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
if u.EffectiveMessage.Media == nil {
return dispatcher.EndGroups
}
if len(u.Entities.Chats) != 0 {
chatId := u.EffectiveChat().GetID()
peerChatId := storage.GetPeerById(chatId)
if peerChatId.Type != int(storage.TypeUser) {
return dispatcher.EndGroups
}
chatId := u.EffectiveChat().GetID()
peer := storage.GetPeerById(config.ValueOf.LogChannelID)
switch storage.EntityType(peer.Type) {
case storage.TypeChat:
Expand Down Expand Up @@ -57,7 +58,21 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
return dispatcher.EndGroups
}
link := fmt.Sprintf("%s/stream/%d", config.ValueOf.Host, messageID)
doc := update.(*tg.Updates).Updates[1].(*tg.UpdateNewChannelMessage).Message.(*tg.Message).Media
file, err := utils.FileFromMedia(doc)
if err != nil {
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
return dispatcher.EndGroups
}
fullHash := utils.PackFile(
file.FileName,
file.FileSize,
file.MimeType,
file.ID,
)

hash := utils.GetShortHash(fullHash)
link := fmt.Sprintf("%s/stream/%d?hash=%s", config.ValueOf.Host, messageID, hash)
ctx.Reply(u, link, nil)
return dispatcher.EndGroups
}
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type config struct {
Dev bool `envconfig:"DEV" default:"false"`
Port int `envconfig:"PORT" default:"8080"`
Host string `envconfig:"HOST" default:"http://localhost:8080"`
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
}

func (c *config) setupEnvVars() {
Expand All @@ -33,6 +34,18 @@ func Load(log *zap.Logger) {
defer log.Info("Loaded config")
ValueOf.setupEnvVars()
ValueOf.LogChannelID = int64(stripInt(log, int(ValueOf.LogChannelID)))
if ValueOf.HashLength == 0 {
log.Sugar().Info("HASH_LENGTH can't be 0, defaulting to 6")
ValueOf.HashLength = 6
}
if ValueOf.HashLength > 32 {
log.Sugar().Info("HASH_LENGTH can't be more than 32, changing to 32")
ValueOf.HashLength = 32
}
if ValueOf.HashLength < 5 {
log.Sugar().Info("HASH_LENGTH can't be less than 5, defaulting to 6")
ValueOf.HashLength = 6
}
}

func stripInt(log *zap.Logger, a int) int {
Expand Down
17 changes: 17 additions & 0 deletions routes/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func getStreamRoute(ctx *gin.Context) {
return
}

authHash := ctx.Query("hash")
if authHash == "" {
http.Error(w, "missing hash param", http.StatusBadRequest)
return
}

ctx.Header("Accept-Ranges", "bytes")
var start, end int64
rangeHeader := r.Header.Get("Range")
Expand All @@ -48,6 +54,17 @@ func getStreamRoute(ctx *gin.Context) {
return
}

expectedHash := utils.PackFile(
file.FileName,
file.FileSize,
file.MimeType,
file.ID,
)
if !utils.CheckHash(authHash, expectedHash) {
http.Error(w, "invalid hash", http.StatusBadRequest)
return
}

if rangeHeader == "" {
start = 0
end = file.FileSize - 1
Expand Down
27 changes: 26 additions & 1 deletion types/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package types

import "github.com/gotd/td/tg"
import (
"bytes"
"crypto/md5"
"encoding/gob"
"fmt"

"github.com/gotd/td/tg"
)

type File struct {
Location *tg.InputDocumentFileLocation
Expand All @@ -9,3 +16,21 @@ type File struct {
MimeType string
ID int64
}

type HashableFileStruct struct {
FileName string
FileSize int64
MimeType string
FileID int64
}

func (f *HashableFileStruct) Pack() string {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(f)
return FromBytes(b.Bytes())
}

func FromBytes(data []byte) string {
result := md5.Sum(data)
return fmt.Sprintf("%x", result)
}
9 changes: 0 additions & 9 deletions types/part.go

This file was deleted.

18 changes: 18 additions & 0 deletions utils/hashing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"EverythingSuckz/fsb/config"
"EverythingSuckz/fsb/types"
)

func PackFile(fileName string, fileSize int64, mimeType string, fileID int64) string {
return (&types.HashableFileStruct{FileName: fileName, FileSize: fileSize, MimeType: mimeType, FileID: fileID}).Pack()
}

func GetShortHash(fullHash string) string {
return fullHash[:config.ValueOf.HashLength]
}

func CheckHash(inputHash string, expectedHash string) bool {
return inputHash == GetShortHash(expectedHash)
}

0 comments on commit 355cef5

Please sign in to comment.