add file listing if nothing is found

This commit is contained in:
Zack Scholl 2019-07-12 15:15:51 -07:00
parent 3520f36f72
commit 78f06e76ca
3 changed files with 124 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package server
import (
"encoding/json"
"fmt"
"html/template"
"math/rand"
@ -168,6 +169,7 @@ Disallow:`))
// send GET request to websockets
var data string
var fs []File
data, err = s.get(domain, pathToFile, ipAddress)
if err != nil {
// try index.html if it doesn't exist
@ -188,7 +190,28 @@ Disallow:`))
}
if err != nil {
log.Debugf("problem getting: %s", err.Error())
return
// just serve files
fs, err = s.getFiles(domain, ipAddress)
log.Debugf("fs: %+v", fs)
if err != nil {
log.Debug(err)
return
}
b, _ := Asset("templates/files.html")
var t *template.Template
t, err = template.New("files").Parse(string(b))
if err != nil {
log.Error(err)
return
}
return t.Execute(w, struct {
Files []File
Domain string
}{
Domain: domain,
Files: fs,
})
}
}
}
@ -299,6 +322,69 @@ func (s *server) isdomain(domain string) bool {
return ok
}
type File struct {
FullPath string `json:"fullPath"`
Upload Upload `json:"upload"`
}
type Upload struct {
UUID string `json:"uuid"`
Total int `json:"total"`
Filename string `json:"filename"`
}
func (s *server) getFiles(domain, ipAddress string) (fs []File, err error) {
var connections []*connection
s.Lock()
if _, ok := s.conn[domain]; ok {
connections = s.conn[domain]
}
s.Unlock()
if connections == nil || len(connections) == 0 {
err = fmt.Errorf("no connections available for domain %s", domain)
log.Debug(err)
return
}
log.Debugf("requesting files of %s from %d connections", domain, len(connections))
// any connection that initated with this key is viable
key := connections[0].Key
// loop through connections randomly and try to get one to serve the file
for _, i := range rand.Perm(len(connections)) {
var p wsconn.Payload
p, err = func() (p wsconn.Payload, err error) {
err = connections[i].ws.Send(wsconn.Payload{
Type: "files",
Message: "all",
IPAddress: ipAddress,
})
if err != nil {
return
}
p, err = connections[i].ws.Receive()
return
}()
if err != nil {
log.Debug(err)
s.dumpConnection(domain, connections[i].ID)
continue
}
log.Tracef("recv: %+v", p)
if p.Type == "files" && p.Key == key {
if !p.Success {
err = fmt.Errorf(p.Message)
return
}
err = json.Unmarshal([]byte(p.Message), &fs)
return
}
log.Debugf("no good data from %d", i)
}
err = fmt.Errorf("invalid response")
return
}
func (s *server) get(domain, filePath, ipAddress string) (payload string, err error) {
var connections []*connection
s.Lock()

18
static/main.js vendored
View file

@ -121,7 +121,23 @@ const socketMessageListener = (event) => {
}
console.log(data)
consoleLog(`[debug] ${data.message}`)
if (data.type == "get") {
if (data.type == "files") {
if (files.length > 0) {
socketSend({
type: "files",
message: JSON.stringify(files),
success: true,
key: document.getElementById("inputKey").value,
});
} else {
socketSend({
type: "files",
message: "none found",
success: false,
key: document.getElementById("inputKey").value,
});
}
} else if (data.type == "get") {
var foundFile = false
var iToSend = 0
for (i = 0; i < files.length; i++) {

20
templates/files.html vendored Normal file
View file

@ -0,0 +1,20 @@
<html>
<head>
</head>
<body>
<ul>
{{range .Files}}
<li>
{{ if .FullPath }}
<a href="/{{$.Domain}}/{{.FullPath}}">{{.FullPath}}</a>
{{else}}
<a href="/{{$.Domain}}/{{.Upload.Filename}}">{{.Upload.Filename}}</a>
{{end}}
</li>
{{end}}
</ul>
</body>
</html>