mirror of
https://github.com/wweir/sower.git
synced 2024-04-21 12:42:15 +00:00
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package quic
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/golang/glog"
|
|
quic "github.com/lucas-clemente/quic-go"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type streamConn struct {
|
|
quic.Stream
|
|
sess quic.Session
|
|
}
|
|
|
|
func (s *streamConn) LocalAddr() net.Addr {
|
|
return s.sess.LocalAddr()
|
|
}
|
|
|
|
func (s *streamConn) RemoteAddr() net.Addr {
|
|
return s.sess.RemoteAddr()
|
|
}
|
|
|
|
func mockTlsPem() *tls.Config {
|
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
if err != nil {
|
|
glog.Fatalln(err)
|
|
}
|
|
template := x509.Certificate{SerialNumber: big.NewInt(1)}
|
|
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
|
|
if err != nil {
|
|
glog.Fatalln(err)
|
|
}
|
|
|
|
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
|
|
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
if err != nil {
|
|
glog.Fatalln(err)
|
|
}
|
|
return &tls.Config{Certificates: []tls.Certificate{tlsCert}}
|
|
}
|
|
|
|
func WithTimeout(fn func() error, timeout time.Duration) error {
|
|
var okCh = make(chan struct{})
|
|
var err error
|
|
|
|
go func() {
|
|
err = fn()
|
|
close(okCh)
|
|
}()
|
|
|
|
select {
|
|
case <-okCh:
|
|
return err
|
|
case <-time.After(timeout):
|
|
return errors.New("timeout: " + timeout.String())
|
|
}
|
|
}
|