Browse Source

fixing issues with disallow bots

master
forest 1 month ago
parent
commit
37c452254c
4 changed files with 26 additions and 49 deletions
  1. +1
    -0
      Dockerfile
  2. +1
    -1
      build-docker.sh
  3. +11
    -41
      disallowbots.gotemplate.html
  4. +13
    -7
      main.go

+ 1
- 0
Dockerfile View File

@ -15,5 +15,6 @@ WORKDIR /app
COPY --from=build /build/picopublish /app/picopublish
COPY ./static /app/static
COPY ./index.html /app/index.html
COPY ./disallowbots.gotemplate.html /app/disallowbots.gotemplate.html
RUN chmod +x /app/picopublish
ENTRYPOINT ["/app/picopublish"]

+ 1
- 1
build-docker.sh View File

@ -1,6 +1,6 @@
#!/bin/bash -e
VERSION="0.2.0"
VERSION="0.2.1-test9"
rm -rf dockerbuild || true
mkdir dockerbuild


+ 11
- 41
disallowbots.gotemplate.html View File

@ -5,41 +5,9 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>picopublish</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.main {
display: flex;
margin-top: 2rem;
margin-bottom: 2rem;
background-color: #aaa;
}
</style>
<script>
function myCreateElement(parent, tag, attr, textContent) {
const element = document.createElement(tag);
if(attr) {
Object.entries(attr).forEach(([k,v]) => {
if (v !== undefined) {
if(typeof v == 'function') {
element[k] = v;
} else if(typeof v == 'object') {
Object.entries(v).forEach(([k1,v1]) => {
element[k][k1] = v1;
});
} else {
element.setAttribute(k, v);
}
}
});
}
if(textContent) {
element.textContent = textContent;
}
parent.appendChild(element);
return element;
}
</script>
<link rel="stylesheet" type="text/css" href="/static/application.css">
<link rel="stylesheet" type="text/css" href="{{ .CaptchaURL }}/static/captcha.css">
<script src="{{ .CaptchaURL }}/static/captcha.js"></script>
</head>
<body>
@ -53,8 +21,7 @@
<div class="splash content">
<h3>Please wait for a moment...</h3>
<form method="POST" action="">
<input type="text" name="item" />
<form method="POST" action="" style="font-size: 13px;">
<input type="hidden" name="challenge" value="{{ .Challenge }}" />
<input type="hidden" name="nonce" />
<div class="captcha-container"
@ -71,10 +38,13 @@
document.querySelector("form").submit();
}, 500);
};
</script>
<script src="{{ .CaptchaURL }}/static/captcha.js"></script>
<script>
// note its important that the CSS is already loaded at this point,
// otherwise the script will wait to create the captcha element
// until the css is loaded, and so code inside sqrCaptchaTrigger will fail
// because it will try to update the captcha element that doesn't exist yet.
window.sqrCaptchaInit();
window.sqrCaptchaTrigger();
</script>
</body>
</html>

+ 13
- 7
main.go View File

@ -80,6 +80,10 @@ func main() {
panic(errors.New("can't start the app because can't parse PICOPUBLISH_CAPTCHA_PUBLIC_URL"))
}
disallowBotsToken = map[string]SolvedDisallowBotsChallenge{}
httpClient = &http.Client{
Timeout: time.Second * time.Duration(5),
}
loadCaptchaChallengesMutex = &sync.Mutex{}
captchaChallengesMutex = &sync.Mutex{}
@ -90,7 +94,7 @@ func main() {
// https://stackoverflow.com/questions/49589685/good-way-to-disable-directory-listing-with-http-fileserver-in-go
noDirectoryListingHTTPDir := justFilesFilesystem{fs: http.Dir(dataPath), readDirBatchSize: 20}
readFileHandler = http.StripPrefix("/files/", http.FileServer(noDirectoryListingHTTPDir))
readFileHandler = http.FileServer(noDirectoryListingHTTPDir)
http.HandleFunc("/files/", files)
@ -148,7 +152,7 @@ func files(response http.ResponseWriter, request *http.Request) {
// Ensure the captcha challenge has been solved by this user within the last day. If so, serve the file.
// Otherwise, redirect to a new challenge.
if getIdentityHash(*request) == solved.IdentityHash && time.Since(solved.Time) < time.Hour*24 {
http.StripPrefix(fmt.Sprintf("/%s/", fileFirstPathElement), readFileHandler).ServeHTTP(response, request)
http.StripPrefix(fmt.Sprintf("/files/%s/", fileFirstPathElement), readFileHandler).ServeHTTP(response, request)
return
} else {
http.Redirect(response, request, strings.Replace(request.RequestURI, fileFirstPathElement, getNewToken(), 1), 302)
@ -186,12 +190,14 @@ func files(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("500 internal server error"))
return
}
response.Header().Set("Content-Type", "text/html; charset=UTF-8")
response.Write(htmlBytes)
return
}
}
// default: just serve the dang file :D
readFileHandler.ServeHTTP(response, request)
http.StripPrefix("/files/", readFileHandler).ServeHTTP(response, request)
} else if request.Method == "POST" {
@ -438,10 +444,10 @@ func getIdentityHash(request http.Request) string {
remoteAddrString = parsedRemoteAddr.IP.String()
}
log.Printf(
"\n\nresolveTCPAddr: %s, X-Forwarded-For: %s, X-Real-IP: %s\n\n",
remoteAddrString, request.Header.Get("X-Forwarded-For"), request.Header.Get("X-Real-IP"),
)
// log.Printf(
// "\n\nresolveTCPAddr: %s, X-Forwarded-For: %s, X-Real-IP: %s\n\n",
// remoteAddrString, request.Header.Get("X-Forwarded-For"), request.Header.Get("X-Real-IP"),
// )
if request.Header.Get("X-Forwarded-For") != "" {
remoteAddrString = request.Header.Get("X-Forwarded-For")
} else if request.Header.Get("X-Real-IP") != "" {


Loading…
Cancel
Save