88 "github.com/hashicorp/yamux"
99 "github.com/jpillora/backoff"
1010 "io"
11- "io/ioutil"
1211 "k8s.io/klog"
1312 "net"
1413 "net/http"
@@ -62,7 +61,8 @@ func (t *Tunnel) keepConnected(ctx context.Context) {
6261 t .gwConn , err = connect (t .address , t .serverName , t .token , t .config )
6362 if err != nil {
6463 d := b .Duration ()
65- klog .Errorf ("%s, reconnecting to %s in %.0fs" , err , t .address , d .Seconds ())
64+ klog .Errorln (err )
65+ klog .Errorf ("reconnecting to %s in %.0fs" , t .address , d .Seconds ())
6666 time .Sleep (d )
6767 continue
6868 }
@@ -83,7 +83,7 @@ func (t *Tunnel) Close() {
8383func main () {
8484 resolverUrl := os .Getenv ("RESOLVER_URL" )
8585 if resolverUrl == "" {
86- resolverUrl = "https://gw.coroot.com/promtun /resolve"
86+ resolverUrl = "https://gw.coroot.com/connect /resolve"
8787 }
8888 token := mustEnv ("PROJECT_TOKEN" )
8989 if len (token ) != 36 {
@@ -160,17 +160,22 @@ func getEndpoints(resolverUrl, token string) ([]string, error) {
160160 return strings .Split (strings .TrimSpace (string (payload )), ";" ), nil
161161}
162162
163- type Header struct {
163+ type RequestHeader struct {
164164 Token [36 ]byte
165165 Version [16 ]byte
166166 ConfigSize uint32
167167}
168168
169+ type ResponseHeader struct {
170+ Status uint16
171+ MessageSize uint16
172+ }
173+
169174func connect (gwAddr , serverName , token string , config []byte ) (net.Conn , error ) {
170- h := Header {}
171- copy (h .Token [:], token )
172- copy (h .Version [:], version )
173- h .ConfigSize = uint32 (len (config ))
175+ requestHeader := RequestHeader {}
176+ copy (requestHeader .Token [:], token )
177+ copy (requestHeader .Version [:], version )
178+ requestHeader .ConfigSize = uint32 (len (config ))
174179
175180 klog .Infof ("connecting to %s (%s)" , gwAddr , serverName )
176181 deadline := time .Now ().Add (timeout )
@@ -183,25 +188,33 @@ func connect(gwAddr, serverName, token string, config []byte) (net.Conn, error)
183188 klog .Infof ("connected to gateway %s" , gwAddr )
184189
185190 _ = gwConn .SetDeadline (deadline )
186- if err = binary .Write (gwConn , binary .LittleEndian , h ); err != nil {
191+ if err = binary .Write (gwConn , binary .LittleEndian , requestHeader ); err != nil {
187192 _ = gwConn .Close ()
188193 return nil , fmt .Errorf ("failed to send config to %s: %s" , gwAddr , err )
189194 }
190195 if _ , err = gwConn .Write (config ); err != nil {
191196 _ = gwConn .Close ()
192197 return nil , fmt .Errorf ("failed to send config to %s: %s" , gwAddr , err )
193198 }
194- var resp uint16
195- if err := binary .Read (gwConn , binary .LittleEndian , & resp ); err != nil {
199+ var responseHeader ResponseHeader
200+ if err := binary .Read (gwConn , binary .LittleEndian , & responseHeader ); err != nil {
196201 _ = gwConn .Close ()
197202 return nil , fmt .Errorf ("failed to read the response from %s: %s" , gwAddr , err )
198203 }
204+ var responseMessage string
205+ if responseHeader .MessageSize > 0 {
206+ buf := make ([]byte , responseHeader .MessageSize )
207+ if _ , err := gwConn .Read (buf ); err != nil {
208+ _ = gwConn .Close ()
209+ return nil , fmt .Errorf ("failed to read the response from %s: %s" , gwAddr , err )
210+ }
211+ responseMessage = string (buf )
212+ }
199213 _ = gwConn .SetDeadline (time.Time {})
200- klog .Infof (`got "%d" from the gateway %s` , resp , gwAddr )
201214
202- if resp != 200 {
215+ if responseHeader . Status != 200 {
203216 _ = gwConn .Close ()
204- return nil , fmt .Errorf ("failed to authenticate project on %s: %d " , gwAddr , resp )
217+ return nil , fmt .Errorf ("got %d from %s: %s " , responseHeader . Status , gwAddr , responseMessage )
205218 }
206219 klog .Infof ("ready to proxy requests from %s" , gwAddr )
207220 return gwConn , nil
@@ -210,7 +223,7 @@ func connect(gwAddr, serverName, token string, config []byte) (net.Conn, error)
210223func proxy (ctx context.Context , gwConn net.Conn ) {
211224 cfg := yamux .DefaultConfig ()
212225 cfg .KeepAliveInterval = time .Second
213- cfg .LogOutput = ioutil .Discard
226+ cfg .LogOutput = io .Discard
214227 session , err := yamux .Server (gwConn , cfg )
215228 if err != nil {
216229 klog .Errorln ("failed to start a TCP multiplexing server:" , err )
0 commit comments