-
-
Save dachinat/b47df9583e262434089605e00166cc96 to your computer and use it in GitHub Desktop.
QUIC Server
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
package main | |
import ( | |
"crypto/tls" | |
"log" | |
"github.com/quic-go/quic-go" | |
"golang.org/x/net/context" | |
) | |
func main() { | |
cert, err := tls.LoadX509KeyPair("/Users/freddiewoodruff/.ssh/my_certificate.pem", "/Users/freddiewoodruff/.ssh/my_key.pem") | |
if err != nil { | |
log.Fatal("Error loading certificate:", err) | |
} | |
tlsConfig := &tls.Config{ | |
Certificates: []tls.Certificate{cert}, | |
InsecureSkipVerify: true, // For testing purposes only | |
NextProtos: []string{"h3", "http/1.1"}, // Enable QUIC and HTTP/3 | |
} | |
listener, err := quic.ListenAddr("localhost:8080", tlsConfig, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer listener.Close() | |
log.Println("QUIC server started on localhost:8080") | |
for { | |
connection, err := listener.Accept(context.Background()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
go handleSession(connection) | |
} | |
} | |
func handleSession(connection quic.Connection) { | |
println("New connection established:", connection.RemoteAddr().String()) | |
for { | |
stream, err := connection.AcceptUniStream(context.Background()) | |
if err != nil { | |
println(err.Error()) | |
return | |
} | |
go handleRequest(stream) | |
} | |
} | |
func handleRequest(stream quic.ReceiveStream) { | |
buffer := make([]byte, 1024) | |
numBytes, err := stream.Read(buffer) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("Received: %s\n", buffer[:numBytes]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment