2011-03-30 00:42:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2010-06-12 21:45:58 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2010-07-11 04:18:16 +00:00
|
|
|
import (
|
2011-11-10 15:20:22 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"crypto/x509/pkix"
|
2011-11-10 15:20:22 +00:00
|
|
|
"encoding/pem"
|
2011-04-02 05:14:23 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2012-03-03 22:27:17 +00:00
|
|
|
"io/ioutil"
|
2011-04-02 05:14:23 +00:00
|
|
|
"log"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"math/big"
|
2012-03-15 12:31:06 +00:00
|
|
|
"net"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2011-07-18 00:30:40 +00:00
|
|
|
"runtime"
|
2011-04-02 05:14:23 +00:00
|
|
|
"strings"
|
2011-11-10 15:20:22 +00:00
|
|
|
"time"
|
2011-04-02 05:14:23 +00:00
|
|
|
|
2012-03-20 04:31:20 +00:00
|
|
|
"camlistore.org/pkg/jsonsign"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
|
|
|
"camlistore.org/pkg/serverconfig"
|
|
|
|
"camlistore.org/pkg/webserver"
|
2011-04-02 05:14:23 +00:00
|
|
|
|
|
|
|
// Storage options:
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
_ "camlistore.org/pkg/blobserver/cond"
|
|
|
|
_ "camlistore.org/pkg/blobserver/localdisk"
|
|
|
|
_ "camlistore.org/pkg/blobserver/remote"
|
|
|
|
_ "camlistore.org/pkg/blobserver/replica"
|
|
|
|
_ "camlistore.org/pkg/blobserver/s3"
|
|
|
|
_ "camlistore.org/pkg/blobserver/shard"
|
2012-03-26 20:57:53 +00:00
|
|
|
// Indexers: (also present themselves as storage targets)
|
2012-04-16 14:08:36 +00:00
|
|
|
_ "camlistore.org/pkg/index" // base indexer + in-memory dev index
|
2012-03-26 20:57:53 +00:00
|
|
|
_ "camlistore.org/pkg/index/mongo"
|
|
|
|
_ "camlistore.org/pkg/index/mysql"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
|
2011-05-30 05:52:31 +00:00
|
|
|
// Handlers:
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
_ "camlistore.org/pkg/search"
|
|
|
|
_ "camlistore.org/pkg/server" // UI, publish, etc
|
2012-03-20 04:31:20 +00:00
|
|
|
|
|
|
|
"camlistore.org/third_party/code.google.com/p/go.crypto/openpgp"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
2010-07-07 04:57:53 +00:00
|
|
|
|
2012-02-29 16:01:49 +00:00
|
|
|
const (
|
|
|
|
defCert = "config/selfgen_cert.pem"
|
2012-03-03 22:27:17 +00:00
|
|
|
defKey = "config/selfgen_key.pem"
|
2012-02-29 16:01:49 +00:00
|
|
|
)
|
2011-11-10 15:20:22 +00:00
|
|
|
|
2012-02-29 16:01:49 +00:00
|
|
|
var (
|
2012-03-03 22:27:17 +00:00
|
|
|
flagConfigFile = flag.String("configfile", "",
|
2012-03-20 04:31:20 +00:00
|
|
|
"Config file to use, relative to the Camlistore configuration directory root. If blank, the default is used or auto-generated.")
|
2012-02-29 16:01:49 +00:00
|
|
|
)
|
2011-03-05 23:09:36 +00:00
|
|
|
|
2012-02-29 16:01:49 +00:00
|
|
|
func exitf(pattern string, args ...interface{}) {
|
2011-02-04 22:31:23 +00:00
|
|
|
if !strings.HasSuffix(pattern, "\n") {
|
|
|
|
pattern = pattern + "\n"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, pattern, args...)
|
|
|
|
os.Exit(1)
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 15:20:22 +00:00
|
|
|
// Mostly copied from $GOROOT/src/pkg/crypto/tls/generate_cert.go
|
2012-03-15 12:31:06 +00:00
|
|
|
func genSelfTLS(listen string) error {
|
2011-11-10 15:20:22 +00:00
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to generate private key: %s", err)
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
now := time.Now()
|
2011-11-10 15:20:22 +00:00
|
|
|
|
2012-03-15 12:31:06 +00:00
|
|
|
hostname, _, err := net.SplitHostPort(listen)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("splitting listen failed: %q", err)
|
2011-11-10 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: new(big.Int).SetInt64(0),
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: hostname,
|
|
|
|
Organization: []string{hostname},
|
|
|
|
},
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
NotBefore: now.Add(-5 * time.Minute).UTC(),
|
|
|
|
NotAfter: now.AddDate(1, 0, 0).UTC(),
|
2011-11-10 15:20:22 +00:00
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4},
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certOut, err := os.Create(defCert)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open %s for writing: %s", defCert, err)
|
|
|
|
}
|
|
|
|
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
certOut.Close()
|
|
|
|
log.Printf("written %s\n", defCert)
|
|
|
|
|
|
|
|
keyOut, err := os.OpenFile(defKey, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open %s for writing:", defKey, err)
|
|
|
|
}
|
|
|
|
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
|
|
|
keyOut.Close()
|
|
|
|
log.Printf("written %s\n", defKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:09:00 +00:00
|
|
|
// findConfigFile returns the absolute path of the user's
|
|
|
|
// config file.
|
|
|
|
// The provided file may be absolute or relative
|
|
|
|
// to the user's configuration directory.
|
|
|
|
// If file is empty, a default high-level config is written
|
|
|
|
// for the user.
|
|
|
|
func findConfigFile(file string) (absPath string, err error) {
|
|
|
|
switch {
|
|
|
|
case file == "":
|
|
|
|
absPath = osutil.UserServerConfigPath()
|
|
|
|
_, err = os.Stat(absPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(osutil.CamliConfigDir(), 0700)
|
|
|
|
log.Printf("Generating template config file %s", absPath)
|
|
|
|
err = newDefaultConfigFile(absPath)
|
2012-03-03 22:27:17 +00:00
|
|
|
}
|
2012-03-19 20:09:00 +00:00
|
|
|
return
|
|
|
|
case filepath.IsAbs(file):
|
|
|
|
absPath = file
|
|
|
|
default:
|
|
|
|
absPath = filepath.Join(osutil.CamliConfigDir(), file)
|
2012-03-03 22:27:17 +00:00
|
|
|
}
|
2012-03-19 20:09:00 +00:00
|
|
|
_, err = os.Stat(absPath)
|
|
|
|
return
|
2012-03-03 22:27:17 +00:00
|
|
|
}
|
|
|
|
|
2012-03-20 04:31:20 +00:00
|
|
|
func keyIdFromRing(filename string) (keyId string, err error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("reading identity secret ring file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
el, err := openpgp.ReadKeyRing(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("reading identity secret ring file %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
if len(el) != 1 {
|
|
|
|
return "", fmt.Errorf("identity secret ring file contained %d identities; expected 1", len(el))
|
|
|
|
}
|
|
|
|
ent := el[0]
|
|
|
|
return ent.PrimaryKey.KeyIdShortString(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateNewSecRing(filename string) (keyId string, err error) {
|
|
|
|
ent, err := jsonsign.NewEntity()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("generating new identity: %v", err)
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = jsonsign.WriteKeyRing(f, openpgp.EntityList([]*openpgp.Entity{ent}))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("writing new key ring to %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
return ent.PrimaryKey.KeyIdShortString(), nil
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:27:17 +00:00
|
|
|
func newDefaultConfigFile(path string) error {
|
2012-03-20 04:31:20 +00:00
|
|
|
conf :=
|
2012-03-03 22:27:17 +00:00
|
|
|
`{
|
2012-03-15 12:31:06 +00:00
|
|
|
"listen": "localhost:3179",
|
|
|
|
"TLS": false,
|
2012-04-08 02:02:31 +00:00
|
|
|
"auth": "userpass:camlistore:pass3179:+localhost",
|
2012-03-20 04:31:20 +00:00
|
|
|
"identity": "%KEYID%",
|
|
|
|
"identitySecretRing": "%SECRING%",
|
2012-03-15 12:31:06 +00:00
|
|
|
"blobPath": "%BLOBPATH%",
|
|
|
|
"mysql": "",
|
|
|
|
"mongo": "",
|
|
|
|
"s3": "",
|
2012-04-22 15:33:22 +00:00
|
|
|
"replicateTo": [],
|
|
|
|
"publish": {}
|
2012-03-03 22:27:17 +00:00
|
|
|
}
|
|
|
|
`
|
2012-03-19 20:09:00 +00:00
|
|
|
blobDir := osutil.CamliBlobRoot()
|
2012-03-03 22:27:17 +00:00
|
|
|
if err := os.MkdirAll(blobDir, 0700); err != nil {
|
|
|
|
return fmt.Errorf("Could not create default blobs directory: %v", err)
|
|
|
|
}
|
2012-03-20 04:31:20 +00:00
|
|
|
conf = strings.Replace(conf, "%BLOBPATH%", blobDir, 1)
|
|
|
|
|
|
|
|
var keyId string
|
2012-04-12 23:54:57 +00:00
|
|
|
secRing := osutil.IdentitySecretRing()
|
2012-03-20 04:31:20 +00:00
|
|
|
_, err := os.Stat(secRing)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
keyId, err = keyIdFromRing(secRing)
|
|
|
|
log.Printf("Re-using identity with keyId %q found in file %s", keyId, secRing)
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
keyId, err = generateNewSecRing(secRing)
|
|
|
|
log.Printf("Generated new identity with keyId %q in file %s", keyId, secRing)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Secret ring: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf = strings.Replace(conf, "%SECRING%", secRing, 1)
|
|
|
|
conf = strings.Replace(conf, "%KEYID%", keyId, 1)
|
|
|
|
if err := ioutil.WriteFile(path, []byte(conf), 0600); err != nil {
|
2012-03-03 22:27:17 +00:00
|
|
|
return fmt.Errorf("Could not create or write default server config: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-15 12:31:06 +00:00
|
|
|
func setupTLS(ws *webserver.Server, config *serverconfig.Config, listen string) {
|
2012-03-05 15:17:58 +00:00
|
|
|
cert, key := config.OptionalString("TLSCertFile", ""), config.OptionalString("TLSKeyFile", "")
|
|
|
|
if !config.OptionalBool("https", true) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (cert != "") != (key != "") {
|
|
|
|
exitf("TLSCertFile and TLSKeyFile must both be either present or absent")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cert == defCert && key == defKey {
|
|
|
|
_, err1 := os.Stat(cert)
|
|
|
|
_, err2 := os.Stat(key)
|
|
|
|
if err1 != nil || err2 != nil {
|
|
|
|
if os.IsNotExist(err1) || os.IsNotExist(err2) {
|
2012-03-15 12:31:06 +00:00
|
|
|
if err := genSelfTLS(listen); err != nil {
|
2012-03-05 15:17:58 +00:00
|
|
|
exitf("Could not generate self-signed TLS cert: %q", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exitf("Could not stat cert or key: %q, %q", err1, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cert == "" && key == "" {
|
2012-03-15 12:31:06 +00:00
|
|
|
err := genSelfTLS(listen)
|
2012-03-05 15:17:58 +00:00
|
|
|
if err != nil {
|
|
|
|
exitf("Could not generate self signed creds: %q", err)
|
|
|
|
}
|
|
|
|
cert = defCert
|
|
|
|
key = defKey
|
|
|
|
}
|
|
|
|
ws.SetTLS(cert, key)
|
|
|
|
}
|
|
|
|
|
2010-06-12 21:45:58 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2012-03-19 20:09:00 +00:00
|
|
|
fileName, err := findConfigFile(*flagConfigFile)
|
2011-09-30 05:07:04 +00:00
|
|
|
if err != nil {
|
2012-03-19 20:09:00 +00:00
|
|
|
exitf("Error finding config file %q: %v", fileName, err)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2012-03-19 20:09:00 +00:00
|
|
|
log.Printf("Using config file %s", fileName)
|
|
|
|
config, err := serverconfig.Load(fileName)
|
2012-03-15 12:31:06 +00:00
|
|
|
if err != nil {
|
2012-03-19 20:09:00 +00:00
|
|
|
exitf("Could not load server config: %v", err)
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
|
2011-04-04 02:58:20 +00:00
|
|
|
ws := webserver.New()
|
2012-03-15 12:31:06 +00:00
|
|
|
baseURL := config.RequiredString("baseURL")
|
|
|
|
listen := *(webserver.Listen)
|
|
|
|
if listen == "" {
|
|
|
|
// if command line was empty, use value in config
|
|
|
|
listen = strings.TrimLeft(baseURL, "http://")
|
|
|
|
listen = strings.TrimLeft(listen, "https://")
|
|
|
|
} else {
|
|
|
|
// else command line takes precedence
|
|
|
|
scheme := strings.Split(baseURL, "://")[0]
|
|
|
|
baseURL = scheme + "://" + listen
|
|
|
|
}
|
2011-04-04 02:58:20 +00:00
|
|
|
|
2012-03-15 12:31:06 +00:00
|
|
|
setupTLS(ws, config, listen)
|
2011-04-04 02:38:22 +00:00
|
|
|
|
2011-10-26 02:40:50 +00:00
|
|
|
err = config.InstallHandlers(ws, baseURL, nil)
|
2011-09-30 04:18:12 +00:00
|
|
|
if err != nil {
|
2012-02-29 16:01:49 +00:00
|
|
|
exitf("Error parsing config: %v", err)
|
2011-09-30 04:18:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 12:31:06 +00:00
|
|
|
ws.Listen(listen)
|
2011-09-30 04:18:12 +00:00
|
|
|
|
2012-04-20 18:59:58 +00:00
|
|
|
urlOpened := false
|
2011-09-30 05:07:04 +00:00
|
|
|
if config.UIPath != "" {
|
|
|
|
uiURL := ws.BaseURL() + config.UIPath
|
2011-09-30 04:18:12 +00:00
|
|
|
log.Printf("UI available at %s", uiURL)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Might be double-clicking an icon with no shell window?
|
|
|
|
// Just open the URL for them.
|
2012-04-20 18:59:58 +00:00
|
|
|
urlOpened = true
|
|
|
|
go osutil.OpenURL(uiURL)
|
2011-09-30 04:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-20 18:59:58 +00:00
|
|
|
if *flagConfigFile == "" && !urlOpened {
|
|
|
|
go func() {
|
|
|
|
err := osutil.OpenURL(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to open %s in browser: %v", baseURL, err)
|
|
|
|
}
|
|
|
|
}()
|
2012-04-16 14:08:36 +00:00
|
|
|
}
|
2011-09-30 04:18:12 +00:00
|
|
|
ws.Serve()
|
|
|
|
}
|