|
| 1 | +// Package certmgr provides a REST API for managing TLS certificates. |
| 2 | +package certmgr |
| 3 | + |
| 4 | +import ( |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/database64128/shadowsocks-go/api/internal/restapi" |
| 8 | + "github.com/database64128/shadowsocks-go/tlscerts" |
| 9 | +) |
| 10 | + |
| 11 | +// StandardError is the standard error response. |
| 12 | +type StandardError struct { |
| 13 | + Message string `json:"error"` |
| 14 | +} |
| 15 | + |
| 16 | +// CertificateManager handles TLS certificate management API requests. |
| 17 | +type CertificateManager struct { |
| 18 | + store *tlscerts.Store |
| 19 | +} |
| 20 | + |
| 21 | +// NewCertificateManager returns a new certificate manager. |
| 22 | +func NewCertificateManager(store *tlscerts.Store) *CertificateManager { |
| 23 | + return &CertificateManager{ |
| 24 | + store: store, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// RegisterHandlers sets up handlers for the /certlists and /x509certpools endpoints. |
| 29 | +func (cm *CertificateManager) RegisterHandlers(register func(method string, path string, handler restapi.HandlerFunc)) { |
| 30 | + register(http.MethodGet, "/certlists", cm.newListCertListsHandler()) |
| 31 | + register(http.MethodGet, "/certlists/{name}", newGetCertListHandler(cm.store)) |
| 32 | + register(http.MethodPost, "/certlists/{name}/reload", newReloadCertListHandler(cm.store)) |
| 33 | + |
| 34 | + register(http.MethodGet, "/x509certpools", cm.newListX509CertPoolsHandler()) |
| 35 | +} |
| 36 | + |
| 37 | +func (cm *CertificateManager) newListCertListsHandler() restapi.HandlerFunc { |
| 38 | + certLists := &cm.store.Config().CertLists |
| 39 | + return func(w http.ResponseWriter, _ *http.Request) (int, error) { |
| 40 | + return restapi.EncodeResponse(w, http.StatusOK, certLists) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (cm *CertificateManager) newListX509CertPoolsHandler() restapi.HandlerFunc { |
| 45 | + certPools := &cm.store.Config().X509CertPools |
| 46 | + return func(w http.ResponseWriter, _ *http.Request) (int, error) { |
| 47 | + return restapi.EncodeResponse(w, http.StatusOK, certPools) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +var ( |
| 52 | + certListNotFoundJSON = []byte(`{"error":"certificate list not found"}`) |
| 53 | + certListNotReloadableJSON = []byte(`{"error":"certificate list is not reloadable"}`) |
| 54 | +) |
| 55 | + |
| 56 | +func newGetCertListHandler(store *tlscerts.Store) restapi.HandlerFunc { |
| 57 | + return func(w http.ResponseWriter, r *http.Request) (int, error) { |
| 58 | + name := r.PathValue("name") |
| 59 | + certList, ok := store.GetCertList(name) |
| 60 | + if !ok { |
| 61 | + return restapi.EncodeResponse(w, http.StatusNotFound, &certListNotFoundJSON) |
| 62 | + } |
| 63 | + return restapi.EncodeResponse(w, http.StatusOK, certList.Config()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func newReloadCertListHandler(store *tlscerts.Store) restapi.HandlerFunc { |
| 68 | + return func(w http.ResponseWriter, r *http.Request) (int, error) { |
| 69 | + name := r.PathValue("name") |
| 70 | + certList, ok := store.GetCertList(name) |
| 71 | + if !ok { |
| 72 | + return restapi.EncodeResponse(w, http.StatusNotFound, &certListNotFoundJSON) |
| 73 | + } |
| 74 | + if err := certList.Reload(); err != nil { |
| 75 | + if err == (tlscerts.ReloadDisabledError{}) { |
| 76 | + return restapi.EncodeResponse(w, http.StatusNotFound, &certListNotReloadableJSON) |
| 77 | + } |
| 78 | + return restapi.EncodeResponse(w, http.StatusInternalServerError, StandardError{Message: err.Error()}) |
| 79 | + } |
| 80 | + return restapi.EncodeResponse(w, http.StatusNoContent, nil) |
| 81 | + } |
| 82 | +} |
0 commit comments