Skip to content

Commit 1e14db4

Browse files
committed
Added option to disable directory listing in cli
1 parent 6934618 commit 1e14db4

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

cmd/path.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55
import (
66
"errors"
77

8+
"github.com/loophole/cli/config"
89
"github.com/loophole/cli/internal/app/loophole"
910
lm "github.com/loophole/cli/internal/app/loophole/models"
1011
"github.com/loophole/cli/internal/pkg/communication"
@@ -55,6 +56,7 @@ To expose local directory (e.g. /data/my-data) simply use 'loophole path /data/m
5556
}
5657

5758
func init() {
59+
dirCmd.PersistentFlags().BoolVar(&config.Config.Display.DisableDirectoryListing, "disable-directory-listing", false, "Disable showing all files when navigating to directories without index.html")
5860
initServeCommand(dirCmd)
5961
rootCmd.AddCommand(dirCmd)
6062
}

config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ type OAuthConfig struct {
1515

1616
// DisplayConfig defines the display switches shape
1717
type DisplayConfig struct {
18-
Verbose bool `json:"verbose"`
19-
QR bool `json:"qr"`
18+
DisableDirectoryListing bool `json:"disabledirectorylisting"`
19+
Verbose bool `json:"verbose"`
20+
QR bool `json:"qr"`
2021
}
2122

2223
// ApplicationConfig defines the application config shape

internal/pkg/httpserver/httpserver.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"net/http"
77
"net/http/httputil"
88
"net/url"
9+
"path/filepath"
910

1011
auth "github.com/abbot/go-http-auth"
12+
"github.com/loophole/cli/config"
1113
lm "github.com/loophole/cli/internal/app/loophole/models"
1214
"github.com/loophole/cli/internal/pkg/urlmaker"
1315
"golang.org/x/crypto/bcrypt"
@@ -18,6 +20,34 @@ const (
1820
logoURL = "https://raw.githubusercontent.com/loophole/website/master/static/img/logo.png"
1921
)
2022

23+
type customFileSystem struct {
24+
fs http.FileSystem
25+
}
26+
27+
func (cfs customFileSystem) Open(path string) (http.File, error) {
28+
f, err := cfs.fs.Open(path)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
s, err := f.Stat()
34+
if err != nil {
35+
return nil, err
36+
}
37+
if s.IsDir() {
38+
index := filepath.Join(path, "index.html")
39+
if _, err := cfs.fs.Open(index); err != nil {
40+
closeErr := f.Close()
41+
if closeErr != nil {
42+
return nil, err
43+
}
44+
return nil, err
45+
}
46+
}
47+
48+
return f, nil
49+
}
50+
2151
type ServerBuilder interface {
2252
WithSiteID(string) ServerBuilder
2353
WithDomain(string) ServerBuilder
@@ -177,7 +207,12 @@ func (ssb *staticServerBuilder) WithBasicAuth(username string, password string)
177207
}
178208

179209
func (ssb *staticServerBuilder) Build() (*http.Server, error) {
180-
fs := http.FileServer(http.Dir(ssb.directory))
210+
var fs http.Handler
211+
if config.Config.Display.DisableDirectoryListing {
212+
fs = http.FileServer(customFileSystem{http.Dir(ssb.directory)})
213+
} else {
214+
fs = http.FileServer(http.Dir(ssb.directory))
215+
}
181216

182217
var server *http.Server
183218

0 commit comments

Comments
 (0)