2017-05-15 17:13:34 -04:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2017-08-07 05:52:40 -04:00
|
|
|
"net"
|
2017-05-15 17:13:34 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Caller can invoke requests on the session
|
|
|
|
type Caller interface {
|
|
|
|
Context() context.Context
|
|
|
|
Supports(method string) bool
|
|
|
|
Conn() *grpc.ClientConn
|
|
|
|
Name() string
|
|
|
|
SharedKey() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
|
|
|
Session
|
|
|
|
cc *grpc.ClientConn
|
|
|
|
supported map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manager is a controller for accessing currently active sessions
|
|
|
|
type Manager struct {
|
|
|
|
sessions map[string]*client
|
|
|
|
mu sync.Mutex
|
|
|
|
updateCondition *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager returns a new Manager
|
|
|
|
func NewManager() (*Manager, error) {
|
|
|
|
sm := &Manager{
|
|
|
|
sessions: make(map[string]*client),
|
|
|
|
}
|
|
|
|
sm.updateCondition = sync.NewCond(&sm.mu)
|
|
|
|
return sm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleHTTPRequest handles an incoming HTTP request
|
|
|
|
func (sm *Manager) HandleHTTPRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
hijacker, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("handler does not support hijack")
|
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
id := r.Header.Get(headerSessionID)
|
2017-05-15 17:13:34 -04:00
|
|
|
|
|
|
|
proto := r.Header.Get("Upgrade")
|
|
|
|
|
|
|
|
sm.mu.Lock()
|
2017-09-29 06:32:26 -04:00
|
|
|
if _, ok := sm.sessions[id]; ok {
|
2017-05-15 17:13:34 -04:00
|
|
|
sm.mu.Unlock()
|
2017-09-29 06:32:26 -04:00
|
|
|
return errors.Errorf("session %s already exists", id)
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if proto == "" {
|
|
|
|
sm.mu.Unlock()
|
|
|
|
return errors.New("no upgrade proto in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if proto != "h2c" {
|
|
|
|
sm.mu.Unlock()
|
|
|
|
return errors.Errorf("protocol %s not supported", proto)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, _, err := hijacker.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
sm.mu.Unlock()
|
|
|
|
return errors.Wrap(err, "failed to hijack connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: http.StatusSwitchingProtocols,
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header{},
|
|
|
|
}
|
|
|
|
resp.Header.Set("Connection", "Upgrade")
|
|
|
|
resp.Header.Set("Upgrade", proto)
|
|
|
|
|
|
|
|
// set raw mode
|
|
|
|
conn.Write([]byte{})
|
|
|
|
resp.Write(conn)
|
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
return sm.handleConn(ctx, conn, r.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleConn handles an incoming raw connection
|
|
|
|
func (sm *Manager) HandleConn(ctx context.Context, conn net.Conn, opts map[string][]string) error {
|
|
|
|
sm.mu.Lock()
|
|
|
|
return sm.handleConn(ctx, conn, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// caller needs to take lock, this function will release it
|
|
|
|
func (sm *Manager) handleConn(ctx context.Context, conn net.Conn, opts map[string][]string) error {
|
2017-05-15 17:13:34 -04:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
opts = canonicalHeaders(opts)
|
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
h := http.Header(opts)
|
2017-09-29 06:32:26 -04:00
|
|
|
id := h.Get(headerSessionID)
|
2017-08-07 05:52:40 -04:00
|
|
|
name := h.Get(headerSessionName)
|
|
|
|
sharedKey := h.Get(headerSessionSharedKey)
|
|
|
|
|
2017-05-15 17:13:34 -04:00
|
|
|
ctx, cc, err := grpcClientConn(ctx, conn)
|
|
|
|
if err != nil {
|
|
|
|
sm.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &client{
|
|
|
|
Session: Session{
|
2017-09-29 06:32:26 -04:00
|
|
|
id: id,
|
2017-05-15 17:13:34 -04:00
|
|
|
name: name,
|
|
|
|
sharedKey: sharedKey,
|
|
|
|
ctx: ctx,
|
|
|
|
cancelCtx: cancel,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
},
|
|
|
|
cc: cc,
|
|
|
|
supported: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
for _, m := range opts[headerSessionMethod] {
|
2017-05-15 17:13:34 -04:00
|
|
|
c.supported[strings.ToLower(m)] = struct{}{}
|
|
|
|
}
|
2017-09-29 06:32:26 -04:00
|
|
|
sm.sessions[id] = c
|
2017-05-15 17:13:34 -04:00
|
|
|
sm.updateCondition.Broadcast()
|
|
|
|
sm.mu.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
sm.mu.Lock()
|
2017-09-29 06:32:26 -04:00
|
|
|
delete(sm.sessions, id)
|
2017-05-15 17:13:34 -04:00
|
|
|
sm.mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-c.ctx.Done()
|
|
|
|
conn.Close()
|
|
|
|
close(c.done)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:26 -04:00
|
|
|
// Get returns a session by ID
|
|
|
|
func (sm *Manager) Get(ctx context.Context, id string) (Caller, error) {
|
2017-05-15 17:13:34 -04:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
sm.updateCondition.Broadcast()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var c *client
|
|
|
|
|
|
|
|
sm.mu.Lock()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
sm.mu.Unlock()
|
2017-09-29 06:32:26 -04:00
|
|
|
return nil, errors.Wrapf(ctx.Err(), "no active session for %s", id)
|
2017-05-15 17:13:34 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
var ok bool
|
2017-09-29 06:32:26 -04:00
|
|
|
c, ok = sm.sessions[id]
|
2017-05-15 17:13:34 -04:00
|
|
|
if !ok || c.closed() {
|
|
|
|
sm.updateCondition.Wait()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sm.mu.Unlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Context() context.Context {
|
|
|
|
return c.context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) SharedKey() string {
|
|
|
|
return c.sharedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Supports(url string) bool {
|
|
|
|
_, ok := c.supported[strings.ToLower(url)]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *client) Conn() *grpc.ClientConn {
|
|
|
|
return c.cc
|
|
|
|
}
|
2017-09-29 06:32:26 -04:00
|
|
|
|
|
|
|
func canonicalHeaders(in map[string][]string) map[string][]string {
|
|
|
|
out := map[string][]string{}
|
|
|
|
for k := range in {
|
|
|
|
out[http.CanonicalHeaderKey(k)] = in[k]
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|