2020-06-10 09:54:06 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package yggconn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-02 16:43:07 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2020-07-03 13:28:43 +00:00
|
|
|
"crypto/x509/pkix"
|
2020-07-02 16:43:07 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
2020-06-10 09:54:06 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2020-07-02 16:43:07 +00:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
2020-07-10 15:28:18 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-07-02 16:43:07 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2020-06-10 09:54:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (n *Node) listenFromYgg() {
|
2020-07-02 16:43:07 +00:00
|
|
|
var err error
|
|
|
|
n.listener, err = quic.Listen(
|
|
|
|
n.packetConn, // yggdrasil.PacketConn
|
|
|
|
n.tlsConfig, // TLS config
|
|
|
|
n.quicConfig, // QUIC config
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-06-10 09:54:06 +00:00
|
|
|
for {
|
2020-07-02 16:43:07 +00:00
|
|
|
session, err := n.listener.Accept(context.TODO())
|
2020-06-10 09:54:06 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Println("n.listener.Accept:", err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-02 16:43:07 +00:00
|
|
|
go n.listenFromQUIC(session)
|
2020-06-10 09:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:43:07 +00:00
|
|
|
func (n *Node) listenFromQUIC(session quic.Session) {
|
2020-06-10 09:54:06 +00:00
|
|
|
n.sessions.Store(session.RemoteAddr().String(), session)
|
|
|
|
defer n.sessions.Delete(session.RemoteAddr())
|
2020-07-10 15:28:18 +00:00
|
|
|
if n.NewSession != nil {
|
|
|
|
if len(session.ConnectionState().PeerCertificates) == 1 {
|
|
|
|
subjectName := session.ConnectionState().PeerCertificates[0].Subject.CommonName
|
|
|
|
go n.NewSession(gomatrixserverlib.ServerName(subjectName))
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 09:54:06 +00:00
|
|
|
for {
|
2020-07-02 16:43:07 +00:00
|
|
|
st, err := session.AcceptStream(context.TODO())
|
2020-06-10 09:54:06 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Println("session.AcceptStream:", err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-02 16:43:07 +00:00
|
|
|
n.incoming <- QUICStream{st, session}
|
2020-06-10 09:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements net.Listener
|
|
|
|
func (n *Node) Accept() (net.Conn, error) {
|
|
|
|
return <-n.incoming, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements net.Listener
|
|
|
|
func (n *Node) Close() error {
|
|
|
|
return n.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements net.Listener
|
|
|
|
func (n *Node) Addr() net.Addr {
|
|
|
|
return n.listener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements http.Transport.Dial
|
|
|
|
func (n *Node) Dial(network, address string) (net.Conn, error) {
|
|
|
|
return n.DialContext(context.TODO(), network, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements http.Transport.DialContext
|
|
|
|
func (n *Node) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
s, ok1 := n.sessions.Load(address)
|
2020-07-02 16:43:07 +00:00
|
|
|
session, ok2 := s.(quic.Session)
|
|
|
|
if !ok1 || !ok2 || (ok1 && ok2 && session.ConnectionState().HandshakeComplete) {
|
|
|
|
dest, err := hex.DecodeString(address)
|
2020-06-10 09:54:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-02 16:43:07 +00:00
|
|
|
if len(dest) != crypto.BoxPubKeyLen {
|
|
|
|
return nil, errors.New("invalid key length supplied")
|
2020-06-10 09:54:06 +00:00
|
|
|
}
|
2020-07-02 16:43:07 +00:00
|
|
|
var pubKey crypto.BoxPubKey
|
|
|
|
copy(pubKey[:], dest)
|
|
|
|
|
|
|
|
session, err = quic.Dial(
|
|
|
|
n.packetConn, // yggdrasil.PacketConn
|
|
|
|
&pubKey, // dial address
|
|
|
|
address, // dial SNI
|
|
|
|
n.tlsConfig, // TLS config
|
|
|
|
n.quicConfig, // QUIC config
|
|
|
|
)
|
2020-06-10 09:54:06 +00:00
|
|
|
if err != nil {
|
2020-07-02 16:43:07 +00:00
|
|
|
n.log.Println("n.dialer.DialContext:", err)
|
2020-06-10 09:54:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-02 16:43:07 +00:00
|
|
|
go n.listenFromQUIC(session)
|
2020-06-10 09:54:06 +00:00
|
|
|
}
|
|
|
|
st, err := session.OpenStream()
|
|
|
|
if err != nil {
|
|
|
|
n.log.Println("session.OpenStream:", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-02 16:43:07 +00:00
|
|
|
return QUICStream{st, session}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) generateTLSConfig() *tls.Config {
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
template := x509.Certificate{
|
2020-07-03 13:28:43 +00:00
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: n.DerivedServerName(),
|
|
|
|
},
|
2020-07-02 16:43:07 +00:00
|
|
|
SerialNumber: big.NewInt(1),
|
|
|
|
NotAfter: time.Now().Add(time.Hour * 24 * 365),
|
|
|
|
DNSNames: []string{n.DerivedSessionName()},
|
|
|
|
}
|
|
|
|
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
|
|
|
|
|
|
|
|
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{tlsCert},
|
|
|
|
NextProtos: []string{"quic-matrix-ygg"},
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
2020-06-10 09:54:06 +00:00
|
|
|
}
|