Option for perm relay file, keep test cert in temp dir

This commit is contained in:
Jakob Borg 2015-09-22 09:02:18 +02:00
parent e1339628d9
commit 168889d999

View File

@ -19,7 +19,6 @@ import (
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
"github.com/juju/ratelimit" "github.com/juju/ratelimit"
"github.com/kardianos/osext"
"github.com/syncthing/relaysrv/client" "github.com/syncthing/relaysrv/client"
"github.com/syncthing/syncthing/lib/sync" "github.com/syncthing/syncthing/lib/sync"
@ -48,7 +47,7 @@ type result struct {
var ( var (
binDir string binDir string
testCert []tls.Certificate testCert tls.Certificate
listen string = ":80" listen string = ":80"
dir string = "" dir string = ""
evictionTime time.Duration = time.Hour evictionTime time.Duration = time.Hour
@ -61,6 +60,7 @@ var (
postLimitAvg = 1 postLimitAvg = 1
getLimit time.Duration getLimit time.Duration
postLimit time.Duration postLimit time.Duration
permRelaysFile string
getMut sync.RWMutex = sync.NewRWMutex() getMut sync.RWMutex = sync.NewRWMutex()
getLRUCache *lru.Cache getLRUCache *lru.Cache
@ -87,6 +87,7 @@ func main() {
flag.IntVar(&postLRUSize, "post-limit-cache", postLRUSize, "Post request limiter cache size") flag.IntVar(&postLRUSize, "post-limit-cache", postLRUSize, "Post request limiter cache size")
flag.IntVar(&postLimitAvg, "post-limit-avg", 2, "Allowed average post request rate, per minute") flag.IntVar(&postLimitAvg, "post-limit-avg", 2, "Allowed average post request rate, per minute")
flag.Int64Var(&postLimitBurst, "post-limit-burst", postLimitBurst, "Allowed burst post requests") flag.Int64Var(&postLimitBurst, "post-limit-burst", postLimitBurst, "Allowed burst post requests")
flag.StringVar(&permRelaysFile, "perm-relays", "", "Path to list of permanent relays")
flag.Parse() flag.Parse()
@ -99,13 +100,12 @@ func main() {
var listener net.Listener var listener net.Listener
var err error var err error
binDir, err = osext.ExecutableFolder() if permRelaysFile != "" {
if err != nil { loadPermanentRelays(permRelaysFile)
log.Fatalln("Failed to locate executable directory")
} }
loadPermanentRelays() testCert = createTestCertificate()
loadOrCreateTestCertificate()
go requestProcessor() go requestProcessor()
if dir != "" { if dir != "" {
@ -292,7 +292,7 @@ func requestProcessor() {
if debug { if debug {
log.Println("Request for", request.relay) log.Println("Request for", request.relay)
} }
if !client.TestRelay(request.uri, testCert, 250*time.Millisecond, 4) { if !client.TestRelay(request.uri, []tls.Certificate{testCert}, 250*time.Millisecond, 4) {
if debug { if debug {
log.Println("Test for relay", request.relay, "failed") log.Println("Test for relay", request.relay, "failed")
} }
@ -375,16 +375,10 @@ func limit(addr string, cache *lru.Cache, lock sync.RWMutex, rate time.Duration,
return false return false
} }
func loadPermanentRelays() { func loadPermanentRelays(file string) {
path, err := osext.ExecutableFolder() content, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
log.Println("Failed to locate executable directory") log.Fatal(err)
return
}
content, err := ioutil.ReadFile(filepath.Join(path, "relays"))
if err != nil {
return
} }
for _, line := range strings.Split(string(content), "\n") { for _, line := range strings.Split(string(content), "\n") {
@ -398,6 +392,7 @@ func loadPermanentRelays() {
log.Println("Skipping permanent relay", line, "due to parse error", err) log.Println("Skipping permanent relay", line, "due to parse error", err)
} }
continue continue
} }
permanentRelays = append(permanentRelays, relay{ permanentRelays = append(permanentRelays, relay{
@ -410,17 +405,17 @@ func loadPermanentRelays() {
} }
} }
func loadOrCreateTestCertificate() { func createTestCertificate() tls.Certificate {
certFile, keyFile := filepath.Join(binDir, "cert.pem"), filepath.Join(binDir, "key.pem") tmpDir, err := ioutil.TempDir("", "relaypoolsrv")
cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil {
if err == nil { log.Fatal(err)
testCert = []tls.Certificate{cert}
return
} }
cert, err = tlsutil.NewCertificate(certFile, keyFile, "relaypoolsrv", 3072) certFile, keyFile := filepath.Join(tmpDir, "cert.pem"), filepath.Join(tmpDir, "key.pem")
cert, err := tlsutil.NewCertificate(certFile, keyFile, "relaypoolsrv", 3072)
if err != nil { if err != nil {
log.Fatalln("Failed to create test X509 key pair:", err) log.Fatalln("Failed to create test X509 key pair:", err)
} }
testCert = []tls.Certificate{cert}
return cert
} }