Skip to content

Commit

Permalink
Fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelreyna committed Aug 20, 2020
1 parent 6b17dd0 commit c389ca3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type Conf struct {

WaitForEOF bool

AllowBots bool

cmdFlagSet *pflag.FlagSet

sstlsLoc string
Expand Down
2 changes: 1 addition & 1 deletion cmd/conf/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Conf) setupDownloadRoute(args []string, srvr *server.Server) (*server.R
header.Set(k, v)
}

route.HandlerFunc = handlers.HandleDownload(file, !c.NoDownload, header, srvr.InfoLog)
route.HandlerFunc = handlers.HandleDownload(file, !c.NoDownload, !c.AllowBots, header, srvr.InfoLog)

return route, nil
}
6 changes: 6 additions & 0 deletions cmd/conf/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ See also: --upload-file; -u, --upload; -L, --no-unix-eol-norm`,

flags.BoolVarP(&c.WaitForEOF, "wait-for-eof", "J", false, `Wait for EOF before starting HTTP(S) server if serving from stdin.
This flag does noting if not serving from stdin.
`,
)

flags.BoolVarP(&c.AllowBots, "allow-bots", "B", false, `Allow bots to attempt download.
By default, bots are prevented from attempting the download; this is required to allow links to be sent over services that provide previews such as Apple iMessage.
A client is considered to be a bot if the 'User-Agent' header contains either 'bot', 'Bot' or 'facebookexternalhit'.
`,
)
}
4 changes: 2 additions & 2 deletions internal/handlers/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/raphaelreyna/oneshot/internal/server"
)

func HandleDownload(file *file.FileReader, download bool, header http.Header,
func HandleDownload(file *file.FileReader, download, noBots bool, header http.Header,
infoLog *log.Logger) func(w http.ResponseWriter, r *http.Request) error {
msg := "transfer complete:\n"
msg += "\tname: %s\n"
Expand Down Expand Up @@ -88,7 +88,7 @@ func HandleDownload(file *file.FileReader, download bool, header http.Header,

return func(w http.ResponseWriter, r *http.Request) error {
// Filter out requests from bots, iMessage, etc.
if headers, exists := r.Header["User-Agent"]; exists {
if headers, exists := r.Header["User-Agent"]; exists && noBots {
for _, header := range headers {
isBot := strings.Contains(header, "bot")
if !isBot {
Expand Down

0 comments on commit c389ca3

Please sign in to comment.