-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
201 lines (166 loc) · 5.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// This file is part of *kellner*
//
// Copyright (C) 2015, Travelping GmbH <copyright@travelping.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
// *kellner* scans package files in a given directory
// and builds a Packages.gz file on the fly. it then serves the
// Packages.gz and the .ipk files by the built-in httpd
// and is ready to be used from opkg
//
// related tools:
// * https://github.com/17twenty/opkg-scanpackages
// * opkg-make-index from the opkg-utils collection
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"path/filepath"
)
const versionString = "kellner-0.6.0"
func main() {
var (
dumpPackageList = flag.Bool("dump", false,
"just dump the package list and exit")
prepareCache = flag.Bool("prep-cache", false, "scan all packages and prepare the cache folder, do not serve anything")
bind = flag.String("bind", ":8080", "address to bind to")
rootName = flag.String("root", "", "directory containing the packages")
cacheName = flag.String("cache", "cache", "directory containing cached meta-files (eg. control)")
nworkers = flag.Int("workers", 4, "number of workers")
addMd5 = flag.Bool("md5", true, "calculate md5 of scanned packages")
addSha1 = flag.Bool("sha1", false, "calculate sha1 of scanned packages")
useGzip = flag.Bool("gzip", true, "use 'gzip' to compress the package index. if false: use golang")
showVersion = flag.Bool("version", false, "show version and exit")
logFileName = flag.String("log", "", "log to given filename")
tlsKey = flag.String("tls-key", "", "PEM encoded ssl-key")
tlsCert = flag.String("tls-cert", "", "PEM encoded ssl-cert")
tlsClientCas = flag.String("tls-client-ca-file", "", "file with PEM encoded list of ssl-certs containing the CAs")
tlsRequireClientCert = flag.Bool("require-client-cert", false, "require a client-cert")
tlsClientIDMuxRoot = flag.String("idmap", "", "directory containing the client-mappings")
printClientCert = flag.String("print-client-cert-id", "", "print client-id for given .cert and exit")
condense = flag.String("condense", "", "condense packages. argument is the target. (\"-\" is stdout and will just list filenames)")
vcomp = flag.Bool("vcomp", false, "compare the first two non-flag arguments as versions")
archsubdirs = flag.Bool("archsubdirs", true, "create a subdir per arch when bundling packages")
listen net.Listener
err error
)
flag.Parse()
if *showVersion {
fmt.Println(versionString)
return
}
if *printClientCert != "" {
if err = printClientIDTo(os.Stdout, *printClientCert); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
return
}
if *bind == "" {
fmt.Fprintf(os.Stderr, "usage error: missing / empty -bind\n")
os.Exit(1)
}
if *condense != "" {
var (
err error
condensate *packageIndex
)
if condensate, err = condensePackages(flag.Args(), *nworkers); err == nil {
if err = condensate.writeBundle(*condense, *archsubdirs); err == nil {
os.Exit(0)
}
}
log.Println("error:", err)
os.Exit(1)
}
if *vcomp {
if len(flag.Args()) != 2 {
fmt.Fprintf(os.Stderr, "vcomp option requires exactly two arguments for comparison\n")
os.Exit(1)
}
v1 := flag.Args()[0]
v2 := flag.Args()[1]
rel := map[int]string{0: "==", 1: ">", -1: "<"}
rc := compareVersion(v1, v2)
fmt.Printf("comparing %s %s %s (comp: %d).\n", v1, rel[rc], v2, rc)
os.Exit(0)
}
if *rootName == "" {
fmt.Fprintf(os.Stderr, "usage error: missing / empty -root\n")
os.Exit(1)
}
*rootName, _ = filepath.Abs(*rootName)
setupLogging(*logFileName)
// simple use-case: scan one directory and dump the created
// packages-list to stdout.
if *dumpPackageList {
dumpPackages(*rootName, *nworkers, *addMd5, *addSha1)
return
}
if *cacheName == "" {
fmt.Fprintf(os.Stderr, "usage error: missing / empty -cache\n")
os.Exit(1)
}
*cacheName, _ = filepath.Abs(*cacheName)
if err = os.MkdirAll(*cacheName, 0755); err != nil {
fmt.Fprintf(os.Stderr, "error creating %q: %v\n", *cacheName, err)
os.Exit(1)
}
gzipper := gzWrite(gzGzipPipe)
if !*useGzip {
gzipper = gzGolang
}
scanRoot(*rootName, *cacheName, *nworkers, *addMd5, *addSha1, gzipper)
if *prepareCache {
return
}
// regular use-case: serve the given directory + the Packages file(s)
// recursively.
//
// setup the listener: either ssl or pure tcp
l, err := net.Listen("tcp", *bind)
if err != nil {
fmt.Fprintf(os.Stderr, "error: binding to %q failed: %v\n", *bind, err)
os.Exit(1)
}
listen = l
if *tlsCert != "" || *tlsKey != "" {
var tlsOpts = tlsOptions{
keyFileName: *tlsKey,
certFileName: *tlsCert,
requireClientCert: *tlsRequireClientCert,
clientCasFileName: *tlsClientCas,
}
if listen, err = initTLS(listen, &tlsOpts); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}
}
go rescan(*rootName, *cacheName, *nworkers, *addMd5, *addSha1, gzipper)
log.Println("listen on", listen.Addr())
// the root-muxer is used either directly (non-ssl-client-cert case) or
// as a lookup-pool for ClientIdMuxer to get the real handler
var rootMuxer = http.NewServeMux()
rootMuxer.Handle("/", makeIndexHandler(*rootName, *cacheName))
var httpHandler http.Handler = rootMuxer
if *tlsClientIDMuxRoot != "" {
httpHandler = &clientIDMuxer{
Folder: *tlsClientIDMuxRoot,
Muxer: rootMuxer,
}
}
httpHandler = logRequests(httpHandler)
log.Println()
proto := "http://"
if *tlsKey != "" {
proto = "https://"
}
log.Printf("serving at %s", proto+listen.Addr().String())
http.Serve(listen, httpHandler)
}