Commit 777d595a by anun

update state connection in pg

parent 4a00a6c8
function hasChanges() {
# old version, just track uncommited + untracked files
# test -n "$(git status -s .)"
# expect 1 to be normal so the untracked file .make-funcition will be ignored
test 0 -ne "$(git status -s . | sed -e '/.make-function/d' | wc -l)"
}
function getRelease() {
# old : use tag in release.conf
# awk -F= '/^RELEASE=/{print $2}' release.conf
# new use tag from git of the form -> 1.0.1-2-3b10c420
TAGN=$(git describe | sed -e "s/-g.*//")
TV=$(echo $TAGN | sed -e "s/-.*//")
TM=$(echo $TAGN | sed -e "s/^[^\-]*//")
if [ -z "$TM" ]
then
TM="-0"
fi
echo $TV$TM
}
function getBaseTag() {
sed -n -e "s/^tag=\(.*\)$(getRelease)\$/\1/p" release.conf
}
function getTag() {
if [ -z "$1" ] ; then
awk -F= '/^tag/{print $2}' release.conf
else
echo "$(getBaseTag)$1"
fi
}
function setRelease() {
if [ -n "$1" ] ; then
sed -i.x -e "s/^tag=.*/tag=$(getTag $1)/" release.conf
sed -i.x -e "s/^RELEASE=.*/RELEASE=$1/g" release.conf
rm -f release.conf.x
runPreTagCommand "$1"
else
echo "ERROR: missing release version parameter " >&2
return 1
fi
}
function runPreTagCommand() {
if [ -n "$1" ] ; then
COMMAND=$(sed -n -e "s/@@RELEASE@@/$1/g" -e 's/^pre_tag_command=\(.*\)/\1/p' release.conf)
if [ -n "$COMMAND" ] ; then
if ! OUTPUT=$(bash -c "$COMMAND" 2>&1) ; then echo $OUTPUT >&2 && exit 1 ; fi
fi
else
echo "ERROR: missing release version parameter " >&2
return 1
fi
}
function tagExists() {
tag=${1:-$(getTag)}
test -n "$tag" && test -n "$(git tag | grep "^$tag\$")"
}
function differsFromRelease() {
tag=$(getTag)
! tagExists $tag || test -n "$(git diff --shortstat -r $tag .)"
}
function getVersion() {
result=$(getRelease)
if hasChanges ; then
result="$result-dirty"
fi
echo $result
}
function getVersionWithHash() {
result=$(getRelease)
if differsFromRelease; then
# output only first 7 characters
# result="$result-$(git log -n 1 --format=%h .)"
# output first 8 characters
result="$result-$(git log -n 1 --format=%H . | head -c 8)"
fi
if hasChanges ; then
result="$result-dirty"
fi
echo $result
}
function nextPatchLevel() {
version=${1:-$(getRelease)}
major_and_minor=$(echo $version | cut -d. -f1,2)
patch=$(echo $version | cut -d. -f3)
version=$(printf "%s.%d" $major_and_minor $(($patch + 1)))
echo $version
}
function nextMinorLevel() {
version=${1:-$(getRelease)}
major=$(echo $version | cut -d. -f1);
minor=$(echo $version | cut -d. -f2);
version=$(printf "%d.%d.0" $major $(($minor + 1))) ;
echo $version
}
function nextMajorLevel() {
version=${1:-$(getRelease)}
major=$(echo $version | cut -d. -f1);
version=$(printf "%d.0.0" $(($major + 1)))
echo $version
}
No preview for this file type
...@@ -219,9 +219,10 @@ func NewClient(c *Config) (*Client, error) { ...@@ -219,9 +219,10 @@ func NewClient(c *Config) (*Client, error) {
} }
client.computed.Remotes = append(client.computed.Remotes, r) client.computed.Remotes = append(client.computed.Remotes, r)
break
} }
// client.computed.Edge = c.Edge // client.computed.Edge = c.Edge
client.Infof("---> Remotes client.computed# %s", client.computed) // client.Debugf("---> Remotes client.computed# %s", client.computed)
//outbound proxy //outbound proxy
if p := c.Proxy; p != "" { if p := c.Proxy; p != "" {
client.proxyURL, err = url.Parse(p) client.proxyURL, err = url.Parse(p)
......
...@@ -16,6 +16,7 @@ require ( ...@@ -16,6 +16,7 @@ require (
require ( require (
github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 // indirect github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 // indirect
github.com/blockloop/scan/v2 v2.5.0 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/joho/godotenv v1.5.1 // indirect
github.com/jpillora/ansi v1.0.3 // indirect github.com/jpillora/ansi v1.0.3 // indirect
......
...@@ -5,6 +5,8 @@ github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 h1:axBiC50cNZ ...@@ -5,6 +5,8 @@ github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 h1:axBiC50cNZ
github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2/go.mod h1:jnzFpU88PccN/tPPhCpnNU8mZphvKxYM9lLNkd8e+os= github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2/go.mod h1:jnzFpU88PccN/tPPhCpnNU8mZphvKxYM9lLNkd8e+os=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/blockloop/scan/v2 v2.5.0 h1:/yNcCwftYn3wf5BJsJFO9E9P48l45wThdUnM3WcDF+o=
github.com/blockloop/scan/v2 v2.5.0/go.mod h1:OFYyMocUdRW3DUWehPI/fSsnpNMUNiyUaYXRMY5NMIY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"time" "time"
"net" "net"
"strconv" "strconv"
"fmt"
"database/sql" "database/sql"
chshare "dev.nexpie.com/anun/chisel/share" chshare "dev.nexpie.com/anun/chisel/share"
...@@ -17,12 +16,16 @@ import ( ...@@ -17,12 +16,16 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/blockloop/scan/v2"
) )
type Edge struct { type Edge struct {
EdgeId string Edgeid string `db:"x.edgeid"`
EdgeSecret string Edgesecret string `db:"x.edgesecret"`
Active bool Edgetype string `db:"x.edgetype"`
Alias string `db:"x.alias"`
Tunnelbrokerport int `db:"x.tunnelbrokerport"`
Active bool `db:"x.active"`
} }
func getTCPPort() (port int, err error) { func getTCPPort() (port int, err error) {
...@@ -155,15 +158,25 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) { ...@@ -155,15 +158,25 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) {
} }
// Anun : Check edge is active // Anun : Check edge is active
edgeActive, err := checkEdge(s.db, c.EdgeId, c.EdgeSecret) edge, err := getEdge(s.db, c.EdgeId, c.EdgeSecret)
if !edgeActive {
failed(s.Errorf("Edge '%s' is denied", c.EdgeId)) l.Debugf("res edge %s", edge)
l.Debugf("res edge %s", len(edge))
if len(edge) == 0 {
failed(s.Errorf("Edge '%s' is not found or inactive.", c.EdgeId))
return
}
for _, r := range edge {
if !r.Active {
failed(s.Errorf("Edge '%s' is inactive.", r.Edgeid))
return return
} }
}
//validate remotes //validate remotes
for _, r := range c.Remotes { for _, r := range c.Remotes {
//if user is provided, ensure they have //if user is provided, ensure they have
//access to the desired remotes //access to the desired remotes
if user != nil { if user != nil {
...@@ -184,8 +197,12 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) { ...@@ -184,8 +197,12 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) {
failed(s.Errorf("Server cannot listen on %s", r.String())) failed(s.Errorf("Server cannot listen on %s", r.String()))
return return
} }
}
if len(c.Remotes) != 0 {
c.Remotes[0].LocalPort = strconv.Itoa(edge[0].Tunnelbrokerport)
} }
//successfuly validated config! //successfuly validated config!
r.Reply(true, nil) r.Reply(true, nil)
//tunnel per ssh connection //tunnel per ssh connection
...@@ -211,23 +228,48 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) { ...@@ -211,23 +228,48 @@ func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) {
//block //block
return tunnel.BindRemotes(ctx, serverInbound) return tunnel.BindRemotes(ctx, serverInbound)
}) })
l.Debugf("Enable tunnel port %d", edge[0].Tunnelbrokerport)
updateConnectionEdge(s.db, edge[0].Edgeid, edge[0].Edgesecret, edge[0].Tunnelbrokerport, true)
err = eg.Wait() err = eg.Wait()
if err != nil && !strings.HasSuffix(err.Error(), "EOF") { if err != nil && !strings.HasSuffix(err.Error(), "EOF") {
l.Debugf("Closed connection (%s)", err) l.Debugf("Closed connection (%s)", err)
l.Debugf("Code (%s)", s.config)
} else { } else {
l.Debugf("Closed connection") l.Debugf("Closed connection")
l.Debugf("Code (%s)", s.config)
} }
if len(edge) != 0 {
l.Debugf("Disable tunnel port %d", edge[0].Tunnelbrokerport)
updateConnectionEdge(s.db, edge[0].Edgeid, edge[0].Edgesecret, edge[0].Tunnelbrokerport, false)
}
} }
func checkEdge(db *sql.DB, edgeid string, edgesecret string) (bool, error) { func updateConnectionEdge(db *sql.DB, edgeid string, edgesecret string, tunnelbrokerport int, conn bool) (bool, error) {
var active bool _, err := db.Exec(`UPDATE edgeregistry."EdgeDevice" SET connected = $1 WHERE edgeid = $2 AND edgesecret = $3 AND tunnelbrokerport = $4`, conn, edgeid, edgesecret, tunnelbrokerport)
if err := db.QueryRow(`SELECT x.active FROM edgeregistry."EdgeDevice" x WHERE x.edgeid = $1 AND x.edgesecret = $2 AND x.active = true`, edgeid, edgesecret).Scan(&active); err != nil { CheckError(err)
if err == sql.ErrNoRows {
return false, fmt.Errorf("Edge %s: unknown", edgeid) if err != nil {
return false, err
}else{
return true, nil
}
}
func getEdge (db *sql.DB, edgeid string, edgesecret string) ([]Edge, error) {
var e []Edge
rows, err := db.Query(`SELECT x.edgeid, x.edgesecret, x.edgetype, x.alias, x.tunnelbrokerport, x.active FROM edgeregistry."EdgeDevice" x WHERE x.edgeid = $1 AND x.edgesecret = $2 AND x.active = true`, edgeid, edgesecret)
defer rows.Close()
CheckError(err)
err = scan.Rows(&e, rows)
if err != nil {
return e, err
} }
return false, fmt.Errorf("Edge %s: %v", edgeid, err)
return e, nil
}
func CheckError(err error) {
if err != nil {
panic(err)
} }
return active, nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment