-
Notifications
You must be signed in to change notification settings - Fork 0
/
orca.go
137 lines (131 loc) · 3.1 KB
/
orca.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"fmt"
"github.com/gorilla/websocket"
"io"
"os"
"os/signal"
"syscall"
"time"
)
// Orca handles the communication between the
// local port and the proxy port
type Orca struct {
tunnelConn *TunnelConnection
localConn *LocalConn
queuedDataToSend []byte
queuedDataToReceive []byte
}
func NewOrca(ctx context.Context) (*Orca, error) {
project := os.Getenv("PROJECT_ID")
zone := os.Getenv("ZONE")
instance := os.Getenv("INSTANCE")
port := os.Getenv("PORT")
localPort := os.Getenv("LOCAL_PORT")
tcPipeReader, tcPipeWriter := io.Pipe()
tc, err := NewTunnelConnection(ctx,
WithProject(project),
WithZone(zone),
WithPort(port),
WithInstanceName(instance),
WithTunnelReader(tcPipeReader),
WithTunnelWriter(tcPipeWriter))
if err != nil {
return nil, err
}
lcPipeReader, lcPipeWriter := io.Pipe()
lc, err := NewLocalConn(ctx,
WithLocalConnPort(localPort),
WithLocalConnReader(lcPipeReader),
WithLocalConnWriter(lcPipeWriter))
if err != nil {
return nil, err
}
orca := &Orca{tunnelConn: tc, localConn: lc}
return orca, nil
}
func (orca *Orca) Run() error {
ctx := context.Background()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// going off of max size from the python library
localbuf := make([]byte, 16384)
socketbuf := make([]byte, 16384)
fmt.Printf("Listening on port %s", orca.localConn.port)
err := orca.localConn.Accept()
if err != nil {
return err
}
fmt.Println("Client connected")
go func() {
for !orca.tunnelConn.connected {
time.Sleep(1 * time.Second)
}
for {
n, err := orca.localConn.Read(localbuf)
if err != nil {
panic(err)
}
msg := localbuf[:n]
newMsg := NewIAPDataMessage(msg)
err = newMsg.CreateDataFrame()
if err != nil {
panic(err)
}
_, err = orca.tunnelConn.Write(newMsg.data)
if err != nil {
panic(err)
}
}
}()
go func() {
err := orca.tunnelConn.Connect(ctx)
if err != nil {
panic(err)
}
for {
n, err := orca.tunnelConn.Read(socketbuf)
if err != nil {
panic(err)
}
d := socketbuf[:n]
msg := NewIAPMessage(d)
tag := msg.PeekMessageTag()
switch tag {
case MessageAck:
fmt.Println("Got Message Ack From IAP")
continue
case MessageConnectSuccessSid:
fmt.Println("Got Success SID")
newMsg := msg.AsConnectSIDMessage()
orca.tunnelConn.SetSid(newMsg.GetSID())
continue
case MessageData:
fmt.Println("Got Data Message")
newMsg := msg.AsDataMessage()
orca.tunnelConn.bytesReceived += newMsg.GetDataLength()
ackBytes := make([]byte, 10)
ackMsg := NewIAPAckMessage(ackBytes)
ackMsg.SetTag(MessageAck)
ackMsg.SetAck(uint64(orca.tunnelConn.bytesReceived))
err = orca.tunnelConn.websocketConn.WriteMessage(websocket.BinaryMessage, ackMsg.data)
if err != nil {
panic(err)
}
orca.tunnelConn.bytesAcked += orca.tunnelConn.bytesReceived
_, err = orca.localConn.Write(newMsg.data)
if err != nil {
panic(err)
}
continue
default:
fmt.Printf("Unknown tag: %d", tag)
panic("unkown tag")
break
}
}
}()
<-c
return nil
}