diff --git a/client.go b/client.go index a97e7e6..9af176d 100644 --- a/client.go +++ b/client.go @@ -1924,6 +1924,7 @@ func (c *Client) FixServices() { func (c *Client) WaitForTermination() { c.wg.Wait() + c.log.Write("", LOG_INFO, "End of service") } func (c *Client) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) { diff --git a/server-ctl.go b/server-ctl.go index a0a6472..9c0ac9f 100644 --- a/server-ctl.go +++ b/server-ctl.go @@ -3,6 +3,9 @@ package hodu import "encoding/json" import "fmt" import "net/http" +import "net/url" +import "strconv" +import "strings" import "sync" import "time" @@ -24,7 +27,7 @@ type json_out_server_conn struct { ClientAddr string `json:"client-addr"` ClientToken string `json:"client-token"` CreatedMilli int64 `json:"created-milli"` - Routes []json_out_server_route `json:"routes"` + Routes []json_out_server_route `json:"routes,omitempty"` } type json_out_server_route struct { @@ -184,13 +187,20 @@ oops: func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { var s *Server + var q url.Values var status_code int var je *json.Encoder + var routes bool var err error s = ctl.s je = json.NewEncoder(w) + q = req.URL.Query() + q = req.URL.Query() + routes, err = strconv.ParseBool(strings.ToLower(q.Get("routes"))) + if err != nil { routes = false } + switch req.Method { case http.MethodGet: var js []json_out_server_conn @@ -204,22 +214,25 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R var ri RouteId cts = s.cts_map[ci] - jsp = make([]json_out_server_route, 0) - cts.route_mtx.Lock() - for _, ri = range cts.route_map.get_sorted_keys() { - var r *ServerRoute - r = cts.route_map[ri] - jsp = append(jsp, json_out_server_route{ - Id: r.Id, - ClientPeerAddr: r.PtcAddr, - ClientPeerName: r.PtcName, - ServerPeerSvcAddr: r.SvcAddr.String(), - ServerPeerSvcNet: r.SvcPermNet.String(), - ServerPeerOption: r.SvcOption.String(), - CreatedMilli: r.Created.UnixMilli(), - }) + if routes { + jsp = make([]json_out_server_route, 0) + cts.route_mtx.Lock() + for _, ri = range cts.route_map.get_sorted_keys() { + var r *ServerRoute + r = cts.route_map[ri] + jsp = append(jsp, json_out_server_route{ + Id: r.Id, + ClientPeerAddr: r.PtcAddr, + ClientPeerName: r.PtcName, + ServerPeerSvcAddr: r.SvcAddr.String(), + ServerPeerSvcNet: r.SvcPermNet.String(), + ServerPeerOption: r.SvcOption.String(), + CreatedMilli: r.Created.UnixMilli(), + }) + } + cts.route_mtx.Unlock() } - cts.route_mtx.Unlock() + js = append(js, json_out_server_conn{ Id: cts.Id, ClientAddr: cts.RemoteAddr.String(), @@ -253,15 +266,21 @@ oops: func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { var s *Server + var q url.Values var status_code int - var err error var je *json.Encoder var conn_id string var cts *ServerConn + var routes bool + var err error s = ctl.s je = json.NewEncoder(w) + q = req.URL.Query() + routes, err = strconv.ParseBool(strings.ToLower(q.Get("routes"))) + if err != nil { routes = false } + conn_id = req.PathValue("conn_id") cts, err = s.FindServerConnByIdStr(conn_id) if err != nil { @@ -270,29 +289,32 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt goto oops } + switch req.Method { case http.MethodGet: var jsp []json_out_server_route var js *json_out_server_conn var ri RouteId - jsp = make([]json_out_server_route, 0) - cts.route_mtx.Lock() - for _, ri = range cts.route_map.get_sorted_keys() { - var r *ServerRoute + if routes { + jsp = make([]json_out_server_route, 0) + cts.route_mtx.Lock() + for _, ri = range cts.route_map.get_sorted_keys() { + var r *ServerRoute - r = cts.route_map[ri] - jsp = append(jsp, json_out_server_route{ - Id: r.Id, - ClientPeerAddr: r.PtcAddr, - ClientPeerName: r.PtcName, - ServerPeerSvcAddr: r.SvcAddr.String(), - ServerPeerSvcNet: r.SvcPermNet.String(), - ServerPeerOption: r.SvcOption.String(), - CreatedMilli: r.Created.UnixMilli(), - }) + r = cts.route_map[ri] + jsp = append(jsp, json_out_server_route{ + Id: r.Id, + ClientPeerAddr: r.PtcAddr, + ClientPeerName: r.PtcName, + ServerPeerSvcAddr: r.SvcAddr.String(), + ServerPeerSvcNet: r.SvcPermNet.String(), + ServerPeerOption: r.SvcOption.String(), + CreatedMilli: r.Created.UnixMilli(), + }) + } + cts.route_mtx.Unlock() } - cts.route_mtx.Unlock() js = &json_out_server_conn{ Id: cts.Id, ClientAddr: cts.RemoteAddr.String(), diff --git a/server.go b/server.go index eaa52d1..90eea18 100644 --- a/server.go +++ b/server.go @@ -1024,6 +1024,10 @@ func (s *Server) PacketStream(strm Hodu_PacketStreamServer) error { return fmt.Errorf("failed to get peer from packet stream context") } + if s.stop_req.Load() { + return fmt.Errorf("new conneciton prohibited after stop - %s", p.Addr.String()) + } + cts, err = s.AddNewServerConn(&p.Addr, &p.LocalAddr, strm) if err != nil { return fmt.Errorf("unable to add client %s - %s", p.Addr.String(), err.Error()) @@ -1042,7 +1046,7 @@ func (s *Server) PacketStream(strm Hodu_PacketStreamServer) error { }, ) - // Don't detached the cts task as a go-routine as this function + // Don't detach the cts task as a go-routine as this function // is invoked as a go-routine by the grpc server. s.cts_wg.Add(1) cts.RunTask(&s.cts_wg) @@ -1553,7 +1557,7 @@ func (s *Server) RunTask(wg *sync.WaitGroup) { go s.run_grpc_server(idx, &s.rpc_wg) } - // most the work is done by in separate goroutines (s.run_grp_server) + // most work is done by in separate goroutines (s.run_grp_server) // this loop serves as a placeholder to prevent the logic flow from // descening down to s.ReqStop() task_loop: @@ -2117,6 +2121,7 @@ func (s *Server) FixServices() { func (s *Server) WaitForTermination() { s.wg.Wait() + s.log.Write("", LOG_INFO, "End of service") } func (s *Server) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) {