NewServer returns a new CUPS server.
(ctx context.Context, c *component.Component, conf ServerConfig, options ...Option)
| 166 | |
| 167 | // NewServer returns a new CUPS server. |
| 168 | func NewServer(ctx context.Context, c *component.Component, conf ServerConfig, options ...Option) (*Server, error) { |
| 169 | options = append([]Option{ |
| 170 | WithAllowCUPSURIUpdate(conf.AllowCUPSURIUpdate), |
| 171 | WithDefaultLNSURI(conf.Default.LNSURI), |
| 172 | }, options...) |
| 173 | |
| 174 | var registerUnknownTo *ttnpb.OrganizationOrUserIdentifiers |
| 175 | switch conf.RegisterUnknown.Type { |
| 176 | case "user": |
| 177 | registerUnknownTo = (&ttnpb.UserIdentifiers{ |
| 178 | UserId: conf.RegisterUnknown.ID, |
| 179 | }).GetOrganizationOrUserIdentifiers() |
| 180 | case "organization": |
| 181 | registerUnknownTo = (&ttnpb.OrganizationIdentifiers{ |
| 182 | OrganizationId: conf.RegisterUnknown.ID, |
| 183 | }).GetOrganizationOrUserIdentifiers() |
| 184 | } |
| 185 | if registerUnknownTo != nil && conf.RegisterUnknown.APIKey != "" { |
| 186 | options = append(options, |
| 187 | WithRegisterUnknown(registerUnknownTo, func(_ context.Context) grpc.CallOption { |
| 188 | return grpc.PerRPCCredentials(rpcmetadata.MD{ |
| 189 | AuthType: "bearer", |
| 190 | AuthValue: conf.RegisterUnknown.APIKey, |
| 191 | AllowInsecure: c.AllowInsecureForCredentials(), |
| 192 | }) |
| 193 | }), |
| 194 | ) |
| 195 | } |
| 196 | |
| 197 | // The Server.tlsConfig is used when dialing a CUPS or an LNS server to query its certificate chain. |
| 198 | // When dialing servers with self-signed certs, the Root CA of target server must either be trusted by the system or |
| 199 | // added explicitly via the `--tls.root-ca` option. |
| 200 | if tlsConfig, err := c.GetTLSClientConfig(c.Context()); err == nil { |
| 201 | options = append(options, WithTLSConfig(tlsConfig)) |
| 202 | } |
| 203 | |
| 204 | s := &Server{ |
| 205 | component: c, |
| 206 | signers: make(map[uint32]stdcrypto.Signer), |
| 207 | trustCache: make(map[string]*x509.Certificate), |
| 208 | } |
| 209 | for _, opt := range options { |
| 210 | opt(s) |
| 211 | } |
| 212 | c.RegisterWeb(s) |
| 213 | return s, nil |
| 214 | } |
| 215 | |
| 216 | // RegisterRoutes implements web.Registerer. |
| 217 | func (s *Server) RegisterRoutes(srv *web.Server) { |