Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
SERVICE_PORT = 62050
NODE_HOST = "0.0.0.0"
# Defaults to 0.0.0.0 with TLS; 127.0.0.1 with TLS_ENABLED=false.
# NODE_HOST = "0.0.0.0"

# Disable only behind a TLS-terminating web server. API-key authentication stays required.
# Plaintext NODE_HOST must be a literal loopback, RFC1918 IPv4, or IPv6 ULA address.
# TLS_ENABLED = true

# use absolute path

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ jobs:
make generate_server_cert
make generate_client_cert

- name: Install nginx for TLS frontend integration coverage
run: sudo apt-get update && sudo apt-get install -y nginx

- name: Run regular test suite
env:
MTPROTO_TELEMT_BIN: /usr/local/bin/telemt
NODE_TEST_NGINX: /usr/sbin/nginx
run: TEST_INTEGRATION=true go test ./... -v -p 1

- name: Check Xray process status races
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,5 @@ Thanks to the all contributors who have helped improve PasarGuard Node:
<p align="center">
Made with <a rel="noopener noreferrer" target="_blank" href="https://contrib.rocks">contrib.rocks</a>
</p>

See [Node TLS](docs/node-tls.md) for certificates, renewal and private TLS backends.
128 changes: 128 additions & 0 deletions cmd/node/listeners_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"context"
"crypto/tls"
"net"
"net/http"
"testing"
"time"

"github.com/google/uuid"
"github.com/pasarguard/node/common"
"github.com/pasarguard/node/config"
"github.com/pasarguard/node/controller/rest"
"github.com/pasarguard/node/controller/rpc"
"github.com/pasarguard/node/pkg/tlsutil"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

func listenerAddress(t *testing.T) string {
t.Helper()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addr := l.Addr().String()
l.Close()
return addr
}

func TestListenerTransports(t *testing.T) {
for _, enabled := range []bool{true, false} {
name := "private backend"
if enabled {
name = "TLS"
}
t.Run(name, func(t *testing.T) {
cfg := &config.Config{TLSEnabled: enabled, ApiKey: uuid.New(), GeneratedConfigPath: t.TempDir(), LogBufferSize: 10}
var serverTLS *tls.Config
client := &http.Client{Timeout: 3 * time.Second}
scheme := "http"
var creds credentials.TransportCredentials = insecure.NewCredentials()
if enabled {
var err error
serverTLS, err = tlsutil.LoadTLSCredentials("../../certs/ssl_cert.pem", "../../certs/ssl_key.pem")
if err != nil {
t.Fatal(err)
}
pool, err := tlsutil.LoadClientPool("../../certs/ssl_cert.pem")
if err != nil {
t.Fatal(err)
}
client.Transport = &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool}}
creds = credentials.NewTLS(&tls.Config{RootCAs: pool})
scheme = "https"
}
addr := listenerAddress(t)
stop, service, err := rest.StartHttpListener(serverTLS, addr, cfg)
if err != nil {
t.Fatal(err)
}
defer service.Disconnect()
defer stop(context.Background())
for _, auth := range []bool{false, true} {
req, _ := http.NewRequest("GET", scheme+"://"+addr+"/info", nil)
if auth {
req.Header.Set("x-api-key", cfg.ApiKey.String())
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
want := http.StatusUnauthorized
if auth {
want = http.StatusOK
}
if resp.StatusCode != want {
t.Fatalf("REST auth=%v: got %d", auth, resp.StatusCode)
}
}
client.CloseIdleConnections()
addr = listenerAddress(t)
stopRPC, serviceRPC, err := rpc.StartGRPCListener(serverTLS, addr, cfg)
if err != nil {
t.Fatal(err)
}
defer serviceRPC.Disconnect()
defer stopRPC(context.Background())
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(creds))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
c := common.NewNodeServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if _, err := c.GetBaseInfo(ctx, &common.Empty{}); status.Code(err) != codes.Unauthenticated {
t.Fatalf("gRPC missing key: %v", err)
}
authCtx := metadata.NewOutgoingContext(ctx, metadata.Pairs("x-api-key", cfg.ApiKey.String()))
if _, err := c.GetBaseInfo(authCtx, &common.Empty{}); err != nil {
t.Fatal(err)
}
})
}
}

func TestListenersRejectUnsafePlaintext(t *testing.T) {
for _, enabled := range []bool{true, false} {
cfg := &config.Config{TLSEnabled: enabled}
addr := "0.0.0.0:0"
if enabled {
addr = "127.0.0.1:0"
}
if _, _, err := rest.StartHttpListener(nil, addr, cfg); err == nil {
t.Fatal("REST allowed unsafe plaintext")
}
if _, _, err := rpc.StartGRPCListener(nil, addr, cfg); err == nil {
t.Fatal("gRPC allowed unsafe plaintext")
}
}
}
10 changes: 7 additions & 3 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"crypto/tls"
"log"
"net"
"os"
Expand All @@ -25,9 +26,12 @@ func main() {

addr := net.JoinHostPort(cfg.NodeHost, strconv.Itoa(cfg.ServicePort))

tlsConfig, err := tlsutil.LoadTLSCredentials(cfg.SslCertFile, cfg.SslKeyFile)
if err != nil {
log.Fatal(err)
var tlsConfig *tls.Config
if cfg.TLSEnabled {
tlsConfig, err = tlsutil.LoadTLSCredentials(cfg.SslCertFile, cfg.SslKeyFile)
if err != nil {
log.Fatal(err)
}
}

log.Printf("Starting Node: v%s", controller.NodeVersion)
Expand Down
Loading
Loading