-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/pion/dtls/v3/examples/util" | ||
quic "github.com/refraction-networking/uquic" | ||
tls "github.com/refraction-networking/utls" | ||
) | ||
|
||
func main() { | ||
var remoteAddr = flag.String("raddr", "127.0.0.1:6666", "remote address") | ||
// var pubkey = flag.String("secret", "0b63baad7f2f4bb5b547c53adc0fbb179852910607935e6f4b5639fd989b1156", "shared secret") | ||
// var covert = flag.String("covert", "1.2.3.4:5678", "covert address") | ||
flag.Parse() | ||
|
||
addr, err := net.ResolveUDPAddr("udp", *remoteAddr) | ||
util.Check(err) | ||
|
||
// pubkeyBytes, err := hex.DecodeString(*pubkey) | ||
// util.Check(err) | ||
|
||
pconn, err := net.ListenUDP("udp", nil) | ||
util.Check(err) | ||
quicSpec, err := quic.QUICID2Spec(quic.QUICFirefox_116) | ||
util.Check(err) | ||
for _, ext := range quicSpec.ClientHelloSpec.Extensions { | ||
if ks, ok := ext.(*tls.KeyShareExtension); ok { | ||
ks.KeyShares = []tls.KeyShare{ | ||
{ | ||
Group: tls.X25519Kyber768Draft00, | ||
Data: []byte{}, | ||
}, | ||
} | ||
break | ||
} | ||
} | ||
|
||
tp := quic.UTransport{ | ||
Transport: &quic.Transport{ | ||
Conn: pconn, | ||
}, | ||
QUICSpec: &quicSpec, | ||
} | ||
|
||
// tp := &quic.Transport{ | ||
// Conn: pconn, | ||
// } | ||
|
||
// econn1, err := tp.DialEarly(context.Background(), addr, &tls.Config{ | ||
// InsecureSkipVerify: true, | ||
// NextProtos: []string{"h3"}, | ||
// }, &quic.Config{}) | ||
// util.Check(err) | ||
// _ = econn1 | ||
|
||
econn, err := tp.DialEarly(context.Background(), addr, &tls.Config{ | ||
InsecureSkipVerify: true, | ||
// CurvePreferences: []tls.CurveID{tls.X25519Kyber768Draft00}, | ||
NextProtos: []string{"h3"}, | ||
}, &quic.Config{}) | ||
util.Check(err) | ||
|
||
stream, err := econn.OpenStream() | ||
util.Check(err) | ||
|
||
stream2, err := econn.OpenStream() | ||
util.Check(err) | ||
|
||
fmt.Println("Connected; type 'exit' to shutdown gracefully") | ||
|
||
stream2.Write([]byte("testt\n")) | ||
|
||
// Simulate a chat session | ||
util.Chat(stream) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/hex" | ||
"encoding/pem" | ||
"flag" | ||
"fmt" | ||
"math/big" | ||
"net" | ||
"time" | ||
|
||
"github.com/pion/dtls/v3/examples/util" | ||
quic "github.com/refraction-networking/uquic" | ||
tls "github.com/refraction-networking/utls" | ||
) | ||
|
||
const ( | ||
receiveMTU = 8192 | ||
cidSize = 8 | ||
keySize = 32 | ||
station_privkey = "203963feed62ddda89b98857940f09866ae840f42e8c90160e411a0029b87e60" | ||
) | ||
|
||
type streamConn struct { | ||
quic.Stream | ||
quic.Connection | ||
} | ||
|
||
func main() { | ||
var listenAddr = flag.String("laddr", "0.0.0.0:6666", "listen address") | ||
|
||
flag.Parse() | ||
|
||
// Prepare the IP to connect to | ||
addr, err := net.ResolveUDPAddr("udp", *listenAddr) | ||
util.Check(err) | ||
|
||
priv, err := hex.DecodeString(station_privkey) | ||
util.Check(err) | ||
|
||
fmt.Printf("%v\n", priv) | ||
|
||
pconn, err := net.ListenUDP("udp", addr) | ||
util.Check(err) | ||
tp := quic.Transport{ | ||
Conn: pconn, | ||
} | ||
|
||
listener, err := tp.ListenEarly(generateTLSConfig(), &quic.Config{}) | ||
util.Check(err) | ||
|
||
// Simulate a chat session | ||
hub := util.NewHub() | ||
|
||
go func() { | ||
for { | ||
// Wait for a connection. | ||
econn, err := listener.Accept(context.Background()) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
for i := 0; i < 2; i++ { | ||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second) | ||
stream, err := econn.AcceptStream(ctx) | ||
if err != nil { | ||
continue | ||
} | ||
hub.Register(&streamConn{Stream: stream, Connection: econn}) | ||
|
||
} | ||
|
||
// `conn` is of type `net.Conn` but may be casted to `dtls.Conn` | ||
// using `dtlsConn := conn.(*dtls.Conn)` in order to to expose | ||
// functions like `ConnectionState` etc. | ||
|
||
// Register the connection with the chat hub | ||
} | ||
}() | ||
|
||
// Start chatting | ||
hub.Chat() | ||
} | ||
|
||
// Setup a bare-bones TLS config for the server | ||
func generateTLSConfig() *tls.Config { | ||
key, err := rsa.GenerateKey(rand.Reader, 1024) | ||
if err != nil { | ||
panic(err) | ||
} | ||
template := x509.Certificate{SerialNumber: big.NewInt(1)} | ||
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{"h3"}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters