Files
orly/main.go
mleku b28acc0c29 Add authentication support and enhance protocol handling
- **protocol/socketapi/challenge.go**:
  - Removed unused `GetListener` function and its associated logic.

- **protocol/socketapi/handleEvent.go**:
  - Refactored `env.Id` references to `env.E.Id` to standardize access to event identifiers.

- **app/realy/server-impl.go**:
  - Added `AuthRequired` method to expose server-level authentication configuration.

- **app/realy/handleRelayinfo.go**:
  - Included `Authentication` in supported protocols.
  - Updated relay information to expose `AuthRequired` status in `Limitation`.

- **encoders/envelopes/authenvelope/authenvelope.go**:
  - Introduced `Id` method for `Response` type.

- **app/realy/server-publish.go**:
  - Refined event saving logic by replacing `chk.E` with a direct `err` check.

- **app/realy/server.go**:
  - Added `authRequired` field in `Server` struct and integrated it into relevant methods.

- **protocol/socketapi/socketapi.go**:
  - Introduced authentication handling in WebSocket listeners if `AuthRequired` is enabled.

- **protocol/socketapi/handleAuth.go**:
  - Implemented authentication response validation against challenges and server URLs.
  - Supported logging of successes and failures.

- **app/config/config.go**:
  - Added `AuthRequired` flag to configuration structure and environment variables.

- **encoders/envelopes/eventenvelope/eventenvelope.go**:
  - Added `Id` method for `Submission` and `Result` types.

- **interfaces/server/server.go**:
  - Added `AuthRequired` and `ServiceURL` methods to the interface.

- **protocol/ws/listener.go**:
  - Added `authRequested` field and methods to track authentication requests.

- **protocol/socketapi/ok.go**:
  - Introduced `Ok` utility functions for standard responses indicating reasons and types of errors.

- **app/realy/auth.go**:
  - Added `ServiceURL` method to determine the relay's URL for authentication responses.

- **encoders/reason/reason.go**:
  - Defined reusable reason templates for responses such as `auth-required` and `error`.

- **protocol/socketapi/ws.go**:
  - Adjusted `NewListener` to handle `authRequired` flag and conditionally generate challenges.
2025-07-16 12:53:52 +01:00

81 lines
1.9 KiB
Go

// Package main is a nostr relay with a simple follow/mute list authentication
// scheme and the new HTTP REST based protocol. Configuration is via environment
// variables or an optional .env file.
package main
import (
"fmt"
"github.com/pkg/profile"
"net/http"
_ "net/http/pprof"
"orly.dev/app/realy"
"orly.dev/app/realy/options"
"orly.dev/utils/chk"
"orly.dev/utils/interrupt"
"orly.dev/utils/log"
realy_lol "orly.dev/version"
"os"
"orly.dev/app"
"orly.dev/app/config"
"orly.dev/database"
"orly.dev/utils/context"
"orly.dev/utils/lol"
)
func main() {
log.I.F("starting realy %s", realy_lol.V)
var err error
var cfg *config.C
if cfg, err = config.New(); chk.T(err) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
}
config.PrintHelp(cfg, os.Stderr)
os.Exit(0)
}
if config.GetEnv() {
config.PrintEnv(cfg, os.Stdout)
os.Exit(0)
}
if config.HelpRequested() {
config.PrintHelp(cfg, os.Stderr)
os.Exit(0)
}
log.I.Ln("log level", cfg.LogLevel)
lol.SetLogLevel(cfg.LogLevel)
if cfg.Pprof {
defer profile.Start(profile.MemProfile).Stop()
go func() {
chk.E(http.ListenAndServe("127.0.0.1:6060", nil))
}()
}
c, cancel := context.Cancel(context.Bg())
storage, err := database.New(c, cancel, cfg.DataDir, cfg.DbLogLevel)
if chk.E(err) {
os.Exit(1)
}
r := &app.Relay{C: cfg, Store: storage}
go app.MonitorResources(c)
var server *realy.Server
serverParams := &realy.ServerParams{
Ctx: c,
Cancel: cancel,
Rl: r,
DbPath: cfg.DataDir,
MaxLimit: 512, // Default max limit for events
AuthRequired: cfg.AuthRequired,
}
var opts []options.O
if server, err = realy.NewServer(serverParams, opts...); chk.E(err) {
os.Exit(1)
}
if err != nil {
log.F.F("failed to create server: %v", err)
}
interrupt.AddHandler(func() { server.Shutdown() })
if err = server.Start(cfg.Listen, cfg.Port); chk.E(err) {
log.F.F("server terminated: %v", err)
}
}