-
Notifications
You must be signed in to change notification settings - Fork 35
/
tcpServer.go
114 lines (105 loc) · 2.81 KB
/
tcpServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/glog"
"github.com/osgochina/dmicro/drpc"
"github.com/osgochina/dmicro/drpc/status"
"github.com/osgochina/dmicro/easyservice"
"github.com/vprix/vncproxy/rfb"
"github.com/vprix/vncproxy/security"
"github.com/vprix/vncproxy/session"
"github.com/vprix/vncproxy/vnc"
"golang.org/x/net/context"
"io"
"net"
)
// TcpSandBox Tcp的服务
type TcpSandBox struct {
id int
name string
cfg *gcfg.Config
service *easyservice.EasyService
lis net.Listener
closed chan struct{}
}
// NewTcpSandBox 创建一个默认的服务沙盒
func NewTcpSandBox(cfg *gcfg.Config) *TcpSandBox {
id := easyservice.GetNextSandBoxId()
sBox := &TcpSandBox{
id: id,
name: fmt.Sprintf("tcp_%d", id),
cfg: cfg,
closed: make(chan struct{}),
}
return sBox
}
func (that *TcpSandBox) ID() int {
return that.id
}
func (that *TcpSandBox) Name() string {
return that.name
}
func (that *TcpSandBox) Setup() error {
var err error
addr := fmt.Sprintf("%s:%d", that.cfg.MustGet(context.TODO(), "tcpHost"), that.cfg.MustGet(context.TODO(), "tcpPort"))
that.lis, err = net.Listen("tcp", addr)
if err != nil {
glog.Fatalf(context.TODO(), "Error listen. %v", err)
}
fmt.Printf("Tcp proxy started! listening %s . vnc server %s:%d\n", that.lis.Addr().String(), that.cfg.MustGet(context.TODO(), "vncHost"), that.cfg.MustGet(context.TODO(), "vncPort"))
securityHandlers := []rfb.ISecurityHandler{&security.ServerAuthNone{}}
if len(that.cfg.MustGet(context.TODO(), "proxyPassword").Bytes()) > 0 {
securityHandlers = append(securityHandlers, &security.ServerAuthVNC{Password: that.cfg.MustGet(context.TODO(), "proxyPassword").Bytes()})
}
for {
conn, err := that.lis.Accept()
if err != nil {
select {
case <-that.closed:
return drpc.ErrListenClosed
default:
}
return err
}
go func(c net.Conn) {
defer func() {
//捕获错误,并且继续执行
if p := recover(); p != nil {
err = fmt.Errorf("panic:%v\n%s", p, status.PanicStackTrace())
}
}()
svrSession := session.NewServerSession(
rfb.OptSecurityHandlers(securityHandlers...),
rfb.OptGetConn(func(sess rfb.ISession) (io.ReadWriteCloser, error) {
return c, nil
}),
)
play := vnc.NewPlayer(that.cfg.MustGet(context.TODO(), "rbsFile").String(), svrSession)
err = play.Start()
if err != nil {
glog.Warning(context.TODO(), err)
return
}
for {
select {
case err = <-play.Error():
glog.Warning(context.TODO(), err)
return
case <-that.closed:
play.Close()
return
case <-play.Wait():
return
}
}
}(conn)
}
}
func (that *TcpSandBox) Shutdown() error {
close(that.closed)
return that.lis.Close()
}
func (that *TcpSandBox) Service() *easyservice.EasyService {
return that.service
}