From 7dfd4ec8ee7ab9034b35061567235c62d8c5522e Mon Sep 17 00:00:00 2001
From: YangXin <245051644@qq.com>
Date: Mon, 5 Dec 2022 21:43:26 +0800
Subject: [PATCH 3/3] enable rexec read net addr from config file.

Signed-off-by: YangXin <245051644@qq.com>
---
 qtfs/rexec/client.go |  3 ++-
 qtfs/rexec/common.go | 33 +++++++++++++++++++++++++++++++--
 qtfs/rexec/server.go |  6 +++++-
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/qtfs/rexec/client.go b/qtfs/rexec/client.go
index 922040d..13b63f5 100644
--- a/qtfs/rexec/client.go
+++ b/qtfs/rexec/client.go
@@ -18,6 +18,7 @@ import (
 
 const (
 	rexecPidDir = "/var/run/rexec/pids"
+	role = "client"
 )
 
 var pidPath string
@@ -111,7 +112,7 @@ func main() {
 	if err := SetParentDeathSignal(uintptr(syscall.SIGHUP)); err != nil {
 		log.Printf("Failed to set Parent Death Signal:%s", err.Error())
 	}
-	na, err := parseNetAddr()
+	na, err := parseNetAddr(role)
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/qtfs/rexec/common.go b/qtfs/rexec/common.go
index a332fae..9ce21c4 100644
--- a/qtfs/rexec/common.go
+++ b/qtfs/rexec/common.go
@@ -8,10 +8,16 @@ import (
 	"os"
 	"strconv"
 	"strings"
+	"io/ioutil"
+	"encoding/json"
 
 	"github.com/docker/libchan"
 )
 
+const (
+	configDir = "/etc/rexec"
+)
+
 // RemoteCommand is the run parameters to be executed remotely
 type RemoteCommand struct {
 	Cmd        string
@@ -82,12 +88,35 @@ func parseUnixAddr(inAddr string) (NetAddr, error) {
 	}, nil
 }
 
-func parseNetAddr() (NetAddr, error) {
+func readAddrFromFile(role string) (string) {
+	fileName := fmt.Sprintf("%s/%s.json", configDir, role)
+	file, err := ioutil.ReadFile(fileName)
+	if err != nil {
+		fmt.Printf("read %s failed: %s", fileName, err)
+		return ""
+	}
+	var netAddr struct {
+		Protocol string `json:"Protocol"`
+		Ipaddr string	`json:"Ipaddr"`
+		Port string		`json:"Port"`
+	}
+	err = json.Unmarshal([]byte(file), &netAddr)
+	if err != nil {
+		fmt.Printf("can not unmarshal %s:%s", fileName, err)
+		return ""
+	}
+	return fmt.Sprintf("%s://%s:%s", netAddr.Protocol, netAddr.Ipaddr, netAddr.Port)
+}
+
+func parseNetAddr(role string) (NetAddr, error) {
 	cna := os.Getenv("CMD_NET_ADDR")
 
 	// default netAddr: tcp://127.0.0.1:9323
 	if strings.TrimSpace(cna) == "" {
-		return NetAddr{}, fmt.Errorf("need CMD_NET_ADDR")
+		cna = readAddrFromFile(role)
+		if cna == "" {
+			return NetAddr{}, fmt.Errorf("please set enviroment variable CMD_NET_ADDR or set Config file %s/%s.json", configDir, role)
+		}
 	}
 
 	parts := strings.SplitN(cna, "://", 2)
diff --git a/qtfs/rexec/server.go b/qtfs/rexec/server.go
index 4e910ea..4559b79 100644
--- a/qtfs/rexec/server.go
+++ b/qtfs/rexec/server.go
@@ -15,6 +15,10 @@ import (
 	"github.com/docker/libchan/spdy"
 )
 
+const (
+	role = "server"
+)
+
 func getHost(addr string) string {
 	return strings.Split(addr, ":")[0]
 }
@@ -24,7 +28,7 @@ func main() {
 	key := os.Getenv("TLS_KEY")
 
 	var listener net.Listener
-	na, err := parseNetAddr()
+	na, err := parseNetAddr(role)
 	if err != nil {
 		log.Fatal(err)
 	}
-- 
2.23.0

