Skip to content

Commit 67b2574

Browse files
committed
worked on quic implementation
1 parent b5d2dc6 commit 67b2574

File tree

10 files changed

+175
-208
lines changed

10 files changed

+175
-208
lines changed

Gopkg.lock

Lines changed: 38 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
branch = "master"
3030
name = "github.com/golang/protobuf"
3131

32-
[[constraint]]
33-
name = "github.com/lucas-clemente/quic-go"
34-
branch = "master"
35-
3632
[[constraint]]
3733
branch = "master"
3834
name = "github.com/miekg/dns"

example/static-quic/main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"crypto/tls"
7+
"crypto/x509"
8+
"encoding/pem"
9+
"fmt"
10+
"log"
11+
"math/big"
12+
"net"
13+
"time"
14+
15+
"github.com/simia-tech/netx"
16+
"github.com/simia-tech/netx/filter/blacklist"
17+
_ "github.com/simia-tech/netx/network/quic"
18+
"github.com/simia-tech/netx/provider/static"
19+
"github.com/simia-tech/netx/selector/roundrobin"
20+
"github.com/simia-tech/netx/value"
21+
)
22+
23+
func main() {
24+
addrOneChan := runEchoServer("127.0.0.1:0")
25+
addrTwoChan := runEchoServer("127.0.0.1:0")
26+
27+
provider := static.NewProvider()
28+
provider.Add("test", value.NewEndpointFromAddr(<-addrOneChan, value.TLS(&tls.Config{InsecureSkipVerify: true})))
29+
provider.Add("test", value.NewEndpointFromAddr(<-addrTwoChan, value.TLS(&tls.Config{InsecureSkipVerify: true})))
30+
31+
md, err := netx.NewMultiDialer(provider, blacklist.NewFilter(blacklist.ConstantBackoff(100*time.Millisecond)), roundrobin.NewSelector())
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
conn, err := md.Dial("test")
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
fmt.Fprintln(conn, "hello")
42+
43+
buffer := make([]byte, 6)
44+
conn.Read(buffer)
45+
fmt.Print(string(buffer))
46+
}
47+
48+
func runEchoServer(address string) chan net.Addr {
49+
ch := make(chan net.Addr)
50+
go func() {
51+
listener, err := netx.Listen("quic", address, value.TLS(generateTLSConfig()))
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
ch <- listener.Addr()
56+
57+
conn, err := listener.Accept()
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
62+
buffer := make([]byte, 6)
63+
conn.Read(buffer)
64+
conn.Write(buffer)
65+
}()
66+
return ch
67+
}
68+
69+
func generateTLSConfig() *tls.Config {
70+
key, err := rsa.GenerateKey(rand.Reader, 1024)
71+
if err != nil {
72+
panic(err)
73+
}
74+
template := x509.Certificate{SerialNumber: big.NewInt(1)}
75+
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
76+
if err != nil {
77+
panic(err)
78+
}
79+
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
80+
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
81+
82+
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
83+
if err != nil {
84+
panic(err)
85+
}
86+
return &tls.Config{Certificates: []tls.Certificate{tlsCert}}
87+
}

network/quic/addr.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package quic
2+
3+
type addr struct {
4+
address string
5+
}
6+
7+
func (a *addr) Network() string {
8+
return "quic"
9+
}
10+
11+
func (a addr) String() string {
12+
return a.address
13+
}

network/quic/conn.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func init() {
2323

2424
// Dial opens a connection to the provided address.
2525
func Dial(address string, options *value.Options) (net.Conn, error) {
26-
session, err := quic.DialAddr(address, options.TLSConfig, &quic.Config{})
26+
session, err := quic.DialAddr(address, options.TLSConfig, nil)
2727
if err != nil {
2828
return nil, err
2929
}
@@ -56,8 +56,7 @@ func (c *conn) Read(data []byte) (int, error) {
5656
}
5757

5858
func (c *conn) Write(data []byte) (int, error) {
59-
n, err := c.stream.Write(data)
60-
return n, err
59+
return c.stream.Write(data)
6160
}
6261

6362
func (c *conn) Close() error {
@@ -77,10 +76,16 @@ func (c *conn) Close() error {
7776
}
7877

7978
func (c *conn) LocalAddr() net.Addr {
79+
if c.session == nil {
80+
return nil
81+
}
8082
return c.session.LocalAddr()
8183
}
8284

8385
func (c *conn) RemoteAddr() net.Addr {
86+
if c.session == nil {
87+
return nil
88+
}
8489
return c.session.RemoteAddr()
8590
}
8691

network/quic/listener.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func init() {
1818

1919
// Listen starts a listener at the provided address.
2020
func Listen(address string, options *value.Options) (net.Listener, error) {
21-
l, err := quic.ListenAddr(address, options.TLSConfig, &quic.Config{})
21+
l, err := quic.ListenAddr(address, options.TLSConfig, nil)
2222
if err != nil {
2323
return nil, err
2424
}
@@ -40,12 +40,13 @@ func (l *listener) Accept() (net.Conn, error) {
4040
}
4141

4242
return &conn{
43-
stream: stream,
43+
session: session,
44+
stream: stream,
4445
}, nil
4546
}
4647

4748
func (l *listener) Addr() net.Addr {
48-
return l.listener.Addr()
49+
return &addr{address: l.listener.Addr().String()}
4950
}
5051

5152
func (l *listener) Close() error {

0 commit comments

Comments
 (0)