| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/tls" |
| 6 |
"crypto/x509" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"net/http" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/folbricht/desync" |
| 16 |
"github.com/spf13/cobra" |
| 17 |
) |
| 18 |
|
| 19 |
type indexServerOptions struct { |
| 20 |
cmdStoreOptions |
| 21 |
cmdServerOptions |
| 22 |
store string |
| 23 |
listenAddresses []string |
| 24 |
writable bool |
| 25 |
logFile string |
| 26 |
} |
| 27 |
|
| 28 |
func newIndexServerCommand(ctx context.Context) *cobra.Command { |
| 29 |
var opt indexServerOptions |
| 30 |
|
| 31 |
cmd := &cobra.Command{ |
| 32 |
Use: "index-server", |
| 33 |
Short: "Server for indexes over HTTP(S)", |
| 34 |
Long: `Starts an HTTP index server that can be used as remote store. It supports |
| 35 |
reading from a single local store or proxying to a remote store. |
| 36 |
If --cert and --key are provided, the server will serve over HTTPS. The -w option |
| 37 |
enables writing to this store.`, |
| 38 |
Example: ` desync index-server -s sftp://192.168.1.1/indexes -l :8080 |
| 39 |
desync index-server -s /path/to/indexes -w -l :8080`, |
| 40 |
Args: cobra.NoArgs, |
| 41 |
RunE: func(cmd *cobra.Command, args []string) error { |
| 42 |
return runIndexServer(ctx, opt, args) |
| 43 |
}, |
| 44 |
SilenceUsage: true, |
| 45 |
} |
| 46 |
flags := cmd.Flags() |
| 47 |
flags.StringVarP(&opt.store, "store", "s", "", "upstream source index store") |
| 48 |
flags.StringSliceVarP(&opt.listenAddresses, "listen", "l", []string{":http"}, "listen address(es), can be repeated") |
| 49 |
flags.BoolVarP(&opt.writable, "writable", "w", false, "support writing") |
| 50 |
flags.BoolVar(&opt.writable, "writeable", false, "support writing") |
| 51 |
_ = flags.MarkDeprecated("writeable", "use --writable instead") |
| 52 |
flags.StringVar(&opt.logFile, "log", "", "request log file or - for STDOUT") |
| 53 |
addStoreOptions(&opt.cmdStoreOptions, flags) |
| 54 |
addServerOptions(&opt.cmdServerOptions, flags) |
| 55 |
return cmd |
| 56 |
} |
| 57 |
|
| 58 |
func runIndexServer(ctx context.Context, opt indexServerOptions, args []string) error { |
| 59 |
if err := opt.cmdStoreOptions.validate(); err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
if err := opt.cmdServerOptions.validate(); err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
if opt.auth == "" { |
| 66 |
opt.auth = os.Getenv("DESYNC_HTTP_AUTH") |
| 67 |
} |
| 68 |
|
| 69 |
addresses := opt.listenAddresses |
| 70 |
if len(addresses) == 0 { |
| 71 |
addresses = []string{":http"} |
| 72 |
} |
| 73 |
|
| 74 |
// Checkout the store |
| 75 |
if opt.store == "" { |
| 76 |
return errors.New("no store provided") |
| 77 |
} |
| 78 |
|
| 79 |
// Making sure we have a "/" at the end |
| 80 |
loc := opt.store |
| 81 |
if !strings.HasSuffix(loc, "/") { |
| 82 |
loc = loc + "/" |
| 83 |
} |
| 84 |
|
| 85 |
var ( |
| 86 |
s desync.IndexStore |
| 87 |
err error |
| 88 |
) |
| 89 |
if opt.writable { |
| 90 |
s, _, err = writableIndexStore(loc, opt.cmdStoreOptions) |
| 91 |
} else { |
| 92 |
s, _, err = indexStoreFromLocation(loc, opt.cmdStoreOptions) |
| 93 |
} |
| 94 |
if err != nil { |
| 95 |
return err |
| 96 |
} |
| 97 |
defer s.Close() |
| 98 |
|
| 99 |
handler := desync.NewHTTPIndexHandler(s, opt.writable, opt.auth) |
| 100 |
|
| 101 |
// Wrap the handler in a logger if requested |
| 102 |
switch opt.logFile { |
| 103 |
case "": // No logging of requests |
| 104 |
case "-": |
| 105 |
handler = withLog(handler, log.New(stderr, "", log.LstdFlags)) |
| 106 |
default: |
| 107 |
l, err := os.OpenFile(opt.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 108 |
if err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
defer l.Close() |
| 112 |
handler = withLog(handler, log.New(l, "", log.LstdFlags)) |
| 113 |
} |
| 114 |
|
| 115 |
http.Handle("/", handler) |
| 116 |
|
| 117 |
// Start the server |
| 118 |
return serve(ctx, opt.cmdServerOptions, addresses...) |
| 119 |
} |
| 120 |
|
| 121 |
func serve(ctx context.Context, opt cmdServerOptions, addresses ...string) error { |
| 122 |
tlsConfig := &tls.Config{} |
| 123 |
if opt.mutualTLS { |
| 124 |
// Defense-in-depth: validate() already rejects this combination, but |
| 125 |
// without an explicit client CA, crypto/tls falls back to the system |
| 126 |
// trust store and would accept any publicly-trusted client cert. |
| 127 |
if opt.clientCA == "" { |
| 128 |
return errors.New("--mutual-tls requires --client-ca") |
| 129 |
} |
| 130 |
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert |
| 131 |
} |
| 132 |
if opt.clientCA != "" { |
| 133 |
certPool := x509.NewCertPool() |
| 134 |
b, err := os.ReadFile(opt.clientCA) |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
if ok := certPool.AppendCertsFromPEM(b); !ok { |
| 139 |
return errors.New("no client CA certificates found in client-ca file") |
| 140 |
} |
| 141 |
tlsConfig.ClientCAs = certPool |
| 142 |
} |
| 143 |
|
| 144 |
// Run the server(s) in a goroutine, and use the main goroutine to wait for |
| 145 |
// a signal or a failing server (ctx gets cancelled in that case) |
| 146 |
ctx, cancel := context.WithCancel(ctx) |
| 147 |
defer cancel() |
| 148 |
for _, addr := range addresses { |
| 149 |
go func(a string) { |
| 150 |
server := &http.Server{ |
| 151 |
Addr: a, |
| 152 |
TLSConfig: tlsConfig, |
| 153 |
ErrorLog: log.New(stderr, "", log.LstdFlags), |
| 154 |
ReadHeaderTimeout: 30 * time.Second, |
| 155 |
IdleTimeout: 120 * time.Second, |
| 156 |
} |
| 157 |
var err error |
| 158 |
if opt.key == "" { |
| 159 |
err = server.ListenAndServe() |
| 160 |
} else { |
| 161 |
err = server.ListenAndServeTLS(opt.cert, opt.key) |
| 162 |
} |
| 163 |
fmt.Fprintln(stderr, err) |
| 164 |
cancel() |
| 165 |
}(addr) |
| 166 |
} |
| 167 |
// wait for either INT/TERM or an issue with the server |
| 168 |
<-ctx.Done() |
| 169 |
return nil |
| 170 |
} |