6161 LNCD_LIMIT_ACTIVE_CONNECTIONS = getEnvAsInt ("LNCD_LIMIT_ACTIVE_CONNECTIONS" , 210 )
6262 LNCD_STATS_INTERVAL = getEnvAsDuration ("LNCD_STATS_INTERVAL" , 1 * time .Minute )
6363 LNCD_DEBUG = getEnvAsBool ("LNCD_DEBUG" , false )
64- LNCD_RECEIVER_PORT = getEnv ("LNCD_RECEIVER_PORT" , "7167" )
65- LNCD_RECEIVER_HOST = getEnv ("LNCD_RECEIVER_HOST" , "0.0.0.0" )
64+ LNCD_PORT = getEnv ("LNCD_PORT" , "7167" )
65+ LNCD_HOST = getEnv ("LNCD_HOST" , "0.0.0.0" )
66+ LNCD_AUTH_TOKEN = getEnv ("LNCD_AUTH_TOKEN" , "" )
67+ LNCD_TLS_CERT_PATH = getEnv ("LNCD_TLS_CERT_PATH" , "" )
68+ LNCD_TLS_KEY_PATH = getEnv ("LNCD_TLS_KEY_PATH" , "" )
6669)
6770
6871// //////////////////////////////
@@ -436,8 +439,23 @@ func parseKeys(localPrivKey, remotePubKey string) (
436439 return localStaticKey , remoteStaticKey , nil
437440}
438441
439-
440-
442+ func authMiddleware (next http.HandlerFunc ) http.HandlerFunc {
443+ return func (w http.ResponseWriter , r * http.Request ) {
444+ if LNCD_AUTH_TOKEN != "" {
445+ authHeader := r .Header .Get ("Authorization" )
446+ if ! strings .HasPrefix (authHeader , "Bearer " ) {
447+ writeJSONError (w , "Unauthorized" , http .StatusUnauthorized )
448+ return
449+ }
450+ token := strings .TrimPrefix (authHeader , "Bearer " )
451+ if token != LNCD_AUTH_TOKEN {
452+ writeJSONError (w , "Unauthorized" , http .StatusUnauthorized )
453+ return
454+ }
455+ }
456+ next .ServeHTTP (w , r )
457+ }
458+ }
441459
442460func main () {
443461 shutdownInterceptor , err := signal .Intercept ()
@@ -452,24 +470,37 @@ func main() {
452470 log .Infof ("LNCD_LIMIT_ACTIVE_CONNECTIONS: %v" , LNCD_LIMIT_ACTIVE_CONNECTIONS )
453471 log .Infof ("LNCD_STATS_INTERVAL: %v" , LNCD_STATS_INTERVAL )
454472 log .Infof ("LNCD_DEBUG: %v" , LNCD_DEBUG )
455- log .Infof ("LNCD_RECEIVER_PORT: %v" , LNCD_RECEIVER_PORT )
456- log .Infof ("LNCD_RECEIVER_HOST: %v" , LNCD_RECEIVER_HOST )
457- log .Debugf ("debug enabled" )
473+ log .Infof ("LNCD_PORT: %v" , LNCD_PORT )
474+ log .Infof ("LNCD_HOST: %v" , LNCD_HOST )
475+ log .Infof ("LNCD_TLS_CERT_PATH: %v" , LNCD_TLS_CERT_PATH )
476+ log .Infof ("LNCD_TLS_KEY_PATH: %v" , LNCD_TLS_KEY_PATH )
477+
458478 if UNSAFE_LOGS {
479+ log .Info ("LNCD_AUTH_TOKEN: %v" , LNCD_AUTH_TOKEN )
459480 log .Infof ("!!! UNSAFE LOGGING ENABLED !!!" )
460481 }
482+ log .Debugf ("debug enabled" )
461483
462484 var pool * ConnectionPool = NewConnectionPool ()
463485 startStatsLoop (pool )
464486
465- http .HandleFunc ("/rpc" , rpcHandler (pool ))
487+ http .HandleFunc ("/rpc" , authMiddleware (rpcHandler (pool )))
488+ http .HandleFunc ("/health" , authMiddleware (healthCheckHandler ))
466489 http .HandleFunc ("/" , formHandler )
467- http .HandleFunc ("/health" , healthCheckHandler )
468490
469- log .Infof ("Server started at " + LNCD_RECEIVER_HOST + ":" + LNCD_RECEIVER_PORT )
470- if err := http .ListenAndServe (LNCD_RECEIVER_HOST + ":" + LNCD_RECEIVER_PORT , nil ); err != nil {
471- log .Errorf ("Error starting server: %v" , err )
472- exit (err )
491+ log .Infof ("Server starting at " + LNCD_HOST + ":" + LNCD_PORT )
492+ var isTLS = LNCD_TLS_CERT_PATH != "" && LNCD_TLS_KEY_PATH != ""
493+ if isTLS {
494+ log .Infof ("TLS enabled" )
495+ if err := http .ListenAndServeTLS (LNCD_HOST + ":" + LNCD_PORT , LNCD_TLS_CERT_PATH , LNCD_TLS_KEY_PATH , nil ); err != nil {
496+ log .Errorf ("Error starting server: %v" , err )
497+ exit (err )
498+ }
499+ } else {
500+ if err := http .ListenAndServe (LNCD_HOST + ":" + LNCD_PORT , nil ); err != nil {
501+ log .Errorf ("Error starting server: %v" , err )
502+ exit (err )
503+ }
473504 }
474505
475506 <- shutdownInterceptor .ShutdownChannel ()
0 commit comments