exposed the cfg field by nameing cfg to Cfg in server.go

This commit is contained in:
hyung-hwan 2025-02-26 14:46:09 +09:00
parent 2d63e81e62
commit 5df95159a3
2 changed files with 31 additions and 31 deletions

View File

@ -105,12 +105,12 @@ func (ctl *server_ctl) Id() string {
} }
func (ctl *server_ctl) Cors(req *http.Request) bool { func (ctl *server_ctl) Cors(req *http.Request) bool {
return ctl.s.cfg.CtlCors return ctl.s.Cfg.CtlCors
} }
func (ctl *server_ctl) Authenticate(req *http.Request) (int, string) { func (ctl *server_ctl) Authenticate(req *http.Request) (int, string) {
if ctl.noauth || ctl.s.cfg.CtlAuth == nil { return http.StatusOK, "" } if ctl.noauth || ctl.s.Cfg.CtlAuth == nil { return http.StatusOK, "" }
return ctl.s.cfg.CtlAuth.Authenticate(req) return ctl.s.Cfg.CtlAuth.Authenticate(req)
} }
// ------------------------------------ // ------------------------------------
@ -131,7 +131,7 @@ func (ctl *server_ctl_token) ServeHTTP(w http.ResponseWriter, req *http.Request)
var tok string var tok string
var now time.Time var now time.Time
if s.cfg.CtlAuth == nil || !s.cfg.CtlAuth.Enabled || s.cfg.CtlAuth.TokenRsaKey == nil { if s.Cfg.CtlAuth == nil || !s.Cfg.CtlAuth.Enabled || s.Cfg.CtlAuth.TokenRsaKey == nil {
status_code = WriteJsonRespHeader(w, http.StatusForbidden) status_code = WriteJsonRespHeader(w, http.StatusForbidden)
err = fmt.Errorf("auth not enabled or token rsa key not set") err = fmt.Errorf("auth not enabled or token rsa key not set")
je.Encode(JsonErrmsg{Text: err.Error()}) je.Encode(JsonErrmsg{Text: err.Error()})
@ -140,8 +140,8 @@ func (ctl *server_ctl_token) ServeHTTP(w http.ResponseWriter, req *http.Request)
now = time.Now() now = time.Now()
claim.IssuedAt = now.Unix() claim.IssuedAt = now.Unix()
claim.ExpiresAt = now.Add(s.cfg.CtlAuth.TokenTtl).Unix() claim.ExpiresAt = now.Add(s.Cfg.CtlAuth.TokenTtl).Unix()
jwt = NewJWT(s.cfg.CtlAuth.TokenRsaKey, &claim) jwt = NewJWT(s.Cfg.CtlAuth.TokenRsaKey, &claim)
tok, err = jwt.SignRS512() tok, err = jwt.SignRS512()
if err != nil { if err != nil {
status_code = WriteJsonRespHeader(w, http.StatusInternalServerError) status_code = WriteJsonRespHeader(w, http.StatusInternalServerError)

View File

@ -70,7 +70,7 @@ type Server struct {
UnimplementedHoduServer UnimplementedHoduServer
Named Named
cfg *ServerConfig Cfg *ServerConfig
Ctx context.Context Ctx context.Context
CtxCancel context.CancelFunc CtxCancel context.CancelFunc
@ -1121,7 +1121,7 @@ func NewServer(ctx context.Context, name string, logger Logger, cfg *ServerConfi
s.rpc = append(s.rpc, l) s.rpc = append(s.rpc, l)
} }
s.cfg = cfg s.Cfg = cfg
s.ext_svcs = make([]Service, 0, 1) s.ext_svcs = make([]Service, 0, 1)
s.pts_limit = cfg.MaxPeers s.pts_limit = cfg.MaxPeers
s.cts_limit = cfg.RpcMaxConns s.cts_limit = cfg.RpcMaxConns
@ -1142,7 +1142,7 @@ func NewServer(ctx context.Context, name string, logger Logger, cfg *ServerConfi
*/ */
opts = append(opts, grpc.StatsHandler(&ConnCatcher{server: &s})) opts = append(opts, grpc.StatsHandler(&ConnCatcher{server: &s}))
if s.cfg.RpcTls != nil { opts = append(opts, grpc.Creds(credentials.NewTLS(s.cfg.RpcTls))) } if s.Cfg.RpcTls != nil { opts = append(opts, grpc.Creds(credentials.NewTLS(s.Cfg.RpcTls))) }
//opts = append(opts, grpc.UnaryInterceptor(unaryInterceptor)) //opts = append(opts, grpc.UnaryInterceptor(unaryInterceptor))
//opts = append(opts, grpc.StreamInterceptor(streamInterceptor)) //opts = append(opts, grpc.StreamInterceptor(streamInterceptor))
s.rpc_svr = grpc.NewServer(opts...) s.rpc_svr = grpc.NewServer(opts...)
@ -1156,32 +1156,32 @@ func NewServer(ctx context.Context, name string, logger Logger, cfg *ServerConfi
s.ctl_mux = http.NewServeMux() s.ctl_mux = http.NewServeMux()
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/server-conns", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-conns",
s.WrapHttpHandler(&server_ctl_server_conns{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_server_conns{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}",
s.WrapHttpHandler(&server_ctl_server_conns_id{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_server_conns_id{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes",
s.WrapHttpHandler(&server_ctl_server_conns_id_routes{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_server_conns_id_routes{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes/{route_id}", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes/{route_id}",
s.WrapHttpHandler(&server_ctl_server_conns_id_routes_id{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_server_conns_id_routes_id{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes/{route_id}/peers", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/server-conns/{conn_id}/routes/{route_id}/peers",
s.WrapHttpHandler(&server_ctl_server_conns_id_routes_id_peers{server_ctl{s: &s, id: HS_ID_CTL}})) 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.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.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/notices", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/notices",
s.WrapHttpHandler(&server_ctl_notices{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_notices{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/notices/{conn_id}", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/notices/{conn_id}",
s.WrapHttpHandler(&server_ctl_notices_id{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_notices_id{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/stats", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/stats",
s.WrapHttpHandler(&server_ctl_stats{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_stats{server_ctl{s: &s, id: HS_ID_CTL}}))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/token", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/token",
s.WrapHttpHandler(&server_ctl_token{server_ctl{s: &s, id: HS_ID_CTL}})) s.WrapHttpHandler(&server_ctl_token{server_ctl{s: &s, id: HS_ID_CTL}}))
// TODO: make this optional. add this endpoint only if it's enabled... // TODO: make this optional. add this endpoint only if it's enabled...
s.promreg = prometheus.NewRegistry() s.promreg = prometheus.NewRegistry()
s.promreg.MustRegister(prometheus.NewGoCollector()) s.promreg.MustRegister(prometheus.NewGoCollector())
s.promreg.MustRegister(NewServerCollector(&s)) s.promreg.MustRegister(NewServerCollector(&s))
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl/metrics", s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl/metrics",
promhttp.HandlerFor(s.promreg, promhttp.HandlerOpts{ EnableOpenMetrics: true })) promhttp.HandlerFor(s.promreg, promhttp.HandlerOpts{ EnableOpenMetrics: true }))
s.ctl = make([]*http.Server, len(cfg.CtlAddrs)) s.ctl = make([]*http.Server, len(cfg.CtlAddrs))
@ -1189,7 +1189,7 @@ func NewServer(ctx context.Context, name string, logger Logger, cfg *ServerConfi
s.ctl[i] = &http.Server{ s.ctl[i] = &http.Server{
Addr: cfg.CtlAddrs[i], Addr: cfg.CtlAddrs[i],
Handler: s.ctl_mux, Handler: s.ctl_mux,
TLSConfig: s.cfg.CtlTls, TLSConfig: s.Cfg.CtlTls,
ErrorLog: hs_log, ErrorLog: hs_log,
// TODO: more settings // TODO: more settings
} }
@ -1381,7 +1381,7 @@ func (s *Server) RunCtlTask(wg *sync.WaitGroup) {
go func(i int, cs *http.Server) { go func(i int, cs *http.Server) {
var l net.Listener var l net.Listener
s.log.Write("", LOG_INFO, "Control channel[%d] started on %s", i, s.cfg.CtlAddrs[i]) s.log.Write("", LOG_INFO, "Control channel[%d] started on %s", i, s.Cfg.CtlAddrs[i])
if s.stop_req.Load() == false { if s.stop_req.Load() == false {
// defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS() // defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS()
@ -1390,10 +1390,10 @@ func (s *Server) RunCtlTask(wg *sync.WaitGroup) {
l, err = net.Listen(TcpAddrStrClass(cs.Addr), cs.Addr) l, err = net.Listen(TcpAddrStrClass(cs.Addr), cs.Addr)
if err == nil { if err == nil {
if s.stop_req.Load() == false { if s.stop_req.Load() == false {
if s.cfg.CtlTls == nil { if s.Cfg.CtlTls == nil {
err = cs.Serve(l) err = cs.Serve(l)
} else { } else {
err = cs.ServeTLS(l, "", "") // s.cfg.CtlTls must provide a certificate and a key err = cs.ServeTLS(l, "", "") // s.Cfg.CtlTls must provide a certificate and a key
} }
} else { } else {
err = fmt.Errorf("stop requested") err = fmt.Errorf("stop requested")
@ -1427,16 +1427,16 @@ func (s *Server) RunPxyTask(wg *sync.WaitGroup) {
go func(i int, cs *http.Server) { go func(i int, cs *http.Server) {
var l net.Listener var l net.Listener
s.log.Write("", LOG_INFO, "Proxy channel[%d] started on %s", i, s.cfg.PxyAddrs[i]) s.log.Write("", LOG_INFO, "Proxy channel[%d] started on %s", i, s.Cfg.PxyAddrs[i])
if s.stop_req.Load() == false { if s.stop_req.Load() == false {
l, err = net.Listen(TcpAddrStrClass(cs.Addr), cs.Addr) l, err = net.Listen(TcpAddrStrClass(cs.Addr), cs.Addr)
if err == nil { if err == nil {
if s.stop_req.Load() == false { if s.stop_req.Load() == false {
if s.cfg.PxyTls == nil { // TODO: change this if s.Cfg.PxyTls == nil { // TODO: change this
err = cs.Serve(l) err = cs.Serve(l)
} else { } else {
err = cs.ServeTLS(l, "", "") // s.cfg.PxyTls must provide a certificate and a key err = cs.ServeTLS(l, "", "") // s.Cfg.PxyTls must provide a certificate and a key
} }
} else { } else {
err = fmt.Errorf("stop requested") err = fmt.Errorf("stop requested")
@ -1470,16 +1470,16 @@ func (s *Server) RunWpxTask(wg *sync.WaitGroup) {
go func(i int, cs *http.Server) { go func(i int, cs *http.Server) {
var l net.Listener var l net.Listener
s.log.Write("", LOG_INFO, "Wpx channel[%d] started on %s", i, s.cfg.WpxAddrs[i]) s.log.Write("", LOG_INFO, "Wpx channel[%d] started on %s", i, s.Cfg.WpxAddrs[i])
if s.stop_req.Load() == false { if s.stop_req.Load() == false {
l, err = net.Listen(TcpAddrStrClass(cs.Addr), cs.Addr) l, err = net.Listen(TcpAddrStrClass(cs.Addr), cs.Addr)
if err == nil { if err == nil {
if s.stop_req.Load() == false { if s.stop_req.Load() == false {
if s.cfg.WpxTls == nil { // TODO: change this if s.Cfg.WpxTls == nil { // TODO: change this
err = cs.Serve(l) err = cs.Serve(l)
} else { } else {
err = cs.ServeTLS(l, "", "") // s.cfg.WpxTls must provide a certificate and a key err = cs.ServeTLS(l, "", "") // s.Cfg.WpxTls must provide a certificate and a key
} }
} else { } else {
err = fmt.Errorf("stop requested") err = fmt.Errorf("stop requested")
@ -1906,7 +1906,7 @@ func (s *Server) SetConnNoticeHandler(handler ServerConnNoticeHandler) {
} }
func (s *Server) AddCtlHandler(path string, handler ServerHttpHandler) { func (s *Server) AddCtlHandler(path string, handler ServerHttpHandler) {
s.ctl_mux.Handle(s.cfg.CtlPrefix + "/_ctl" + path, s.WrapHttpHandler(handler)) s.ctl_mux.Handle(s.Cfg.CtlPrefix + "/_ctl" + path, s.WrapHttpHandler(handler))
} }
func (s *Server) AddCtlMetricsCollector(col prometheus.Collector) error { func (s *Server) AddCtlMetricsCollector(col prometheus.Collector) error {