iconia/server.go

55 lines
1009 B
Go

package main
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"
"sync"
"github.com/hashicorp/yamux"
"within.website/x/localca"
)
// Config uration for the server
type Config struct {
HTTPPort, HTTPSPort, YamuxPort, StatusPort, DomainSuffix string
}
// Server is the iconia gateway server
type Server struct {
Config
clients map[string][]*yamux.Session
clientsLock *sync.RWMutex
certManager localca.Manager
plainServer, statusServer *http.Server
tlsListener, yamuxListener net.Listener
tokenInfo map[string]string
tokensLock *sync.Mutex
}
func (s *Server) handleYamuxClientHello(chi *tls.ClientHelloInfo) (*tls.Config, error) {
var found bool
s.tokensLock.Lock()
var token = s.tokenInfo[strings.Split(chi.ServerName, s.Config.DomainSuffix)[0]]
s.tokensLock.Unlock()
for _, proto := range chi.NextProtos {
if proto == token {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("unknown token for domain %s", chi.ServerName)
}
return nil, nil
}