-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configure directory listing #136
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//necessary bits and pieces for being able to serve e.g. the custom notification html | ||
// without needing to create files in the users folders | ||
package customfilesystem | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/fs" | ||
"net/http" | ||
"path/filepath" | ||
) | ||
|
||
var DirectoryListingDisabledPage = []byte("<!DOCTYPE html><html><body><img src=\"https://raw.githubusercontent.com/loophole/website/master/static/img/logo.png\" alt=\"https://raw.githubusercontent.com/loophole/website/master/static/img/logo.png\" class=\"transparent shrinkToFit\" width=\"400\" height=\"88\"><p>Directory index listing has been disabled. Please enter the path of a file.</p></body></html>") | ||
|
||
type CustomFileSystem struct { | ||
FS http.FileSystem | ||
} | ||
|
||
//the file cannot be reused since it's io.Reader can only be read from once, | ||
// so we need a reusable way to create it | ||
func writeDirectoryListingDisabledPageFile(pageFile *MyFile) { | ||
*pageFile = MyFile{ | ||
Reader: bytes.NewReader(DirectoryListingDisabledPage), | ||
mif: myFileInfo{ | ||
name: "customIndex.html", | ||
data: DirectoryListingDisabledPage, | ||
}, | ||
} | ||
} | ||
|
||
func (cfs CustomFileSystem) Open(path string) (http.File, error) { | ||
f, err := _Open(path, cfs) | ||
|
||
if err != nil { | ||
var pathErrorInstance error = &fs.PathError{ | ||
Err: errors.New(""), | ||
} | ||
if errors.As(err, &pathErrorInstance) { | ||
return nil, err | ||
} | ||
var pageFile *MyFile = &MyFile{} | ||
writeDirectoryListingDisabledPageFile(pageFile) | ||
return pageFile, nil | ||
} | ||
return f, nil | ||
} | ||
|
||
//if there is an elegant way to integrate the following into the function above without | ||
// using labeled breaks or adding even more control structures let me know | ||
func _Open(path string, cfs CustomFileSystem) (http.File, error) { | ||
f, err := cfs.FS.Open(path) | ||
if err != nil { | ||
if path == "/" { | ||
var pageFile *MyFile = &MyFile{} | ||
writeDirectoryListingDisabledPageFile(pageFile) | ||
return pageFile, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
s, err := f.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if s.IsDir() { | ||
index := filepath.Join(path, "index.html") | ||
if _, err := cfs.FS.Open(index); err != nil { | ||
var pageFile *MyFile = &MyFile{} | ||
writeDirectoryListingDisabledPageFile(pageFile) | ||
return pageFile, nil | ||
} | ||
} | ||
return f, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//taken from https://stackoverflow.com/questions/52697277/simples-way-to-make-a-byte-into-a-virtual-file-object-in-golang | ||
package customfilesystem | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"time" | ||
) | ||
|
||
type File interface { | ||
io.Closer | ||
io.Reader | ||
io.Seeker | ||
Readdir(count int) ([]os.FileInfo, error) | ||
Stat() (os.FileInfo, error) | ||
} | ||
|
||
type myFileInfo struct { | ||
name string | ||
data []byte | ||
} | ||
|
||
func (mif myFileInfo) Name() string { return mif.name } | ||
func (mif myFileInfo) Size() int64 { return int64(len(mif.data)) } | ||
func (mif myFileInfo) Mode() os.FileMode { return 0444 } // Read for all | ||
func (mif myFileInfo) ModTime() time.Time { return time.Time{} } // Return anything | ||
func (mif myFileInfo) IsDir() bool { return false } | ||
func (mif myFileInfo) Sys() interface{} { return nil } | ||
|
||
type MyFile struct { | ||
*bytes.Reader | ||
mif myFileInfo | ||
} | ||
|
||
func (mf *MyFile) Close() error { return nil } // Noop, nothing to do | ||
|
||
func (mf *MyFile) Readdir(count int) ([]os.FileInfo, error) { | ||
return nil, nil // We are not a directory but a single file | ||
} | ||
|
||
func (mf *MyFile) Stat() (os.FileInfo, error) { | ||
return mf.mif, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react"; | ||
|
||
interface DirectorySettingsProps { | ||
usingValue: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
usingChangeCallback: Function; | ||
} | ||
|
||
const DirectorySettings = (props: DirectorySettingsProps): JSX.Element => { | ||
const disableDirectoryListing = props.usingValue; | ||
const setDisableDirectoryListing = props.usingChangeCallback; | ||
|
||
return ( | ||
<div> | ||
<div className="field"> | ||
<div className="control"> | ||
<label className="checkbox"> | ||
<input | ||
type="checkbox" | ||
onChange={(e) => { | ||
setDisableDirectoryListing(!disableDirectoryListing); | ||
}} | ||
/>{" "} | ||
I want to disable Directory Listing | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DirectorySettings; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,4 +5,5 @@ import RemoteEndpointSpecs from './RemoteEndpointSpecs'; | |||||
export default interface ExposeDirectoryMessage { | ||||||
local: LocalDirectorySpecs; | ||||||
remote: RemoteEndpointSpecs; | ||||||
deactivatedirectorylisting: boolean; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ import { | |||||
isLocalPathValid, | ||||||
isLoopholeHostnameValid, | ||||||
} from "../features/validator/validators"; | ||||||
import DirectorySettings from "../components/form/DirectorySettings"; | ||||||
|
||||||
const DirectoryPage = () => { | ||||||
const dispatch = useDispatch(); | ||||||
|
@@ -31,6 +32,7 @@ const DirectoryPage = () => { | |||||
const [basicAuthUsername, setBasicAuthUsername] = useState(""); | ||||||
const [basicAuthPassword, setBasicAuthPassword] = useState(""); | ||||||
const [disableOldCiphers, setDisableOldCiphers] = useState(false); | ||||||
const [disableDirectoryListing, setDisableDirectoryListing] = useState(false); | ||||||
|
||||||
const areInputsValid = (): boolean => { | ||||||
if (!isLocalPathValid(path)) return false; | ||||||
|
@@ -55,6 +57,7 @@ const DirectoryPage = () => { | |||||
disableOldCiphers: false, | ||||||
tunnelId: uuidv4(), | ||||||
}, | ||||||
deactivatedirectorylisting: disableDirectoryListing | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
as in other places? |
||||||
}; | ||||||
if (usingCustomHostname) { | ||||||
options.remote.siteId = customHostname; | ||||||
|
@@ -122,6 +125,13 @@ const DirectoryPage = () => { | |||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
<div className="column is-12"> | ||||||
<h5 className="title is-5">Directory Listing</h5> | ||||||
<DirectorySettings | ||||||
usingValue={disableDirectoryListing} | ||||||
usingChangeCallback={setDisableDirectoryListing} | ||||||
/> | ||||||
</div> | ||||||
<div className="column is-12"> | ||||||
<div className="field is-grouped is-pulled-right"> | ||||||
<div className="control"> | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -55,6 +55,7 @@ const WebDav = () => { | |||||
disableOldCiphers: false, | ||||||
tunnelId: uuidv4(), | ||||||
}, | ||||||
deactivatedirectorylisting: false, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
as in other places? |
||||||
}; | ||||||
if (usingCustomHostname) { | ||||||
options.remote.siteId = customHostname; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
|
||
"github.com/gorilla/websocket" | ||
|
||
"github.com/loophole/cli/config" | ||
"github.com/loophole/cli/internal/app/loophole" | ||
lm "github.com/loophole/cli/internal/app/loophole/models" | ||
"github.com/loophole/cli/internal/pkg/cache" | ||
|
@@ -98,6 +99,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) { | |
communication.Warn(err.Error()) | ||
} | ||
|
||
config.Config.Display.DisableDirectoryListing = exposeDirectoryConfig.DisableDirectoryListing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would not use |
||
tunnelQuitChannel := make(chan bool) | ||
go func() { | ||
sshDir := cache.GetLocalStorageDir(".ssh") //getting our sshDir and creating it, if it doesn't exist | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use camelCase syntax