forked from quic-go/quic-go
Merge pull request #353 from carlosmn/no-gopath-certs
Don't rely on GOPATH to load the certificates or error codes
This commit is contained in:
@@ -10,7 +10,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"path"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -99,6 +100,15 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getBuildDir() string {
|
||||||
|
_, filename, _, ok := runtime.Caller(0)
|
||||||
|
if !ok {
|
||||||
|
panic("Failed to get current frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.Dir(filename)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// defer profile.Start().Stop()
|
// defer profile.Start().Stop()
|
||||||
go func() {
|
go func() {
|
||||||
@@ -109,7 +119,7 @@ func main() {
|
|||||||
verbose := flag.Bool("v", false, "verbose")
|
verbose := flag.Bool("v", false, "verbose")
|
||||||
bs := binds{}
|
bs := binds{}
|
||||||
flag.Var(&bs, "bind", "bind to")
|
flag.Var(&bs, "bind", "bind to")
|
||||||
certPath := flag.String("certpath", os.Getenv("GOPATH")+"/src/github.com/lucas-clemente/quic-go/example/", "certificate directory")
|
certPath := flag.String("certpath", getBuildDir(), "certificate directory")
|
||||||
www := flag.String("www", "/var/www", "www data")
|
www := flag.String("www", "/var/www", "www data")
|
||||||
tcp := flag.Bool("tcp", false, "also listen on TCP")
|
tcp := flag.Bool("tcp", false, "also listen on TCP")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|||||||
@@ -33,9 +33,6 @@ func (s *mockSession) RemoteAddr() *net.UDPAddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("H2 server", func() {
|
var _ = Describe("H2 server", func() {
|
||||||
certPath := os.Getenv("GOPATH")
|
|
||||||
certPath += "/src/github.com/lucas-clemente/quic-go/example/"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
s *Server
|
s *Server
|
||||||
session *mockSession
|
session *mockSession
|
||||||
@@ -243,7 +240,7 @@ var _ = Describe("H2 server", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("should error when ListenAndServeTLS is called with s.Server nil", func() {
|
It("should error when ListenAndServeTLS is called with s.Server nil", func() {
|
||||||
err := (&Server{}).ListenAndServeTLS(certPath+"fullchain.pem", certPath+"privkey.pem")
|
err := (&Server{}).ListenAndServeTLS(testdata.GetCertificatePaths())
|
||||||
Expect(err).To(MatchError("use of h2quic.Server without http.Server"))
|
Expect(err).To(MatchError("use of h2quic.Server without http.Server"))
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -296,7 +293,7 @@ var _ = Describe("H2 server", func() {
|
|||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
defer GinkgoRecover()
|
defer GinkgoRecover()
|
||||||
err := s.ListenAndServeTLS(certPath+"fullchain.pem", certPath+"privkey.pem")
|
err := s.ListenAndServeTLS(testdata.GetCertificatePaths())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cErr <- err
|
cErr <- err
|
||||||
}
|
}
|
||||||
@@ -325,7 +322,8 @@ var _ = Describe("H2 server", func() {
|
|||||||
c, err := net.ListenUDP("udp", udpAddr)
|
c, err := net.ListenUDP("udp", udpAddr)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
err = ListenAndServeQUIC(addr, certPath+"fullchain.pem", certPath+"privkey.pem", nil)
|
fullpem, privkey := testdata.GetCertificatePaths()
|
||||||
|
err = ListenAndServeQUIC(addr, fullpem, privkey, nil)
|
||||||
// Check that it's an EADDRINUSE
|
// Check that it's an EADDRINUSE
|
||||||
Expect(err).ToNot(BeNil())
|
Expect(err).ToNot(BeNil())
|
||||||
opErr, ok := err.(*net.OpError)
|
opErr, ok := err.(*net.OpError)
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import (
|
|||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"os"
|
"path"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
@@ -16,7 +17,11 @@ var _ = Describe("error codes", func() {
|
|||||||
It("has a string representation for every error code", func() {
|
It("has a string representation for every error code", func() {
|
||||||
// We parse the error code file, extract all constants, and verify that
|
// We parse the error code file, extract all constants, and verify that
|
||||||
// each of them has a string version. Go FTW!
|
// each of them has a string version. Go FTW!
|
||||||
filename := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/qerr/error_codes.go"
|
_, thisfile, _, ok := runtime.Caller(0)
|
||||||
|
if !ok {
|
||||||
|
panic("Failed to get current frame")
|
||||||
|
}
|
||||||
|
filename := path.Join(path.Dir(thisfile), "error_codes.go")
|
||||||
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
|
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
constSpecs := fileAst.Decls[0].(*ast.GenDecl).Specs
|
constSpecs := fileAst.Decls[0].(*ast.GenDecl).Specs
|
||||||
|
|||||||
21
testdata/cert.go
vendored
21
testdata/cert.go
vendored
@@ -2,19 +2,30 @@ package testdata
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"os"
|
"path"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var certPath string
|
var certPath string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
certPath = os.Getenv("GOPATH")
|
_, filename, _, ok := runtime.Caller(0)
|
||||||
certPath += "/src/github.com/lucas-clemente/quic-go/example"
|
if !ok {
|
||||||
|
panic("Failed to get current frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
certPath = path.Join(path.Dir(path.Dir(filename)), "example")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCertificatePaths returns the paths to 'fullchain.pem' and 'privkey.pem' for the
|
||||||
|
// quic.clemente.io cert.
|
||||||
|
func GetCertificatePaths() (string, string) {
|
||||||
|
return path.Join(certPath, "fullchain.pem"), path.Join(certPath, "privkey.pem")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTLSConfig returns a tls config for quic.clemente.io
|
// GetTLSConfig returns a tls config for quic.clemente.io
|
||||||
func GetTLSConfig() *tls.Config {
|
func GetTLSConfig() *tls.Config {
|
||||||
cert, err := tls.LoadX509KeyPair(certPath+"/fullchain.pem", certPath+"/privkey.pem")
|
cert, err := tls.LoadX509KeyPair(GetCertificatePaths())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -25,7 +36,7 @@ func GetTLSConfig() *tls.Config {
|
|||||||
|
|
||||||
// GetCertificate returns a certificate for quic.clemente.io
|
// GetCertificate returns a certificate for quic.clemente.io
|
||||||
func GetCertificate() tls.Certificate {
|
func GetCertificate() tls.Certificate {
|
||||||
cert, err := tls.LoadX509KeyPair(certPath+"/fullchain.pem", certPath+"/privkey.pem")
|
cert, err := tls.LoadX509KeyPair(GetCertificatePaths())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user