delete moq, create transport rooms
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m48s
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m48s
This commit is contained in:
146
main.go
Normal file
146
main.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
"github.com/quic-go/webtransport-go"
|
||||
)
|
||||
|
||||
type Room struct {
|
||||
conns map[*webtransport.Session]bool
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
rooms map[string]*Room
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
return &Server{
|
||||
rooms: make(map[string]*Room),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getRoom(name string) *Room {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if room, ok := s.rooms[name]; ok {
|
||||
return room
|
||||
}
|
||||
|
||||
room := &Room{
|
||||
conns: make(map[*webtransport.Session]bool),
|
||||
}
|
||||
s.rooms[name] = room
|
||||
return room
|
||||
}
|
||||
|
||||
func (s *Server) handleSession(roomName string, sess *webtransport.Session) {
|
||||
room := s.getRoom(roomName)
|
||||
|
||||
room.mu.Lock()
|
||||
room.conns[sess] = true
|
||||
room.mu.Unlock()
|
||||
|
||||
log.Println("joined room:", roomName)
|
||||
|
||||
defer func() {
|
||||
room.mu.Lock()
|
||||
delete(room.conns, sess)
|
||||
room.mu.Unlock()
|
||||
log.Println("left room:", roomName)
|
||||
}()
|
||||
|
||||
for {
|
||||
stream, err := sess.AcceptStream(context.Background())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
go s.handleStream(room, sess, stream)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleStream(room *Room, sender *webtransport.Session, stream *webtransport.Stream) {
|
||||
buf := make([]byte, 4096)
|
||||
|
||||
for {
|
||||
n, err := stream.Read(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
room.mu.Lock()
|
||||
for conn := range room.conns {
|
||||
if conn == sender {
|
||||
continue
|
||||
}
|
||||
go func(c *webtransport.Session) {
|
||||
out, err := c.OpenStream()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer out.Close()
|
||||
out.Write(buf[:n])
|
||||
}(conn)
|
||||
}
|
||||
room.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
domain := "qgo-server.quizer.space"
|
||||
email := "serverussnap@outlook.com"
|
||||
|
||||
certmagic.DefaultACME.Email = email
|
||||
certmagic.DefaultACME.Agreed = true
|
||||
certmagic.Default.Storage = &certmagic.FileStorage{
|
||||
Path: "/root/.local/share/certmagic",
|
||||
}
|
||||
|
||||
tlsConf, err := certmagic.TLS([]string{domain})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
tlsConf.NextProtos = []string{"h3"}
|
||||
tlsConf.MinVersion = tls.VersionTLS13
|
||||
|
||||
server := NewServer()
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
wtServer := &webtransport.Server{
|
||||
H3: &http3.Server{
|
||||
Addr: ":443",
|
||||
TLSConfig: tlsConf,
|
||||
},
|
||||
}
|
||||
|
||||
mux.HandleFunc("/room/", func(w http.ResponseWriter, r *http.Request) {
|
||||
roomName := strings.TrimPrefix(r.URL.Path, "/room/")
|
||||
|
||||
sess, err := wtServer.Upgrade(w, r)
|
||||
if err != nil {
|
||||
log.Println("upgrade error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
go server.handleSession(roomName, sess)
|
||||
})
|
||||
|
||||
wtServer.H3.Handler = mux
|
||||
|
||||
log.Println("Relay running on https://" + domain + "/room/{room}")
|
||||
|
||||
log.Fatal(wtServer.ListenAndServe())
|
||||
}
|
||||
Reference in New Issue
Block a user