mirror of
https://github.com/lwch/natpass.git
synced 2026-10-21 04:45:09 +00:00
修改变量名称
This commit is contained in:
@@ -16,17 +16,17 @@ import (
|
||||
|
||||
type Client struct {
|
||||
sync.RWMutex
|
||||
cfg *global.Configure
|
||||
conn *network.Conn
|
||||
tunnels map[string]*tunnel
|
||||
cfg *global.Configure
|
||||
conn *network.Conn
|
||||
links map[string]*link
|
||||
}
|
||||
|
||||
// New create client
|
||||
func New(cfg *global.Configure, conn *network.Conn) *Client {
|
||||
return &Client{
|
||||
cfg: cfg,
|
||||
conn: conn,
|
||||
tunnels: make(map[string]*tunnel),
|
||||
cfg: cfg,
|
||||
conn: conn,
|
||||
links: make(map[string]*link),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,18 +97,18 @@ func (c *Client) handleTcpTunnel(ctx context.Context, t global.Tunnel) {
|
||||
id, err := runtime.UUID(16, "0123456789abcdef")
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
logging.Error("generate tunnel id failed, err=%v", err)
|
||||
logging.Error("generate link id failed, err=%v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
tn := newTunnel(id, t.Name, t.Target, c, conn)
|
||||
c.sendConnect(tn.id, t)
|
||||
link := newLink(id, t.Name, t.Target, c, conn)
|
||||
c.sendConnect(link.id, t)
|
||||
|
||||
c.Lock()
|
||||
c.tunnels[tn.id] = tn
|
||||
c.links[link.id] = link
|
||||
c.Unlock()
|
||||
|
||||
go tn.loop(ctx)
|
||||
go link.loop(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,12 +140,12 @@ func (c *Client) handleConnect(ctx context.Context, from, to string, req *networ
|
||||
c.connectError(to, req.GetId(), err.Error())
|
||||
return
|
||||
}
|
||||
tn := newTunnel(req.GetId(), req.GetName(), from, c, conn)
|
||||
link := newLink(req.GetId(), req.GetName(), from, c, conn)
|
||||
c.Lock()
|
||||
c.tunnels[tn.id] = tn
|
||||
c.links[link.id] = link
|
||||
c.Unlock()
|
||||
c.connectOK(to, req.GetId())
|
||||
go tn.loop(ctx)
|
||||
go link.loop(ctx)
|
||||
}
|
||||
|
||||
// handleDisconnect handle disconnect message from remote, this means remote connection is closed
|
||||
@@ -153,14 +153,14 @@ func (c *Client) handleDisconnect(data *network.Disconnect) {
|
||||
id := data.GetId()
|
||||
|
||||
c.RLock()
|
||||
tn := c.tunnels[id]
|
||||
tn := c.links[id]
|
||||
c.RUnlock()
|
||||
|
||||
if tn != nil {
|
||||
tn.close()
|
||||
|
||||
c.Lock()
|
||||
delete(c.tunnels, id)
|
||||
delete(c.links, id)
|
||||
c.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -169,10 +169,10 @@ func (c *Client) handleDisconnect(data *network.Disconnect) {
|
||||
func (c *Client) handleData(data *network.Data) {
|
||||
id := data.GetCid()
|
||||
c.RLock()
|
||||
tn := c.tunnels[id]
|
||||
tn := c.links[id]
|
||||
c.RUnlock()
|
||||
if tn == nil {
|
||||
logging.Error("tunnel %s not found", id)
|
||||
logging.Error("link %s not found", id)
|
||||
return
|
||||
}
|
||||
tn.write(data.GetData())
|
||||
|
||||
@@ -95,7 +95,7 @@ func (c *Client) disconnect(id, to string) {
|
||||
}
|
||||
c.conn.WriteMessage(&msg, time.Second)
|
||||
c.Lock()
|
||||
delete(c.tunnels, id)
|
||||
delete(c.links, id)
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -9,17 +9,17 @@ import (
|
||||
"github.com/lwch/logging"
|
||||
)
|
||||
|
||||
type tunnel struct {
|
||||
type link struct {
|
||||
cli *Client
|
||||
id string
|
||||
name string
|
||||
target string
|
||||
id string // link id
|
||||
name string // tunnel name
|
||||
target string // remote client id
|
||||
c net.Conn
|
||||
}
|
||||
|
||||
func newTunnel(id, name, target string, cli *Client, conn net.Conn) *tunnel {
|
||||
logging.Info("create tunnel %s: %s", name, id)
|
||||
return &tunnel{
|
||||
func newLink(id, name, target string, cli *Client, conn net.Conn) *link {
|
||||
logging.Info("create link %s: %s", name, id)
|
||||
return &link{
|
||||
cli: cli,
|
||||
id: id,
|
||||
name: name,
|
||||
@@ -28,16 +28,16 @@ func newTunnel(id, name, target string, cli *Client, conn net.Conn) *tunnel {
|
||||
}
|
||||
}
|
||||
|
||||
func (tn *tunnel) close() {
|
||||
logging.Info("disconnect tunnel %s: %s", tn.name, tn.id)
|
||||
err := tn.c.Close()
|
||||
func (l *link) close() {
|
||||
logging.Info("disconnect tunnel %s on link %s", l.name, l.id)
|
||||
err := l.c.Close()
|
||||
if err == nil {
|
||||
tn.cli.disconnect(tn.id, tn.target)
|
||||
l.cli.disconnect(l.id, l.target)
|
||||
}
|
||||
}
|
||||
|
||||
func (tn *tunnel) loop(ctx context.Context) {
|
||||
defer tn.close()
|
||||
func (l *link) loop(ctx context.Context) {
|
||||
defer l.close()
|
||||
buf := make([]byte, 32*1024)
|
||||
for {
|
||||
select {
|
||||
@@ -45,20 +45,20 @@ func (tn *tunnel) loop(ctx context.Context) {
|
||||
return
|
||||
default:
|
||||
}
|
||||
n, err := tn.c.Read(buf)
|
||||
n, err := l.c.Read(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
logging.Debug("tunnel %s read from local %d bytes", tn.name, n)
|
||||
tn.cli.send(tn.id, tn.target, buf[:n])
|
||||
logging.Debug("link %s on tunnel %s read from local %d bytes", l.id, l.name, n)
|
||||
l.cli.send(l.id, l.target, buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
func (tn *tunnel) write(data []byte) error {
|
||||
logging.Debug("tunnel %s write from remote %d bytes", tn.name, len(data))
|
||||
_, err := io.Copy(tn.c, bytes.NewReader(data))
|
||||
func (l *link) write(data []byte) error {
|
||||
logging.Debug("link %s on tunnel %s write from remote %d bytes", l.id, l.name, len(data))
|
||||
_, err := io.Copy(l.c, bytes.NewReader(data))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,18 +11,18 @@ import (
|
||||
|
||||
type client struct {
|
||||
sync.RWMutex
|
||||
parent *Handler
|
||||
id string
|
||||
c *network.Conn
|
||||
tunnels map[string]struct{}
|
||||
parent *Handler
|
||||
id string
|
||||
c *network.Conn
|
||||
links map[string]struct{} // link id => struct{}
|
||||
}
|
||||
|
||||
func newClient(parent *Handler, id string, conn *network.Conn) *client {
|
||||
return &client{
|
||||
parent: parent,
|
||||
id: id,
|
||||
c: conn,
|
||||
tunnels: make(map[string]struct{}),
|
||||
parent: parent,
|
||||
id: id,
|
||||
c: conn,
|
||||
links: make(map[string]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,23 +44,23 @@ func (c *client) writeMessage(msg *network.Msg) error {
|
||||
return c.c.WriteMessage(msg, time.Second)
|
||||
}
|
||||
|
||||
func (c *client) addTunnel(id string) {
|
||||
func (c *client) addLink(id string) {
|
||||
c.Lock()
|
||||
c.tunnels[id] = struct{}{}
|
||||
c.links[id] = struct{}{}
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c *client) removeTunnel(id string) {
|
||||
func (c *client) removeLink(id string) {
|
||||
c.Lock()
|
||||
delete(c.tunnels, id)
|
||||
delete(c.links, id)
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c *client) getTunnels() []string {
|
||||
ret := make([]string, 0, len(c.tunnels))
|
||||
func (c *client) getLinks() []string {
|
||||
ret := make([]string, 0, len(c.links))
|
||||
c.RLock()
|
||||
for tn := range c.tunnels {
|
||||
ret = append(ret, tn)
|
||||
for link := range c.links {
|
||||
ret = append(ret, link)
|
||||
}
|
||||
c.RUnlock()
|
||||
return ret
|
||||
@@ -78,6 +78,6 @@ func (c *client) close(id string) {
|
||||
}
|
||||
c.c.WriteMessage(&msg, time.Second)
|
||||
c.Lock()
|
||||
delete(c.tunnels, id)
|
||||
delete(c.links, id)
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type Handler struct {
|
||||
sync.RWMutex
|
||||
cfg *global.Configure
|
||||
clients map[string]*client // client id => client
|
||||
tunnels map[string][2]*client // tunnel id => endpoints
|
||||
links map[string][2]*client // link id => endpoints
|
||||
}
|
||||
|
||||
// New create handler
|
||||
@@ -23,7 +23,7 @@ func New(cfg *global.Configure) *Handler {
|
||||
return &Handler{
|
||||
cfg: cfg,
|
||||
clients: make(map[string]*client),
|
||||
tunnels: make(map[string][2]*client),
|
||||
links: make(map[string][2]*client),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,33 +112,33 @@ func (h *Handler) msgHook(msg *network.Msg) {
|
||||
id := msg.GetCrep().GetId()
|
||||
var pair [2]*client
|
||||
if fromCli != nil {
|
||||
fromCli.addTunnel(id)
|
||||
fromCli.addLink(id)
|
||||
pair[0] = fromCli
|
||||
}
|
||||
if toCli != nil {
|
||||
toCli.addTunnel(id)
|
||||
toCli.addLink(id)
|
||||
pair[1] = toCli
|
||||
}
|
||||
h.Lock()
|
||||
h.tunnels[id] = pair
|
||||
h.links[id] = pair
|
||||
h.Unlock()
|
||||
}
|
||||
case network.Msg_disconnect:
|
||||
if fromCli != nil {
|
||||
fromCli.removeTunnel(msg.GetXDisconnect().GetId())
|
||||
fromCli.removeLink(msg.GetXDisconnect().GetId())
|
||||
}
|
||||
if toCli != nil {
|
||||
toCli.removeTunnel(msg.GetXDisconnect().GetId())
|
||||
toCli.removeLink(msg.GetXDisconnect().GetId())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// closeAll close all tunnel from client
|
||||
// closeAll close all links from client
|
||||
func (h *Handler) closeAll(cli *client) {
|
||||
tunnels := cli.getTunnels()
|
||||
for _, t := range tunnels {
|
||||
links := cli.getLinks()
|
||||
for _, t := range links {
|
||||
h.RLock()
|
||||
pair := h.tunnels[t]
|
||||
pair := h.links[t]
|
||||
h.RUnlock()
|
||||
if pair[0] != nil {
|
||||
pair[0].close(t)
|
||||
@@ -147,7 +147,7 @@ func (h *Handler) closeAll(cli *client) {
|
||||
pair[1].close(t)
|
||||
}
|
||||
h.Lock()
|
||||
delete(h.tunnels, t)
|
||||
delete(h.links, t)
|
||||
h.Unlock()
|
||||
}
|
||||
h.Lock()
|
||||
|
||||
Reference in New Issue
Block a user