-
Notifications
You must be signed in to change notification settings - Fork 25
Add automatic TLS certificate support via Let's Encrypt #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1cb0564
7ded489
e3b61ef
d81723f
4ad21a0
b38d4b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| "mcpServers": { | ||
| "test": { | ||
| "type": "http", | ||
| "url": "http://localhost:8081/mcp" | ||
| "url": "http://localhost/mcp" | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,15 @@ | ||||||||||||
| package mcpproxy | ||||||||||||
|
|
||||||||||||
| import ( | ||||||||||||
| "context" | ||||||||||||
| "crypto/sha256" | ||||||||||||
| "errors" | ||||||||||||
| "fmt" | ||||||||||||
| "net/http" | ||||||||||||
| "net/url" | ||||||||||||
| "os" | ||||||||||||
| "path" | ||||||||||||
| "sync" | ||||||||||||
| "time" | ||||||||||||
|
|
||||||||||||
| "github.com/blendle/zapdriver" | ||||||||||||
|
|
@@ -19,11 +23,17 @@ import ( | |||||||||||
| "github.com/sigbit/mcp-auth-proxy/pkg/repository" | ||||||||||||
| "github.com/sigbit/mcp-auth-proxy/pkg/utils" | ||||||||||||
| "go.uber.org/zap" | ||||||||||||
| "golang.org/x/crypto/acme" | ||||||||||||
| "golang.org/x/crypto/acme/autocert" | ||||||||||||
| "golang.org/x/crypto/bcrypt" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| func Run( | ||||||||||||
| listen string, | ||||||||||||
| listenTLS string, | ||||||||||||
| tlsHost string, | ||||||||||||
| tlsDirectoryURL string, | ||||||||||||
| tlsAcceptTOS bool, | ||||||||||||
| dataPath string, | ||||||||||||
| externalURL string, | ||||||||||||
| proxyURL string, | ||||||||||||
|
|
@@ -131,6 +141,77 @@ func Run( | |||||||||||
| idpRouter.SetupRoutes(router) | ||||||||||||
| proxyRouter.SetupRoutes(router) | ||||||||||||
|
|
||||||||||||
| logger.Info("Starting server", zap.String("listen", listen)) | ||||||||||||
| return router.Run(listen) | ||||||||||||
| if tlsHost != "" { | ||||||||||||
| m := autocert.Manager{ | ||||||||||||
| Prompt: func(tosURL string) bool { | ||||||||||||
| return tlsAcceptTOS | ||||||||||||
| }, | ||||||||||||
| HostPolicy: autocert.HostWhitelist(tlsHost), | ||||||||||||
| Cache: autocert.DirCache(path.Join(dataPath, "certs")), | ||||||||||||
| Client: &acme.Client{ | ||||||||||||
| DirectoryURL: tlsDirectoryURL, | ||||||||||||
| }, | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| exit := make(chan struct{}, 2) | ||||||||||||
| var wg sync.WaitGroup | ||||||||||||
|
|
||||||||||||
| httpServer := &http.Server{ | ||||||||||||
| Addr: listen, | ||||||||||||
| Handler: m.HTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||||||||||
| host := r.Host | ||||||||||||
| if host == "" { | ||||||||||||
| host = r.URL.Host | ||||||||||||
| } | ||||||||||||
| target := "https://" + host + r.RequestURI | ||||||||||||
| http.Redirect(w, r, target, http.StatusMovedPermanently) | ||||||||||||
| })), | ||||||||||||
| } | ||||||||||||
| httpsServer := &http.Server{ | ||||||||||||
| Addr: listenTLS, | ||||||||||||
| Handler: router, | ||||||||||||
| TLSConfig: m.TLSConfig(), | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| wg.Add(2) | ||||||||||||
| errs := []error{} | ||||||||||||
| lock := sync.Mutex{} | ||||||||||||
| go func() { | ||||||||||||
| defer wg.Done() | ||||||||||||
| err := httpServer.ListenAndServe() | ||||||||||||
| if err != nil && !errors.Is(err, http.ErrServerClosed) { | ||||||||||||
| lock.Lock() | ||||||||||||
| errs = append(errs, err) | ||||||||||||
| lock.Unlock() | ||||||||||||
| } | ||||||||||||
| exit <- struct{}{} | ||||||||||||
| }() | ||||||||||||
|
|
||||||||||||
| go func() { | ||||||||||||
| defer wg.Done() | ||||||||||||
| err := httpsServer.ListenAndServeTLS("", "") | ||||||||||||
| if err != nil && !errors.Is(err, http.ErrServerClosed) { | ||||||||||||
| lock.Lock() | ||||||||||||
| errs = append(errs, err) | ||||||||||||
| lock.Unlock() | ||||||||||||
| } | ||||||||||||
| exit <- struct{}{} | ||||||||||||
| }() | ||||||||||||
|
|
||||||||||||
| logger.Info("Starting server", zap.Strings("listen", []string{listen, listenTLS})) | ||||||||||||
| <-exit | ||||||||||||
|
||||||||||||
| <-exit | |
| }() | |
| logger.Info("Starting server", zap.Strings("listen", []string{listen, listenTLS})) | |
| wg.Wait() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The acme.Client configuration is incomplete. The Client field should not be set manually when using autocert.Manager, as the manager handles ACME client configuration internally. Remove the Client field assignment.