2021-12-03 22:04:09 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-12-08 05:36:41 +00:00
|
|
|
"strings"
|
2021-12-06 19:13:04 +00:00
|
|
|
"time"
|
2021-12-03 22:04:09 +00:00
|
|
|
|
2022-01-13 17:40:15 +00:00
|
|
|
"git.t-juice.club/torjus/ezshare/store"
|
2021-12-03 22:04:09 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-12-06 06:55:30 +00:00
|
|
|
"go.uber.org/zap"
|
2021-12-03 22:04:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPServer struct {
|
2021-12-06 06:55:30 +00:00
|
|
|
Logger *zap.SugaredLogger
|
2021-12-06 05:08:17 +00:00
|
|
|
store store.FileStore
|
2021-12-08 05:36:41 +00:00
|
|
|
binaryStore store.BinaryStore
|
2021-12-06 05:08:17 +00:00
|
|
|
serverGRPCCert []byte
|
|
|
|
grpcEndpoint string
|
2021-12-03 22:04:09 +00:00
|
|
|
|
|
|
|
http.Server
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:08:17 +00:00
|
|
|
type MetadataResponse struct {
|
|
|
|
GRPCEndpoint string `json:"grpc_endpoint"`
|
|
|
|
}
|
|
|
|
|
2021-12-08 05:36:41 +00:00
|
|
|
func NewHTTPSever(store store.FileStore, binaryStore store.BinaryStore, certBytes []byte, grpcEndpoint string) *HTTPServer {
|
2021-12-03 22:04:09 +00:00
|
|
|
srv := &HTTPServer{
|
2021-12-06 06:55:30 +00:00
|
|
|
Logger: zap.NewNop().Sugar(),
|
2021-12-06 05:08:17 +00:00
|
|
|
store: store,
|
2021-12-08 05:36:41 +00:00
|
|
|
binaryStore: binaryStore,
|
2021-12-06 05:08:17 +00:00
|
|
|
serverGRPCCert: certBytes,
|
|
|
|
grpcEndpoint: grpcEndpoint,
|
2021-12-03 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := chi.NewRouter()
|
2021-12-06 05:08:17 +00:00
|
|
|
r.Get("/server.pem", srv.ServerCertHandler)
|
|
|
|
r.Get("/metadata", srv.MetadataHandler)
|
2021-12-03 22:04:09 +00:00
|
|
|
r.Get("/files/{id}", srv.FileHandler)
|
2021-12-08 05:36:41 +00:00
|
|
|
r.Get("/b", srv.BinaryIndexHandler)
|
|
|
|
r.Get("/b/{filename}", srv.BinaryHandler)
|
2021-12-03 22:04:09 +00:00
|
|
|
|
|
|
|
srv.Handler = r
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:08:17 +00:00
|
|
|
func (s *HTTPServer) ServerCertHandler(w http.ResponseWriter, r *http.Request) {
|
2021-12-06 06:55:30 +00:00
|
|
|
if _, err := w.Write(s.serverGRPCCert); err != nil {
|
|
|
|
s.Logger.Warnw("Error sending server certificate.", "error", err, "remote_addr", r.RemoteAddr)
|
|
|
|
|
|
|
|
}
|
|
|
|
s.Logger.Infow("Sent server certificate.", "remote_addr", r.RemoteAddr)
|
2021-12-06 05:08:17 +00:00
|
|
|
}
|
2021-12-08 05:36:41 +00:00
|
|
|
func (s *HTTPServer) BinaryIndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
binaries, err := s.binaryStore.List()
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, "error listing binaries")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, bin := range binaries {
|
|
|
|
w.Write([]byte(fmt.Sprintf("%s\n", bin)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (s *HTTPServer) BinaryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := chi.URLParam(r, "filename")
|
|
|
|
split := strings.Split(filename, "-")
|
|
|
|
if len(split) != 4 {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, "invalid filename")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
version := split[1][1:]
|
|
|
|
operatingSystem := split[2]
|
|
|
|
arch := split[3]
|
|
|
|
release, err := s.binaryStore.GetBinary(version, operatingSystem, arch)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusNotFound, "release not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if operatingSystem == "windows" {
|
|
|
|
filename = fmt.Sprintf("%s.exe", filename)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
|
|
|
w.Write(release.Data)
|
|
|
|
}
|
2021-12-06 05:08:17 +00:00
|
|
|
func (s *HTTPServer) MetadataHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
md := &MetadataResponse{
|
|
|
|
GRPCEndpoint: s.grpcEndpoint,
|
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(w)
|
2021-12-06 06:55:30 +00:00
|
|
|
if err := encoder.Encode(md); err != nil {
|
|
|
|
s.Logger.Warnw("Error encoding or sending metadata.", "error", err, "remote_addr", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
s.Logger.Infow("Wrote server cert.", "remote_addr", r.RemoteAddr)
|
2021-12-06 05:08:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 22:04:09 +00:00
|
|
|
func (s *HTTPServer) FileHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := chi.URLParam(r, "id")
|
|
|
|
f, err := s.store.GetFile(id)
|
|
|
|
if err != nil {
|
2021-12-05 13:55:18 +00:00
|
|
|
if err == store.ErrNoSuchItem {
|
2021-12-06 06:55:30 +00:00
|
|
|
s.Logger.Debugw("Tried to get non-existing file.", "remote_addr", r.RemoteAddr)
|
2021-12-03 22:04:09 +00:00
|
|
|
WriteErrorResponse(w, http.StatusNotFound, "file not found")
|
|
|
|
return
|
|
|
|
}
|
2021-12-06 06:55:30 +00:00
|
|
|
s.Logger.Warnw("Error getting file from store.", "error", err, "remote_addr", r.RemoteAddr)
|
2021-12-03 22:04:09 +00:00
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-06 19:13:04 +00:00
|
|
|
// Check if expired, passcode required, or other reasons for not sending file
|
|
|
|
// To many views
|
|
|
|
if f.Metadata.MaxViews != 0 {
|
|
|
|
if f.Metadata.CurrentViews >= f.Metadata.MaxViews {
|
|
|
|
// File has been viewed max amount of times - delete
|
|
|
|
s.Logger.Infow("File has been viewed too many times.", "id", id)
|
|
|
|
if err := s.store.DeleteFile(id); err != nil {
|
|
|
|
s.Logger.Warnw("Error deleting file.", "id", id)
|
|
|
|
}
|
|
|
|
WriteErrorResponse(w, http.StatusNotFound, "file viewed too many times")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expired
|
|
|
|
if f.Metadata.ExpiresOn != nil {
|
|
|
|
if f.Metadata.ExpiresOn.AsTime().Before(time.Now()) {
|
|
|
|
// File has expired
|
|
|
|
s.Logger.Infow("File has been expired.", "id", id)
|
|
|
|
if err := s.store.DeleteFile(id); err != nil {
|
|
|
|
s.Logger.Warnw("Error deleting file.", "id", id)
|
|
|
|
}
|
|
|
|
WriteErrorResponse(w, http.StatusNotFound, "file not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Passcode required
|
|
|
|
if f.Metadata.Passcode != "" {
|
|
|
|
if f.Metadata.Passcode != r.URL.Query().Get("passcode") {
|
|
|
|
WriteErrorResponse(w, http.StatusUnauthorized, "wrong passcode")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 22:04:09 +00:00
|
|
|
w.Header().Add("Content-Type", http.DetectContentType(f.Data))
|
2021-12-06 06:55:30 +00:00
|
|
|
if _, err := w.Write(f.Data); err != nil {
|
|
|
|
s.Logger.Warnw("Error sending file.", "error", err, "remote_addr", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
s.Logger.Infow("Sent file.", "remote_addr", r.RemoteAddr)
|
2021-12-06 19:13:04 +00:00
|
|
|
f.Metadata.CurrentViews++
|
|
|
|
|
|
|
|
if _, err := s.store.StoreFile(f); err != nil {
|
|
|
|
s.Logger.Warnw("Error storing updated file.", "error", err)
|
|
|
|
}
|
2021-12-03 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteErrorResponse(w http.ResponseWriter, status int, message string) {
|
|
|
|
errMessage := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Error: message,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
encoder.Encode(&errMessage)
|
|
|
|
}
|