-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package acme | ||
|
||
import ( | ||
"crypto/tls" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"golang.org/x/crypto/acme/autocert" | ||
) | ||
|
||
func NewAcme(domains []string, dir string) *tls.Config { | ||
m := &autocert.Manager{ | ||
Prompt: autocert.AcceptTOS, | ||
} | ||
if len(domains) > 0 { | ||
m.HostPolicy = autocert.HostWhitelist(domains...) | ||
} | ||
if dir == "" { | ||
dir = cacheDir() | ||
} | ||
m.Cache = autocert.DirCache(dir) | ||
|
||
tlsConfig := m.TLSConfig() | ||
return tlsConfig | ||
} | ||
|
||
func homeDir() string { | ||
if runtime.GOOS == "windows" { | ||
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") | ||
} | ||
if h := os.Getenv("HOME"); h != "" { | ||
return h | ||
} | ||
return "/" | ||
} | ||
|
||
func cacheDir() string { | ||
const base = "autocert" | ||
switch runtime.GOOS { | ||
case "darwin": | ||
return filepath.Join(homeDir(), "Library", "Caches", base) | ||
case "windows": | ||
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} { | ||
if v := os.Getenv(ev); v != "" { | ||
return filepath.Join(v, base) | ||
} | ||
} | ||
// Worst case: | ||
return filepath.Join(homeDir(), base) | ||
} | ||
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" { | ||
return filepath.Join(xdg, base) | ||
} | ||
return filepath.Join(homeDir(), ".cache", base) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"time" | ||
|
||
"github.com/daocloud/crproxy/internal/acme" | ||
"github.com/wzshiming/cmux" | ||
"github.com/wzshiming/cmux/pattern" | ||
) | ||
|
||
func Run(ctx context.Context, address string, handler http.Handler, acmeHosts []string, acmeCache string, certFile, privateKeyFile string) error { | ||
listener, err := net.Listen("tcp", address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
muxListener := cmux.NewMuxListener(listener) | ||
tlsListener, err := muxListener.MatchPrefix(pattern.Pattern[pattern.TLS]...) | ||
if err != nil { | ||
return fmt.Errorf("match tls listener: %w", err) | ||
} | ||
unmatchedListener, err := muxListener.Unmatched() | ||
if err != nil { | ||
return fmt.Errorf("unmatched listener: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
errCh := make(chan error, 1) | ||
|
||
if (certFile != "" && privateKeyFile != "") || len(acmeHosts) != 0 { | ||
go func() { | ||
svc := &http.Server{ | ||
ReadHeaderTimeout: 5 * time.Second, | ||
BaseContext: func(_ net.Listener) context.Context { | ||
return ctx | ||
}, | ||
Addr: address, | ||
Handler: handler, | ||
} | ||
if len(acmeHosts) != 0 { | ||
svc.TLSConfig = acme.NewAcme(acmeHosts, acmeCache) | ||
} | ||
err = svc.ServeTLS(tlsListener, certFile, privateKeyFile) | ||
if err != nil { | ||
errCh <- fmt.Errorf("serve https: %w", err) | ||
} | ||
}() | ||
} else { | ||
svc := httptest.Server{ | ||
Listener: tlsListener, | ||
Config: &http.Server{ | ||
ReadHeaderTimeout: 5 * time.Second, | ||
BaseContext: func(_ net.Listener) context.Context { | ||
return ctx | ||
}, | ||
Addr: address, | ||
Handler: handler, | ||
}, | ||
} | ||
svc.StartTLS() | ||
} | ||
|
||
go func() { | ||
svc := &http.Server{ | ||
ReadHeaderTimeout: 5 * time.Second, | ||
BaseContext: func(_ net.Listener) context.Context { | ||
return ctx | ||
}, | ||
Addr: address, | ||
Handler: handler, | ||
} | ||
err = svc.Serve(unmatchedListener) | ||
if err != nil { | ||
errCh <- fmt.Errorf("serve http: %w", err) | ||
} | ||
}() | ||
|
||
select { | ||
case err = <-errCh: | ||
case <-ctx.Done(): | ||
err = ctx.Err() | ||
} | ||
|
||
return err | ||
} |