37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
peer.go 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
package remotedialer
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
)
var (
Token = "X-API-Tunnel-Token"
ID = "X-API-Tunnel-ID"
)
func (s *Server) AddPeer(url, id, token string) {
if s.PeerID == "" || s.PeerToken == "" {
return
}
ctx, cancel := context.WithCancel(context.Background())
peer := peer{
url: url,
id: id,
token: token,
cancel: cancel,
}
logrus.Infof("Adding peer %s, %s", url, id)
s.peerLock.Lock()
defer s.peerLock.Unlock()
if p, ok := s.peers[id]; ok {
if p.equals(peer) {
return
}
p.cancel()
}
s.peers[id] = peer
go peer.start(ctx, s)
}
func (s *Server) RemovePeer(id string) {
s.peerLock.Lock()
defer s.peerLock.Unlock()
if p, ok := s.peers[id]; ok {
logrus.Infof("Removing peer %s", id)
p.cancel()
}
delete(s.peers, id)
}
type peer struct {
url, id, token string
cancel func()
}
func (p peer) equals(other peer) bool {
return p.url == other.url &&
p.id == other.id &&
p.token == other.token
}
func (p *peer) start(ctx context.Context, s *Server) {
headers := http.Header{
ID: {s.PeerID},
Token: {s.PeerToken},
}
dialer := &websocket.Dialer{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
outer:
for {
select {
case <-ctx.Done():
break outer
default:
}
ws, _, err := dialer.Dial(p.url, headers)
if err != nil {
logrus.Errorf("Failed to connect to peer %s [local ID=%s]: %v", p.url, s.PeerID, err)
time.Sleep(5 * time.Second)
continue
}
session := newClientSession(func(string, string) bool { return true }, ws)
session.dialer = func(network, address string) (net.Conn, error) {
parts := strings.SplitN(network, "::", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid clientKey/proto: %s", network)
}
return s.Dial(parts[0], 15*time.Second, parts[1], address)
}
s.sessions.addListener(session)
_, err = session.serve()
s.sessions.removeListener(session)
session.Close()
if err != nil {
logrus.Errorf("Failed to serve peer connection %s: %v", p.id, err)
}
ws.Close()
time.Sleep(5 * time.Second)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.14-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385