2015-09-18 17:13:38 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-11-05 17:18:42 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-18 17:13:38 +02:00
|
|
|
"io/ioutil"
|
2016-12-27 01:35:45 +01:00
|
|
|
"log"
|
2015-09-18 17:13:38 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-12-28 00:57:25 +01:00
|
|
|
"strings"
|
2015-09-18 17:13:38 +02:00
|
|
|
"time"
|
2016-12-27 18:57:49 +01:00
|
|
|
|
2016-12-28 00:57:25 +01:00
|
|
|
"goji.io/middleware"
|
2016-12-27 18:57:49 +01:00
|
|
|
"goji.io/pat"
|
2015-09-18 17:13:38 +02:00
|
|
|
)
|
|
|
|
|
2016-11-11 01:29:55 +01:00
|
|
|
func isHashed(dir string) bool {
|
|
|
|
return dir == "data"
|
|
|
|
}
|
|
|
|
|
2016-12-28 00:57:25 +01:00
|
|
|
func getRepo(r *http.Request) string {
|
2017-01-25 20:59:18 +01:00
|
|
|
if strings.HasPrefix(fmt.Sprintf("%s", middleware.Pattern(r.Context())), "/:repo") {
|
2016-12-30 20:57:48 +01:00
|
|
|
return filepath.Join(config.path, pat.Param(r, "repo"))
|
2016-12-28 00:57:25 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 20:57:48 +01:00
|
|
|
return config.path
|
2016-12-28 00:57:25 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
func createDirectories(path string) {
|
2017-01-25 20:59:18 +01:00
|
|
|
log.Printf("Creating repository directories in %s\n", path)
|
2016-12-27 01:35:45 +01:00
|
|
|
|
2016-12-27 16:15:54 +01:00
|
|
|
if err := os.MkdirAll(path, 0700); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-12-27 01:35:45 +01:00
|
|
|
dirs := []string{
|
|
|
|
"data",
|
|
|
|
"index",
|
|
|
|
"keys",
|
|
|
|
"locks",
|
|
|
|
"snapshots",
|
|
|
|
"tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dirs {
|
2016-12-27 13:42:43 +01:00
|
|
|
if err := os.MkdirAll(filepath.Join(path, d), 0700); err != nil {
|
2016-12-27 01:35:45 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 256; i++ {
|
2016-12-27 13:42:43 +01:00
|
|
|
if err := os.MkdirAll(filepath.Join(path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
|
2016-12-27 01:35:45 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:15:33 +01:00
|
|
|
// AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
|
|
|
|
// stored in f and returns the http.HandlerFunc.
|
2015-09-19 14:52:50 +02:00
|
|
|
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2016-11-06 18:59:19 +01:00
|
|
|
if username, password, ok := r.BasicAuth(); !ok || !f.Validate(username, password) {
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
2015-09-19 14:52:50 +02:00
|
|
|
return
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2015-09-19 14:52:50 +02:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// CheckConfig checks whether a configuration exists.
|
|
|
|
func CheckConfig(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("CheckConfig()")
|
|
|
|
}
|
2017-01-16 23:00:46 +01:00
|
|
|
cfg := filepath.Join(getRepo(r), "config")
|
|
|
|
st, err := os.Stat(cfg)
|
2016-12-27 13:42:43 +01:00
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// GetConfig allows for a config to be retrieved.
|
|
|
|
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("GetConfig()")
|
|
|
|
}
|
2017-01-16 23:00:46 +01:00
|
|
|
cfg := filepath.Join(getRepo(r), "config")
|
|
|
|
bytes, err := ioutil.ReadFile(cfg)
|
2016-12-27 13:42:43 +01:00
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Write(bytes)
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// SaveConfig allows for a config to be saved.
|
|
|
|
func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("SaveConfig()")
|
|
|
|
}
|
2017-01-16 23:00:46 +01:00
|
|
|
cfg := filepath.Join(getRepo(r), "config")
|
2016-12-27 13:42:43 +01:00
|
|
|
bytes, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2017-01-16 23:00:46 +01:00
|
|
|
if err := ioutil.WriteFile(cfg, bytes, 0600); err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Write([]byte("200 ok"))
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
// DeleteConfig removes a config.
|
|
|
|
func DeleteConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if config.debug {
|
|
|
|
log.Println("DeleteConfig()")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Remove(filepath.Join(getRepo(r), "config"))
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// ListBlobs lists all blobs of a given type in an arbitrary order.
|
|
|
|
func ListBlobs(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("ListBlobs()")
|
|
|
|
}
|
2016-12-27 18:57:49 +01:00
|
|
|
dir := pat.Param(r, "type")
|
2016-12-28 00:57:25 +01:00
|
|
|
path := filepath.Join(getRepo(r), dir)
|
2016-12-27 13:42:43 +01:00
|
|
|
|
|
|
|
items, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
var names []string
|
|
|
|
for _, i := range items {
|
|
|
|
if isHashed(dir) {
|
|
|
|
subpath := filepath.Join(path, i.Name())
|
|
|
|
subitems, err := ioutil.ReadDir(subpath)
|
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2016-11-06 14:02:43 +01:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
for _, f := range subitems {
|
|
|
|
names = append(names, f.Name())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
names = append(names, i.Name())
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
data, err := json.Marshal(names)
|
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Write(data)
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-28 19:51:25 +01:00
|
|
|
// CheckBlob tests whether a blob exists.
|
2016-12-27 13:42:43 +01:00
|
|
|
func CheckBlob(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("CheckBlob()")
|
|
|
|
}
|
2016-12-27 18:57:49 +01:00
|
|
|
dir := pat.Param(r, "type")
|
|
|
|
name := pat.Param(r, "name")
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
if isHashed(dir) {
|
|
|
|
name = filepath.Join(name[:2], name)
|
|
|
|
}
|
2016-12-28 00:57:25 +01:00
|
|
|
path := filepath.Join(getRepo(r), dir, name)
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
st, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// GetBlob retrieves a blob from the repository.
|
|
|
|
func GetBlob(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("GetBlob()")
|
|
|
|
}
|
2016-12-27 18:57:49 +01:00
|
|
|
dir := pat.Param(r, "type")
|
|
|
|
name := pat.Param(r, "name")
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
if isHashed(dir) {
|
|
|
|
name = filepath.Join(name[:2], name)
|
|
|
|
}
|
2016-12-28 00:57:25 +01:00
|
|
|
path := filepath.Join(getRepo(r), dir, name)
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
|
|
|
|
http.ServeContent(w, r, "", time.Unix(0, 0), file)
|
|
|
|
file.Close()
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// SaveBlob saves a blob to the repository.
|
|
|
|
func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("SaveBlob()")
|
|
|
|
}
|
2016-12-28 00:57:25 +01:00
|
|
|
repo := getRepo(r)
|
2016-12-27 18:57:49 +01:00
|
|
|
dir := pat.Param(r, "type")
|
|
|
|
name := pat.Param(r, "name")
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-01-25 20:59:18 +01:00
|
|
|
// Legacy code, required for restic client <= v0.3.3
|
|
|
|
// TODO: remove sometime later
|
2016-12-27 13:42:43 +01:00
|
|
|
if dir == "keys" {
|
|
|
|
if _, err := os.Stat("keys"); err != nil && os.IsNotExist(err) {
|
2016-12-28 00:57:25 +01:00
|
|
|
createDirectories(repo)
|
2016-12-27 01:35:45 +01:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-12-27 01:35:45 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
if isHashed(dir) {
|
|
|
|
name = filepath.Join(name[:2], name)
|
|
|
|
}
|
|
|
|
path := filepath.Join(repo, dir, name)
|
2016-11-06 15:46:11 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
tf, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
|
2016-12-27 13:42:43 +01:00
|
|
|
if err != nil {
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-06 15:46:11 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
if _, err := io.Copy(tf, r.Body); err != nil {
|
|
|
|
tf.Close()
|
2017-03-18 13:11:29 +01:00
|
|
|
os.Remove(path)
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := tf.Sync(); err != nil {
|
|
|
|
tf.Close()
|
2017-03-18 13:11:29 +01:00
|
|
|
os.Remove(path)
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := tf.Close(); err != nil {
|
|
|
|
os.Remove(path)
|
2017-01-16 23:39:56 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Write([]byte("200 ok"))
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// DeleteBlob deletes a blob from the repository.
|
|
|
|
func DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
2016-12-30 20:57:48 +01:00
|
|
|
if config.debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("DeleteBlob()")
|
|
|
|
}
|
2017-03-18 13:11:29 +01:00
|
|
|
|
2016-12-27 18:57:49 +01:00
|
|
|
dir := pat.Param(r, "type")
|
|
|
|
name := pat.Param(r, "name")
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
if isHashed(dir) {
|
|
|
|
name = filepath.Join(name[:2], name)
|
|
|
|
}
|
2016-12-28 00:57:25 +01:00
|
|
|
path := filepath.Join(getRepo(r), dir, name)
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
err := os.Remove(path)
|
|
|
|
if err == nil {
|
|
|
|
w.Write([]byte("200 ok"))
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
if config.debug {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2017-01-25 20:59:18 +01:00
|
|
|
|
|
|
|
// CreateRepo creates repository directories.
|
|
|
|
func CreateRepo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if config.debug {
|
|
|
|
log.Println("CreateRepo()")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("create") != "true" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createDirectories(getRepo(r))
|
|
|
|
|
|
|
|
w.Write([]byte("200 ok"))
|
|
|
|
}
|