Skip to content

Commit fc76058

Browse files
committed
add tls and authentication
1 parent 99e08e6 commit fc76058

File tree

7 files changed

+70
-18
lines changed

7 files changed

+70
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist
2+
certs

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ Configuration options can be specified via environment variables (all are option
1616
| `LNCD_LIMIT_ACTIVE_CONNECTIONS` | `210` | Maximum number of active connections allowed. |
1717
| `LNCD_STATS_INTERVAL` | `1m` | Interval for logging connection pool statistics. |
1818
| `LNCD_DEBUG` | `false` | Flag to enable or disable debug logging. |
19-
| `LNCD_RECEIVER_PORT` | `7167` | Port on which the receiver server listens. |
20-
| `LNCD_RECEIVER_HOST` | `0.0.0.0` | Host address on which the receiver server listens. |
19+
| `LNCD_PORT` | `7167` | Port on which the server listens. |
20+
| `LNCD_HOST` | `0.0.0.0` | Host address on which the server listens. |
21+
| `LNCD_TLS_CERT_PATH` | `""` | Path to the TLS certificate file (empty to disable TLS). |
22+
| `LNCD_TLS_KEY_PATH` | `""` | Path to the TLS key file (empty to disable TLS). |
23+
| `LNCD_AUTH_TOKEN` | `""` | Bearer token required to access the server (empty to disable authentication). |
2124
| `LNCD_DEV_UNSAFE_LOG` | `false` | Enable or disable logging of sensitive data. |
2225

2326

build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ script_dir=$(dirname $0)
55
cd "$script_dir/lncd"
66
mkdir -p ../dist
77
go build -o ../dist/lncd -tags="$RPC_TAGS" .
8+
cd ..

gen-devcerts.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
mkdir -p certs
3+
openssl req -x509 -newkey rsa:2048 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes \
4+
-subj "/C=IT/ST=Venice/L=Venice/O=LNCD/OU=LNCD/CN=localhost"

lncd/lncd.go

+44-13
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ var (
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

442460
func 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()

lncd/ui.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func formHandler(w http.ResponseWriter, r *http.Request) {
1313
event.preventDefault();
1414
const form = event.target;
1515
const response = document.getElementById('response');
16+
const authToken = form.authtoken.value;
1617
const data = {
1718
Connection: {
1819
Mailbox: form.mailbox.value,
@@ -26,7 +27,8 @@ func formHandler(w http.ResponseWriter, r *http.Request) {
2627
fetch('/rpc', {
2728
method: 'POST',
2829
headers: {
29-
'Content-Type': 'application/json'
30+
'Content-Type': 'application/json',
31+
'Authorization': 'Bearer ' + authToken
3032
},
3133
body: JSON.stringify(data)
3234
})
@@ -53,6 +55,8 @@ func formHandler(w http.ResponseWriter, r *http.Request) {
5355
<body>
5456
<h1>LNCD Test Form</h1>
5557
<form onsubmit="submitForm(event)">
58+
<label for="mailbox">AuthToken:</label><br>
59+
<input value="" type="text" id="authtoken" name="authtoken"><br>
5660
<label for="mailbox">Mailbox:</label><br>
5761
<input value="mailbox.terminal.lightning.today:443" type="text" id="mailbox" name="mailbox"><br>
5862
<label for="pairingPhrase">Pairing Phrase:</label><br>

run.sh

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#!/bin/bash
22
source build.sh
3-
chmod +x ../dist/lncd
3+
chmod +x dist/lncd
4+
cd dist
5+
46
export LNCD_DEBUG="true"
57
export LNCD_TIMEOUT="1m"
68
export LNCD_STATS_INTERVAL="10s"
79
export LNCD_DEV_UNSAFE_LOG="true"
8-
../dist/lncd
10+
11+
if [ -f ../certs/cert.pem ]; then
12+
export LNCD_TLS_CERT_PATH="../certs/cert.pem"
13+
export LNCD_TLS_KEY_PATH="../certs/key.pem"
14+
fi
15+
16+
./lncd

0 commit comments

Comments
 (0)