2017-10-01 15:14:54 +00:00
|
|
|
// +build linux
|
|
|
|
|
2017-03-26 22:13:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-01 15:14:54 +00:00
|
|
|
"context"
|
2017-03-26 22:13:32 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2017-10-01 15:23:08 +00:00
|
|
|
"git.xeserv.us/xena/route/internal/tun2"
|
2017-03-26 22:13:32 +00:00
|
|
|
"github.com/Xe/ln"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
token = mustEnv("ROUTE_TOKEN", "")
|
|
|
|
domain = mustEnv("ROUTE_DOMAIN", "")
|
|
|
|
backend = mustEnv("ROUTE_BACKEND", "")
|
|
|
|
method = mustEnv("ROUTE_METHOD", "")
|
|
|
|
serverAddr = mustEnv("ROUTE_LOAD_BALANCER", "")
|
|
|
|
connMethod = mustEnv("ROUTE_METHOD", "tcp")
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustEnv(key string, def string) string {
|
|
|
|
val := os.Getenv(key)
|
|
|
|
if val == "" {
|
|
|
|
log.Printf("autohttpagent: %s not found, things may not work", key)
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2017-10-05 20:15:24 +00:00
|
|
|
func doHTTPAgent() {
|
2017-03-26 22:13:32 +00:00
|
|
|
go func() {
|
2017-10-01 15:14:54 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2017-03-26 22:13:32 +00:00
|
|
|
cfg := &tun2.ClientConfig{
|
|
|
|
TLSConfig: &tls.Config{},
|
|
|
|
ConnType: connMethod,
|
|
|
|
ServerAddr: serverAddr,
|
|
|
|
Token: token,
|
|
|
|
Domain: domain,
|
|
|
|
BackendURL: backend,
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _ := tun2.NewClient(cfg)
|
2017-10-05 20:15:24 +00:00
|
|
|
err := client.Connect(ctx)
|
2017-03-26 22:13:32 +00:00
|
|
|
if err != nil {
|
2017-10-01 15:14:54 +00:00
|
|
|
ln.Error(ctx, err, ln.Action("client connection error, restarting"))
|
2017-03-26 22:13:32 +00:00
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2017-10-05 20:15:24 +00:00
|
|
|
doHTTPAgent()
|
2017-03-26 22:13:32 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-10-05 20:15:24 +00:00
|
|
|
doHTTPAgent()
|
2017-03-26 22:13:32 +00:00
|
|
|
}
|