37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.go 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-09-17 15:49 . Shard user controllers
package remotedialer
import (
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
errFailedAuth = errors.New("failed authentication")
errWrongMessageType = errors.New("wrong websocket message type")
)
type Authorizer func(req *http.Request) (clientKey string, authed bool, err error)
type ErrorWriter func(rw http.ResponseWriter, req *http.Request, code int, err error)
func DefaultErrorWriter(rw http.ResponseWriter, req *http.Request, code int, err error) {
rw.Write([]byte(err.Error()))
rw.WriteHeader(code)
}
type Server struct {
PeerID string
PeerToken string
authorizer Authorizer
errorWriter ErrorWriter
sessions *sessionManager
peers map[string]peer
peerLock sync.Mutex
}
func New(auth Authorizer, errorWriter ErrorWriter) *Server {
return &Server{
peers: map[string]peer{},
authorizer: auth,
errorWriter: errorWriter,
sessions: newSessionManager(),
}
}
func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
clientKey, authed, peer, err := s.auth(req)
if err != nil {
s.errorWriter(rw, req, 400, err)
return
}
if !authed {
s.errorWriter(rw, req, 401, errFailedAuth)
return
}
logrus.Infof("Handling backend connection request [%s]", clientKey)
upgrader := websocket.Upgrader{
HandshakeTimeout: 5 * time.Second,
CheckOrigin: func(r *http.Request) bool { return true },
Error: s.errorWriter,
}
wsConn, err := upgrader.Upgrade(rw, req, nil)
if err != nil {
s.errorWriter(rw, req, 400, errors.Wrapf(err, "Error during upgrade for host [%v]", clientKey))
return
}
session := s.sessions.add(clientKey, wsConn, peer)
defer s.sessions.remove(session)
// Don't need to associate req.Context() to the session, it will cancel otherwise
code, err := session.serve()
if err != nil {
// Hijacked so we can't write to the client
logrus.Infof("error in remotedialer server [%d]: %v", code, err)
}
}
func (s *Server) auth(req *http.Request) (clientKey string, authed, peer bool, err error) {
id := req.Header.Get(ID)
token := req.Header.Get(Token)
if id != "" && token != "" {
// peer authentication
s.peerLock.Lock()
p, ok := s.peers[id]
s.peerLock.Unlock()
if ok && p.token == token {
return id, true, true, nil
}
}
id, authed, err = s.authorizer(req)
return id, authed, false, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.5-rc3

搜索帮助

0d507c66 1850385 C8b1a773 1850385