Skip to content

Commit

Permalink
support acme http challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhaoxx committed Apr 22, 2024
1 parent 0123b4a commit 7d81ccc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions http/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (rw *NgResponseWriter) ErrorPage(code int, err string) {
CODE string
UTC string
// ELA string
TAR string
}{
MSG: err,
CODE: strconv.Itoa(code),
Expand All @@ -42,6 +43,7 @@ func (rw *NgResponseWriter) ErrorPage(code int, err string) {
// CID: strconv.FormatUint(rw.ctx.conn.Id, 10),
UTC: rw.ctx.starttime.UTC().Format("2006-01-02 15:04:05 UTC"),
// ELA: time.Since(rw.ctx.starttime).String(),
TAR: rw.ctx.Req.Host + rw.ctx.Req.RequestURI,
})
}

Expand Down
2 changes: 1 addition & 1 deletion http/html/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>

<div style="position: fixed; bottom: 0; width: 100%;color: #929292;font-family: monospace; font-weight: lighter;">
RequestID: {{.RID}} · {{.RIP}} · {{.UTC}}
{{.TAR}} · RequestID: {{.RID}} · {{.RIP}} · {{.UTC}}
</div>
</body>

Expand Down
4 changes: 3 additions & 1 deletion tcp/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
KeyTLS = "tls"
KeyTlsSni = "sni"
KeyUnkownBytes = "unkownheadbytes"
KeyHTTPRequest = "http"
)

func readClientHello(reader io.Reader) (*tls.ClientHelloInfo, error) {
Expand Down Expand Up @@ -82,10 +83,11 @@ func (det *Detect) Handle(c *Conn) SerRet {
}

func DetectHTTP(r io.Reader, c *Conn) string {
_, err := http.ReadRequest(bufio.NewReader(r))
rr, err := http.ReadRequest(bufio.NewReader(r))
if err != nil {
return ""
}
c.Store(KeyHTTPRequest, rr)
return "HTTP1"
}

Expand Down
6 changes: 6 additions & 0 deletions ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Cfg struct {

IPFilter IpfilterConfig `yaml:"IPFilter,flow"`
SSH SSHConfig `yaml:"SSH,flow"`
ACME ACMEConfig `yaml:"ACME,flow"`
}

type authConfig struct {
Expand Down Expand Up @@ -128,3 +129,8 @@ type SSHConfig struct {
PrivateKeys []string `yaml:"PrivateKeys,flow"`
Banner string `yaml:"Banner"`
}

type ACMEConfig struct {
Hosts []string `yaml:"Hosts,flow"`
WWWRoot string `yaml:"WWWRoot"`
}
45 changes: 45 additions & 0 deletions ui/myservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"errors"
"net"
stdhttp "net/http"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -51,6 +52,42 @@ var builtinHttpServices = map[string]http.Service{
"NgUI": &UI{},
}

type AcmeWebRoot struct {
AllowedHosts []string
WWWRoot string
}

func (a *AcmeWebRoot) Handle(conn *tcp.Conn) tcp.SerRet {
_req, ok := conn.Load(tcp.KeyHTTPRequest)
if !ok {
return tcp.Continue
}

req, ok := _req.(*stdhttp.Request)

if !ok {
return tcp.Continue
}

if !strings.HasPrefix(req.URL.Path, "/.well-known/acme-challenge/") {
return tcp.Continue
}
for _, h := range a.AllowedHosts {
if req.Host == h {
goto allowed
}
}
return tcp.Continue

allowed:
s := stdhttp.FileServer(stdhttp.Dir(a.WWWRoot))
stdhttp.Serve(utils.ConnGetSocket(conn.TopConn()), stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
s.ServeHTTP(w, r)
}))
return tcp.Close

}

var builtinTcpServices = map[string]tcp.ServiceHandler{
"tls": TlsMgr,
"knock": Knock,
Expand Down Expand Up @@ -231,6 +268,14 @@ func LoadCfg(cfgs []byte) error {
os.Exit(-1)
}

if len(cfg.ACME.Hosts) > 0 {
acmec := AcmeWebRoot{
AllowedHosts: cfg.ACME.Hosts,
WWWRoot: cfg.ACME.WWWRoot,
}
builtinTcpServices["acme"] = &acmec
}

var prik []gossh.Signer
if len(cfg.SSH.PrivateKeys) > 0 {
for _, key := range cfg.SSH.PrivateKeys {
Expand Down

0 comments on commit 7d81ccc

Please sign in to comment.