diff --git a/client-ctl.go b/client-ctl.go index 9071947..9115f22 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -28,7 +28,7 @@ type json_in_client_conn struct { } type json_in_client_route struct { - Id RouteId `json:"id"` // 0 for auto-assignement. + RId RouteId `json:"route-id"` // 0 for auto-assignement. ClientPeerAddr string `json:"client-peer-addr"` ClientPeerName string `json:"client-peer-name"` ServerPeerOption string `json:"server-peer-option"` @@ -42,15 +42,15 @@ type json_in_client_route struct { } type json_in_client_route_update struct { - Lifetime string `sjon:"lifetime"` + Lifetime string `json:"lifetime"` } type json_out_client_conn_id struct { - Id ConnId `json:"id"` + CId ConnId `json:"conn-id"` } type json_out_client_conn struct { - Id ConnId `json:"id"` + CId ConnId `json:"conn-id"` ReqServerAddrs []string `json:"req-server-addrs"` // server addresses requested. may include a domain name CurrentServerIndex int `json:"current-server-index"` ServerAddr string `json:"server-addr"` // actual server address @@ -60,12 +60,13 @@ type json_out_client_conn struct { } type json_out_client_route_id struct { - Id RouteId `json:"id"` - CtsId ConnId `json:"conn-id"` + CId ConnId `json:"conn-id"` + RId RouteId `json:"route-id"` } type json_out_client_route struct { - Id RouteId `json:"id"` + CId ConnId `json:"conn-id"` + RId RouteId `json:"route-id"` ClientPeerAddr string `json:"client-peer-addr"` ClientPeerName string `json:"client-peer-name"` ServerPeerOption string `json:"server-peer-option"` @@ -80,7 +81,9 @@ type json_out_client_route struct { } type json_out_client_peer struct { - Id PeerId `json:"id"` + CId ConnId `json:"conn-id"` + RId RouteId `json:"route-id"` + PId PeerId `json:"peer-id"` ClientPeerAddr string `json:"client-peer-addr"` ClientLocalAddr string `json:"client-local-addr"` ServerPeerAddr string `json:"server-peer-addr"` @@ -252,7 +255,8 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R lftsta, lftdur = r.GetLifetimeInfo() jsp = append(jsp, json_out_client_route{ - Id: r.Id, + CId: cts.Id, + RId: r.Id, ClientPeerAddr: r.PeerAddr, ClientPeerName: r.PeerName, ServerPeerSvcAddr: r.ServerPeerSvcAddr, @@ -265,7 +269,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R cts.route_mtx.Unlock() js = append(js, json_out_client_conn{ - Id: cts.Id, + CId: cts.Id, ReqServerAddrs: cts.cfg.ServerAddrs, CurrentServerIndex: cts.cfg.Index, ServerAddr: cts.remote_addr.Get(), @@ -306,7 +310,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R goto oops } else { status_code = WriteJsonRespHeader(w, http.StatusCreated) - if err = je.Encode(json_out_client_conn_id{Id: cts.Id}); err != nil { goto oops } + if err = je.Encode(json_out_client_conn_id{CId: cts.Id}); err != nil { goto oops } } case http.MethodDelete: @@ -367,7 +371,8 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt lftsta, lftdur = r.GetLifetimeInfo() jsp = append(jsp, json_out_client_route{ - Id: r.Id, + CId: cts.Id, + RId: r.Id, ClientPeerAddr: r.PeerAddr, ClientPeerName: r.PeerName, ServerPeerSvcAddr: r.ServerPeerSvcAddr, @@ -380,7 +385,7 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt cts.route_mtx.Unlock() js = &json_out_client_conn{ - Id: cts.Id, + CId: cts.Id, ReqServerAddrs: cts.cfg.ServerAddrs, CurrentServerIndex: cts.cfg.Index, ServerAddr: cts.remote_addr.Get(), @@ -445,7 +450,8 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r lftsta, lftdur = r.GetLifetimeInfo() jsp = append(jsp, json_out_client_route{ - Id: r.Id, + CId: r.cts.Id, + RId: r.Id, ClientPeerAddr: r.PeerAddr, ClientPeerName: r.PeerName, ServerPeerSvcAddr: r.ServerPeerSvcAddr, @@ -494,7 +500,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r } rc = &ClientRouteConfig{ - Id: jcr.Id, + Id: jcr.RId, PeerAddr: jcr.ClientPeerAddr, PeerName: jcr.ClientPeerName, Option: server_peer_option, @@ -511,7 +517,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r goto oops } else { status_code = WriteJsonRespHeader(w, http.StatusCreated) - if err = je.Encode(json_out_client_route_id{Id: r.Id, CtsId: r.cts.Id}); err != nil { goto oops } + if err = je.Encode(json_out_client_route_id{RId: r.Id, CId: r.cts.Id}); err != nil { goto oops } } case http.MethodDelete: @@ -562,7 +568,8 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter lftsta, lftdur = r.GetLifetimeInfo() err = je.Encode(json_out_client_route{ - Id: r.Id, + CId: r.cts.Id, + RId: r.Id, ClientPeerAddr: r.PeerAddr, ClientPeerName: r.PeerName, ServerPeerSvcAddr: r.ServerPeerSvcAddr, @@ -649,7 +656,8 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit lftsta, lftdur = r.GetLifetimeInfo() err = je.Encode(json_out_client_route{ - Id: r.Id, + CId: r.cts.Id, + RId: r.Id, ClientPeerAddr: r.PeerAddr, ClientPeerName: r.PeerName, ServerPeerSvcAddr: r.ServerPeerSvcAddr, @@ -738,7 +746,9 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers) ServeHTTP(w http.Response p = r.ptc_map[pi] jcp = append(jcp, json_out_client_peer{ - Id: p.conn_id, + CId: r.cts.Id, + RId: r.Id, + PId: p.conn_id, ClientPeerAddr: p.conn.RemoteAddr().String(), ClientLocalAddr: p.conn.LocalAddr().String(), ServerPeerAddr: p.pts_raddr, @@ -795,7 +805,9 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo var jcp *json_out_client_peer jcp = &json_out_client_peer{ - Id: p.conn_id, + CId: p.route.cts.Id, + RId: p.route.Id, + PId: p.conn_id, ClientPeerAddr: p.conn.RemoteAddr().String(), ClientLocalAddr: p.conn.LocalAddr().String(), ServerPeerAddr: p.pts_raddr, diff --git a/server-ctl.go b/server-ctl.go index e191146..a7cf0a3 100644 --- a/server-ctl.go +++ b/server-ctl.go @@ -23,7 +23,7 @@ type json_out_token struct { } type json_out_server_conn struct { - Id ConnId `json:"id"` + CId ConnId `json:"conn-id"` ServerAddr string `json:"server-addr"` ClientAddr string `json:"client-addr"` ClientToken string `json:"client-token"` @@ -32,7 +32,8 @@ type json_out_server_conn struct { } type json_out_server_route struct { - Id RouteId `json:"id"` + CId ConnId `json:"conn-id"` + RId RouteId `json:"route-id"` ClientPeerAddr string `json:"client-peer-addr"` ClientPeerName string `json:"client-peer-name"` ServerPeerOption string `json:"server-peer-option"` @@ -42,7 +43,9 @@ type json_out_server_route struct { } type json_out_server_peer struct { - Id PeerId `json:"id"` + CId ConnId `json:"conn-id"` + RId RouteId `json:"route-id"` + PId PeerId `json:"peer-id"` ServerPeerAddr string `json:"server-peer-addr"` ServerLocalAddr string `json:"server-local-addr"` ClientPeerAddr string `json:"client-peer-addr"` @@ -102,6 +105,10 @@ type server_ctl_server_conns_id_routes_id_peers_id struct { server_ctl } +type server_ctl_server_routes struct { + server_ctl +} + type server_ctl_server_peers struct { server_ctl } @@ -225,7 +232,8 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R var r *ServerRoute r = cts.route_map[ri] jsp = append(jsp, json_out_server_route{ - Id: r.Id, + CId: cts.Id, + RId: r.Id, ClientPeerAddr: r.PtcAddr, ClientPeerName: r.PtcName, ServerPeerSvcAddr: r.SvcAddr.String(), @@ -238,7 +246,7 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R } js = append(js, json_out_server_conn{ - Id: cts.Id, + CId: cts.Id, ClientAddr: cts.RemoteAddr.String(), ServerAddr: cts.LocalAddr.String(), ClientToken: cts.ClientToken.Get(), @@ -308,7 +316,8 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt r = cts.route_map[ri] jsp = append(jsp, json_out_server_route{ - Id: r.Id, + CId: cts.Id, + RId: r.Id, ClientPeerAddr: r.PtcAddr, ClientPeerName: r.PtcName, ServerPeerSvcAddr: r.SvcAddr.String(), @@ -320,7 +329,7 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt cts.route_mtx.Unlock() } js = &json_out_server_conn{ - Id: cts.Id, + CId: cts.Id, ClientAddr: cts.RemoteAddr.String(), ServerAddr: cts.LocalAddr.String(), ClientToken: cts.ClientToken.Get(), @@ -380,7 +389,8 @@ func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, r r = cts.route_map[ri] jsp = append(jsp, json_out_server_route{ - Id: r.Id, + CId: cts.Id, + RId: r.Id, ClientPeerAddr: r.PtcAddr, ClientPeerName: r.PtcName, ServerPeerSvcAddr: r.SvcAddr.String(), @@ -468,7 +478,8 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter case http.MethodGet: status_code = WriteJsonRespHeader(w, http.StatusOK) err = je.Encode(json_out_server_route{ - Id: r.Id, + CId: r.Cts.Id, + RId: r.Id, ClientPeerAddr: r.PtcAddr, ClientPeerName: r.PtcName, ServerPeerSvcAddr: r.SvcAddr.String(), @@ -533,7 +544,9 @@ func (ctl *server_ctl_server_conns_id_routes_id_peers) ServeHTTP(w http.Response var p *ServerPeerConn p = r.pts_map[pi] jcp = append(jcp, json_out_server_peer{ - Id: p.conn_id, + CId: r.Cts.Id, + RId: r.Id, + PId: p.conn_id, ServerPeerAddr: p.conn.RemoteAddr().String(), ServerLocalAddr: p.conn.LocalAddr().String(), ClientPeerAddr: p.client_peer_raddr.Get(), @@ -591,7 +604,9 @@ func (ctl *server_ctl_server_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo var jcp *json_out_server_peer jcp = &json_out_server_peer{ - Id: p.conn_id, + CId: p.route.Cts.Id, + RId: p.route.Id, + PId: p.conn_id, ServerPeerAddr: p.conn.RemoteAddr().String(), ServerLocalAddr: p.conn.LocalAddr().String(), ClientPeerAddr: p.client_peer_raddr.Get(), @@ -613,6 +628,53 @@ func (ctl *server_ctl_server_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo //done: return status_code, nil +oops: + return status_code, err +} +// ------------------------------------ + +func (ctl *server_ctl_server_routes) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { + var s *Server + var status_code int + var je *json.Encoder + var err error + + s = ctl.s + je = json.NewEncoder(w) + + switch req.Method { + case http.MethodGet: + var js []json_out_server_route + var e *list.Element + + js = make([]json_out_server_route, 0) + s.route_mtx.Lock() + for e = s.route_list.Front(); e != nil; e = e.Next() { + var r *ServerRoute + r = e.Value.(*ServerRoute) + js = append(js, json_out_server_route{ + CId: r.Cts.Id, + RId: r.Id, + ClientPeerAddr: r.PtcAddr, + ClientPeerName: r.PtcName, + ServerPeerSvcAddr: r.SvcAddr.String(), + ServerPeerSvcNet: r.SvcPermNet.String(), + ServerPeerOption: r.SvcOption.String(), + CreatedMilli: r.Created.UnixMilli(), + }) + } + s.route_mtx.Unlock() + + status_code = WriteJsonRespHeader(w, http.StatusOK) + if err = je.Encode(js); err != nil { goto oops } + + default: + status_code = WriteEmptyRespHeader(w, http.StatusMethodNotAllowed) + } + +//done: + return status_code, nil + oops: return status_code, err } @@ -629,18 +691,18 @@ func (ctl *server_ctl_server_peers) ServeHTTP(w http.ResponseWriter, req *http.R switch req.Method { case http.MethodGet: - var js []ServerEventPeerAdded + var js []json_out_server_peer var e *list.Element - js = make([]ServerEventPeerAdded, 0) + js = make([]json_out_server_peer, 0) s.pts_mtx.Lock() for e = s.pts_list.Front(); e != nil; e = e.Next() { var pts *ServerPeerConn pts = e.Value.(*ServerPeerConn) - js = append(js, ServerEventPeerAdded{ // TODO: rename or create an alias type? - Conn: pts.route.Cts.Id, - Route: pts.route.Id, - Peer: pts.conn_id, + js = append(js, json_out_server_peer{ + CId: pts.route.Cts.Id, + RId: pts.route.Id, + PId: pts.conn_id, ServerPeerAddr: pts.conn.RemoteAddr().String(), ServerLocalAddr: pts.conn.LocalAddr().String(), ClientPeerAddr: pts.client_peer_raddr.Get(), diff --git a/server.go b/server.go index 606b3d0..cb58273 100644 --- a/server.go +++ b/server.go @@ -175,6 +175,9 @@ type Server struct { pts_mtx sync.Mutex pts_list *list.List + route_mtx sync.Mutex + route_list *list.List + log Logger conn_notice_handlers []ServerConnNoticeHandler @@ -223,6 +226,8 @@ type ServerRoute struct { Id RouteId Created time.Time + node_in_server *list.Element + svc_l *net.TCPListener SvcAddr *net.TCPAddr // actual listening address SvcReqAddr string @@ -430,6 +435,11 @@ func (r *ServerRoute) RunTask(wg *sync.WaitGroup) { r.Cts.RemoveServerRoute(r) // final phase... + r.Cts.S.route_mtx.Lock() + r.Cts.S.route_list.Remove(r.node_in_server) + r.node_in_server = nil + r.Cts.S.route_mtx.Unlock() + r.Cts.S.bulletin.Enqueue( &ServerEvent{ Kind: SERVER_EVENT_ROUTE_DELETED, @@ -574,6 +584,10 @@ func (cts *ServerConn) AddNewServerRoute(route_id RouteId, proto RouteOption, pt cts.S.stats.routes.Add(1) cts.route_mtx.Unlock() + cts.S.route_mtx.Lock() + r.node_in_server = cts.S.route_list.PushBack(r) + cts.S.route_mtx.Unlock() + cts.S.bulletin.Enqueue( &ServerEvent{ Kind: SERVER_EVENT_ROUTE_ADDED, @@ -1363,6 +1377,7 @@ func NewServer(ctx context.Context, name string, logger Logger, cfg *ServerConfi s.ext_svcs = make([]Service, 0, 1) s.pts_limit = cfg.MaxPeers s.pts_list = list.New() + s.route_list = list.New() s.cts_limit = cfg.RpcMaxConns s.cts_next_id = 1 s.cts_map = make(ServerConnMap) @@ -1400,6 +1415,8 @@ func NewServer(ctx context.Context, name string, logger Logger, cfg *ServerConfi s.WrapHttpHandler(&server_ctl_server_conns_id_routes_id_peers{server_ctl{s: &s, id: HS_ID_CTL}})) s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes/{route_id}/peers/{peer_id}", s.WrapHttpHandler(&server_ctl_server_conns_id_routes_id_peers_id{server_ctl{s: &s, id: HS_ID_CTL}})) + s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-routes", + s.WrapHttpHandler(&server_ctl_server_routes{server_ctl{s: &s, id: HS_ID_CTL}})) s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-peers", s.WrapHttpHandler(&server_ctl_server_peers{server_ctl{s: &s, id: HS_ID_CTL}})) s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/notices",