2017-06-12 14:11:23 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-26 17:35:40 +03:00
|
|
|
"bytes"
|
2017-06-12 14:11:23 +03:00
|
|
|
"context"
|
2017-06-26 17:35:40 +03:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-06-12 14:11:23 +03:00
|
|
|
"log"
|
|
|
|
"net"
|
2017-06-18 21:01:01 +03:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2017-06-26 17:35:40 +03:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-06-12 14:11:23 +03:00
|
|
|
|
2018-07-27 16:55:06 +03:00
|
|
|
vnc "github.com/sdstack/go-rfb"
|
2017-06-12 14:11:23 +03:00
|
|
|
)
|
|
|
|
|
2017-06-26 17:35:40 +03:00
|
|
|
type Auth struct {
|
|
|
|
Username []byte
|
|
|
|
Password []byte
|
|
|
|
}
|
2017-06-18 21:01:01 +03:00
|
|
|
|
2017-06-26 17:35:40 +03:00
|
|
|
type Proxy struct {
|
|
|
|
cc vnc.Conn
|
|
|
|
conns chan vnc.Conn
|
|
|
|
inp chan vnc.ClientMessage
|
|
|
|
out chan vnc.ServerMessage
|
|
|
|
}
|
2017-06-12 14:11:23 +03:00
|
|
|
|
2017-06-26 17:35:40 +03:00
|
|
|
var (
|
|
|
|
cliconns = make(map[string]*Proxy)
|
|
|
|
srvconns = make(map[vnc.Conn]string)
|
|
|
|
m sync.Mutex
|
|
|
|
)
|
2017-06-12 14:11:23 +03:00
|
|
|
|
2017-06-26 17:35:40 +03:00
|
|
|
func newConn(hostport string, password []byte) (vnc.Conn, chan vnc.ClientMessage, chan vnc.ServerMessage, chan vnc.Conn, error) {
|
|
|
|
fmt.Printf("new conn to %s with %s\n", hostport, password)
|
|
|
|
if cc, ok := cliconns[hostport]; ok {
|
|
|
|
return cc.cc, cc.inp, cc.out, cc.conns, nil
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
c, err := net.DialTimeout("tcp", hostport, 10*time.Second)
|
2017-06-12 14:11:23 +03:00
|
|
|
if err != nil {
|
2017-06-26 17:35:40 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-13 01:52:07 +03:00
|
|
|
cchServer := make(chan vnc.ServerMessage)
|
|
|
|
cchClient := make(chan vnc.ClientMessage)
|
2017-06-26 17:35:40 +03:00
|
|
|
errorCh := make(chan error)
|
2017-06-13 01:52:07 +03:00
|
|
|
ccfg := &vnc.ClientConfig{
|
2017-06-26 17:35:40 +03:00
|
|
|
SecurityHandlers: []vnc.SecurityHandler{&vnc.ClientAuthVNC{Password: password}},
|
|
|
|
PixelFormat: vnc.PixelFormat32bit,
|
|
|
|
ClientMessageCh: cchClient,
|
|
|
|
ServerMessageCh: cchServer,
|
|
|
|
ServerMessages: vnc.DefaultServerMessages,
|
|
|
|
Encodings: []vnc.Encoding{&vnc.RawEncoding{}},
|
|
|
|
ErrorCh: errorCh,
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
csrv := make(chan vnc.Conn)
|
|
|
|
inp := make(chan vnc.ClientMessage)
|
|
|
|
out := make(chan vnc.ServerMessage)
|
|
|
|
fmt.Printf("connect to vnc\n")
|
2017-06-13 01:52:07 +03:00
|
|
|
cc, err := vnc.Connect(context.Background(), c, ccfg)
|
2017-06-12 14:11:23 +03:00
|
|
|
if err != nil {
|
2017-06-26 17:35:40 +03:00
|
|
|
return nil, nil, nil, nil, err
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
fmt.Printf("connected to vnc %#+v\n", cc)
|
|
|
|
ds := &vnc.DefaultClientMessageHandler{}
|
|
|
|
go ds.Handle(cc)
|
|
|
|
go handleIO(cc, inp, out, csrv)
|
2017-06-13 16:20:35 +03:00
|
|
|
|
2017-06-26 17:35:40 +03:00
|
|
|
return cc, inp, out, csrv, nil
|
|
|
|
}
|
2017-06-13 16:20:35 +03:00
|
|
|
|
2017-06-26 17:35:40 +03:00
|
|
|
func handleIO(cli vnc.Conn, inp chan vnc.ClientMessage, out chan vnc.ServerMessage, csrv chan vnc.Conn) {
|
|
|
|
fmt.Printf("handle io\n")
|
|
|
|
ccfg := cli.Config().(*vnc.ClientConfig)
|
|
|
|
defer cli.Close()
|
|
|
|
var conns []vnc.Conn
|
|
|
|
var prepared bool
|
2017-06-12 14:11:23 +03:00
|
|
|
|
|
|
|
for {
|
2017-06-13 01:52:07 +03:00
|
|
|
select {
|
2017-06-26 17:35:40 +03:00
|
|
|
case err := <-ccfg.ErrorCh:
|
|
|
|
for _, srv := range conns {
|
|
|
|
srv.Close()
|
|
|
|
}
|
|
|
|
fmt.Printf("err %v\n", err)
|
|
|
|
return
|
|
|
|
case msg := <-ccfg.ServerMessageCh:
|
|
|
|
for _, srv := range conns {
|
|
|
|
scfg := srv.Config().(*vnc.ServerConfig)
|
|
|
|
scfg.ServerMessageCh <- msg
|
|
|
|
}
|
|
|
|
case msg := <-inp:
|
|
|
|
// messages from real clients
|
|
|
|
fmt.Printf("3 %#+v\n", msg)
|
2017-06-13 01:52:07 +03:00
|
|
|
switch msg.Type() {
|
2017-06-26 17:35:40 +03:00
|
|
|
case vnc.SetPixelFormatMsgType:
|
|
|
|
|
2017-06-13 01:52:07 +03:00
|
|
|
case vnc.SetEncodingsMsgType:
|
2017-06-18 19:42:41 +03:00
|
|
|
var encTypes []vnc.EncodingType
|
2017-06-26 17:35:40 +03:00
|
|
|
encs := []vnc.Encoding{
|
|
|
|
// &vnc.TightPngEncoding{},
|
|
|
|
&vnc.CopyRectEncoding{},
|
|
|
|
&vnc.RawEncoding{},
|
|
|
|
}
|
|
|
|
for _, senc := range encs {
|
2017-06-20 00:56:17 +03:00
|
|
|
for _, cenc := range msg.(*vnc.SetEncodings).Encodings {
|
|
|
|
if cenc == senc.Type() {
|
|
|
|
encTypes = append(encTypes, senc.Type())
|
2017-06-18 19:42:41 +03:00
|
|
|
}
|
2017-06-13 16:20:35 +03:00
|
|
|
}
|
2017-06-20 00:56:17 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
ccfg.ClientMessageCh <- &vnc.SetEncodings{Encodings: encTypes}
|
2017-06-13 01:52:07 +03:00
|
|
|
default:
|
2017-06-26 17:35:40 +03:00
|
|
|
ccfg.ClientMessageCh <- msg
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
case msg := <-out:
|
|
|
|
fmt.Printf("4 %#+v\n", msg)
|
|
|
|
case srv := <-csrv:
|
|
|
|
conns = append(conns, srv)
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type HijackHandler struct{}
|
|
|
|
|
|
|
|
func (*HijackHandler) Handle(c vnc.Conn) error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
hostport, ok := srvconns[c]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("client connect in server pool not found")
|
|
|
|
}
|
|
|
|
proxy, ok := cliconns[hostport]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("client connect to qemu not found")
|
|
|
|
}
|
|
|
|
cfg := c.Config().(*vnc.ServerConfig)
|
|
|
|
cfg.ClientMessageCh = proxy.inp
|
|
|
|
cfg.ServerMessageCh = proxy.out
|
|
|
|
|
|
|
|
proxy.conns <- c
|
|
|
|
ds := &vnc.DefaultServerMessageHandler{}
|
|
|
|
go ds.Handle(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthVNCHTTP struct {
|
|
|
|
c *http.Client
|
|
|
|
vnc.ServerAuthVNC
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *AuthVNCHTTP) Auth(c vnc.Conn) error {
|
|
|
|
auth.ServerAuthVNC.Challenge = []byte("clodo.ruclodo.ru")
|
|
|
|
if err := auth.ServerAuthVNC.WriteChallenge(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := auth.ServerAuthVNC.ReadChallenge(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := base64.NewEncoder(base64.StdEncoding, buf)
|
|
|
|
enc.Write(auth.ServerAuthVNC.Crypted)
|
|
|
|
enc.Close()
|
|
|
|
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("hash", buf.String())
|
|
|
|
buf.Reset()
|
|
|
|
src, _, _ := net.SplitHostPort(c.Conn().RemoteAddr().String())
|
|
|
|
v.Set("ip", src)
|
|
|
|
res, err := auth.c.PostForm("https://api.ix.clodo.ru/system/vnc", v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 || res.Body == nil {
|
|
|
|
if res.Body != nil {
|
|
|
|
io.Copy(buf, res.Body)
|
|
|
|
}
|
|
|
|
fmt.Printf("failed to get auth data: code %d body %s\n", res.StatusCode, buf.String())
|
|
|
|
defer buf.Reset()
|
|
|
|
return fmt.Errorf("failed to get auth data: code %d body %s", res.StatusCode, buf.String())
|
|
|
|
}
|
|
|
|
_, err = io.Copy(buf, res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get auth data: %s", err.Error())
|
|
|
|
}
|
|
|
|
log.Printf("http auth: %s\n", buf.Bytes())
|
|
|
|
res.Body.Close()
|
|
|
|
data := strings.Split(buf.String(), " ")
|
|
|
|
if len(data) < 2 {
|
|
|
|
return fmt.Errorf("failed to get auth data data invalid")
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
hostport := string(data[0])
|
|
|
|
password := []byte(data[1])
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
cc, inp, out, conns, err := newConn(hostport, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliconns[hostport] = &Proxy{cc, conns, inp, out}
|
|
|
|
srvconns[c] = hostport
|
|
|
|
c.SetWidth(cc.Width())
|
|
|
|
c.SetHeight(cc.Height())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*AuthVNCHTTP) Type() vnc.SecurityType {
|
|
|
|
return vnc.SecTypeVNC
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*AuthVNCHTTP) SubType() vnc.SecuritySubType {
|
|
|
|
return vnc.SecSubTypeUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
go func() {
|
|
|
|
log.Println(http.ListenAndServe(":6060", nil))
|
|
|
|
}()
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", ":6900")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error listen. %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
schClient := make(chan vnc.ClientMessage)
|
|
|
|
schServer := make(chan vnc.ServerMessage)
|
|
|
|
|
|
|
|
scfg := &vnc.ServerConfig{
|
|
|
|
SecurityHandlers: []vnc.SecurityHandler{
|
|
|
|
&AuthVNCHTTP{c: &http.Client{}},
|
|
|
|
},
|
|
|
|
Encodings: []vnc.Encoding{
|
|
|
|
// &vnc.TightPngEncoding{},
|
|
|
|
&vnc.CopyRectEncoding{},
|
|
|
|
&vnc.RawEncoding{},
|
|
|
|
},
|
|
|
|
PixelFormat: vnc.PixelFormat32bit,
|
|
|
|
ClientMessageCh: schClient,
|
|
|
|
ServerMessageCh: schServer,
|
|
|
|
ClientMessages: vnc.DefaultClientMessages,
|
|
|
|
DesktopName: []byte("vnc proxy"),
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
2017-06-26 17:35:40 +03:00
|
|
|
scfg.Handlers = append(scfg.Handlers, vnc.DefaultServerHandlers...)
|
|
|
|
scfg.Handlers = append(scfg.Handlers[:len(scfg.Handlers)-1], &HijackHandler{})
|
|
|
|
vnc.Serve(context.Background(), ln, scfg)
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|