config.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "net/url"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. // Simple error for duplicate listener definitions
  12. errDuplicateListener = errors.New("duplicate listener definition")
  13. // Simple error for missing BOS listeners
  14. errNoBOSListeners = errors.New("at least one BOS listener is required")
  15. )
  16. // Custom error types for URI-related errors
  17. type uriFormatError struct {
  18. URI string
  19. Err error
  20. }
  21. func (e uriFormatError) Error() string {
  22. return fmt.Sprintf("invalid listener URI %q: %v. Valid format: SCHEME://HOST:PORT (e.g., LOCAL://0.0.0.0:5190)", e.URI, e.Err)
  23. }
  24. type Build struct {
  25. Version string `json:"version"`
  26. Commit string `json:"commit"`
  27. Date string `json:"date"`
  28. }
  29. type Listener struct {
  30. BOSListenAddress string
  31. BOSAdvertisedHostPlain string
  32. BOSAdvertisedHostSSL string
  33. KerberosListenAddress string
  34. HasSSL bool
  35. }
  36. //go:generate go run ../cmd/config_generator unix settings.env ssl
  37. type Config struct {
  38. BOSListeners []string `envconfig:"OSCAR_LISTENERS" required:"true" basic:"LOCAL://0.0.0.0:5190" ssl:"LOCAL://0.0.0.0:5190" description:"Network listeners for core OSCAR services. For multi-homed servers, allows users to connect from multiple networks. For example, you can allow both LAN and Internet clients to connect to the same server using different connection settings.\n\nFormat:\n\t- Comma-separated list of [NAME]://[HOSTNAME]:[PORT]\n\t- Listener names and ports must be unique\n\t- Listener names are user-defined\n\t- Each listener needs a listener in OSCAR_ADVERTISED_LISTENERS_PLAIN\n\nExamples:\n\t// Listen on all interfaces\n\tLAN://0.0.0.0:5190\n\t// Separate Internet and LAN config\n\tWAN://142.250.176.206:5190,LAN://192.168.1.10:5191"`
  39. BOSAdvertisedHostsPlain []string `envconfig:"OSCAR_ADVERTISED_LISTENERS_PLAIN" required:"true" basic:"LOCAL://127.0.0.1:5190" ssl:"LOCAL://127.0.0.1:5190" description:"Hostnames published by the server that clients connect to for accessing various OSCAR services. These hostnames are NOT the bind addresses. For multi-homed use servers, allows clients to connect using separate hostnames per network.\n\nFormat:\n\t- Comma-separated list of [NAME]://[HOSTNAME]:[PORT]\n\t- Each listener config must correspond to a config in OSCAR_LISTENERS\n\t- Clients MUST be able to connect to these hostnames\n\nExamples:\n\t// Local LAN config, server behind NAT\n\tLAN://192.168.1.10:5190\n\t// Separate Internet and LAN config\n\tWAN://aim.example.com:5190,LAN://192.168.1.10:5191"`
  40. BOSAdvertisedHostsSSL []string `envconfig:"OSCAR_ADVERTISED_LISTENERS_SSL" required:"false" basic:"" ssl:"LOCAL://ras.dev:5193" description:"Same as OSCAR_ADVERTISED_LISTENERS_PLAIN, except the hostname is for the server that terminates SSL."`
  41. KerberosListeners []string `envconfig:"KERBEROS_LISTENERS" required:"false" basic:"" ssl:"LOCAL://0.0.0.0:1088" description:"Network listeners for Kerberos authentication. See OSCAR_LISTENERS doc for more details.\n\nExamples:\n\t// Listen on all interfaces\n\tLAN://0.0.0.0:1088\n\t// Separate Internet and LAN config\n\tWAN://142.250.176.206:1088,LAN://192.168.1.10:1087"`
  42. TOCListeners []string `envconfig:"TOC_LISTENERS" required:"true" basic:"0.0.0.0:9898" ssl:"0.0.0.0:9898" description:"Network listeners for TOC protocol service.\n\nFormat: Comma-separated list of hostname:port pairs.\n\nExamples:\n\t// All interfaces\n\t0.0.0.0:9898\n\t// Multiple listeners\n\t0.0.0.0:9898,192.168.1.10:9899"`
  43. APIListener string `envconfig:"API_LISTENER" required:"true" basic:"127.0.0.1:8080" ssl:"127.0.0.1:8080" description:"Network listener for management API binds to. Only 1 listener can be specified. (Default 127.0.0.1 restricts to same machine only)."`
  44. DBPath string `envconfig:"DB_PATH" required:"true" basic:"oscar.sqlite" ssl:"oscar.sqlite" description:"The path to the SQLite database file. The file and DB schema are auto-created if they doesn't exist."`
  45. DisableAuth bool `envconfig:"DISABLE_AUTH" required:"true" basic:"true" ssl:"true" description:"Disable password check and auto-create new users at login time. Useful for quickly creating new accounts during development without having to register new users via the management API."`
  46. DisableMultiLoginNotif bool `envconfig:"DISABLE_MULTI_LOGIN_NOTIF" required:"false" basic:"true" ssl:"true" description:"Disable notification sent when another client signs in with the same screen name."`
  47. LogLevel string `envconfig:"LOG_LEVEL" required:"true" basic:"info" ssl:"info" description:"Set logging granularity. Possible values: 'trace', 'debug', 'info', 'warn', 'error'."`
  48. // ICQ Legacy Protocol Configuration
  49. ICQLegacy ICQLegacyConfig
  50. }
  51. // ICQLegacyConfig holds configuration for legacy ICQ protocol support (v2-v5)
  52. type ICQLegacyConfig struct {
  53. Enabled bool `envconfig:"ICQ_LEGACY_ENABLED" required:"false" basic:"true" ssl:"true" description:"Enable legacy ICQ protocol support (v2-v5). Allows vintage ICQ clients to connect."`
  54. UDPListener string `envconfig:"ICQ_LEGACY_UDP_LISTENER" required:"false" basic:"0.0.0.0:4000" ssl:"0.0.0.0:4000" description:"UDP listener address for legacy ICQ protocols.\n\nFormat: HOST:PORT\n\nExamples:\n\t// All interfaces\n\t0.0.0.0:4000\n\t// Specific interface\n\t192.168.1.10:4000"`
  55. SupportedVersions []int `envconfig:"ICQ_LEGACY_VERSIONS" required:"false" basic:"2,3,4,5" ssl:"2,3,4,5" description:"Comma-separated list of supported ICQ protocol versions. Valid values: 1, 2, 3, 4, 5 (V1 is experimental)."`
  56. SessionTimeout time.Duration `envconfig:"ICQ_LEGACY_SESSION_TIMEOUT" required:"false" basic:"120s" ssl:"120s" description:"Session timeout for legacy ICQ connections. Sessions are cleaned up after this duration of inactivity."`
  57. KeepAliveInterval time.Duration `envconfig:"ICQ_LEGACY_KEEPALIVE_INTERVAL" required:"false" basic:"120s" ssl:"120s" description:"Expected keep-alive interval from clients. Used for timeout calculations."`
  58. AutoRegistration bool `envconfig:"ICQ_LEGACY_AUTO_REGISTRATION" required:"false" basic:"false" ssl:"false" description:"Allow automatic user registration from legacy clients. When enabled, new UINs can be created via the legacy protocol."`
  59. DepartmentsEnabled bool `envconfig:"ICQ_LEGACY_DEPARTMENTS_ENABLED" required:"false" basic:"false" ssl:"false" description:"Enable department listing feature (groupware functionality)."`
  60. BroadcastEnabled bool `envconfig:"ICQ_LEGACY_BROADCAST_ENABLED" required:"false" basic:"true" ssl:"true" description:"Enable broadcast message functionality."`
  61. WWPEnabled bool `envconfig:"ICQ_LEGACY_WWP_ENABLED" required:"false" basic:"true" ssl:"true" description:"Enable Web Pager (WWP) message support."`
  62. DirectConnections []int `envconfig:"ICQ_LEGACY_DIRECT_CONNECTIONS" required:"false" basic:"5" ssl:"5" description:"Comma-separated list of protocol versions that send real connection info (IP, port) in user online notifications. Disabled for privacy and interoperability. Required for peer-to-peer features (file transfer, direct chat). Example: 5 or 3,4,5"`
  63. }
  64. // DefaultICQLegacyConfig returns the default configuration for ICQ legacy protocol
  65. func DefaultICQLegacyConfig() ICQLegacyConfig {
  66. return ICQLegacyConfig{
  67. Enabled: true,
  68. UDPListener: "0.0.0.0:4000",
  69. SupportedVersions: []int{2, 3, 4, 5},
  70. SessionTimeout: 120 * time.Second,
  71. KeepAliveInterval: 120 * time.Second,
  72. AutoRegistration: false,
  73. DepartmentsEnabled: false,
  74. BroadcastEnabled: true,
  75. WWPEnabled: true,
  76. }
  77. }
  78. // SupportsVersion checks if a specific protocol version is enabled
  79. func (c *ICQLegacyConfig) SupportsVersion(version int) bool {
  80. for _, v := range c.SupportedVersions {
  81. if v == version {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. // DirectConnectionEnabled checks if direct connections are enabled for a specific protocol version
  88. func (c *ICQLegacyConfig) DirectConnectionEnabled(version int) bool {
  89. for _, v := range c.DirectConnections {
  90. if v == version {
  91. return true
  92. }
  93. }
  94. return false
  95. }
  96. func (c *Config) ParseListenersCfg() ([]Listener, error) {
  97. // Helper function to parse and validate a single URI
  98. parseURI := func(uriStr string) (*url.URL, error) {
  99. uriStr = strings.TrimSpace(uriStr)
  100. if uriStr == "" {
  101. return nil, nil
  102. }
  103. u, err := url.Parse(uriStr)
  104. if err != nil {
  105. return nil, uriFormatError{URI: uriStr, Err: err}
  106. }
  107. switch {
  108. case u.Scheme == "":
  109. return nil, uriFormatError{URI: uriStr, Err: errors.New("missing scheme")}
  110. case u.Hostname() == "":
  111. return nil, uriFormatError{URI: uriStr, Err: errors.New("missing host")}
  112. case u.Port() == "":
  113. return nil, uriFormatError{URI: uriStr, Err: errors.New("missing port")}
  114. }
  115. return u, nil
  116. }
  117. m := make(map[string]*Listener)
  118. // Parse BOS listeners
  119. for _, uriStr := range c.BOSListeners {
  120. u, err := parseURI(uriStr)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if u == nil {
  125. continue
  126. }
  127. if _, ok := m[u.Scheme]; !ok {
  128. m[u.Scheme] = &Listener{}
  129. }
  130. if m[u.Scheme].BOSListenAddress != "" {
  131. return nil, errDuplicateListener
  132. }
  133. m[u.Scheme].BOSListenAddress = net.JoinHostPort(u.Hostname(), u.Port())
  134. }
  135. // Parse plaintext BOS advertised listeners
  136. for _, uriStr := range c.BOSAdvertisedHostsPlain {
  137. u, err := parseURI(uriStr)
  138. if err != nil {
  139. return nil, err
  140. }
  141. if u == nil {
  142. continue
  143. }
  144. if _, ok := m[u.Scheme]; !ok {
  145. m[u.Scheme] = &Listener{}
  146. }
  147. if m[u.Scheme].BOSAdvertisedHostPlain != "" {
  148. return nil, errDuplicateListener
  149. }
  150. m[u.Scheme].BOSAdvertisedHostPlain = net.JoinHostPort(u.Hostname(), u.Port())
  151. }
  152. // Parse SSL BOS advertised listeners
  153. for _, uriStr := range c.BOSAdvertisedHostsSSL {
  154. u, err := parseURI(uriStr)
  155. if err != nil {
  156. return nil, err
  157. }
  158. if u == nil {
  159. continue
  160. }
  161. if _, ok := m[u.Scheme]; !ok {
  162. m[u.Scheme] = &Listener{}
  163. }
  164. if m[u.Scheme].BOSAdvertisedHostSSL != "" {
  165. return nil, errDuplicateListener
  166. }
  167. m[u.Scheme].HasSSL = true
  168. m[u.Scheme].BOSAdvertisedHostSSL = net.JoinHostPort(u.Hostname(), u.Port())
  169. }
  170. // Parse Kerberos listeners
  171. for _, uriStr := range c.KerberosListeners {
  172. u, err := parseURI(uriStr)
  173. if err != nil {
  174. return nil, err
  175. }
  176. if u == nil {
  177. continue
  178. }
  179. if _, ok := m[u.Scheme]; !ok {
  180. m[u.Scheme] = &Listener{}
  181. }
  182. if m[u.Scheme].KerberosListenAddress != "" {
  183. return nil, errDuplicateListener
  184. }
  185. m[u.Scheme].KerberosListenAddress = net.JoinHostPort(u.Hostname(), u.Port())
  186. }
  187. ret := make([]Listener, 0, len(m))
  188. for k, v := range m {
  189. switch {
  190. case v.BOSAdvertisedHostPlain == "":
  191. return nil, fmt.Errorf("missing BOS advertise address for listener `%s://`", k)
  192. case v.BOSListenAddress == "":
  193. return nil, fmt.Errorf("missing BOS listen address for listener `%s://`", k)
  194. }
  195. ret = append(ret, *v)
  196. }
  197. if len(ret) == 0 {
  198. return nil, errNoBOSListeners
  199. }
  200. return ret, nil
  201. }
  202. func (c *Config) Validate() error {
  203. // Validate TOCListeners (format: hostname:port pairs)
  204. for _, listener := range c.TOCListeners {
  205. listener = strings.TrimSpace(listener)
  206. if listener == "" {
  207. continue
  208. }
  209. host, port, err := net.SplitHostPort(listener)
  210. if err != nil {
  211. return fmt.Errorf("invalid TOC listener %q: %v. Valid format: HOST:PORT (e.g., 0.0.0.0:9898)", listener, err)
  212. }
  213. if host == "" {
  214. return fmt.Errorf("invalid TOC listener %q: missing host. Valid format: HOST:PORT (e.g., 0.0.0.0:9898)", listener)
  215. }
  216. if port == "" {
  217. return fmt.Errorf("invalid TOC listener %q: missing port. Valid format: HOST:PORT (e.g., 0.0.0.0:9898)", listener)
  218. }
  219. }
  220. // Validate APIListener (format: hostname:port pair, no scheme)
  221. apiListener := strings.TrimSpace(c.APIListener)
  222. if apiListener == "" {
  223. return fmt.Errorf("APIListener is required and cannot be empty")
  224. }
  225. host, port, err := net.SplitHostPort(apiListener)
  226. if err != nil {
  227. return fmt.Errorf("invalid API listener %q: %v. Valid format: HOST:PORT (e.g., 127.0.0.1:8080)", c.APIListener, err)
  228. }
  229. if host == "" {
  230. return fmt.Errorf("invalid API listener %q: missing host. Valid format: HOST:PORT (e.g., 127.0.0.1:8080)", c.APIListener)
  231. }
  232. if port == "" {
  233. return fmt.Errorf("invalid API listener %q: missing port. Valid format: HOST:PORT (e.g., 127.0.0.1:8080)", c.APIListener)
  234. }
  235. return nil
  236. }