2017-03-29 23:49:02 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-03-31 00:04:28 +00:00
|
|
|
"context"
|
2017-03-29 23:49:02 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2017-03-31 00:04:28 +00:00
|
|
|
"net/url"
|
2017-03-31 00:13:27 +00:00
|
|
|
"time"
|
2017-03-29 23:49:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2017-04-03 23:56:28 +00:00
|
|
|
// The Config struct holds all of the information needed to establish and handle a connection
|
|
|
|
// with the RVPN server.
|
2017-03-29 23:49:02 +00:00
|
|
|
type Config struct {
|
|
|
|
Server string
|
|
|
|
Token string
|
|
|
|
Insecure bool
|
2017-04-03 23:56:28 +00:00
|
|
|
Services map[string]map[string]int
|
2017-03-29 23:49:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 23:56:28 +00:00
|
|
|
// Run establishes a connection with the RVPN server specified in the config. If the first attempt
|
|
|
|
// to connect fails it is assumed that something is wrong with the authentication and it will
|
|
|
|
// return an error. Otherwise it will continuously attempt to reconnect whenever the connection
|
|
|
|
// is broken.
|
2017-03-31 00:04:28 +00:00
|
|
|
func Run(ctx context.Context, config *Config) error {
|
2017-03-29 23:49:02 +00:00
|
|
|
serverURL, err := url.Parse(config.Server)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid server URL: %v", err)
|
|
|
|
}
|
|
|
|
if serverURL.Scheme == "" {
|
|
|
|
serverURL.Scheme = "wss"
|
|
|
|
}
|
|
|
|
serverURL.Path = ""
|
|
|
|
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Set("access_token", config.Token)
|
|
|
|
serverURL.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
dialer := websocket.Dialer{}
|
|
|
|
if config.Insecure {
|
|
|
|
dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:56:28 +00:00
|
|
|
for name, portList := range config.Services {
|
|
|
|
if _, ok := portList["*"]; !ok {
|
|
|
|
return fmt.Errorf(`service %s missing port for "*"`, name)
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 00:04:28 +00:00
|
|
|
handler := NewWsHandler(config.Services)
|
|
|
|
|
2017-03-31 00:13:27 +00:00
|
|
|
authenticated := false
|
|
|
|
for {
|
2020-05-01 05:47:46 +00:00
|
|
|
fmt.Printf("debug serverURL:\n%+v", serverURL)
|
2017-03-31 00:13:27 +00:00
|
|
|
if conn, _, err := dialer.Dial(serverURL.String(), nil); err == nil {
|
2017-04-03 23:56:28 +00:00
|
|
|
loginfo.Println("connected to remote server")
|
2017-03-31 00:13:27 +00:00
|
|
|
authenticated = true
|
|
|
|
handler.HandleConn(ctx, conn)
|
|
|
|
} else if !authenticated {
|
2020-05-01 05:47:46 +00:00
|
|
|
return fmt.Errorf("first connection to server failed - check auth: %s", err.Error())
|
2017-03-31 00:13:27 +00:00
|
|
|
}
|
|
|
|
loginfo.Println("disconnected from remote server")
|
|
|
|
|
|
|
|
// Sleep for a few seconds before trying again, but only if the context is still active
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
}
|
|
|
|
loginfo.Println("attempting reconnect to remote server")
|
2017-03-29 23:49:02 +00:00
|
|
|
}
|
|
|
|
}
|